socket.c 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001-2003 Intel Corp.
  6. * Copyright (c) 2001-2002 Nokia, Inc.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * This file is part of the SCTP kernel reference Implementation
  10. *
  11. * These functions interface with the sockets layer to implement the
  12. * SCTP Extensions for the Sockets API.
  13. *
  14. * Note that the descriptions from the specification are USER level
  15. * functions--this file is the functions which populate the struct proto
  16. * for SCTP which is the BOTTOM of the sockets interface.
  17. *
  18. * The SCTP reference implementation is free software;
  19. * you can redistribute it and/or modify it under the terms of
  20. * the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2, or (at your option)
  22. * any later version.
  23. *
  24. * The SCTP reference implementation is distributed in the hope that it
  25. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  26. * ************************
  27. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  28. * See the GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with GNU CC; see the file COPYING. If not, write to
  32. * the Free Software Foundation, 59 Temple Place - Suite 330,
  33. * Boston, MA 02111-1307, USA.
  34. *
  35. * Please send any bug reports or fixes you make to the
  36. * email address(es):
  37. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  38. *
  39. * Or submit a bug report through the following website:
  40. * http://www.sf.net/projects/lksctp
  41. *
  42. * Written or modified by:
  43. * La Monte H.P. Yarroll <piggy@acm.org>
  44. * Narasimha Budihal <narsi@refcode.org>
  45. * Karl Knutson <karl@athena.chicago.il.us>
  46. * Jon Grimm <jgrimm@us.ibm.com>
  47. * Xingang Guo <xingang.guo@intel.com>
  48. * Daisy Chang <daisyc@us.ibm.com>
  49. * Sridhar Samudrala <samudrala@us.ibm.com>
  50. * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
  51. * Ardelle Fan <ardelle.fan@intel.com>
  52. * Ryan Layer <rmlayer@us.ibm.com>
  53. * Anup Pemmaiah <pemmaiah@cc.usu.edu>
  54. * Kevin Gao <kevin.gao@intel.com>
  55. *
  56. * Any bugs reported given to us we will try to fix... any fixes shared will
  57. * be incorporated into the next SCTP release.
  58. */
  59. #include <linux/config.h>
  60. #include <linux/types.h>
  61. #include <linux/kernel.h>
  62. #include <linux/wait.h>
  63. #include <linux/time.h>
  64. #include <linux/ip.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/poll.h>
  67. #include <linux/init.h>
  68. #include <linux/crypto.h>
  69. #include <net/ip.h>
  70. #include <net/icmp.h>
  71. #include <net/route.h>
  72. #include <net/ipv6.h>
  73. #include <net/inet_common.h>
  74. #include <linux/socket.h> /* for sa_family_t */
  75. #include <net/sock.h>
  76. #include <net/sctp/sctp.h>
  77. #include <net/sctp/sm.h>
  78. /* WARNING: Please do not remove the SCTP_STATIC attribute to
  79. * any of the functions below as they are used to export functions
  80. * used by a project regression testsuite.
  81. */
  82. /* Forward declarations for internal helper functions. */
  83. static int sctp_writeable(struct sock *sk);
  84. static void sctp_wfree(struct sk_buff *skb);
  85. static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
  86. size_t msg_len);
  87. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
  88. static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  89. static int sctp_wait_for_accept(struct sock *sk, long timeo);
  90. static void sctp_wait_for_close(struct sock *sk, long timeo);
  91. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  92. union sctp_addr *addr, int len);
  93. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  94. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  95. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  96. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  97. static int sctp_send_asconf(struct sctp_association *asoc,
  98. struct sctp_chunk *chunk);
  99. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  100. static int sctp_autobind(struct sock *sk);
  101. static void sctp_sock_migrate(struct sock *, struct sock *,
  102. struct sctp_association *, sctp_socket_type_t);
  103. static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
  104. extern kmem_cache_t *sctp_bucket_cachep;
  105. /* Get the sndbuf space available at the time on the association. */
  106. static inline int sctp_wspace(struct sctp_association *asoc)
  107. {
  108. struct sock *sk = asoc->base.sk;
  109. int amt = 0;
  110. if (asoc->ep->sndbuf_policy) {
  111. /* make sure that no association uses more than sk_sndbuf */
  112. amt = sk->sk_sndbuf - asoc->sndbuf_used;
  113. } else {
  114. /* do socket level accounting */
  115. amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
  116. }
  117. if (amt < 0)
  118. amt = 0;
  119. return amt;
  120. }
  121. /* Increment the used sndbuf space count of the corresponding association by
  122. * the size of the outgoing data chunk.
  123. * Also, set the skb destructor for sndbuf accounting later.
  124. *
  125. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  126. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  127. * destructor in the data chunk skb for the purpose of the sndbuf space
  128. * tracking.
  129. */
  130. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  131. {
  132. struct sctp_association *asoc = chunk->asoc;
  133. struct sock *sk = asoc->base.sk;
  134. /* The sndbuf space is tracked per association. */
  135. sctp_association_hold(asoc);
  136. skb_set_owner_w(chunk->skb, sk);
  137. chunk->skb->destructor = sctp_wfree;
  138. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  139. *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
  140. asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
  141. sizeof(struct sk_buff) +
  142. sizeof(struct sctp_chunk);
  143. sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk) +
  144. sizeof(struct sk_buff) +
  145. sizeof(struct sctp_chunk);
  146. atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  147. }
  148. /* Verify that this is a valid address. */
  149. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  150. int len)
  151. {
  152. struct sctp_af *af;
  153. /* Verify basic sockaddr. */
  154. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  155. if (!af)
  156. return -EINVAL;
  157. /* Is this a valid SCTP address? */
  158. if (!af->addr_valid(addr, sctp_sk(sk)))
  159. return -EINVAL;
  160. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  161. return -EINVAL;
  162. return 0;
  163. }
  164. /* Look up the association by its id. If this is not a UDP-style
  165. * socket, the ID field is always ignored.
  166. */
  167. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  168. {
  169. struct sctp_association *asoc = NULL;
  170. /* If this is not a UDP-style socket, assoc id should be ignored. */
  171. if (!sctp_style(sk, UDP)) {
  172. /* Return NULL if the socket state is not ESTABLISHED. It
  173. * could be a TCP-style listening socket or a socket which
  174. * hasn't yet called connect() to establish an association.
  175. */
  176. if (!sctp_sstate(sk, ESTABLISHED))
  177. return NULL;
  178. /* Get the first and the only association from the list. */
  179. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  180. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  181. struct sctp_association, asocs);
  182. return asoc;
  183. }
  184. /* Otherwise this is a UDP-style socket. */
  185. if (!id || (id == (sctp_assoc_t)-1))
  186. return NULL;
  187. spin_lock_bh(&sctp_assocs_id_lock);
  188. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  189. spin_unlock_bh(&sctp_assocs_id_lock);
  190. if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
  191. return NULL;
  192. return asoc;
  193. }
  194. /* Look up the transport from an address and an assoc id. If both address and
  195. * id are specified, the associations matching the address and the id should be
  196. * the same.
  197. */
  198. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  199. struct sockaddr_storage *addr,
  200. sctp_assoc_t id)
  201. {
  202. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  203. struct sctp_transport *transport;
  204. union sctp_addr *laddr = (union sctp_addr *)addr;
  205. laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
  206. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  207. (union sctp_addr *)addr,
  208. &transport);
  209. laddr->v4.sin_port = htons(laddr->v4.sin_port);
  210. if (!addr_asoc)
  211. return NULL;
  212. id_asoc = sctp_id2assoc(sk, id);
  213. if (id_asoc && (id_asoc != addr_asoc))
  214. return NULL;
  215. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  216. (union sctp_addr *)addr);
  217. return transport;
  218. }
  219. /* API 3.1.2 bind() - UDP Style Syntax
  220. * The syntax of bind() is,
  221. *
  222. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  223. *
  224. * sd - the socket descriptor returned by socket().
  225. * addr - the address structure (struct sockaddr_in or struct
  226. * sockaddr_in6 [RFC 2553]),
  227. * addr_len - the size of the address structure.
  228. */
  229. SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
  230. {
  231. int retval = 0;
  232. sctp_lock_sock(sk);
  233. SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
  234. sk, addr, addr_len);
  235. /* Disallow binding twice. */
  236. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  237. retval = sctp_do_bind(sk, (union sctp_addr *)addr,
  238. addr_len);
  239. else
  240. retval = -EINVAL;
  241. sctp_release_sock(sk);
  242. return retval;
  243. }
  244. static long sctp_get_port_local(struct sock *, union sctp_addr *);
  245. /* Verify this is a valid sockaddr. */
  246. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  247. union sctp_addr *addr, int len)
  248. {
  249. struct sctp_af *af;
  250. /* Check minimum size. */
  251. if (len < sizeof (struct sockaddr))
  252. return NULL;
  253. /* Does this PF support this AF? */
  254. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  255. return NULL;
  256. /* If we get this far, af is valid. */
  257. af = sctp_get_af_specific(addr->sa.sa_family);
  258. if (len < af->sockaddr_len)
  259. return NULL;
  260. return af;
  261. }
  262. /* Bind a local address either to an endpoint or to an association. */
  263. SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  264. {
  265. struct sctp_sock *sp = sctp_sk(sk);
  266. struct sctp_endpoint *ep = sp->ep;
  267. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  268. struct sctp_af *af;
  269. unsigned short snum;
  270. int ret = 0;
  271. /* Common sockaddr verification. */
  272. af = sctp_sockaddr_af(sp, addr, len);
  273. if (!af) {
  274. SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
  275. sk, addr, len);
  276. return -EINVAL;
  277. }
  278. snum = ntohs(addr->v4.sin_port);
  279. SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
  280. ", port: %d, new port: %d, len: %d)\n",
  281. sk,
  282. addr,
  283. bp->port, snum,
  284. len);
  285. /* PF specific bind() address verification. */
  286. if (!sp->pf->bind_verify(sp, addr))
  287. return -EADDRNOTAVAIL;
  288. /* We must either be unbound, or bind to the same port. */
  289. if (bp->port && (snum != bp->port)) {
  290. SCTP_DEBUG_PRINTK("sctp_do_bind:"
  291. " New port %d does not match existing port "
  292. "%d.\n", snum, bp->port);
  293. return -EINVAL;
  294. }
  295. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  296. return -EACCES;
  297. /* Make sure we are allowed to bind here.
  298. * The function sctp_get_port_local() does duplicate address
  299. * detection.
  300. */
  301. if ((ret = sctp_get_port_local(sk, addr))) {
  302. if (ret == (long) sk) {
  303. /* This endpoint has a conflicting address. */
  304. return -EINVAL;
  305. } else {
  306. return -EADDRINUSE;
  307. }
  308. }
  309. /* Refresh ephemeral port. */
  310. if (!bp->port)
  311. bp->port = inet_sk(sk)->num;
  312. /* Add the address to the bind address list. */
  313. sctp_local_bh_disable();
  314. sctp_write_lock(&ep->base.addr_lock);
  315. /* Use GFP_ATOMIC since BHs are disabled. */
  316. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  317. ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
  318. addr->v4.sin_port = htons(addr->v4.sin_port);
  319. sctp_write_unlock(&ep->base.addr_lock);
  320. sctp_local_bh_enable();
  321. /* Copy back into socket for getsockname() use. */
  322. if (!ret) {
  323. inet_sk(sk)->sport = htons(inet_sk(sk)->num);
  324. af->to_sk_saddr(addr, sk);
  325. }
  326. return ret;
  327. }
  328. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  329. *
  330. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  331. * at any one time. If a sender, after sending an ASCONF chunk, decides
  332. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  333. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  334. * subsequent ASCONF. Note this restriction binds each side, so at any
  335. * time two ASCONF may be in-transit on any given association (one sent
  336. * from each endpoint).
  337. */
  338. static int sctp_send_asconf(struct sctp_association *asoc,
  339. struct sctp_chunk *chunk)
  340. {
  341. int retval = 0;
  342. /* If there is an outstanding ASCONF chunk, queue it for later
  343. * transmission.
  344. */
  345. if (asoc->addip_last_asconf) {
  346. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  347. goto out;
  348. }
  349. /* Hold the chunk until an ASCONF_ACK is received. */
  350. sctp_chunk_hold(chunk);
  351. retval = sctp_primitive_ASCONF(asoc, chunk);
  352. if (retval)
  353. sctp_chunk_free(chunk);
  354. else
  355. asoc->addip_last_asconf = chunk;
  356. out:
  357. return retval;
  358. }
  359. /* Add a list of addresses as bind addresses to local endpoint or
  360. * association.
  361. *
  362. * Basically run through each address specified in the addrs/addrcnt
  363. * array/length pair, determine if it is IPv6 or IPv4 and call
  364. * sctp_do_bind() on it.
  365. *
  366. * If any of them fails, then the operation will be reversed and the
  367. * ones that were added will be removed.
  368. *
  369. * Only sctp_setsockopt_bindx() is supposed to call this function.
  370. */
  371. int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  372. {
  373. int cnt;
  374. int retval = 0;
  375. void *addr_buf;
  376. struct sockaddr *sa_addr;
  377. struct sctp_af *af;
  378. SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
  379. sk, addrs, addrcnt);
  380. addr_buf = addrs;
  381. for (cnt = 0; cnt < addrcnt; cnt++) {
  382. /* The list may contain either IPv4 or IPv6 address;
  383. * determine the address length for walking thru the list.
  384. */
  385. sa_addr = (struct sockaddr *)addr_buf;
  386. af = sctp_get_af_specific(sa_addr->sa_family);
  387. if (!af) {
  388. retval = -EINVAL;
  389. goto err_bindx_add;
  390. }
  391. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  392. af->sockaddr_len);
  393. addr_buf += af->sockaddr_len;
  394. err_bindx_add:
  395. if (retval < 0) {
  396. /* Failed. Cleanup the ones that have been added */
  397. if (cnt > 0)
  398. sctp_bindx_rem(sk, addrs, cnt);
  399. return retval;
  400. }
  401. }
  402. return retval;
  403. }
  404. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  405. * associations that are part of the endpoint indicating that a list of local
  406. * addresses are added to the endpoint.
  407. *
  408. * If any of the addresses is already in the bind address list of the
  409. * association, we do not send the chunk for that association. But it will not
  410. * affect other associations.
  411. *
  412. * Only sctp_setsockopt_bindx() is supposed to call this function.
  413. */
  414. static int sctp_send_asconf_add_ip(struct sock *sk,
  415. struct sockaddr *addrs,
  416. int addrcnt)
  417. {
  418. struct sctp_sock *sp;
  419. struct sctp_endpoint *ep;
  420. struct sctp_association *asoc;
  421. struct sctp_bind_addr *bp;
  422. struct sctp_chunk *chunk;
  423. struct sctp_sockaddr_entry *laddr;
  424. union sctp_addr *addr;
  425. void *addr_buf;
  426. struct sctp_af *af;
  427. struct list_head *pos;
  428. struct list_head *p;
  429. int i;
  430. int retval = 0;
  431. if (!sctp_addip_enable)
  432. return retval;
  433. sp = sctp_sk(sk);
  434. ep = sp->ep;
  435. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  436. __FUNCTION__, sk, addrs, addrcnt);
  437. list_for_each(pos, &ep->asocs) {
  438. asoc = list_entry(pos, struct sctp_association, asocs);
  439. if (!asoc->peer.asconf_capable)
  440. continue;
  441. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  442. continue;
  443. if (!sctp_state(asoc, ESTABLISHED))
  444. continue;
  445. /* Check if any address in the packed array of addresses is
  446. * in the bind address list of the association. If so,
  447. * do not send the asconf chunk to its peer, but continue with
  448. * other associations.
  449. */
  450. addr_buf = addrs;
  451. for (i = 0; i < addrcnt; i++) {
  452. addr = (union sctp_addr *)addr_buf;
  453. af = sctp_get_af_specific(addr->v4.sin_family);
  454. if (!af) {
  455. retval = -EINVAL;
  456. goto out;
  457. }
  458. if (sctp_assoc_lookup_laddr(asoc, addr))
  459. break;
  460. addr_buf += af->sockaddr_len;
  461. }
  462. if (i < addrcnt)
  463. continue;
  464. /* Use the first address in bind addr list of association as
  465. * Address Parameter of ASCONF CHUNK.
  466. */
  467. sctp_read_lock(&asoc->base.addr_lock);
  468. bp = &asoc->base.bind_addr;
  469. p = bp->address_list.next;
  470. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  471. sctp_read_unlock(&asoc->base.addr_lock);
  472. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  473. addrcnt, SCTP_PARAM_ADD_IP);
  474. if (!chunk) {
  475. retval = -ENOMEM;
  476. goto out;
  477. }
  478. retval = sctp_send_asconf(asoc, chunk);
  479. /* FIXME: After sending the add address ASCONF chunk, we
  480. * cannot append the address to the association's binding
  481. * address list, because the new address may be used as the
  482. * source of a message sent to the peer before the ASCONF
  483. * chunk is received by the peer. So we should wait until
  484. * ASCONF_ACK is received.
  485. */
  486. }
  487. out:
  488. return retval;
  489. }
  490. /* Remove a list of addresses from bind addresses list. Do not remove the
  491. * last address.
  492. *
  493. * Basically run through each address specified in the addrs/addrcnt
  494. * array/length pair, determine if it is IPv6 or IPv4 and call
  495. * sctp_del_bind() on it.
  496. *
  497. * If any of them fails, then the operation will be reversed and the
  498. * ones that were removed will be added back.
  499. *
  500. * At least one address has to be left; if only one address is
  501. * available, the operation will return -EBUSY.
  502. *
  503. * Only sctp_setsockopt_bindx() is supposed to call this function.
  504. */
  505. int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  506. {
  507. struct sctp_sock *sp = sctp_sk(sk);
  508. struct sctp_endpoint *ep = sp->ep;
  509. int cnt;
  510. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  511. int retval = 0;
  512. union sctp_addr saveaddr;
  513. void *addr_buf;
  514. struct sockaddr *sa_addr;
  515. struct sctp_af *af;
  516. SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
  517. sk, addrs, addrcnt);
  518. addr_buf = addrs;
  519. for (cnt = 0; cnt < addrcnt; cnt++) {
  520. /* If the bind address list is empty or if there is only one
  521. * bind address, there is nothing more to be removed (we need
  522. * at least one address here).
  523. */
  524. if (list_empty(&bp->address_list) ||
  525. (sctp_list_single_entry(&bp->address_list))) {
  526. retval = -EBUSY;
  527. goto err_bindx_rem;
  528. }
  529. /* The list may contain either IPv4 or IPv6 address;
  530. * determine the address length to copy the address to
  531. * saveaddr.
  532. */
  533. sa_addr = (struct sockaddr *)addr_buf;
  534. af = sctp_get_af_specific(sa_addr->sa_family);
  535. if (!af) {
  536. retval = -EINVAL;
  537. goto err_bindx_rem;
  538. }
  539. memcpy(&saveaddr, sa_addr, af->sockaddr_len);
  540. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  541. if (saveaddr.v4.sin_port != bp->port) {
  542. retval = -EINVAL;
  543. goto err_bindx_rem;
  544. }
  545. /* FIXME - There is probably a need to check if sk->sk_saddr and
  546. * sk->sk_rcv_addr are currently set to one of the addresses to
  547. * be removed. This is something which needs to be looked into
  548. * when we are fixing the outstanding issues with multi-homing
  549. * socket routing and failover schemes. Refer to comments in
  550. * sctp_do_bind(). -daisy
  551. */
  552. sctp_local_bh_disable();
  553. sctp_write_lock(&ep->base.addr_lock);
  554. retval = sctp_del_bind_addr(bp, &saveaddr);
  555. sctp_write_unlock(&ep->base.addr_lock);
  556. sctp_local_bh_enable();
  557. addr_buf += af->sockaddr_len;
  558. err_bindx_rem:
  559. if (retval < 0) {
  560. /* Failed. Add the ones that has been removed back */
  561. if (cnt > 0)
  562. sctp_bindx_add(sk, addrs, cnt);
  563. return retval;
  564. }
  565. }
  566. return retval;
  567. }
  568. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  569. * the associations that are part of the endpoint indicating that a list of
  570. * local addresses are removed from the endpoint.
  571. *
  572. * If any of the addresses is already in the bind address list of the
  573. * association, we do not send the chunk for that association. But it will not
  574. * affect other associations.
  575. *
  576. * Only sctp_setsockopt_bindx() is supposed to call this function.
  577. */
  578. static int sctp_send_asconf_del_ip(struct sock *sk,
  579. struct sockaddr *addrs,
  580. int addrcnt)
  581. {
  582. struct sctp_sock *sp;
  583. struct sctp_endpoint *ep;
  584. struct sctp_association *asoc;
  585. struct sctp_bind_addr *bp;
  586. struct sctp_chunk *chunk;
  587. union sctp_addr *laddr;
  588. void *addr_buf;
  589. struct sctp_af *af;
  590. struct list_head *pos;
  591. int i;
  592. int retval = 0;
  593. if (!sctp_addip_enable)
  594. return retval;
  595. sp = sctp_sk(sk);
  596. ep = sp->ep;
  597. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  598. __FUNCTION__, sk, addrs, addrcnt);
  599. list_for_each(pos, &ep->asocs) {
  600. asoc = list_entry(pos, struct sctp_association, asocs);
  601. if (!asoc->peer.asconf_capable)
  602. continue;
  603. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  604. continue;
  605. if (!sctp_state(asoc, ESTABLISHED))
  606. continue;
  607. /* Check if any address in the packed array of addresses is
  608. * not present in the bind address list of the association.
  609. * If so, do not send the asconf chunk to its peer, but
  610. * continue with other associations.
  611. */
  612. addr_buf = addrs;
  613. for (i = 0; i < addrcnt; i++) {
  614. laddr = (union sctp_addr *)addr_buf;
  615. af = sctp_get_af_specific(laddr->v4.sin_family);
  616. if (!af) {
  617. retval = -EINVAL;
  618. goto out;
  619. }
  620. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  621. break;
  622. addr_buf += af->sockaddr_len;
  623. }
  624. if (i < addrcnt)
  625. continue;
  626. /* Find one address in the association's bind address list
  627. * that is not in the packed array of addresses. This is to
  628. * make sure that we do not delete all the addresses in the
  629. * association.
  630. */
  631. sctp_read_lock(&asoc->base.addr_lock);
  632. bp = &asoc->base.bind_addr;
  633. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  634. addrcnt, sp);
  635. sctp_read_unlock(&asoc->base.addr_lock);
  636. if (!laddr)
  637. continue;
  638. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  639. SCTP_PARAM_DEL_IP);
  640. if (!chunk) {
  641. retval = -ENOMEM;
  642. goto out;
  643. }
  644. retval = sctp_send_asconf(asoc, chunk);
  645. /* FIXME: After sending the delete address ASCONF chunk, we
  646. * cannot remove the addresses from the association's bind
  647. * address list, because there maybe some packet send to
  648. * the delete addresses, so we should wait until ASCONF_ACK
  649. * packet is received.
  650. */
  651. }
  652. out:
  653. return retval;
  654. }
  655. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  656. *
  657. * API 8.1
  658. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  659. * int flags);
  660. *
  661. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  662. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  663. * or IPv6 addresses.
  664. *
  665. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  666. * Section 3.1.2 for this usage.
  667. *
  668. * addrs is a pointer to an array of one or more socket addresses. Each
  669. * address is contained in its appropriate structure (i.e. struct
  670. * sockaddr_in or struct sockaddr_in6) the family of the address type
  671. * must be used to distengish the address length (note that this
  672. * representation is termed a "packed array" of addresses). The caller
  673. * specifies the number of addresses in the array with addrcnt.
  674. *
  675. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  676. * -1, and sets errno to the appropriate error code.
  677. *
  678. * For SCTP, the port given in each socket address must be the same, or
  679. * sctp_bindx() will fail, setting errno to EINVAL.
  680. *
  681. * The flags parameter is formed from the bitwise OR of zero or more of
  682. * the following currently defined flags:
  683. *
  684. * SCTP_BINDX_ADD_ADDR
  685. *
  686. * SCTP_BINDX_REM_ADDR
  687. *
  688. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  689. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  690. * addresses from the association. The two flags are mutually exclusive;
  691. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  692. * not remove all addresses from an association; sctp_bindx() will
  693. * reject such an attempt with EINVAL.
  694. *
  695. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  696. * additional addresses with an endpoint after calling bind(). Or use
  697. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  698. * socket is associated with so that no new association accepted will be
  699. * associated with those addresses. If the endpoint supports dynamic
  700. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  701. * endpoint to send the appropriate message to the peer to change the
  702. * peers address lists.
  703. *
  704. * Adding and removing addresses from a connected association is
  705. * optional functionality. Implementations that do not support this
  706. * functionality should return EOPNOTSUPP.
  707. *
  708. * Basically do nothing but copying the addresses from user to kernel
  709. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  710. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  711. * from userspace.
  712. *
  713. * We don't use copy_from_user() for optimization: we first do the
  714. * sanity checks (buffer size -fast- and access check-healthy
  715. * pointer); if all of those succeed, then we can alloc the memory
  716. * (expensive operation) needed to copy the data to kernel. Then we do
  717. * the copying without checking the user space area
  718. * (__copy_from_user()).
  719. *
  720. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  721. * it.
  722. *
  723. * sk The sk of the socket
  724. * addrs The pointer to the addresses in user land
  725. * addrssize Size of the addrs buffer
  726. * op Operation to perform (add or remove, see the flags of
  727. * sctp_bindx)
  728. *
  729. * Returns 0 if ok, <0 errno code on error.
  730. */
  731. SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
  732. struct sockaddr __user *addrs,
  733. int addrs_size, int op)
  734. {
  735. struct sockaddr *kaddrs;
  736. int err;
  737. int addrcnt = 0;
  738. int walk_size = 0;
  739. struct sockaddr *sa_addr;
  740. void *addr_buf;
  741. struct sctp_af *af;
  742. SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
  743. " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
  744. if (unlikely(addrs_size <= 0))
  745. return -EINVAL;
  746. /* Check the user passed a healthy pointer. */
  747. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  748. return -EFAULT;
  749. /* Alloc space for the address array in kernel memory. */
  750. kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
  751. if (unlikely(!kaddrs))
  752. return -ENOMEM;
  753. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  754. kfree(kaddrs);
  755. return -EFAULT;
  756. }
  757. /* Walk through the addrs buffer and count the number of addresses. */
  758. addr_buf = kaddrs;
  759. while (walk_size < addrs_size) {
  760. sa_addr = (struct sockaddr *)addr_buf;
  761. af = sctp_get_af_specific(sa_addr->sa_family);
  762. /* If the address family is not supported or if this address
  763. * causes the address buffer to overflow return EINVAL.
  764. */
  765. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  766. kfree(kaddrs);
  767. return -EINVAL;
  768. }
  769. addrcnt++;
  770. addr_buf += af->sockaddr_len;
  771. walk_size += af->sockaddr_len;
  772. }
  773. /* Do the work. */
  774. switch (op) {
  775. case SCTP_BINDX_ADD_ADDR:
  776. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  777. if (err)
  778. goto out;
  779. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  780. break;
  781. case SCTP_BINDX_REM_ADDR:
  782. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  783. if (err)
  784. goto out;
  785. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  786. break;
  787. default:
  788. err = -EINVAL;
  789. break;
  790. };
  791. out:
  792. kfree(kaddrs);
  793. return err;
  794. }
  795. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  796. *
  797. * Common routine for handling connect() and sctp_connectx().
  798. * Connect will come in with just a single address.
  799. */
  800. static int __sctp_connect(struct sock* sk,
  801. struct sockaddr *kaddrs,
  802. int addrs_size)
  803. {
  804. struct sctp_sock *sp;
  805. struct sctp_endpoint *ep;
  806. struct sctp_association *asoc = NULL;
  807. struct sctp_association *asoc2;
  808. struct sctp_transport *transport;
  809. union sctp_addr to;
  810. struct sctp_af *af;
  811. sctp_scope_t scope;
  812. long timeo;
  813. int err = 0;
  814. int addrcnt = 0;
  815. int walk_size = 0;
  816. struct sockaddr *sa_addr;
  817. void *addr_buf;
  818. sp = sctp_sk(sk);
  819. ep = sp->ep;
  820. /* connect() cannot be done on a socket that is already in ESTABLISHED
  821. * state - UDP-style peeled off socket or a TCP-style socket that
  822. * is already connected.
  823. * It cannot be done even on a TCP-style listening socket.
  824. */
  825. if (sctp_sstate(sk, ESTABLISHED) ||
  826. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
  827. err = -EISCONN;
  828. goto out_free;
  829. }
  830. /* Walk through the addrs buffer and count the number of addresses. */
  831. addr_buf = kaddrs;
  832. while (walk_size < addrs_size) {
  833. sa_addr = (struct sockaddr *)addr_buf;
  834. af = sctp_get_af_specific(sa_addr->sa_family);
  835. /* If the address family is not supported or if this address
  836. * causes the address buffer to overflow return EINVAL.
  837. */
  838. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  839. err = -EINVAL;
  840. goto out_free;
  841. }
  842. err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
  843. af->sockaddr_len);
  844. if (err)
  845. goto out_free;
  846. memcpy(&to, sa_addr, af->sockaddr_len);
  847. to.v4.sin_port = ntohs(to.v4.sin_port);
  848. /* Check if there already is a matching association on the
  849. * endpoint (other than the one created here).
  850. */
  851. asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  852. if (asoc2 && asoc2 != asoc) {
  853. if (asoc2->state >= SCTP_STATE_ESTABLISHED)
  854. err = -EISCONN;
  855. else
  856. err = -EALREADY;
  857. goto out_free;
  858. }
  859. /* If we could not find a matching association on the endpoint,
  860. * make sure that there is no peeled-off association matching
  861. * the peer address even on another socket.
  862. */
  863. if (sctp_endpoint_is_peeled_off(ep, &to)) {
  864. err = -EADDRNOTAVAIL;
  865. goto out_free;
  866. }
  867. if (!asoc) {
  868. /* If a bind() or sctp_bindx() is not called prior to
  869. * an sctp_connectx() call, the system picks an
  870. * ephemeral port and will choose an address set
  871. * equivalent to binding with a wildcard address.
  872. */
  873. if (!ep->base.bind_addr.port) {
  874. if (sctp_autobind(sk)) {
  875. err = -EAGAIN;
  876. goto out_free;
  877. }
  878. } else {
  879. /*
  880. * If an unprivileged user inherits a 1-many
  881. * style socket with open associations on a
  882. * privileged port, it MAY be permitted to
  883. * accept new associations, but it SHOULD NOT
  884. * be permitted to open new associations.
  885. */
  886. if (ep->base.bind_addr.port < PROT_SOCK &&
  887. !capable(CAP_NET_BIND_SERVICE)) {
  888. err = -EACCES;
  889. goto out_free;
  890. }
  891. }
  892. scope = sctp_scope(&to);
  893. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  894. if (!asoc) {
  895. err = -ENOMEM;
  896. goto out_free;
  897. }
  898. }
  899. /* Prime the peer's transport structures. */
  900. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
  901. SCTP_UNKNOWN);
  902. if (!transport) {
  903. err = -ENOMEM;
  904. goto out_free;
  905. }
  906. addrcnt++;
  907. addr_buf += af->sockaddr_len;
  908. walk_size += af->sockaddr_len;
  909. }
  910. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  911. if (err < 0) {
  912. goto out_free;
  913. }
  914. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  915. if (err < 0) {
  916. goto out_free;
  917. }
  918. /* Initialize sk's dport and daddr for getpeername() */
  919. inet_sk(sk)->dport = htons(asoc->peer.port);
  920. af = sctp_get_af_specific(to.sa.sa_family);
  921. af->to_sk_daddr(&to, sk);
  922. timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  923. err = sctp_wait_for_connect(asoc, &timeo);
  924. /* Don't free association on exit. */
  925. asoc = NULL;
  926. out_free:
  927. SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
  928. " kaddrs: %p err: %d\n",
  929. asoc, kaddrs, err);
  930. if (asoc)
  931. sctp_association_free(asoc);
  932. return err;
  933. }
  934. /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
  935. *
  936. * API 8.9
  937. * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
  938. *
  939. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  940. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  941. * or IPv6 addresses.
  942. *
  943. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  944. * Section 3.1.2 for this usage.
  945. *
  946. * addrs is a pointer to an array of one or more socket addresses. Each
  947. * address is contained in its appropriate structure (i.e. struct
  948. * sockaddr_in or struct sockaddr_in6) the family of the address type
  949. * must be used to distengish the address length (note that this
  950. * representation is termed a "packed array" of addresses). The caller
  951. * specifies the number of addresses in the array with addrcnt.
  952. *
  953. * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
  954. * -1, and sets errno to the appropriate error code.
  955. *
  956. * For SCTP, the port given in each socket address must be the same, or
  957. * sctp_connectx() will fail, setting errno to EINVAL.
  958. *
  959. * An application can use sctp_connectx to initiate an association with
  960. * an endpoint that is multi-homed. Much like sctp_bindx() this call
  961. * allows a caller to specify multiple addresses at which a peer can be
  962. * reached. The way the SCTP stack uses the list of addresses to set up
  963. * the association is implementation dependant. This function only
  964. * specifies that the stack will try to make use of all the addresses in
  965. * the list when needed.
  966. *
  967. * Note that the list of addresses passed in is only used for setting up
  968. * the association. It does not necessarily equal the set of addresses
  969. * the peer uses for the resulting association. If the caller wants to
  970. * find out the set of peer addresses, it must use sctp_getpaddrs() to
  971. * retrieve them after the association has been set up.
  972. *
  973. * Basically do nothing but copying the addresses from user to kernel
  974. * land and invoking either sctp_connectx(). This is used for tunneling
  975. * the sctp_connectx() request through sctp_setsockopt() from userspace.
  976. *
  977. * We don't use copy_from_user() for optimization: we first do the
  978. * sanity checks (buffer size -fast- and access check-healthy
  979. * pointer); if all of those succeed, then we can alloc the memory
  980. * (expensive operation) needed to copy the data to kernel. Then we do
  981. * the copying without checking the user space area
  982. * (__copy_from_user()).
  983. *
  984. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  985. * it.
  986. *
  987. * sk The sk of the socket
  988. * addrs The pointer to the addresses in user land
  989. * addrssize Size of the addrs buffer
  990. *
  991. * Returns 0 if ok, <0 errno code on error.
  992. */
  993. SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
  994. struct sockaddr __user *addrs,
  995. int addrs_size)
  996. {
  997. int err = 0;
  998. struct sockaddr *kaddrs;
  999. SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
  1000. __FUNCTION__, sk, addrs, addrs_size);
  1001. if (unlikely(addrs_size <= 0))
  1002. return -EINVAL;
  1003. /* Check the user passed a healthy pointer. */
  1004. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  1005. return -EFAULT;
  1006. /* Alloc space for the address array in kernel memory. */
  1007. kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
  1008. if (unlikely(!kaddrs))
  1009. return -ENOMEM;
  1010. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  1011. err = -EFAULT;
  1012. } else {
  1013. err = __sctp_connect(sk, kaddrs, addrs_size);
  1014. }
  1015. kfree(kaddrs);
  1016. return err;
  1017. }
  1018. /* API 3.1.4 close() - UDP Style Syntax
  1019. * Applications use close() to perform graceful shutdown (as described in
  1020. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  1021. * by a UDP-style socket.
  1022. *
  1023. * The syntax is
  1024. *
  1025. * ret = close(int sd);
  1026. *
  1027. * sd - the socket descriptor of the associations to be closed.
  1028. *
  1029. * To gracefully shutdown a specific association represented by the
  1030. * UDP-style socket, an application should use the sendmsg() call,
  1031. * passing no user data, but including the appropriate flag in the
  1032. * ancillary data (see Section xxxx).
  1033. *
  1034. * If sd in the close() call is a branched-off socket representing only
  1035. * one association, the shutdown is performed on that association only.
  1036. *
  1037. * 4.1.6 close() - TCP Style Syntax
  1038. *
  1039. * Applications use close() to gracefully close down an association.
  1040. *
  1041. * The syntax is:
  1042. *
  1043. * int close(int sd);
  1044. *
  1045. * sd - the socket descriptor of the association to be closed.
  1046. *
  1047. * After an application calls close() on a socket descriptor, no further
  1048. * socket operations will succeed on that descriptor.
  1049. *
  1050. * API 7.1.4 SO_LINGER
  1051. *
  1052. * An application using the TCP-style socket can use this option to
  1053. * perform the SCTP ABORT primitive. The linger option structure is:
  1054. *
  1055. * struct linger {
  1056. * int l_onoff; // option on/off
  1057. * int l_linger; // linger time
  1058. * };
  1059. *
  1060. * To enable the option, set l_onoff to 1. If the l_linger value is set
  1061. * to 0, calling close() is the same as the ABORT primitive. If the
  1062. * value is set to a negative value, the setsockopt() call will return
  1063. * an error. If the value is set to a positive value linger_time, the
  1064. * close() can be blocked for at most linger_time ms. If the graceful
  1065. * shutdown phase does not finish during this period, close() will
  1066. * return but the graceful shutdown phase continues in the system.
  1067. */
  1068. SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
  1069. {
  1070. struct sctp_endpoint *ep;
  1071. struct sctp_association *asoc;
  1072. struct list_head *pos, *temp;
  1073. SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
  1074. sctp_lock_sock(sk);
  1075. sk->sk_shutdown = SHUTDOWN_MASK;
  1076. ep = sctp_sk(sk)->ep;
  1077. /* Walk all associations on a socket, not on an endpoint. */
  1078. list_for_each_safe(pos, temp, &ep->asocs) {
  1079. asoc = list_entry(pos, struct sctp_association, asocs);
  1080. if (sctp_style(sk, TCP)) {
  1081. /* A closed association can still be in the list if
  1082. * it belongs to a TCP-style listening socket that is
  1083. * not yet accepted. If so, free it. If not, send an
  1084. * ABORT or SHUTDOWN based on the linger options.
  1085. */
  1086. if (sctp_state(asoc, CLOSED)) {
  1087. sctp_unhash_established(asoc);
  1088. sctp_association_free(asoc);
  1089. } else if (sock_flag(sk, SOCK_LINGER) &&
  1090. !sk->sk_lingertime)
  1091. sctp_primitive_ABORT(asoc, NULL);
  1092. else
  1093. sctp_primitive_SHUTDOWN(asoc, NULL);
  1094. } else
  1095. sctp_primitive_SHUTDOWN(asoc, NULL);
  1096. }
  1097. /* Clean up any skbs sitting on the receive queue. */
  1098. sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  1099. sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  1100. /* On a TCP-style socket, block for at most linger_time if set. */
  1101. if (sctp_style(sk, TCP) && timeout)
  1102. sctp_wait_for_close(sk, timeout);
  1103. /* This will run the backlog queue. */
  1104. sctp_release_sock(sk);
  1105. /* Supposedly, no process has access to the socket, but
  1106. * the net layers still may.
  1107. */
  1108. sctp_local_bh_disable();
  1109. sctp_bh_lock_sock(sk);
  1110. /* Hold the sock, since sk_common_release() will put sock_put()
  1111. * and we have just a little more cleanup.
  1112. */
  1113. sock_hold(sk);
  1114. sk_common_release(sk);
  1115. sctp_bh_unlock_sock(sk);
  1116. sctp_local_bh_enable();
  1117. sock_put(sk);
  1118. SCTP_DBG_OBJCNT_DEC(sock);
  1119. }
  1120. /* Handle EPIPE error. */
  1121. static int sctp_error(struct sock *sk, int flags, int err)
  1122. {
  1123. if (err == -EPIPE)
  1124. err = sock_error(sk) ? : -EPIPE;
  1125. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  1126. send_sig(SIGPIPE, current, 0);
  1127. return err;
  1128. }
  1129. /* API 3.1.3 sendmsg() - UDP Style Syntax
  1130. *
  1131. * An application uses sendmsg() and recvmsg() calls to transmit data to
  1132. * and receive data from its peer.
  1133. *
  1134. * ssize_t sendmsg(int socket, const struct msghdr *message,
  1135. * int flags);
  1136. *
  1137. * socket - the socket descriptor of the endpoint.
  1138. * message - pointer to the msghdr structure which contains a single
  1139. * user message and possibly some ancillary data.
  1140. *
  1141. * See Section 5 for complete description of the data
  1142. * structures.
  1143. *
  1144. * flags - flags sent or received with the user message, see Section
  1145. * 5 for complete description of the flags.
  1146. *
  1147. * Note: This function could use a rewrite especially when explicit
  1148. * connect support comes in.
  1149. */
  1150. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  1151. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
  1152. SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
  1153. struct msghdr *msg, size_t msg_len)
  1154. {
  1155. struct sctp_sock *sp;
  1156. struct sctp_endpoint *ep;
  1157. struct sctp_association *new_asoc=NULL, *asoc=NULL;
  1158. struct sctp_transport *transport, *chunk_tp;
  1159. struct sctp_chunk *chunk;
  1160. union sctp_addr to;
  1161. struct sockaddr *msg_name = NULL;
  1162. struct sctp_sndrcvinfo default_sinfo = { 0 };
  1163. struct sctp_sndrcvinfo *sinfo;
  1164. struct sctp_initmsg *sinit;
  1165. sctp_assoc_t associd = 0;
  1166. sctp_cmsgs_t cmsgs = { NULL };
  1167. int err;
  1168. sctp_scope_t scope;
  1169. long timeo;
  1170. __u16 sinfo_flags = 0;
  1171. struct sctp_datamsg *datamsg;
  1172. struct list_head *pos;
  1173. int msg_flags = msg->msg_flags;
  1174. SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
  1175. sk, msg, msg_len);
  1176. err = 0;
  1177. sp = sctp_sk(sk);
  1178. ep = sp->ep;
  1179. SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
  1180. /* We cannot send a message over a TCP-style listening socket. */
  1181. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
  1182. err = -EPIPE;
  1183. goto out_nounlock;
  1184. }
  1185. /* Parse out the SCTP CMSGs. */
  1186. err = sctp_msghdr_parse(msg, &cmsgs);
  1187. if (err) {
  1188. SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
  1189. goto out_nounlock;
  1190. }
  1191. /* Fetch the destination address for this packet. This
  1192. * address only selects the association--it is not necessarily
  1193. * the address we will send to.
  1194. * For a peeled-off socket, msg_name is ignored.
  1195. */
  1196. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  1197. int msg_namelen = msg->msg_namelen;
  1198. err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
  1199. msg_namelen);
  1200. if (err)
  1201. return err;
  1202. if (msg_namelen > sizeof(to))
  1203. msg_namelen = sizeof(to);
  1204. memcpy(&to, msg->msg_name, msg_namelen);
  1205. SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
  1206. "0x%x:%u.\n",
  1207. to.v4.sin_addr.s_addr, to.v4.sin_port);
  1208. to.v4.sin_port = ntohs(to.v4.sin_port);
  1209. msg_name = msg->msg_name;
  1210. }
  1211. sinfo = cmsgs.info;
  1212. sinit = cmsgs.init;
  1213. /* Did the user specify SNDRCVINFO? */
  1214. if (sinfo) {
  1215. sinfo_flags = sinfo->sinfo_flags;
  1216. associd = sinfo->sinfo_assoc_id;
  1217. }
  1218. SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
  1219. msg_len, sinfo_flags);
  1220. /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
  1221. if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
  1222. err = -EINVAL;
  1223. goto out_nounlock;
  1224. }
  1225. /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
  1226. * length messages when SCTP_EOF|SCTP_ABORT is not set.
  1227. * If SCTP_ABORT is set, the message length could be non zero with
  1228. * the msg_iov set to the user abort reason.
  1229. */
  1230. if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
  1231. (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
  1232. err = -EINVAL;
  1233. goto out_nounlock;
  1234. }
  1235. /* If SCTP_ADDR_OVER is set, there must be an address
  1236. * specified in msg_name.
  1237. */
  1238. if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
  1239. err = -EINVAL;
  1240. goto out_nounlock;
  1241. }
  1242. transport = NULL;
  1243. SCTP_DEBUG_PRINTK("About to look up association.\n");
  1244. sctp_lock_sock(sk);
  1245. /* If a msg_name has been specified, assume this is to be used. */
  1246. if (msg_name) {
  1247. /* Look for a matching association on the endpoint. */
  1248. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  1249. if (!asoc) {
  1250. /* If we could not find a matching association on the
  1251. * endpoint, make sure that it is not a TCP-style
  1252. * socket that already has an association or there is
  1253. * no peeled-off association on another socket.
  1254. */
  1255. if ((sctp_style(sk, TCP) &&
  1256. sctp_sstate(sk, ESTABLISHED)) ||
  1257. sctp_endpoint_is_peeled_off(ep, &to)) {
  1258. err = -EADDRNOTAVAIL;
  1259. goto out_unlock;
  1260. }
  1261. }
  1262. } else {
  1263. asoc = sctp_id2assoc(sk, associd);
  1264. if (!asoc) {
  1265. err = -EPIPE;
  1266. goto out_unlock;
  1267. }
  1268. }
  1269. if (asoc) {
  1270. SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
  1271. /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
  1272. * socket that has an association in CLOSED state. This can
  1273. * happen when an accepted socket has an association that is
  1274. * already CLOSED.
  1275. */
  1276. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
  1277. err = -EPIPE;
  1278. goto out_unlock;
  1279. }
  1280. if (sinfo_flags & SCTP_EOF) {
  1281. SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
  1282. asoc);
  1283. sctp_primitive_SHUTDOWN(asoc, NULL);
  1284. err = 0;
  1285. goto out_unlock;
  1286. }
  1287. if (sinfo_flags & SCTP_ABORT) {
  1288. SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
  1289. sctp_primitive_ABORT(asoc, msg);
  1290. err = 0;
  1291. goto out_unlock;
  1292. }
  1293. }
  1294. /* Do we need to create the association? */
  1295. if (!asoc) {
  1296. SCTP_DEBUG_PRINTK("There is no association yet.\n");
  1297. if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
  1298. err = -EINVAL;
  1299. goto out_unlock;
  1300. }
  1301. /* Check for invalid stream against the stream counts,
  1302. * either the default or the user specified stream counts.
  1303. */
  1304. if (sinfo) {
  1305. if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
  1306. /* Check against the defaults. */
  1307. if (sinfo->sinfo_stream >=
  1308. sp->initmsg.sinit_num_ostreams) {
  1309. err = -EINVAL;
  1310. goto out_unlock;
  1311. }
  1312. } else {
  1313. /* Check against the requested. */
  1314. if (sinfo->sinfo_stream >=
  1315. sinit->sinit_num_ostreams) {
  1316. err = -EINVAL;
  1317. goto out_unlock;
  1318. }
  1319. }
  1320. }
  1321. /*
  1322. * API 3.1.2 bind() - UDP Style Syntax
  1323. * If a bind() or sctp_bindx() is not called prior to a
  1324. * sendmsg() call that initiates a new association, the
  1325. * system picks an ephemeral port and will choose an address
  1326. * set equivalent to binding with a wildcard address.
  1327. */
  1328. if (!ep->base.bind_addr.port) {
  1329. if (sctp_autobind(sk)) {
  1330. err = -EAGAIN;
  1331. goto out_unlock;
  1332. }
  1333. } else {
  1334. /*
  1335. * If an unprivileged user inherits a one-to-many
  1336. * style socket with open associations on a privileged
  1337. * port, it MAY be permitted to accept new associations,
  1338. * but it SHOULD NOT be permitted to open new
  1339. * associations.
  1340. */
  1341. if (ep->base.bind_addr.port < PROT_SOCK &&
  1342. !capable(CAP_NET_BIND_SERVICE)) {
  1343. err = -EACCES;
  1344. goto out_unlock;
  1345. }
  1346. }
  1347. scope = sctp_scope(&to);
  1348. new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1349. if (!new_asoc) {
  1350. err = -ENOMEM;
  1351. goto out_unlock;
  1352. }
  1353. asoc = new_asoc;
  1354. /* If the SCTP_INIT ancillary data is specified, set all
  1355. * the association init values accordingly.
  1356. */
  1357. if (sinit) {
  1358. if (sinit->sinit_num_ostreams) {
  1359. asoc->c.sinit_num_ostreams =
  1360. sinit->sinit_num_ostreams;
  1361. }
  1362. if (sinit->sinit_max_instreams) {
  1363. asoc->c.sinit_max_instreams =
  1364. sinit->sinit_max_instreams;
  1365. }
  1366. if (sinit->sinit_max_attempts) {
  1367. asoc->max_init_attempts
  1368. = sinit->sinit_max_attempts;
  1369. }
  1370. if (sinit->sinit_max_init_timeo) {
  1371. asoc->max_init_timeo =
  1372. msecs_to_jiffies(sinit->sinit_max_init_timeo);
  1373. }
  1374. }
  1375. /* Prime the peer's transport structures. */
  1376. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
  1377. if (!transport) {
  1378. err = -ENOMEM;
  1379. goto out_free;
  1380. }
  1381. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  1382. if (err < 0) {
  1383. err = -ENOMEM;
  1384. goto out_free;
  1385. }
  1386. }
  1387. /* ASSERT: we have a valid association at this point. */
  1388. SCTP_DEBUG_PRINTK("We have a valid association.\n");
  1389. if (!sinfo) {
  1390. /* If the user didn't specify SNDRCVINFO, make up one with
  1391. * some defaults.
  1392. */
  1393. default_sinfo.sinfo_stream = asoc->default_stream;
  1394. default_sinfo.sinfo_flags = asoc->default_flags;
  1395. default_sinfo.sinfo_ppid = asoc->default_ppid;
  1396. default_sinfo.sinfo_context = asoc->default_context;
  1397. default_sinfo.sinfo_timetolive = asoc->default_timetolive;
  1398. default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
  1399. sinfo = &default_sinfo;
  1400. }
  1401. /* API 7.1.7, the sndbuf size per association bounds the
  1402. * maximum size of data that can be sent in a single send call.
  1403. */
  1404. if (msg_len > sk->sk_sndbuf) {
  1405. err = -EMSGSIZE;
  1406. goto out_free;
  1407. }
  1408. /* If fragmentation is disabled and the message length exceeds the
  1409. * association fragmentation point, return EMSGSIZE. The I-D
  1410. * does not specify what this error is, but this looks like
  1411. * a great fit.
  1412. */
  1413. if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
  1414. err = -EMSGSIZE;
  1415. goto out_free;
  1416. }
  1417. if (sinfo) {
  1418. /* Check for invalid stream. */
  1419. if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
  1420. err = -EINVAL;
  1421. goto out_free;
  1422. }
  1423. }
  1424. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1425. if (!sctp_wspace(asoc)) {
  1426. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
  1427. if (err)
  1428. goto out_free;
  1429. }
  1430. /* If an address is passed with the sendto/sendmsg call, it is used
  1431. * to override the primary destination address in the TCP model, or
  1432. * when SCTP_ADDR_OVER flag is set in the UDP model.
  1433. */
  1434. if ((sctp_style(sk, TCP) && msg_name) ||
  1435. (sinfo_flags & SCTP_ADDR_OVER)) {
  1436. chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
  1437. if (!chunk_tp) {
  1438. err = -EINVAL;
  1439. goto out_free;
  1440. }
  1441. } else
  1442. chunk_tp = NULL;
  1443. /* Auto-connect, if we aren't connected already. */
  1444. if (sctp_state(asoc, CLOSED)) {
  1445. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  1446. if (err < 0)
  1447. goto out_free;
  1448. SCTP_DEBUG_PRINTK("We associated primitively.\n");
  1449. }
  1450. /* Break the message into multiple chunks of maximum size. */
  1451. datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
  1452. if (!datamsg) {
  1453. err = -ENOMEM;
  1454. goto out_free;
  1455. }
  1456. /* Now send the (possibly) fragmented message. */
  1457. list_for_each(pos, &datamsg->chunks) {
  1458. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  1459. sctp_datamsg_track(chunk);
  1460. /* Do accounting for the write space. */
  1461. sctp_set_owner_w(chunk);
  1462. chunk->transport = chunk_tp;
  1463. /* Send it to the lower layers. Note: all chunks
  1464. * must either fail or succeed. The lower layer
  1465. * works that way today. Keep it that way or this
  1466. * breaks.
  1467. */
  1468. err = sctp_primitive_SEND(asoc, chunk);
  1469. /* Did the lower layer accept the chunk? */
  1470. if (err)
  1471. sctp_chunk_free(chunk);
  1472. SCTP_DEBUG_PRINTK("We sent primitively.\n");
  1473. }
  1474. sctp_datamsg_free(datamsg);
  1475. if (err)
  1476. goto out_free;
  1477. else
  1478. err = msg_len;
  1479. /* If we are already past ASSOCIATE, the lower
  1480. * layers are responsible for association cleanup.
  1481. */
  1482. goto out_unlock;
  1483. out_free:
  1484. if (new_asoc)
  1485. sctp_association_free(asoc);
  1486. out_unlock:
  1487. sctp_release_sock(sk);
  1488. out_nounlock:
  1489. return sctp_error(sk, msg_flags, err);
  1490. #if 0
  1491. do_sock_err:
  1492. if (msg_len)
  1493. err = msg_len;
  1494. else
  1495. err = sock_error(sk);
  1496. goto out;
  1497. do_interrupted:
  1498. if (msg_len)
  1499. err = msg_len;
  1500. goto out;
  1501. #endif /* 0 */
  1502. }
  1503. /* This is an extended version of skb_pull() that removes the data from the
  1504. * start of a skb even when data is spread across the list of skb's in the
  1505. * frag_list. len specifies the total amount of data that needs to be removed.
  1506. * when 'len' bytes could be removed from the skb, it returns 0.
  1507. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1508. * could not be removed.
  1509. */
  1510. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1511. {
  1512. struct sk_buff *list;
  1513. int skb_len = skb_headlen(skb);
  1514. int rlen;
  1515. if (len <= skb_len) {
  1516. __skb_pull(skb, len);
  1517. return 0;
  1518. }
  1519. len -= skb_len;
  1520. __skb_pull(skb, skb_len);
  1521. for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
  1522. rlen = sctp_skb_pull(list, len);
  1523. skb->len -= (len-rlen);
  1524. skb->data_len -= (len-rlen);
  1525. if (!rlen)
  1526. return 0;
  1527. len = rlen;
  1528. }
  1529. return len;
  1530. }
  1531. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1532. *
  1533. * ssize_t recvmsg(int socket, struct msghdr *message,
  1534. * int flags);
  1535. *
  1536. * socket - the socket descriptor of the endpoint.
  1537. * message - pointer to the msghdr structure which contains a single
  1538. * user message and possibly some ancillary data.
  1539. *
  1540. * See Section 5 for complete description of the data
  1541. * structures.
  1542. *
  1543. * flags - flags sent or received with the user message, see Section
  1544. * 5 for complete description of the flags.
  1545. */
  1546. static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
  1547. SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
  1548. struct msghdr *msg, size_t len, int noblock,
  1549. int flags, int *addr_len)
  1550. {
  1551. struct sctp_ulpevent *event = NULL;
  1552. struct sctp_sock *sp = sctp_sk(sk);
  1553. struct sk_buff *skb;
  1554. int copied;
  1555. int err = 0;
  1556. int skb_len;
  1557. SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
  1558. "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
  1559. "len", len, "knoblauch", noblock,
  1560. "flags", flags, "addr_len", addr_len);
  1561. sctp_lock_sock(sk);
  1562. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
  1563. err = -ENOTCONN;
  1564. goto out;
  1565. }
  1566. skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
  1567. if (!skb)
  1568. goto out;
  1569. /* Get the total length of the skb including any skb's in the
  1570. * frag_list.
  1571. */
  1572. skb_len = skb->len;
  1573. copied = skb_len;
  1574. if (copied > len)
  1575. copied = len;
  1576. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1577. event = sctp_skb2event(skb);
  1578. if (err)
  1579. goto out_free;
  1580. sock_recv_timestamp(msg, sk, skb);
  1581. if (sctp_ulpevent_is_notification(event)) {
  1582. msg->msg_flags |= MSG_NOTIFICATION;
  1583. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1584. } else {
  1585. sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
  1586. }
  1587. /* Check if we allow SCTP_SNDRCVINFO. */
  1588. if (sp->subscribe.sctp_data_io_event)
  1589. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1590. #if 0
  1591. /* FIXME: we should be calling IP/IPv6 layers. */
  1592. if (sk->sk_protinfo.af_inet.cmsg_flags)
  1593. ip_cmsg_recv(msg, skb);
  1594. #endif
  1595. err = copied;
  1596. /* If skb's length exceeds the user's buffer, update the skb and
  1597. * push it back to the receive_queue so that the next call to
  1598. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1599. */
  1600. if (skb_len > copied) {
  1601. msg->msg_flags &= ~MSG_EOR;
  1602. if (flags & MSG_PEEK)
  1603. goto out_free;
  1604. sctp_skb_pull(skb, copied);
  1605. skb_queue_head(&sk->sk_receive_queue, skb);
  1606. /* When only partial message is copied to the user, increase
  1607. * rwnd by that amount. If all the data in the skb is read,
  1608. * rwnd is updated when the event is freed.
  1609. */
  1610. sctp_assoc_rwnd_increase(event->asoc, copied);
  1611. goto out;
  1612. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1613. (event->msg_flags & MSG_EOR))
  1614. msg->msg_flags |= MSG_EOR;
  1615. else
  1616. msg->msg_flags &= ~MSG_EOR;
  1617. out_free:
  1618. if (flags & MSG_PEEK) {
  1619. /* Release the skb reference acquired after peeking the skb in
  1620. * sctp_skb_recv_datagram().
  1621. */
  1622. kfree_skb(skb);
  1623. } else {
  1624. /* Free the event which includes releasing the reference to
  1625. * the owner of the skb, freeing the skb and updating the
  1626. * rwnd.
  1627. */
  1628. sctp_ulpevent_free(event);
  1629. }
  1630. out:
  1631. sctp_release_sock(sk);
  1632. return err;
  1633. }
  1634. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1635. *
  1636. * This option is a on/off flag. If enabled no SCTP message
  1637. * fragmentation will be performed. Instead if a message being sent
  1638. * exceeds the current PMTU size, the message will NOT be sent and
  1639. * instead a error will be indicated to the user.
  1640. */
  1641. static int sctp_setsockopt_disable_fragments(struct sock *sk,
  1642. char __user *optval, int optlen)
  1643. {
  1644. int val;
  1645. if (optlen < sizeof(int))
  1646. return -EINVAL;
  1647. if (get_user(val, (int __user *)optval))
  1648. return -EFAULT;
  1649. sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
  1650. return 0;
  1651. }
  1652. static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
  1653. int optlen)
  1654. {
  1655. if (optlen != sizeof(struct sctp_event_subscribe))
  1656. return -EINVAL;
  1657. if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
  1658. return -EFAULT;
  1659. return 0;
  1660. }
  1661. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1662. *
  1663. * This socket option is applicable to the UDP-style socket only. When
  1664. * set it will cause associations that are idle for more than the
  1665. * specified number of seconds to automatically close. An association
  1666. * being idle is defined an association that has NOT sent or received
  1667. * user data. The special value of '0' indicates that no automatic
  1668. * close of any associations should be performed. The option expects an
  1669. * integer defining the number of seconds of idle time before an
  1670. * association is closed.
  1671. */
  1672. static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
  1673. int optlen)
  1674. {
  1675. struct sctp_sock *sp = sctp_sk(sk);
  1676. /* Applicable to UDP-style socket only */
  1677. if (sctp_style(sk, TCP))
  1678. return -EOPNOTSUPP;
  1679. if (optlen != sizeof(int))
  1680. return -EINVAL;
  1681. if (copy_from_user(&sp->autoclose, optval, optlen))
  1682. return -EFAULT;
  1683. return 0;
  1684. }
  1685. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1686. *
  1687. * Applications can enable or disable heartbeats for any peer address of
  1688. * an association, modify an address's heartbeat interval, force a
  1689. * heartbeat to be sent immediately, and adjust the address's maximum
  1690. * number of retransmissions sent before an address is considered
  1691. * unreachable. The following structure is used to access and modify an
  1692. * address's parameters:
  1693. *
  1694. * struct sctp_paddrparams {
  1695. * sctp_assoc_t spp_assoc_id;
  1696. * struct sockaddr_storage spp_address;
  1697. * uint32_t spp_hbinterval;
  1698. * uint16_t spp_pathmaxrxt;
  1699. * };
  1700. *
  1701. * spp_assoc_id - (UDP style socket) This is filled in the application,
  1702. * and identifies the association for this query.
  1703. * spp_address - This specifies which address is of interest.
  1704. * spp_hbinterval - This contains the value of the heartbeat interval,
  1705. * in milliseconds. A value of 0, when modifying the
  1706. * parameter, specifies that the heartbeat on this
  1707. * address should be disabled. A value of UINT32_MAX
  1708. * (4294967295), when modifying the parameter,
  1709. * specifies that a heartbeat should be sent
  1710. * immediately to the peer address, and the current
  1711. * interval should remain unchanged.
  1712. * spp_pathmaxrxt - This contains the maximum number of
  1713. * retransmissions before this address shall be
  1714. * considered unreachable.
  1715. */
  1716. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  1717. char __user *optval, int optlen)
  1718. {
  1719. struct sctp_paddrparams params;
  1720. struct sctp_transport *trans;
  1721. int error;
  1722. if (optlen != sizeof(struct sctp_paddrparams))
  1723. return -EINVAL;
  1724. if (copy_from_user(&params, optval, optlen))
  1725. return -EFAULT;
  1726. /*
  1727. * API 7. Socket Options (setting the default value for the endpoint)
  1728. * All options that support specific settings on an association by
  1729. * filling in either an association id variable or a sockaddr_storage
  1730. * SHOULD also support setting of the same value for the entire endpoint
  1731. * (i.e. future associations). To accomplish this the following logic is
  1732. * used when setting one of these options:
  1733. * c) If neither the sockaddr_storage or association identification is
  1734. * set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and
  1735. * the association identification is 0, the settings are a default
  1736. * and to be applied to the endpoint (all future associations).
  1737. */
  1738. /* update default value for endpoint (all future associations) */
  1739. if (!params.spp_assoc_id &&
  1740. sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  1741. /* Manual heartbeat on an endpoint is invalid. */
  1742. if (0xffffffff == params.spp_hbinterval)
  1743. return -EINVAL;
  1744. else if (params.spp_hbinterval)
  1745. sctp_sk(sk)->paddrparam.spp_hbinterval =
  1746. params.spp_hbinterval;
  1747. if (params.spp_pathmaxrxt)
  1748. sctp_sk(sk)->paddrparam.spp_pathmaxrxt =
  1749. params.spp_pathmaxrxt;
  1750. return 0;
  1751. }
  1752. trans = sctp_addr_id2transport(sk, &params.spp_address,
  1753. params.spp_assoc_id);
  1754. if (!trans)
  1755. return -EINVAL;
  1756. /* Applications can enable or disable heartbeats for any peer address
  1757. * of an association, modify an address's heartbeat interval, force a
  1758. * heartbeat to be sent immediately, and adjust the address's maximum
  1759. * number of retransmissions sent before an address is considered
  1760. * unreachable.
  1761. *
  1762. * The value of the heartbeat interval, in milliseconds. A value of
  1763. * UINT32_MAX (4294967295), when modifying the parameter, specifies
  1764. * that a heartbeat should be sent immediately to the peer address,
  1765. * and the current interval should remain unchanged.
  1766. */
  1767. if (0xffffffff == params.spp_hbinterval) {
  1768. error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
  1769. if (error)
  1770. return error;
  1771. } else {
  1772. /* The value of the heartbeat interval, in milliseconds. A value of 0,
  1773. * when modifying the parameter, specifies that the heartbeat on this
  1774. * address should be disabled.
  1775. */
  1776. if (params.spp_hbinterval) {
  1777. trans->hb_allowed = 1;
  1778. trans->hb_interval =
  1779. msecs_to_jiffies(params.spp_hbinterval);
  1780. } else
  1781. trans->hb_allowed = 0;
  1782. }
  1783. /* spp_pathmaxrxt contains the maximum number of retransmissions
  1784. * before this address shall be considered unreachable.
  1785. */
  1786. if (params.spp_pathmaxrxt)
  1787. trans->max_retrans = params.spp_pathmaxrxt;
  1788. return 0;
  1789. }
  1790. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  1791. *
  1792. * Applications can specify protocol parameters for the default association
  1793. * initialization. The option name argument to setsockopt() and getsockopt()
  1794. * is SCTP_INITMSG.
  1795. *
  1796. * Setting initialization parameters is effective only on an unconnected
  1797. * socket (for UDP-style sockets only future associations are effected
  1798. * by the change). With TCP-style sockets, this option is inherited by
  1799. * sockets derived from a listener socket.
  1800. */
  1801. static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
  1802. {
  1803. struct sctp_initmsg sinit;
  1804. struct sctp_sock *sp = sctp_sk(sk);
  1805. if (optlen != sizeof(struct sctp_initmsg))
  1806. return -EINVAL;
  1807. if (copy_from_user(&sinit, optval, optlen))
  1808. return -EFAULT;
  1809. if (sinit.sinit_num_ostreams)
  1810. sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
  1811. if (sinit.sinit_max_instreams)
  1812. sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
  1813. if (sinit.sinit_max_attempts)
  1814. sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
  1815. if (sinit.sinit_max_init_timeo)
  1816. sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
  1817. return 0;
  1818. }
  1819. /*
  1820. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  1821. *
  1822. * Applications that wish to use the sendto() system call may wish to
  1823. * specify a default set of parameters that would normally be supplied
  1824. * through the inclusion of ancillary data. This socket option allows
  1825. * such an application to set the default sctp_sndrcvinfo structure.
  1826. * The application that wishes to use this socket option simply passes
  1827. * in to this call the sctp_sndrcvinfo structure defined in Section
  1828. * 5.2.2) The input parameters accepted by this call include
  1829. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  1830. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  1831. * to this call if the caller is using the UDP model.
  1832. */
  1833. static int sctp_setsockopt_default_send_param(struct sock *sk,
  1834. char __user *optval, int optlen)
  1835. {
  1836. struct sctp_sndrcvinfo info;
  1837. struct sctp_association *asoc;
  1838. struct sctp_sock *sp = sctp_sk(sk);
  1839. if (optlen != sizeof(struct sctp_sndrcvinfo))
  1840. return -EINVAL;
  1841. if (copy_from_user(&info, optval, optlen))
  1842. return -EFAULT;
  1843. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  1844. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  1845. return -EINVAL;
  1846. if (asoc) {
  1847. asoc->default_stream = info.sinfo_stream;
  1848. asoc->default_flags = info.sinfo_flags;
  1849. asoc->default_ppid = info.sinfo_ppid;
  1850. asoc->default_context = info.sinfo_context;
  1851. asoc->default_timetolive = info.sinfo_timetolive;
  1852. } else {
  1853. sp->default_stream = info.sinfo_stream;
  1854. sp->default_flags = info.sinfo_flags;
  1855. sp->default_ppid = info.sinfo_ppid;
  1856. sp->default_context = info.sinfo_context;
  1857. sp->default_timetolive = info.sinfo_timetolive;
  1858. }
  1859. return 0;
  1860. }
  1861. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  1862. *
  1863. * Requests that the local SCTP stack use the enclosed peer address as
  1864. * the association primary. The enclosed address must be one of the
  1865. * association peer's addresses.
  1866. */
  1867. static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
  1868. int optlen)
  1869. {
  1870. struct sctp_prim prim;
  1871. struct sctp_transport *trans;
  1872. if (optlen != sizeof(struct sctp_prim))
  1873. return -EINVAL;
  1874. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  1875. return -EFAULT;
  1876. trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
  1877. if (!trans)
  1878. return -EINVAL;
  1879. sctp_assoc_set_primary(trans->asoc, trans);
  1880. return 0;
  1881. }
  1882. /*
  1883. * 7.1.5 SCTP_NODELAY
  1884. *
  1885. * Turn on/off any Nagle-like algorithm. This means that packets are
  1886. * generally sent as soon as possible and no unnecessary delays are
  1887. * introduced, at the cost of more packets in the network. Expects an
  1888. * integer boolean flag.
  1889. */
  1890. static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
  1891. int optlen)
  1892. {
  1893. int val;
  1894. if (optlen < sizeof(int))
  1895. return -EINVAL;
  1896. if (get_user(val, (int __user *)optval))
  1897. return -EFAULT;
  1898. sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
  1899. return 0;
  1900. }
  1901. /*
  1902. *
  1903. * 7.1.1 SCTP_RTOINFO
  1904. *
  1905. * The protocol parameters used to initialize and bound retransmission
  1906. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  1907. * and modify these parameters.
  1908. * All parameters are time values, in milliseconds. A value of 0, when
  1909. * modifying the parameters, indicates that the current value should not
  1910. * be changed.
  1911. *
  1912. */
  1913. static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
  1914. struct sctp_rtoinfo rtoinfo;
  1915. struct sctp_association *asoc;
  1916. if (optlen != sizeof (struct sctp_rtoinfo))
  1917. return -EINVAL;
  1918. if (copy_from_user(&rtoinfo, optval, optlen))
  1919. return -EFAULT;
  1920. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  1921. /* Set the values to the specific association */
  1922. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  1923. return -EINVAL;
  1924. if (asoc) {
  1925. if (rtoinfo.srto_initial != 0)
  1926. asoc->rto_initial =
  1927. msecs_to_jiffies(rtoinfo.srto_initial);
  1928. if (rtoinfo.srto_max != 0)
  1929. asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
  1930. if (rtoinfo.srto_min != 0)
  1931. asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
  1932. } else {
  1933. /* If there is no association or the association-id = 0
  1934. * set the values to the endpoint.
  1935. */
  1936. struct sctp_sock *sp = sctp_sk(sk);
  1937. if (rtoinfo.srto_initial != 0)
  1938. sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
  1939. if (rtoinfo.srto_max != 0)
  1940. sp->rtoinfo.srto_max = rtoinfo.srto_max;
  1941. if (rtoinfo.srto_min != 0)
  1942. sp->rtoinfo.srto_min = rtoinfo.srto_min;
  1943. }
  1944. return 0;
  1945. }
  1946. /*
  1947. *
  1948. * 7.1.2 SCTP_ASSOCINFO
  1949. *
  1950. * This option is used to tune the the maximum retransmission attempts
  1951. * of the association.
  1952. * Returns an error if the new association retransmission value is
  1953. * greater than the sum of the retransmission value of the peer.
  1954. * See [SCTP] for more information.
  1955. *
  1956. */
  1957. static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
  1958. {
  1959. struct sctp_assocparams assocparams;
  1960. struct sctp_association *asoc;
  1961. if (optlen != sizeof(struct sctp_assocparams))
  1962. return -EINVAL;
  1963. if (copy_from_user(&assocparams, optval, optlen))
  1964. return -EFAULT;
  1965. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  1966. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  1967. return -EINVAL;
  1968. /* Set the values to the specific association */
  1969. if (asoc) {
  1970. if (assocparams.sasoc_asocmaxrxt != 0)
  1971. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  1972. if (assocparams.sasoc_cookie_life != 0) {
  1973. asoc->cookie_life.tv_sec =
  1974. assocparams.sasoc_cookie_life / 1000;
  1975. asoc->cookie_life.tv_usec =
  1976. (assocparams.sasoc_cookie_life % 1000)
  1977. * 1000;
  1978. }
  1979. } else {
  1980. /* Set the values to the endpoint */
  1981. struct sctp_sock *sp = sctp_sk(sk);
  1982. if (assocparams.sasoc_asocmaxrxt != 0)
  1983. sp->assocparams.sasoc_asocmaxrxt =
  1984. assocparams.sasoc_asocmaxrxt;
  1985. if (assocparams.sasoc_cookie_life != 0)
  1986. sp->assocparams.sasoc_cookie_life =
  1987. assocparams.sasoc_cookie_life;
  1988. }
  1989. return 0;
  1990. }
  1991. /*
  1992. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  1993. *
  1994. * This socket option is a boolean flag which turns on or off mapped V4
  1995. * addresses. If this option is turned on and the socket is type
  1996. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  1997. * If this option is turned off, then no mapping will be done of V4
  1998. * addresses and a user will receive both PF_INET6 and PF_INET type
  1999. * addresses on the socket.
  2000. */
  2001. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
  2002. {
  2003. int val;
  2004. struct sctp_sock *sp = sctp_sk(sk);
  2005. if (optlen < sizeof(int))
  2006. return -EINVAL;
  2007. if (get_user(val, (int __user *)optval))
  2008. return -EFAULT;
  2009. if (val)
  2010. sp->v4mapped = 1;
  2011. else
  2012. sp->v4mapped = 0;
  2013. return 0;
  2014. }
  2015. /*
  2016. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  2017. *
  2018. * This socket option specifies the maximum size to put in any outgoing
  2019. * SCTP chunk. If a message is larger than this size it will be
  2020. * fragmented by SCTP into the specified size. Note that the underlying
  2021. * SCTP implementation may fragment into smaller sized chunks when the
  2022. * PMTU of the underlying association is smaller than the value set by
  2023. * the user.
  2024. */
  2025. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
  2026. {
  2027. struct sctp_association *asoc;
  2028. struct list_head *pos;
  2029. struct sctp_sock *sp = sctp_sk(sk);
  2030. int val;
  2031. if (optlen < sizeof(int))
  2032. return -EINVAL;
  2033. if (get_user(val, (int __user *)optval))
  2034. return -EFAULT;
  2035. if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
  2036. return -EINVAL;
  2037. sp->user_frag = val;
  2038. /* Update the frag_point of the existing associations. */
  2039. list_for_each(pos, &(sp->ep->asocs)) {
  2040. asoc = list_entry(pos, struct sctp_association, asocs);
  2041. asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
  2042. }
  2043. return 0;
  2044. }
  2045. /*
  2046. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2047. *
  2048. * Requests that the peer mark the enclosed address as the association
  2049. * primary. The enclosed address must be one of the association's
  2050. * locally bound addresses. The following structure is used to make a
  2051. * set primary request:
  2052. */
  2053. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  2054. int optlen)
  2055. {
  2056. struct sctp_sock *sp;
  2057. struct sctp_endpoint *ep;
  2058. struct sctp_association *asoc = NULL;
  2059. struct sctp_setpeerprim prim;
  2060. struct sctp_chunk *chunk;
  2061. int err;
  2062. sp = sctp_sk(sk);
  2063. ep = sp->ep;
  2064. if (!sctp_addip_enable)
  2065. return -EPERM;
  2066. if (optlen != sizeof(struct sctp_setpeerprim))
  2067. return -EINVAL;
  2068. if (copy_from_user(&prim, optval, optlen))
  2069. return -EFAULT;
  2070. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  2071. if (!asoc)
  2072. return -EINVAL;
  2073. if (!asoc->peer.asconf_capable)
  2074. return -EPERM;
  2075. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2076. return -EPERM;
  2077. if (!sctp_state(asoc, ESTABLISHED))
  2078. return -ENOTCONN;
  2079. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  2080. return -EADDRNOTAVAIL;
  2081. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2082. chunk = sctp_make_asconf_set_prim(asoc,
  2083. (union sctp_addr *)&prim.sspp_addr);
  2084. if (!chunk)
  2085. return -ENOMEM;
  2086. err = sctp_send_asconf(asoc, chunk);
  2087. SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
  2088. return err;
  2089. }
  2090. static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
  2091. int optlen)
  2092. {
  2093. struct sctp_setadaption adaption;
  2094. if (optlen != sizeof(struct sctp_setadaption))
  2095. return -EINVAL;
  2096. if (copy_from_user(&adaption, optval, optlen))
  2097. return -EFAULT;
  2098. sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
  2099. return 0;
  2100. }
  2101. /* API 6.2 setsockopt(), getsockopt()
  2102. *
  2103. * Applications use setsockopt() and getsockopt() to set or retrieve
  2104. * socket options. Socket options are used to change the default
  2105. * behavior of sockets calls. They are described in Section 7.
  2106. *
  2107. * The syntax is:
  2108. *
  2109. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  2110. * int __user *optlen);
  2111. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  2112. * int optlen);
  2113. *
  2114. * sd - the socket descript.
  2115. * level - set to IPPROTO_SCTP for all SCTP options.
  2116. * optname - the option name.
  2117. * optval - the buffer to store the value of the option.
  2118. * optlen - the size of the buffer.
  2119. */
  2120. SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
  2121. char __user *optval, int optlen)
  2122. {
  2123. int retval = 0;
  2124. SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
  2125. sk, optname);
  2126. /* I can hardly begin to describe how wrong this is. This is
  2127. * so broken as to be worse than useless. The API draft
  2128. * REALLY is NOT helpful here... I am not convinced that the
  2129. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  2130. * are at all well-founded.
  2131. */
  2132. if (level != SOL_SCTP) {
  2133. struct sctp_af *af = sctp_sk(sk)->pf->af;
  2134. retval = af->setsockopt(sk, level, optname, optval, optlen);
  2135. goto out_nounlock;
  2136. }
  2137. sctp_lock_sock(sk);
  2138. switch (optname) {
  2139. case SCTP_SOCKOPT_BINDX_ADD:
  2140. /* 'optlen' is the size of the addresses buffer. */
  2141. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2142. optlen, SCTP_BINDX_ADD_ADDR);
  2143. break;
  2144. case SCTP_SOCKOPT_BINDX_REM:
  2145. /* 'optlen' is the size of the addresses buffer. */
  2146. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2147. optlen, SCTP_BINDX_REM_ADDR);
  2148. break;
  2149. case SCTP_SOCKOPT_CONNECTX:
  2150. /* 'optlen' is the size of the addresses buffer. */
  2151. retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
  2152. optlen);
  2153. break;
  2154. case SCTP_DISABLE_FRAGMENTS:
  2155. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  2156. break;
  2157. case SCTP_EVENTS:
  2158. retval = sctp_setsockopt_events(sk, optval, optlen);
  2159. break;
  2160. case SCTP_AUTOCLOSE:
  2161. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  2162. break;
  2163. case SCTP_PEER_ADDR_PARAMS:
  2164. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  2165. break;
  2166. case SCTP_INITMSG:
  2167. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  2168. break;
  2169. case SCTP_DEFAULT_SEND_PARAM:
  2170. retval = sctp_setsockopt_default_send_param(sk, optval,
  2171. optlen);
  2172. break;
  2173. case SCTP_PRIMARY_ADDR:
  2174. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  2175. break;
  2176. case SCTP_SET_PEER_PRIMARY_ADDR:
  2177. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  2178. break;
  2179. case SCTP_NODELAY:
  2180. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  2181. break;
  2182. case SCTP_RTOINFO:
  2183. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  2184. break;
  2185. case SCTP_ASSOCINFO:
  2186. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  2187. break;
  2188. case SCTP_I_WANT_MAPPED_V4_ADDR:
  2189. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  2190. break;
  2191. case SCTP_MAXSEG:
  2192. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  2193. break;
  2194. case SCTP_ADAPTION_LAYER:
  2195. retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
  2196. break;
  2197. default:
  2198. retval = -ENOPROTOOPT;
  2199. break;
  2200. };
  2201. sctp_release_sock(sk);
  2202. out_nounlock:
  2203. return retval;
  2204. }
  2205. /* API 3.1.6 connect() - UDP Style Syntax
  2206. *
  2207. * An application may use the connect() call in the UDP model to initiate an
  2208. * association without sending data.
  2209. *
  2210. * The syntax is:
  2211. *
  2212. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  2213. *
  2214. * sd: the socket descriptor to have a new association added to.
  2215. *
  2216. * nam: the address structure (either struct sockaddr_in or struct
  2217. * sockaddr_in6 defined in RFC2553 [7]).
  2218. *
  2219. * len: the size of the address.
  2220. */
  2221. SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
  2222. int addr_len)
  2223. {
  2224. int err = 0;
  2225. struct sctp_af *af;
  2226. sctp_lock_sock(sk);
  2227. SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
  2228. __FUNCTION__, sk, addr, addr_len);
  2229. /* Validate addr_len before calling common connect/connectx routine. */
  2230. af = sctp_get_af_specific(addr->sa_family);
  2231. if (!af || addr_len < af->sockaddr_len) {
  2232. err = -EINVAL;
  2233. } else {
  2234. /* Pass correct addr len to common routine (so it knows there
  2235. * is only one address being passed.
  2236. */
  2237. err = __sctp_connect(sk, addr, af->sockaddr_len);
  2238. }
  2239. sctp_release_sock(sk);
  2240. return err;
  2241. }
  2242. /* FIXME: Write comments. */
  2243. SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
  2244. {
  2245. return -EOPNOTSUPP; /* STUB */
  2246. }
  2247. /* 4.1.4 accept() - TCP Style Syntax
  2248. *
  2249. * Applications use accept() call to remove an established SCTP
  2250. * association from the accept queue of the endpoint. A new socket
  2251. * descriptor will be returned from accept() to represent the newly
  2252. * formed association.
  2253. */
  2254. SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
  2255. {
  2256. struct sctp_sock *sp;
  2257. struct sctp_endpoint *ep;
  2258. struct sock *newsk = NULL;
  2259. struct sctp_association *asoc;
  2260. long timeo;
  2261. int error = 0;
  2262. sctp_lock_sock(sk);
  2263. sp = sctp_sk(sk);
  2264. ep = sp->ep;
  2265. if (!sctp_style(sk, TCP)) {
  2266. error = -EOPNOTSUPP;
  2267. goto out;
  2268. }
  2269. if (!sctp_sstate(sk, LISTENING)) {
  2270. error = -EINVAL;
  2271. goto out;
  2272. }
  2273. timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  2274. error = sctp_wait_for_accept(sk, timeo);
  2275. if (error)
  2276. goto out;
  2277. /* We treat the list of associations on the endpoint as the accept
  2278. * queue and pick the first association on the list.
  2279. */
  2280. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  2281. newsk = sp->pf->create_accept_sk(sk, asoc);
  2282. if (!newsk) {
  2283. error = -ENOMEM;
  2284. goto out;
  2285. }
  2286. /* Populate the fields of the newsk from the oldsk and migrate the
  2287. * asoc to the newsk.
  2288. */
  2289. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  2290. out:
  2291. sctp_release_sock(sk);
  2292. *err = error;
  2293. return newsk;
  2294. }
  2295. /* The SCTP ioctl handler. */
  2296. SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  2297. {
  2298. return -ENOIOCTLCMD;
  2299. }
  2300. /* This is the function which gets called during socket creation to
  2301. * initialized the SCTP-specific portion of the sock.
  2302. * The sock structure should already be zero-filled memory.
  2303. */
  2304. SCTP_STATIC int sctp_init_sock(struct sock *sk)
  2305. {
  2306. struct sctp_endpoint *ep;
  2307. struct sctp_sock *sp;
  2308. SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
  2309. sp = sctp_sk(sk);
  2310. /* Initialize the SCTP per socket area. */
  2311. switch (sk->sk_type) {
  2312. case SOCK_SEQPACKET:
  2313. sp->type = SCTP_SOCKET_UDP;
  2314. break;
  2315. case SOCK_STREAM:
  2316. sp->type = SCTP_SOCKET_TCP;
  2317. break;
  2318. default:
  2319. return -ESOCKTNOSUPPORT;
  2320. }
  2321. /* Initialize default send parameters. These parameters can be
  2322. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  2323. */
  2324. sp->default_stream = 0;
  2325. sp->default_ppid = 0;
  2326. sp->default_flags = 0;
  2327. sp->default_context = 0;
  2328. sp->default_timetolive = 0;
  2329. /* Initialize default setup parameters. These parameters
  2330. * can be modified with the SCTP_INITMSG socket option or
  2331. * overridden by the SCTP_INIT CMSG.
  2332. */
  2333. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  2334. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  2335. sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
  2336. sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
  2337. /* Initialize default RTO related parameters. These parameters can
  2338. * be modified for with the SCTP_RTOINFO socket option.
  2339. */
  2340. sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
  2341. sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
  2342. sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
  2343. /* Initialize default association related parameters. These parameters
  2344. * can be modified with the SCTP_ASSOCINFO socket option.
  2345. */
  2346. sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
  2347. sp->assocparams.sasoc_number_peer_destinations = 0;
  2348. sp->assocparams.sasoc_peer_rwnd = 0;
  2349. sp->assocparams.sasoc_local_rwnd = 0;
  2350. sp->assocparams.sasoc_cookie_life =
  2351. jiffies_to_msecs(sctp_valid_cookie_life);
  2352. /* Initialize default event subscriptions. By default, all the
  2353. * options are off.
  2354. */
  2355. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  2356. /* Default Peer Address Parameters. These defaults can
  2357. * be modified via SCTP_PEER_ADDR_PARAMS
  2358. */
  2359. sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
  2360. sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
  2361. /* If enabled no SCTP message fragmentation will be performed.
  2362. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  2363. */
  2364. sp->disable_fragments = 0;
  2365. /* Turn on/off any Nagle-like algorithm. */
  2366. sp->nodelay = 1;
  2367. /* Enable by default. */
  2368. sp->v4mapped = 1;
  2369. /* Auto-close idle associations after the configured
  2370. * number of seconds. A value of 0 disables this
  2371. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  2372. * for UDP-style sockets only.
  2373. */
  2374. sp->autoclose = 0;
  2375. /* User specified fragmentation limit. */
  2376. sp->user_frag = 0;
  2377. sp->adaption_ind = 0;
  2378. sp->pf = sctp_get_pf_specific(sk->sk_family);
  2379. /* Control variables for partial data delivery. */
  2380. sp->pd_mode = 0;
  2381. skb_queue_head_init(&sp->pd_lobby);
  2382. /* Create a per socket endpoint structure. Even if we
  2383. * change the data structure relationships, this may still
  2384. * be useful for storing pre-connect address information.
  2385. */
  2386. ep = sctp_endpoint_new(sk, GFP_KERNEL);
  2387. if (!ep)
  2388. return -ENOMEM;
  2389. sp->ep = ep;
  2390. sp->hmac = NULL;
  2391. SCTP_DBG_OBJCNT_INC(sock);
  2392. return 0;
  2393. }
  2394. /* Cleanup any SCTP per socket resources. */
  2395. SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
  2396. {
  2397. struct sctp_endpoint *ep;
  2398. SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
  2399. /* Release our hold on the endpoint. */
  2400. ep = sctp_sk(sk)->ep;
  2401. sctp_endpoint_free(ep);
  2402. return 0;
  2403. }
  2404. /* API 4.1.7 shutdown() - TCP Style Syntax
  2405. * int shutdown(int socket, int how);
  2406. *
  2407. * sd - the socket descriptor of the association to be closed.
  2408. * how - Specifies the type of shutdown. The values are
  2409. * as follows:
  2410. * SHUT_RD
  2411. * Disables further receive operations. No SCTP
  2412. * protocol action is taken.
  2413. * SHUT_WR
  2414. * Disables further send operations, and initiates
  2415. * the SCTP shutdown sequence.
  2416. * SHUT_RDWR
  2417. * Disables further send and receive operations
  2418. * and initiates the SCTP shutdown sequence.
  2419. */
  2420. SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
  2421. {
  2422. struct sctp_endpoint *ep;
  2423. struct sctp_association *asoc;
  2424. if (!sctp_style(sk, TCP))
  2425. return;
  2426. if (how & SEND_SHUTDOWN) {
  2427. ep = sctp_sk(sk)->ep;
  2428. if (!list_empty(&ep->asocs)) {
  2429. asoc = list_entry(ep->asocs.next,
  2430. struct sctp_association, asocs);
  2431. sctp_primitive_SHUTDOWN(asoc, NULL);
  2432. }
  2433. }
  2434. }
  2435. /* 7.2.1 Association Status (SCTP_STATUS)
  2436. * Applications can retrieve current status information about an
  2437. * association, including association state, peer receiver window size,
  2438. * number of unacked data chunks, and number of data chunks pending
  2439. * receipt. This information is read-only.
  2440. */
  2441. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  2442. char __user *optval,
  2443. int __user *optlen)
  2444. {
  2445. struct sctp_status status;
  2446. struct sctp_association *asoc = NULL;
  2447. struct sctp_transport *transport;
  2448. sctp_assoc_t associd;
  2449. int retval = 0;
  2450. if (len != sizeof(status)) {
  2451. retval = -EINVAL;
  2452. goto out;
  2453. }
  2454. if (copy_from_user(&status, optval, sizeof(status))) {
  2455. retval = -EFAULT;
  2456. goto out;
  2457. }
  2458. associd = status.sstat_assoc_id;
  2459. asoc = sctp_id2assoc(sk, associd);
  2460. if (!asoc) {
  2461. retval = -EINVAL;
  2462. goto out;
  2463. }
  2464. transport = asoc->peer.primary_path;
  2465. status.sstat_assoc_id = sctp_assoc2id(asoc);
  2466. status.sstat_state = asoc->state;
  2467. status.sstat_rwnd = asoc->peer.rwnd;
  2468. status.sstat_unackdata = asoc->unack_data;
  2469. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  2470. status.sstat_instrms = asoc->c.sinit_max_instreams;
  2471. status.sstat_outstrms = asoc->c.sinit_num_ostreams;
  2472. status.sstat_fragmentation_point = asoc->frag_point;
  2473. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2474. memcpy(&status.sstat_primary.spinfo_address,
  2475. &(transport->ipaddr), sizeof(union sctp_addr));
  2476. /* Map ipv4 address into v4-mapped-on-v6 address. */
  2477. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2478. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  2479. status.sstat_primary.spinfo_state = transport->state;
  2480. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  2481. status.sstat_primary.spinfo_srtt = transport->srtt;
  2482. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  2483. status.sstat_primary.spinfo_mtu = transport->pmtu;
  2484. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  2485. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  2486. if (put_user(len, optlen)) {
  2487. retval = -EFAULT;
  2488. goto out;
  2489. }
  2490. SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
  2491. len, status.sstat_state, status.sstat_rwnd,
  2492. status.sstat_assoc_id);
  2493. if (copy_to_user(optval, &status, len)) {
  2494. retval = -EFAULT;
  2495. goto out;
  2496. }
  2497. out:
  2498. return (retval);
  2499. }
  2500. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  2501. *
  2502. * Applications can retrieve information about a specific peer address
  2503. * of an association, including its reachability state, congestion
  2504. * window, and retransmission timer values. This information is
  2505. * read-only.
  2506. */
  2507. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  2508. char __user *optval,
  2509. int __user *optlen)
  2510. {
  2511. struct sctp_paddrinfo pinfo;
  2512. struct sctp_transport *transport;
  2513. int retval = 0;
  2514. if (len != sizeof(pinfo)) {
  2515. retval = -EINVAL;
  2516. goto out;
  2517. }
  2518. if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
  2519. retval = -EFAULT;
  2520. goto out;
  2521. }
  2522. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  2523. pinfo.spinfo_assoc_id);
  2524. if (!transport)
  2525. return -EINVAL;
  2526. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2527. pinfo.spinfo_state = transport->state;
  2528. pinfo.spinfo_cwnd = transport->cwnd;
  2529. pinfo.spinfo_srtt = transport->srtt;
  2530. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  2531. pinfo.spinfo_mtu = transport->pmtu;
  2532. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  2533. pinfo.spinfo_state = SCTP_ACTIVE;
  2534. if (put_user(len, optlen)) {
  2535. retval = -EFAULT;
  2536. goto out;
  2537. }
  2538. if (copy_to_user(optval, &pinfo, len)) {
  2539. retval = -EFAULT;
  2540. goto out;
  2541. }
  2542. out:
  2543. return (retval);
  2544. }
  2545. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  2546. *
  2547. * This option is a on/off flag. If enabled no SCTP message
  2548. * fragmentation will be performed. Instead if a message being sent
  2549. * exceeds the current PMTU size, the message will NOT be sent and
  2550. * instead a error will be indicated to the user.
  2551. */
  2552. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  2553. char __user *optval, int __user *optlen)
  2554. {
  2555. int val;
  2556. if (len < sizeof(int))
  2557. return -EINVAL;
  2558. len = sizeof(int);
  2559. val = (sctp_sk(sk)->disable_fragments == 1);
  2560. if (put_user(len, optlen))
  2561. return -EFAULT;
  2562. if (copy_to_user(optval, &val, len))
  2563. return -EFAULT;
  2564. return 0;
  2565. }
  2566. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  2567. *
  2568. * This socket option is used to specify various notifications and
  2569. * ancillary data the user wishes to receive.
  2570. */
  2571. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  2572. int __user *optlen)
  2573. {
  2574. if (len != sizeof(struct sctp_event_subscribe))
  2575. return -EINVAL;
  2576. if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
  2577. return -EFAULT;
  2578. return 0;
  2579. }
  2580. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  2581. *
  2582. * This socket option is applicable to the UDP-style socket only. When
  2583. * set it will cause associations that are idle for more than the
  2584. * specified number of seconds to automatically close. An association
  2585. * being idle is defined an association that has NOT sent or received
  2586. * user data. The special value of '0' indicates that no automatic
  2587. * close of any associations should be performed. The option expects an
  2588. * integer defining the number of seconds of idle time before an
  2589. * association is closed.
  2590. */
  2591. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2592. {
  2593. /* Applicable to UDP-style socket only */
  2594. if (sctp_style(sk, TCP))
  2595. return -EOPNOTSUPP;
  2596. if (len != sizeof(int))
  2597. return -EINVAL;
  2598. if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
  2599. return -EFAULT;
  2600. return 0;
  2601. }
  2602. /* Helper routine to branch off an association to a new socket. */
  2603. SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
  2604. struct socket **sockp)
  2605. {
  2606. struct sock *sk = asoc->base.sk;
  2607. struct socket *sock;
  2608. int err = 0;
  2609. /* An association cannot be branched off from an already peeled-off
  2610. * socket, nor is this supported for tcp style sockets.
  2611. */
  2612. if (!sctp_style(sk, UDP))
  2613. return -EINVAL;
  2614. /* Create a new socket. */
  2615. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  2616. if (err < 0)
  2617. return err;
  2618. /* Populate the fields of the newsk from the oldsk and migrate the
  2619. * asoc to the newsk.
  2620. */
  2621. sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  2622. *sockp = sock;
  2623. return err;
  2624. }
  2625. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2626. {
  2627. sctp_peeloff_arg_t peeloff;
  2628. struct socket *newsock;
  2629. int retval = 0;
  2630. struct sctp_association *asoc;
  2631. if (len != sizeof(sctp_peeloff_arg_t))
  2632. return -EINVAL;
  2633. if (copy_from_user(&peeloff, optval, len))
  2634. return -EFAULT;
  2635. asoc = sctp_id2assoc(sk, peeloff.associd);
  2636. if (!asoc) {
  2637. retval = -EINVAL;
  2638. goto out;
  2639. }
  2640. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
  2641. retval = sctp_do_peeloff(asoc, &newsock);
  2642. if (retval < 0)
  2643. goto out;
  2644. /* Map the socket to an unused fd that can be returned to the user. */
  2645. retval = sock_map_fd(newsock);
  2646. if (retval < 0) {
  2647. sock_release(newsock);
  2648. goto out;
  2649. }
  2650. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
  2651. __FUNCTION__, sk, asoc, newsock->sk, retval);
  2652. /* Return the fd mapped to the new socket. */
  2653. peeloff.sd = retval;
  2654. if (copy_to_user(optval, &peeloff, len))
  2655. retval = -EFAULT;
  2656. out:
  2657. return retval;
  2658. }
  2659. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  2660. *
  2661. * Applications can enable or disable heartbeats for any peer address of
  2662. * an association, modify an address's heartbeat interval, force a
  2663. * heartbeat to be sent immediately, and adjust the address's maximum
  2664. * number of retransmissions sent before an address is considered
  2665. * unreachable. The following structure is used to access and modify an
  2666. * address's parameters:
  2667. *
  2668. * struct sctp_paddrparams {
  2669. * sctp_assoc_t spp_assoc_id;
  2670. * struct sockaddr_storage spp_address;
  2671. * uint32_t spp_hbinterval;
  2672. * uint16_t spp_pathmaxrxt;
  2673. * };
  2674. *
  2675. * spp_assoc_id - (UDP style socket) This is filled in the application,
  2676. * and identifies the association for this query.
  2677. * spp_address - This specifies which address is of interest.
  2678. * spp_hbinterval - This contains the value of the heartbeat interval,
  2679. * in milliseconds. A value of 0, when modifying the
  2680. * parameter, specifies that the heartbeat on this
  2681. * address should be disabled. A value of UINT32_MAX
  2682. * (4294967295), when modifying the parameter,
  2683. * specifies that a heartbeat should be sent
  2684. * immediately to the peer address, and the current
  2685. * interval should remain unchanged.
  2686. * spp_pathmaxrxt - This contains the maximum number of
  2687. * retransmissions before this address shall be
  2688. * considered unreachable.
  2689. */
  2690. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  2691. char __user *optval, int __user *optlen)
  2692. {
  2693. struct sctp_paddrparams params;
  2694. struct sctp_transport *trans;
  2695. if (len != sizeof(struct sctp_paddrparams))
  2696. return -EINVAL;
  2697. if (copy_from_user(&params, optval, len))
  2698. return -EFAULT;
  2699. /* If no association id is specified retrieve the default value
  2700. * for the endpoint that will be used for all future associations
  2701. */
  2702. if (!params.spp_assoc_id &&
  2703. sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  2704. params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
  2705. params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
  2706. goto done;
  2707. }
  2708. trans = sctp_addr_id2transport(sk, &params.spp_address,
  2709. params.spp_assoc_id);
  2710. if (!trans)
  2711. return -EINVAL;
  2712. /* The value of the heartbeat interval, in milliseconds. A value of 0,
  2713. * when modifying the parameter, specifies that the heartbeat on this
  2714. * address should be disabled.
  2715. */
  2716. if (!trans->hb_allowed)
  2717. params.spp_hbinterval = 0;
  2718. else
  2719. params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
  2720. /* spp_pathmaxrxt contains the maximum number of retransmissions
  2721. * before this address shall be considered unreachable.
  2722. */
  2723. params.spp_pathmaxrxt = trans->max_retrans;
  2724. done:
  2725. if (copy_to_user(optval, &params, len))
  2726. return -EFAULT;
  2727. if (put_user(len, optlen))
  2728. return -EFAULT;
  2729. return 0;
  2730. }
  2731. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2732. *
  2733. * Applications can specify protocol parameters for the default association
  2734. * initialization. The option name argument to setsockopt() and getsockopt()
  2735. * is SCTP_INITMSG.
  2736. *
  2737. * Setting initialization parameters is effective only on an unconnected
  2738. * socket (for UDP-style sockets only future associations are effected
  2739. * by the change). With TCP-style sockets, this option is inherited by
  2740. * sockets derived from a listener socket.
  2741. */
  2742. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2743. {
  2744. if (len != sizeof(struct sctp_initmsg))
  2745. return -EINVAL;
  2746. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  2747. return -EFAULT;
  2748. return 0;
  2749. }
  2750. static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
  2751. char __user *optval,
  2752. int __user *optlen)
  2753. {
  2754. sctp_assoc_t id;
  2755. struct sctp_association *asoc;
  2756. struct list_head *pos;
  2757. int cnt = 0;
  2758. if (len != sizeof(sctp_assoc_t))
  2759. return -EINVAL;
  2760. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  2761. return -EFAULT;
  2762. /* For UDP-style sockets, id specifies the association to query. */
  2763. asoc = sctp_id2assoc(sk, id);
  2764. if (!asoc)
  2765. return -EINVAL;
  2766. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2767. cnt ++;
  2768. }
  2769. return cnt;
  2770. }
  2771. /*
  2772. * Old API for getting list of peer addresses. Does not work for 32-bit
  2773. * programs running on a 64-bit kernel
  2774. */
  2775. static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
  2776. char __user *optval,
  2777. int __user *optlen)
  2778. {
  2779. struct sctp_association *asoc;
  2780. struct list_head *pos;
  2781. int cnt = 0;
  2782. struct sctp_getaddrs_old getaddrs;
  2783. struct sctp_transport *from;
  2784. void __user *to;
  2785. union sctp_addr temp;
  2786. struct sctp_sock *sp = sctp_sk(sk);
  2787. int addrlen;
  2788. if (len != sizeof(struct sctp_getaddrs_old))
  2789. return -EINVAL;
  2790. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
  2791. return -EFAULT;
  2792. if (getaddrs.addr_num <= 0) return -EINVAL;
  2793. /* For UDP-style sockets, id specifies the association to query. */
  2794. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  2795. if (!asoc)
  2796. return -EINVAL;
  2797. to = (void __user *)getaddrs.addrs;
  2798. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2799. from = list_entry(pos, struct sctp_transport, transports);
  2800. memcpy(&temp, &from->ipaddr, sizeof(temp));
  2801. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  2802. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  2803. temp.v4.sin_port = htons(temp.v4.sin_port);
  2804. if (copy_to_user(to, &temp, addrlen))
  2805. return -EFAULT;
  2806. to += addrlen ;
  2807. cnt ++;
  2808. if (cnt >= getaddrs.addr_num) break;
  2809. }
  2810. getaddrs.addr_num = cnt;
  2811. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
  2812. return -EFAULT;
  2813. return 0;
  2814. }
  2815. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  2816. char __user *optval, int __user *optlen)
  2817. {
  2818. struct sctp_association *asoc;
  2819. struct list_head *pos;
  2820. int cnt = 0;
  2821. struct sctp_getaddrs getaddrs;
  2822. struct sctp_transport *from;
  2823. void __user *to;
  2824. union sctp_addr temp;
  2825. struct sctp_sock *sp = sctp_sk(sk);
  2826. int addrlen;
  2827. size_t space_left;
  2828. int bytes_copied;
  2829. if (len < sizeof(struct sctp_getaddrs))
  2830. return -EINVAL;
  2831. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  2832. return -EFAULT;
  2833. /* For UDP-style sockets, id specifies the association to query. */
  2834. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  2835. if (!asoc)
  2836. return -EINVAL;
  2837. to = optval + offsetof(struct sctp_getaddrs,addrs);
  2838. space_left = len - sizeof(struct sctp_getaddrs) -
  2839. offsetof(struct sctp_getaddrs,addrs);
  2840. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2841. from = list_entry(pos, struct sctp_transport, transports);
  2842. memcpy(&temp, &from->ipaddr, sizeof(temp));
  2843. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  2844. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  2845. if(space_left < addrlen)
  2846. return -ENOMEM;
  2847. temp.v4.sin_port = htons(temp.v4.sin_port);
  2848. if (copy_to_user(to, &temp, addrlen))
  2849. return -EFAULT;
  2850. to += addrlen;
  2851. cnt++;
  2852. space_left -= addrlen;
  2853. }
  2854. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  2855. return -EFAULT;
  2856. bytes_copied = ((char __user *)to) - optval;
  2857. if (put_user(bytes_copied, optlen))
  2858. return -EFAULT;
  2859. return 0;
  2860. }
  2861. static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
  2862. char __user *optval,
  2863. int __user *optlen)
  2864. {
  2865. sctp_assoc_t id;
  2866. struct sctp_bind_addr *bp;
  2867. struct sctp_association *asoc;
  2868. struct list_head *pos;
  2869. struct sctp_sockaddr_entry *addr;
  2870. rwlock_t *addr_lock;
  2871. unsigned long flags;
  2872. int cnt = 0;
  2873. if (len != sizeof(sctp_assoc_t))
  2874. return -EINVAL;
  2875. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  2876. return -EFAULT;
  2877. /*
  2878. * For UDP-style sockets, id specifies the association to query.
  2879. * If the id field is set to the value '0' then the locally bound
  2880. * addresses are returned without regard to any particular
  2881. * association.
  2882. */
  2883. if (0 == id) {
  2884. bp = &sctp_sk(sk)->ep->base.bind_addr;
  2885. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  2886. } else {
  2887. asoc = sctp_id2assoc(sk, id);
  2888. if (!asoc)
  2889. return -EINVAL;
  2890. bp = &asoc->base.bind_addr;
  2891. addr_lock = &asoc->base.addr_lock;
  2892. }
  2893. sctp_read_lock(addr_lock);
  2894. /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
  2895. * addresses from the global local address list.
  2896. */
  2897. if (sctp_list_single_entry(&bp->address_list)) {
  2898. addr = list_entry(bp->address_list.next,
  2899. struct sctp_sockaddr_entry, list);
  2900. if (sctp_is_any(&addr->a)) {
  2901. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  2902. list_for_each(pos, &sctp_local_addr_list) {
  2903. addr = list_entry(pos,
  2904. struct sctp_sockaddr_entry,
  2905. list);
  2906. if ((PF_INET == sk->sk_family) &&
  2907. (AF_INET6 == addr->a.sa.sa_family))
  2908. continue;
  2909. cnt++;
  2910. }
  2911. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  2912. flags);
  2913. } else {
  2914. cnt = 1;
  2915. }
  2916. goto done;
  2917. }
  2918. list_for_each(pos, &bp->address_list) {
  2919. cnt ++;
  2920. }
  2921. done:
  2922. sctp_read_unlock(addr_lock);
  2923. return cnt;
  2924. }
  2925. /* Helper function that copies local addresses to user and returns the number
  2926. * of addresses copied.
  2927. */
  2928. static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
  2929. void __user *to)
  2930. {
  2931. struct list_head *pos;
  2932. struct sctp_sockaddr_entry *addr;
  2933. unsigned long flags;
  2934. union sctp_addr temp;
  2935. int cnt = 0;
  2936. int addrlen;
  2937. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  2938. list_for_each(pos, &sctp_local_addr_list) {
  2939. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  2940. if ((PF_INET == sk->sk_family) &&
  2941. (AF_INET6 == addr->a.sa.sa_family))
  2942. continue;
  2943. memcpy(&temp, &addr->a, sizeof(temp));
  2944. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2945. &temp);
  2946. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  2947. temp.v4.sin_port = htons(port);
  2948. if (copy_to_user(to, &temp, addrlen)) {
  2949. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  2950. flags);
  2951. return -EFAULT;
  2952. }
  2953. to += addrlen;
  2954. cnt ++;
  2955. if (cnt >= max_addrs) break;
  2956. }
  2957. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
  2958. return cnt;
  2959. }
  2960. static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
  2961. void * __user *to, size_t space_left)
  2962. {
  2963. struct list_head *pos;
  2964. struct sctp_sockaddr_entry *addr;
  2965. unsigned long flags;
  2966. union sctp_addr temp;
  2967. int cnt = 0;
  2968. int addrlen;
  2969. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  2970. list_for_each(pos, &sctp_local_addr_list) {
  2971. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  2972. if ((PF_INET == sk->sk_family) &&
  2973. (AF_INET6 == addr->a.sa.sa_family))
  2974. continue;
  2975. memcpy(&temp, &addr->a, sizeof(temp));
  2976. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2977. &temp);
  2978. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  2979. if(space_left<addrlen)
  2980. return -ENOMEM;
  2981. temp.v4.sin_port = htons(port);
  2982. if (copy_to_user(*to, &temp, addrlen)) {
  2983. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  2984. flags);
  2985. return -EFAULT;
  2986. }
  2987. *to += addrlen;
  2988. cnt ++;
  2989. space_left -= addrlen;
  2990. }
  2991. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
  2992. return cnt;
  2993. }
  2994. /* Old API for getting list of local addresses. Does not work for 32-bit
  2995. * programs running on a 64-bit kernel
  2996. */
  2997. static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
  2998. char __user *optval, int __user *optlen)
  2999. {
  3000. struct sctp_bind_addr *bp;
  3001. struct sctp_association *asoc;
  3002. struct list_head *pos;
  3003. int cnt = 0;
  3004. struct sctp_getaddrs_old getaddrs;
  3005. struct sctp_sockaddr_entry *addr;
  3006. void __user *to;
  3007. union sctp_addr temp;
  3008. struct sctp_sock *sp = sctp_sk(sk);
  3009. int addrlen;
  3010. rwlock_t *addr_lock;
  3011. int err = 0;
  3012. if (len != sizeof(struct sctp_getaddrs_old))
  3013. return -EINVAL;
  3014. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
  3015. return -EFAULT;
  3016. if (getaddrs.addr_num <= 0) return -EINVAL;
  3017. /*
  3018. * For UDP-style sockets, id specifies the association to query.
  3019. * If the id field is set to the value '0' then the locally bound
  3020. * addresses are returned without regard to any particular
  3021. * association.
  3022. */
  3023. if (0 == getaddrs.assoc_id) {
  3024. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3025. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  3026. } else {
  3027. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3028. if (!asoc)
  3029. return -EINVAL;
  3030. bp = &asoc->base.bind_addr;
  3031. addr_lock = &asoc->base.addr_lock;
  3032. }
  3033. to = getaddrs.addrs;
  3034. sctp_read_lock(addr_lock);
  3035. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  3036. * addresses from the global local address list.
  3037. */
  3038. if (sctp_list_single_entry(&bp->address_list)) {
  3039. addr = list_entry(bp->address_list.next,
  3040. struct sctp_sockaddr_entry, list);
  3041. if (sctp_is_any(&addr->a)) {
  3042. cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
  3043. getaddrs.addr_num,
  3044. to);
  3045. if (cnt < 0) {
  3046. err = cnt;
  3047. goto unlock;
  3048. }
  3049. goto copy_getaddrs;
  3050. }
  3051. }
  3052. list_for_each(pos, &bp->address_list) {
  3053. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3054. memcpy(&temp, &addr->a, sizeof(temp));
  3055. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3056. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3057. temp.v4.sin_port = htons(temp.v4.sin_port);
  3058. if (copy_to_user(to, &temp, addrlen)) {
  3059. err = -EFAULT;
  3060. goto unlock;
  3061. }
  3062. to += addrlen;
  3063. cnt ++;
  3064. if (cnt >= getaddrs.addr_num) break;
  3065. }
  3066. copy_getaddrs:
  3067. getaddrs.addr_num = cnt;
  3068. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
  3069. err = -EFAULT;
  3070. unlock:
  3071. sctp_read_unlock(addr_lock);
  3072. return err;
  3073. }
  3074. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  3075. char __user *optval, int __user *optlen)
  3076. {
  3077. struct sctp_bind_addr *bp;
  3078. struct sctp_association *asoc;
  3079. struct list_head *pos;
  3080. int cnt = 0;
  3081. struct sctp_getaddrs getaddrs;
  3082. struct sctp_sockaddr_entry *addr;
  3083. void __user *to;
  3084. union sctp_addr temp;
  3085. struct sctp_sock *sp = sctp_sk(sk);
  3086. int addrlen;
  3087. rwlock_t *addr_lock;
  3088. int err = 0;
  3089. size_t space_left;
  3090. int bytes_copied;
  3091. if (len <= sizeof(struct sctp_getaddrs))
  3092. return -EINVAL;
  3093. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  3094. return -EFAULT;
  3095. /*
  3096. * For UDP-style sockets, id specifies the association to query.
  3097. * If the id field is set to the value '0' then the locally bound
  3098. * addresses are returned without regard to any particular
  3099. * association.
  3100. */
  3101. if (0 == getaddrs.assoc_id) {
  3102. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3103. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  3104. } else {
  3105. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3106. if (!asoc)
  3107. return -EINVAL;
  3108. bp = &asoc->base.bind_addr;
  3109. addr_lock = &asoc->base.addr_lock;
  3110. }
  3111. to = optval + offsetof(struct sctp_getaddrs,addrs);
  3112. space_left = len - sizeof(struct sctp_getaddrs) -
  3113. offsetof(struct sctp_getaddrs,addrs);
  3114. sctp_read_lock(addr_lock);
  3115. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  3116. * addresses from the global local address list.
  3117. */
  3118. if (sctp_list_single_entry(&bp->address_list)) {
  3119. addr = list_entry(bp->address_list.next,
  3120. struct sctp_sockaddr_entry, list);
  3121. if (sctp_is_any(&addr->a)) {
  3122. cnt = sctp_copy_laddrs_to_user(sk, bp->port,
  3123. &to, space_left);
  3124. if (cnt < 0) {
  3125. err = cnt;
  3126. goto unlock;
  3127. }
  3128. goto copy_getaddrs;
  3129. }
  3130. }
  3131. list_for_each(pos, &bp->address_list) {
  3132. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3133. memcpy(&temp, &addr->a, sizeof(temp));
  3134. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3135. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3136. if(space_left < addrlen)
  3137. return -ENOMEM; /*fixme: right error?*/
  3138. temp.v4.sin_port = htons(temp.v4.sin_port);
  3139. if (copy_to_user(to, &temp, addrlen)) {
  3140. err = -EFAULT;
  3141. goto unlock;
  3142. }
  3143. to += addrlen;
  3144. cnt ++;
  3145. space_left -= addrlen;
  3146. }
  3147. copy_getaddrs:
  3148. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  3149. return -EFAULT;
  3150. bytes_copied = ((char __user *)to) - optval;
  3151. if (put_user(bytes_copied, optlen))
  3152. return -EFAULT;
  3153. unlock:
  3154. sctp_read_unlock(addr_lock);
  3155. return err;
  3156. }
  3157. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  3158. *
  3159. * Requests that the local SCTP stack use the enclosed peer address as
  3160. * the association primary. The enclosed address must be one of the
  3161. * association peer's addresses.
  3162. */
  3163. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  3164. char __user *optval, int __user *optlen)
  3165. {
  3166. struct sctp_prim prim;
  3167. struct sctp_association *asoc;
  3168. struct sctp_sock *sp = sctp_sk(sk);
  3169. if (len != sizeof(struct sctp_prim))
  3170. return -EINVAL;
  3171. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  3172. return -EFAULT;
  3173. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  3174. if (!asoc)
  3175. return -EINVAL;
  3176. if (!asoc->peer.primary_path)
  3177. return -ENOTCONN;
  3178. asoc->peer.primary_path->ipaddr.v4.sin_port =
  3179. htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
  3180. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  3181. sizeof(union sctp_addr));
  3182. asoc->peer.primary_path->ipaddr.v4.sin_port =
  3183. ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
  3184. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
  3185. (union sctp_addr *)&prim.ssp_addr);
  3186. if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
  3187. return -EFAULT;
  3188. return 0;
  3189. }
  3190. /*
  3191. * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
  3192. *
  3193. * Requests that the local endpoint set the specified Adaption Layer
  3194. * Indication parameter for all future INIT and INIT-ACK exchanges.
  3195. */
  3196. static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
  3197. char __user *optval, int __user *optlen)
  3198. {
  3199. struct sctp_setadaption adaption;
  3200. if (len != sizeof(struct sctp_setadaption))
  3201. return -EINVAL;
  3202. adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
  3203. if (copy_to_user(optval, &adaption, len))
  3204. return -EFAULT;
  3205. return 0;
  3206. }
  3207. /*
  3208. *
  3209. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  3210. *
  3211. * Applications that wish to use the sendto() system call may wish to
  3212. * specify a default set of parameters that would normally be supplied
  3213. * through the inclusion of ancillary data. This socket option allows
  3214. * such an application to set the default sctp_sndrcvinfo structure.
  3215. * The application that wishes to use this socket option simply passes
  3216. * in to this call the sctp_sndrcvinfo structure defined in Section
  3217. * 5.2.2) The input parameters accepted by this call include
  3218. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  3219. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  3220. * to this call if the caller is using the UDP model.
  3221. *
  3222. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  3223. */
  3224. static int sctp_getsockopt_default_send_param(struct sock *sk,
  3225. int len, char __user *optval,
  3226. int __user *optlen)
  3227. {
  3228. struct sctp_sndrcvinfo info;
  3229. struct sctp_association *asoc;
  3230. struct sctp_sock *sp = sctp_sk(sk);
  3231. if (len != sizeof(struct sctp_sndrcvinfo))
  3232. return -EINVAL;
  3233. if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
  3234. return -EFAULT;
  3235. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  3236. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  3237. return -EINVAL;
  3238. if (asoc) {
  3239. info.sinfo_stream = asoc->default_stream;
  3240. info.sinfo_flags = asoc->default_flags;
  3241. info.sinfo_ppid = asoc->default_ppid;
  3242. info.sinfo_context = asoc->default_context;
  3243. info.sinfo_timetolive = asoc->default_timetolive;
  3244. } else {
  3245. info.sinfo_stream = sp->default_stream;
  3246. info.sinfo_flags = sp->default_flags;
  3247. info.sinfo_ppid = sp->default_ppid;
  3248. info.sinfo_context = sp->default_context;
  3249. info.sinfo_timetolive = sp->default_timetolive;
  3250. }
  3251. if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
  3252. return -EFAULT;
  3253. return 0;
  3254. }
  3255. /*
  3256. *
  3257. * 7.1.5 SCTP_NODELAY
  3258. *
  3259. * Turn on/off any Nagle-like algorithm. This means that packets are
  3260. * generally sent as soon as possible and no unnecessary delays are
  3261. * introduced, at the cost of more packets in the network. Expects an
  3262. * integer boolean flag.
  3263. */
  3264. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  3265. char __user *optval, int __user *optlen)
  3266. {
  3267. int val;
  3268. if (len < sizeof(int))
  3269. return -EINVAL;
  3270. len = sizeof(int);
  3271. val = (sctp_sk(sk)->nodelay == 1);
  3272. if (put_user(len, optlen))
  3273. return -EFAULT;
  3274. if (copy_to_user(optval, &val, len))
  3275. return -EFAULT;
  3276. return 0;
  3277. }
  3278. /*
  3279. *
  3280. * 7.1.1 SCTP_RTOINFO
  3281. *
  3282. * The protocol parameters used to initialize and bound retransmission
  3283. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  3284. * and modify these parameters.
  3285. * All parameters are time values, in milliseconds. A value of 0, when
  3286. * modifying the parameters, indicates that the current value should not
  3287. * be changed.
  3288. *
  3289. */
  3290. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  3291. char __user *optval,
  3292. int __user *optlen) {
  3293. struct sctp_rtoinfo rtoinfo;
  3294. struct sctp_association *asoc;
  3295. if (len != sizeof (struct sctp_rtoinfo))
  3296. return -EINVAL;
  3297. if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
  3298. return -EFAULT;
  3299. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  3300. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  3301. return -EINVAL;
  3302. /* Values corresponding to the specific association. */
  3303. if (asoc) {
  3304. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  3305. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  3306. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  3307. } else {
  3308. /* Values corresponding to the endpoint. */
  3309. struct sctp_sock *sp = sctp_sk(sk);
  3310. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  3311. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  3312. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  3313. }
  3314. if (put_user(len, optlen))
  3315. return -EFAULT;
  3316. if (copy_to_user(optval, &rtoinfo, len))
  3317. return -EFAULT;
  3318. return 0;
  3319. }
  3320. /*
  3321. *
  3322. * 7.1.2 SCTP_ASSOCINFO
  3323. *
  3324. * This option is used to tune the the maximum retransmission attempts
  3325. * of the association.
  3326. * Returns an error if the new association retransmission value is
  3327. * greater than the sum of the retransmission value of the peer.
  3328. * See [SCTP] for more information.
  3329. *
  3330. */
  3331. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  3332. char __user *optval,
  3333. int __user *optlen)
  3334. {
  3335. struct sctp_assocparams assocparams;
  3336. struct sctp_association *asoc;
  3337. struct list_head *pos;
  3338. int cnt = 0;
  3339. if (len != sizeof (struct sctp_assocparams))
  3340. return -EINVAL;
  3341. if (copy_from_user(&assocparams, optval,
  3342. sizeof (struct sctp_assocparams)))
  3343. return -EFAULT;
  3344. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  3345. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  3346. return -EINVAL;
  3347. /* Values correspoinding to the specific association */
  3348. if (asoc) {
  3349. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  3350. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  3351. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  3352. assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
  3353. * 1000) +
  3354. (asoc->cookie_life.tv_usec
  3355. / 1000);
  3356. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3357. cnt ++;
  3358. }
  3359. assocparams.sasoc_number_peer_destinations = cnt;
  3360. } else {
  3361. /* Values corresponding to the endpoint */
  3362. struct sctp_sock *sp = sctp_sk(sk);
  3363. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  3364. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  3365. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  3366. assocparams.sasoc_cookie_life =
  3367. sp->assocparams.sasoc_cookie_life;
  3368. assocparams.sasoc_number_peer_destinations =
  3369. sp->assocparams.
  3370. sasoc_number_peer_destinations;
  3371. }
  3372. if (put_user(len, optlen))
  3373. return -EFAULT;
  3374. if (copy_to_user(optval, &assocparams, len))
  3375. return -EFAULT;
  3376. return 0;
  3377. }
  3378. /*
  3379. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  3380. *
  3381. * This socket option is a boolean flag which turns on or off mapped V4
  3382. * addresses. If this option is turned on and the socket is type
  3383. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  3384. * If this option is turned off, then no mapping will be done of V4
  3385. * addresses and a user will receive both PF_INET6 and PF_INET type
  3386. * addresses on the socket.
  3387. */
  3388. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  3389. char __user *optval, int __user *optlen)
  3390. {
  3391. int val;
  3392. struct sctp_sock *sp = sctp_sk(sk);
  3393. if (len < sizeof(int))
  3394. return -EINVAL;
  3395. len = sizeof(int);
  3396. val = sp->v4mapped;
  3397. if (put_user(len, optlen))
  3398. return -EFAULT;
  3399. if (copy_to_user(optval, &val, len))
  3400. return -EFAULT;
  3401. return 0;
  3402. }
  3403. /*
  3404. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  3405. *
  3406. * This socket option specifies the maximum size to put in any outgoing
  3407. * SCTP chunk. If a message is larger than this size it will be
  3408. * fragmented by SCTP into the specified size. Note that the underlying
  3409. * SCTP implementation may fragment into smaller sized chunks when the
  3410. * PMTU of the underlying association is smaller than the value set by
  3411. * the user.
  3412. */
  3413. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  3414. char __user *optval, int __user *optlen)
  3415. {
  3416. int val;
  3417. if (len < sizeof(int))
  3418. return -EINVAL;
  3419. len = sizeof(int);
  3420. val = sctp_sk(sk)->user_frag;
  3421. if (put_user(len, optlen))
  3422. return -EFAULT;
  3423. if (copy_to_user(optval, &val, len))
  3424. return -EFAULT;
  3425. return 0;
  3426. }
  3427. SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
  3428. char __user *optval, int __user *optlen)
  3429. {
  3430. int retval = 0;
  3431. int len;
  3432. SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
  3433. sk, optname);
  3434. /* I can hardly begin to describe how wrong this is. This is
  3435. * so broken as to be worse than useless. The API draft
  3436. * REALLY is NOT helpful here... I am not convinced that the
  3437. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  3438. * are at all well-founded.
  3439. */
  3440. if (level != SOL_SCTP) {
  3441. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3442. retval = af->getsockopt(sk, level, optname, optval, optlen);
  3443. return retval;
  3444. }
  3445. if (get_user(len, optlen))
  3446. return -EFAULT;
  3447. sctp_lock_sock(sk);
  3448. switch (optname) {
  3449. case SCTP_STATUS:
  3450. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  3451. break;
  3452. case SCTP_DISABLE_FRAGMENTS:
  3453. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  3454. optlen);
  3455. break;
  3456. case SCTP_EVENTS:
  3457. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  3458. break;
  3459. case SCTP_AUTOCLOSE:
  3460. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  3461. break;
  3462. case SCTP_SOCKOPT_PEELOFF:
  3463. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  3464. break;
  3465. case SCTP_PEER_ADDR_PARAMS:
  3466. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  3467. optlen);
  3468. break;
  3469. case SCTP_INITMSG:
  3470. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  3471. break;
  3472. case SCTP_GET_PEER_ADDRS_NUM_OLD:
  3473. retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
  3474. optlen);
  3475. break;
  3476. case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
  3477. retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
  3478. optlen);
  3479. break;
  3480. case SCTP_GET_PEER_ADDRS_OLD:
  3481. retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
  3482. optlen);
  3483. break;
  3484. case SCTP_GET_LOCAL_ADDRS_OLD:
  3485. retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
  3486. optlen);
  3487. break;
  3488. case SCTP_GET_PEER_ADDRS:
  3489. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  3490. optlen);
  3491. break;
  3492. case SCTP_GET_LOCAL_ADDRS:
  3493. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  3494. optlen);
  3495. break;
  3496. case SCTP_DEFAULT_SEND_PARAM:
  3497. retval = sctp_getsockopt_default_send_param(sk, len,
  3498. optval, optlen);
  3499. break;
  3500. case SCTP_PRIMARY_ADDR:
  3501. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  3502. break;
  3503. case SCTP_NODELAY:
  3504. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  3505. break;
  3506. case SCTP_RTOINFO:
  3507. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  3508. break;
  3509. case SCTP_ASSOCINFO:
  3510. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  3511. break;
  3512. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3513. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  3514. break;
  3515. case SCTP_MAXSEG:
  3516. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  3517. break;
  3518. case SCTP_GET_PEER_ADDR_INFO:
  3519. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  3520. optlen);
  3521. break;
  3522. case SCTP_ADAPTION_LAYER:
  3523. retval = sctp_getsockopt_adaption_layer(sk, len, optval,
  3524. optlen);
  3525. break;
  3526. default:
  3527. retval = -ENOPROTOOPT;
  3528. break;
  3529. };
  3530. sctp_release_sock(sk);
  3531. return retval;
  3532. }
  3533. static void sctp_hash(struct sock *sk)
  3534. {
  3535. /* STUB */
  3536. }
  3537. static void sctp_unhash(struct sock *sk)
  3538. {
  3539. /* STUB */
  3540. }
  3541. /* Check if port is acceptable. Possibly find first available port.
  3542. *
  3543. * The port hash table (contained in the 'global' SCTP protocol storage
  3544. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  3545. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  3546. * list (the list number is the port number hashed out, so as you
  3547. * would expect from a hash function, all the ports in a given list have
  3548. * such a number that hashes out to the same list number; you were
  3549. * expecting that, right?); so each list has a set of ports, with a
  3550. * link to the socket (struct sock) that uses it, the port number and
  3551. * a fastreuse flag (FIXME: NPI ipg).
  3552. */
  3553. static struct sctp_bind_bucket *sctp_bucket_create(
  3554. struct sctp_bind_hashbucket *head, unsigned short snum);
  3555. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  3556. {
  3557. struct sctp_bind_hashbucket *head; /* hash list */
  3558. struct sctp_bind_bucket *pp; /* hash list port iterator */
  3559. unsigned short snum;
  3560. int ret;
  3561. /* NOTE: Remember to put this back to net order. */
  3562. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  3563. snum = addr->v4.sin_port;
  3564. SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
  3565. sctp_local_bh_disable();
  3566. if (snum == 0) {
  3567. /* Search for an available port.
  3568. *
  3569. * 'sctp_port_rover' was the last port assigned, so
  3570. * we start to search from 'sctp_port_rover +
  3571. * 1'. What we do is first check if port 'rover' is
  3572. * already in the hash table; if not, we use that; if
  3573. * it is, we try next.
  3574. */
  3575. int low = sysctl_local_port_range[0];
  3576. int high = sysctl_local_port_range[1];
  3577. int remaining = (high - low) + 1;
  3578. int rover;
  3579. int index;
  3580. sctp_spin_lock(&sctp_port_alloc_lock);
  3581. rover = sctp_port_rover;
  3582. do {
  3583. rover++;
  3584. if ((rover < low) || (rover > high))
  3585. rover = low;
  3586. index = sctp_phashfn(rover);
  3587. head = &sctp_port_hashtable[index];
  3588. sctp_spin_lock(&head->lock);
  3589. for (pp = head->chain; pp; pp = pp->next)
  3590. if (pp->port == rover)
  3591. goto next;
  3592. break;
  3593. next:
  3594. sctp_spin_unlock(&head->lock);
  3595. } while (--remaining > 0);
  3596. sctp_port_rover = rover;
  3597. sctp_spin_unlock(&sctp_port_alloc_lock);
  3598. /* Exhausted local port range during search? */
  3599. ret = 1;
  3600. if (remaining <= 0)
  3601. goto fail;
  3602. /* OK, here is the one we will use. HEAD (the port
  3603. * hash table list entry) is non-NULL and we hold it's
  3604. * mutex.
  3605. */
  3606. snum = rover;
  3607. } else {
  3608. /* We are given an specific port number; we verify
  3609. * that it is not being used. If it is used, we will
  3610. * exahust the search in the hash list corresponding
  3611. * to the port number (snum) - we detect that with the
  3612. * port iterator, pp being NULL.
  3613. */
  3614. head = &sctp_port_hashtable[sctp_phashfn(snum)];
  3615. sctp_spin_lock(&head->lock);
  3616. for (pp = head->chain; pp; pp = pp->next) {
  3617. if (pp->port == snum)
  3618. goto pp_found;
  3619. }
  3620. }
  3621. pp = NULL;
  3622. goto pp_not_found;
  3623. pp_found:
  3624. if (!hlist_empty(&pp->owner)) {
  3625. /* We had a port hash table hit - there is an
  3626. * available port (pp != NULL) and it is being
  3627. * used by other socket (pp->owner not empty); that other
  3628. * socket is going to be sk2.
  3629. */
  3630. int reuse = sk->sk_reuse;
  3631. struct sock *sk2;
  3632. struct hlist_node *node;
  3633. SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
  3634. if (pp->fastreuse && sk->sk_reuse)
  3635. goto success;
  3636. /* Run through the list of sockets bound to the port
  3637. * (pp->port) [via the pointers bind_next and
  3638. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  3639. * we get the endpoint they describe and run through
  3640. * the endpoint's list of IP (v4 or v6) addresses,
  3641. * comparing each of the addresses with the address of
  3642. * the socket sk. If we find a match, then that means
  3643. * that this port/socket (sk) combination are already
  3644. * in an endpoint.
  3645. */
  3646. sk_for_each_bound(sk2, node, &pp->owner) {
  3647. struct sctp_endpoint *ep2;
  3648. ep2 = sctp_sk(sk2)->ep;
  3649. if (reuse && sk2->sk_reuse)
  3650. continue;
  3651. if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
  3652. sctp_sk(sk))) {
  3653. ret = (long)sk2;
  3654. goto fail_unlock;
  3655. }
  3656. }
  3657. SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
  3658. }
  3659. pp_not_found:
  3660. /* If there was a hash table miss, create a new port. */
  3661. ret = 1;
  3662. if (!pp && !(pp = sctp_bucket_create(head, snum)))
  3663. goto fail_unlock;
  3664. /* In either case (hit or miss), make sure fastreuse is 1 only
  3665. * if sk->sk_reuse is too (that is, if the caller requested
  3666. * SO_REUSEADDR on this socket -sk-).
  3667. */
  3668. if (hlist_empty(&pp->owner))
  3669. pp->fastreuse = sk->sk_reuse ? 1 : 0;
  3670. else if (pp->fastreuse && !sk->sk_reuse)
  3671. pp->fastreuse = 0;
  3672. /* We are set, so fill up all the data in the hash table
  3673. * entry, tie the socket list information with the rest of the
  3674. * sockets FIXME: Blurry, NPI (ipg).
  3675. */
  3676. success:
  3677. inet_sk(sk)->num = snum;
  3678. if (!sctp_sk(sk)->bind_hash) {
  3679. sk_add_bind_node(sk, &pp->owner);
  3680. sctp_sk(sk)->bind_hash = pp;
  3681. }
  3682. ret = 0;
  3683. fail_unlock:
  3684. sctp_spin_unlock(&head->lock);
  3685. fail:
  3686. sctp_local_bh_enable();
  3687. addr->v4.sin_port = htons(addr->v4.sin_port);
  3688. return ret;
  3689. }
  3690. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  3691. * port is requested.
  3692. */
  3693. static int sctp_get_port(struct sock *sk, unsigned short snum)
  3694. {
  3695. long ret;
  3696. union sctp_addr addr;
  3697. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3698. /* Set up a dummy address struct from the sk. */
  3699. af->from_sk(&addr, sk);
  3700. addr.v4.sin_port = htons(snum);
  3701. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  3702. ret = sctp_get_port_local(sk, &addr);
  3703. return (ret ? 1 : 0);
  3704. }
  3705. /*
  3706. * 3.1.3 listen() - UDP Style Syntax
  3707. *
  3708. * By default, new associations are not accepted for UDP style sockets.
  3709. * An application uses listen() to mark a socket as being able to
  3710. * accept new associations.
  3711. */
  3712. SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
  3713. {
  3714. struct sctp_sock *sp = sctp_sk(sk);
  3715. struct sctp_endpoint *ep = sp->ep;
  3716. /* Only UDP style sockets that are not peeled off are allowed to
  3717. * listen().
  3718. */
  3719. if (!sctp_style(sk, UDP))
  3720. return -EINVAL;
  3721. /* If backlog is zero, disable listening. */
  3722. if (!backlog) {
  3723. if (sctp_sstate(sk, CLOSED))
  3724. return 0;
  3725. sctp_unhash_endpoint(ep);
  3726. sk->sk_state = SCTP_SS_CLOSED;
  3727. }
  3728. /* Return if we are already listening. */
  3729. if (sctp_sstate(sk, LISTENING))
  3730. return 0;
  3731. /*
  3732. * If a bind() or sctp_bindx() is not called prior to a listen()
  3733. * call that allows new associations to be accepted, the system
  3734. * picks an ephemeral port and will choose an address set equivalent
  3735. * to binding with a wildcard address.
  3736. *
  3737. * This is not currently spelled out in the SCTP sockets
  3738. * extensions draft, but follows the practice as seen in TCP
  3739. * sockets.
  3740. */
  3741. if (!ep->base.bind_addr.port) {
  3742. if (sctp_autobind(sk))
  3743. return -EAGAIN;
  3744. }
  3745. sk->sk_state = SCTP_SS_LISTENING;
  3746. sctp_hash_endpoint(ep);
  3747. return 0;
  3748. }
  3749. /*
  3750. * 4.1.3 listen() - TCP Style Syntax
  3751. *
  3752. * Applications uses listen() to ready the SCTP endpoint for accepting
  3753. * inbound associations.
  3754. */
  3755. SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
  3756. {
  3757. struct sctp_sock *sp = sctp_sk(sk);
  3758. struct sctp_endpoint *ep = sp->ep;
  3759. /* If backlog is zero, disable listening. */
  3760. if (!backlog) {
  3761. if (sctp_sstate(sk, CLOSED))
  3762. return 0;
  3763. sctp_unhash_endpoint(ep);
  3764. sk->sk_state = SCTP_SS_CLOSED;
  3765. }
  3766. if (sctp_sstate(sk, LISTENING))
  3767. return 0;
  3768. /*
  3769. * If a bind() or sctp_bindx() is not called prior to a listen()
  3770. * call that allows new associations to be accepted, the system
  3771. * picks an ephemeral port and will choose an address set equivalent
  3772. * to binding with a wildcard address.
  3773. *
  3774. * This is not currently spelled out in the SCTP sockets
  3775. * extensions draft, but follows the practice as seen in TCP
  3776. * sockets.
  3777. */
  3778. if (!ep->base.bind_addr.port) {
  3779. if (sctp_autobind(sk))
  3780. return -EAGAIN;
  3781. }
  3782. sk->sk_state = SCTP_SS_LISTENING;
  3783. sk->sk_max_ack_backlog = backlog;
  3784. sctp_hash_endpoint(ep);
  3785. return 0;
  3786. }
  3787. /*
  3788. * Move a socket to LISTENING state.
  3789. */
  3790. int sctp_inet_listen(struct socket *sock, int backlog)
  3791. {
  3792. struct sock *sk = sock->sk;
  3793. struct crypto_tfm *tfm=NULL;
  3794. int err = -EINVAL;
  3795. if (unlikely(backlog < 0))
  3796. goto out;
  3797. sctp_lock_sock(sk);
  3798. if (sock->state != SS_UNCONNECTED)
  3799. goto out;
  3800. /* Allocate HMAC for generating cookie. */
  3801. if (sctp_hmac_alg) {
  3802. tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
  3803. if (!tfm) {
  3804. err = -ENOSYS;
  3805. goto out;
  3806. }
  3807. }
  3808. switch (sock->type) {
  3809. case SOCK_SEQPACKET:
  3810. err = sctp_seqpacket_listen(sk, backlog);
  3811. break;
  3812. case SOCK_STREAM:
  3813. err = sctp_stream_listen(sk, backlog);
  3814. break;
  3815. default:
  3816. break;
  3817. };
  3818. if (err)
  3819. goto cleanup;
  3820. /* Store away the transform reference. */
  3821. sctp_sk(sk)->hmac = tfm;
  3822. out:
  3823. sctp_release_sock(sk);
  3824. return err;
  3825. cleanup:
  3826. sctp_crypto_free_tfm(tfm);
  3827. goto out;
  3828. }
  3829. /*
  3830. * This function is done by modeling the current datagram_poll() and the
  3831. * tcp_poll(). Note that, based on these implementations, we don't
  3832. * lock the socket in this function, even though it seems that,
  3833. * ideally, locking or some other mechanisms can be used to ensure
  3834. * the integrity of the counters (sndbuf and wmem_queued) used
  3835. * in this place. We assume that we don't need locks either until proven
  3836. * otherwise.
  3837. *
  3838. * Another thing to note is that we include the Async I/O support
  3839. * here, again, by modeling the current TCP/UDP code. We don't have
  3840. * a good way to test with it yet.
  3841. */
  3842. unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  3843. {
  3844. struct sock *sk = sock->sk;
  3845. struct sctp_sock *sp = sctp_sk(sk);
  3846. unsigned int mask;
  3847. poll_wait(file, sk->sk_sleep, wait);
  3848. /* A TCP-style listening socket becomes readable when the accept queue
  3849. * is not empty.
  3850. */
  3851. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  3852. return (!list_empty(&sp->ep->asocs)) ?
  3853. (POLLIN | POLLRDNORM) : 0;
  3854. mask = 0;
  3855. /* Is there any exceptional events? */
  3856. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  3857. mask |= POLLERR;
  3858. if (sk->sk_shutdown == SHUTDOWN_MASK)
  3859. mask |= POLLHUP;
  3860. /* Is it readable? Reconsider this code with TCP-style support. */
  3861. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  3862. (sk->sk_shutdown & RCV_SHUTDOWN))
  3863. mask |= POLLIN | POLLRDNORM;
  3864. /* The association is either gone or not ready. */
  3865. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  3866. return mask;
  3867. /* Is it writable? */
  3868. if (sctp_writeable(sk)) {
  3869. mask |= POLLOUT | POLLWRNORM;
  3870. } else {
  3871. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  3872. /*
  3873. * Since the socket is not locked, the buffer
  3874. * might be made available after the writeable check and
  3875. * before the bit is set. This could cause a lost I/O
  3876. * signal. tcp_poll() has a race breaker for this race
  3877. * condition. Based on their implementation, we put
  3878. * in the following code to cover it as well.
  3879. */
  3880. if (sctp_writeable(sk))
  3881. mask |= POLLOUT | POLLWRNORM;
  3882. }
  3883. return mask;
  3884. }
  3885. /********************************************************************
  3886. * 2nd Level Abstractions
  3887. ********************************************************************/
  3888. static struct sctp_bind_bucket *sctp_bucket_create(
  3889. struct sctp_bind_hashbucket *head, unsigned short snum)
  3890. {
  3891. struct sctp_bind_bucket *pp;
  3892. pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
  3893. SCTP_DBG_OBJCNT_INC(bind_bucket);
  3894. if (pp) {
  3895. pp->port = snum;
  3896. pp->fastreuse = 0;
  3897. INIT_HLIST_HEAD(&pp->owner);
  3898. if ((pp->next = head->chain) != NULL)
  3899. pp->next->pprev = &pp->next;
  3900. head->chain = pp;
  3901. pp->pprev = &head->chain;
  3902. }
  3903. return pp;
  3904. }
  3905. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  3906. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  3907. {
  3908. if (hlist_empty(&pp->owner)) {
  3909. if (pp->next)
  3910. pp->next->pprev = pp->pprev;
  3911. *(pp->pprev) = pp->next;
  3912. kmem_cache_free(sctp_bucket_cachep, pp);
  3913. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  3914. }
  3915. }
  3916. /* Release this socket's reference to a local port. */
  3917. static inline void __sctp_put_port(struct sock *sk)
  3918. {
  3919. struct sctp_bind_hashbucket *head =
  3920. &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
  3921. struct sctp_bind_bucket *pp;
  3922. sctp_spin_lock(&head->lock);
  3923. pp = sctp_sk(sk)->bind_hash;
  3924. __sk_del_bind_node(sk);
  3925. sctp_sk(sk)->bind_hash = NULL;
  3926. inet_sk(sk)->num = 0;
  3927. sctp_bucket_destroy(pp);
  3928. sctp_spin_unlock(&head->lock);
  3929. }
  3930. void sctp_put_port(struct sock *sk)
  3931. {
  3932. sctp_local_bh_disable();
  3933. __sctp_put_port(sk);
  3934. sctp_local_bh_enable();
  3935. }
  3936. /*
  3937. * The system picks an ephemeral port and choose an address set equivalent
  3938. * to binding with a wildcard address.
  3939. * One of those addresses will be the primary address for the association.
  3940. * This automatically enables the multihoming capability of SCTP.
  3941. */
  3942. static int sctp_autobind(struct sock *sk)
  3943. {
  3944. union sctp_addr autoaddr;
  3945. struct sctp_af *af;
  3946. unsigned short port;
  3947. /* Initialize a local sockaddr structure to INADDR_ANY. */
  3948. af = sctp_sk(sk)->pf->af;
  3949. port = htons(inet_sk(sk)->num);
  3950. af->inaddr_any(&autoaddr, port);
  3951. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  3952. }
  3953. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  3954. *
  3955. * From RFC 2292
  3956. * 4.2 The cmsghdr Structure *
  3957. *
  3958. * When ancillary data is sent or received, any number of ancillary data
  3959. * objects can be specified by the msg_control and msg_controllen members of
  3960. * the msghdr structure, because each object is preceded by
  3961. * a cmsghdr structure defining the object's length (the cmsg_len member).
  3962. * Historically Berkeley-derived implementations have passed only one object
  3963. * at a time, but this API allows multiple objects to be
  3964. * passed in a single call to sendmsg() or recvmsg(). The following example
  3965. * shows two ancillary data objects in a control buffer.
  3966. *
  3967. * |<--------------------------- msg_controllen -------------------------->|
  3968. * | |
  3969. *
  3970. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  3971. *
  3972. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  3973. * | | |
  3974. *
  3975. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  3976. *
  3977. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  3978. * | | | | |
  3979. *
  3980. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  3981. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  3982. *
  3983. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  3984. *
  3985. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  3986. * ^
  3987. * |
  3988. *
  3989. * msg_control
  3990. * points here
  3991. */
  3992. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
  3993. sctp_cmsgs_t *cmsgs)
  3994. {
  3995. struct cmsghdr *cmsg;
  3996. for (cmsg = CMSG_FIRSTHDR(msg);
  3997. cmsg != NULL;
  3998. cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
  3999. if (!CMSG_OK(msg, cmsg))
  4000. return -EINVAL;
  4001. /* Should we parse this header or ignore? */
  4002. if (cmsg->cmsg_level != IPPROTO_SCTP)
  4003. continue;
  4004. /* Strictly check lengths following example in SCM code. */
  4005. switch (cmsg->cmsg_type) {
  4006. case SCTP_INIT:
  4007. /* SCTP Socket API Extension
  4008. * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
  4009. *
  4010. * This cmsghdr structure provides information for
  4011. * initializing new SCTP associations with sendmsg().
  4012. * The SCTP_INITMSG socket option uses this same data
  4013. * structure. This structure is not used for
  4014. * recvmsg().
  4015. *
  4016. * cmsg_level cmsg_type cmsg_data[]
  4017. * ------------ ------------ ----------------------
  4018. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  4019. */
  4020. if (cmsg->cmsg_len !=
  4021. CMSG_LEN(sizeof(struct sctp_initmsg)))
  4022. return -EINVAL;
  4023. cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
  4024. break;
  4025. case SCTP_SNDRCV:
  4026. /* SCTP Socket API Extension
  4027. * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
  4028. *
  4029. * This cmsghdr structure specifies SCTP options for
  4030. * sendmsg() and describes SCTP header information
  4031. * about a received message through recvmsg().
  4032. *
  4033. * cmsg_level cmsg_type cmsg_data[]
  4034. * ------------ ------------ ----------------------
  4035. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  4036. */
  4037. if (cmsg->cmsg_len !=
  4038. CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  4039. return -EINVAL;
  4040. cmsgs->info =
  4041. (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  4042. /* Minimally, validate the sinfo_flags. */
  4043. if (cmsgs->info->sinfo_flags &
  4044. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  4045. SCTP_ABORT | SCTP_EOF))
  4046. return -EINVAL;
  4047. break;
  4048. default:
  4049. return -EINVAL;
  4050. };
  4051. }
  4052. return 0;
  4053. }
  4054. /*
  4055. * Wait for a packet..
  4056. * Note: This function is the same function as in core/datagram.c
  4057. * with a few modifications to make lksctp work.
  4058. */
  4059. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
  4060. {
  4061. int error;
  4062. DEFINE_WAIT(wait);
  4063. prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  4064. /* Socket errors? */
  4065. error = sock_error(sk);
  4066. if (error)
  4067. goto out;
  4068. if (!skb_queue_empty(&sk->sk_receive_queue))
  4069. goto ready;
  4070. /* Socket shut down? */
  4071. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4072. goto out;
  4073. /* Sequenced packets can come disconnected. If so we report the
  4074. * problem.
  4075. */
  4076. error = -ENOTCONN;
  4077. /* Is there a good reason to think that we may receive some data? */
  4078. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  4079. goto out;
  4080. /* Handle signals. */
  4081. if (signal_pending(current))
  4082. goto interrupted;
  4083. /* Let another process have a go. Since we are going to sleep
  4084. * anyway. Note: This may cause odd behaviors if the message
  4085. * does not fit in the user's buffer, but this seems to be the
  4086. * only way to honor MSG_DONTWAIT realistically.
  4087. */
  4088. sctp_release_sock(sk);
  4089. *timeo_p = schedule_timeout(*timeo_p);
  4090. sctp_lock_sock(sk);
  4091. ready:
  4092. finish_wait(sk->sk_sleep, &wait);
  4093. return 0;
  4094. interrupted:
  4095. error = sock_intr_errno(*timeo_p);
  4096. out:
  4097. finish_wait(sk->sk_sleep, &wait);
  4098. *err = error;
  4099. return error;
  4100. }
  4101. /* Receive a datagram.
  4102. * Note: This is pretty much the same routine as in core/datagram.c
  4103. * with a few changes to make lksctp work.
  4104. */
  4105. static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  4106. int noblock, int *err)
  4107. {
  4108. int error;
  4109. struct sk_buff *skb;
  4110. long timeo;
  4111. timeo = sock_rcvtimeo(sk, noblock);
  4112. SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
  4113. timeo, MAX_SCHEDULE_TIMEOUT);
  4114. do {
  4115. /* Again only user level code calls this function,
  4116. * so nothing interrupt level
  4117. * will suddenly eat the receive_queue.
  4118. *
  4119. * Look at current nfs client by the way...
  4120. * However, this function was corrent in any case. 8)
  4121. */
  4122. if (flags & MSG_PEEK) {
  4123. spin_lock_bh(&sk->sk_receive_queue.lock);
  4124. skb = skb_peek(&sk->sk_receive_queue);
  4125. if (skb)
  4126. atomic_inc(&skb->users);
  4127. spin_unlock_bh(&sk->sk_receive_queue.lock);
  4128. } else {
  4129. skb = skb_dequeue(&sk->sk_receive_queue);
  4130. }
  4131. if (skb)
  4132. return skb;
  4133. /* Caller is allowed not to check sk->sk_err before calling. */
  4134. error = sock_error(sk);
  4135. if (error)
  4136. goto no_packet;
  4137. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4138. break;
  4139. /* User doesn't want to wait. */
  4140. error = -EAGAIN;
  4141. if (!timeo)
  4142. goto no_packet;
  4143. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  4144. return NULL;
  4145. no_packet:
  4146. *err = error;
  4147. return NULL;
  4148. }
  4149. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  4150. static void __sctp_write_space(struct sctp_association *asoc)
  4151. {
  4152. struct sock *sk = asoc->base.sk;
  4153. struct socket *sock = sk->sk_socket;
  4154. if ((sctp_wspace(asoc) > 0) && sock) {
  4155. if (waitqueue_active(&asoc->wait))
  4156. wake_up_interruptible(&asoc->wait);
  4157. if (sctp_writeable(sk)) {
  4158. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  4159. wake_up_interruptible(sk->sk_sleep);
  4160. /* Note that we try to include the Async I/O support
  4161. * here by modeling from the current TCP/UDP code.
  4162. * We have not tested with it yet.
  4163. */
  4164. if (sock->fasync_list &&
  4165. !(sk->sk_shutdown & SEND_SHUTDOWN))
  4166. sock_wake_async(sock, 2, POLL_OUT);
  4167. }
  4168. }
  4169. }
  4170. /* Do accounting for the sndbuf space.
  4171. * Decrement the used sndbuf space of the corresponding association by the
  4172. * data size which was just transmitted(freed).
  4173. */
  4174. static void sctp_wfree(struct sk_buff *skb)
  4175. {
  4176. struct sctp_association *asoc;
  4177. struct sctp_chunk *chunk;
  4178. struct sock *sk;
  4179. /* Get the saved chunk pointer. */
  4180. chunk = *((struct sctp_chunk **)(skb->cb));
  4181. asoc = chunk->asoc;
  4182. sk = asoc->base.sk;
  4183. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  4184. sizeof(struct sk_buff) +
  4185. sizeof(struct sctp_chunk);
  4186. sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk) +
  4187. sizeof(struct sk_buff) +
  4188. sizeof(struct sctp_chunk);
  4189. atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  4190. sock_wfree(skb);
  4191. __sctp_write_space(asoc);
  4192. sctp_association_put(asoc);
  4193. }
  4194. /* Helper function to wait for space in the sndbuf. */
  4195. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  4196. size_t msg_len)
  4197. {
  4198. struct sock *sk = asoc->base.sk;
  4199. int err = 0;
  4200. long current_timeo = *timeo_p;
  4201. DEFINE_WAIT(wait);
  4202. SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
  4203. asoc, (long)(*timeo_p), msg_len);
  4204. /* Increment the association's refcnt. */
  4205. sctp_association_hold(asoc);
  4206. /* Wait on the association specific sndbuf space. */
  4207. for (;;) {
  4208. prepare_to_wait_exclusive(&asoc->wait, &wait,
  4209. TASK_INTERRUPTIBLE);
  4210. if (!*timeo_p)
  4211. goto do_nonblock;
  4212. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  4213. asoc->base.dead)
  4214. goto do_error;
  4215. if (signal_pending(current))
  4216. goto do_interrupted;
  4217. if (msg_len <= sctp_wspace(asoc))
  4218. break;
  4219. /* Let another process have a go. Since we are going
  4220. * to sleep anyway.
  4221. */
  4222. sctp_release_sock(sk);
  4223. current_timeo = schedule_timeout(current_timeo);
  4224. sctp_lock_sock(sk);
  4225. *timeo_p = current_timeo;
  4226. }
  4227. out:
  4228. finish_wait(&asoc->wait, &wait);
  4229. /* Release the association's refcnt. */
  4230. sctp_association_put(asoc);
  4231. return err;
  4232. do_error:
  4233. err = -EPIPE;
  4234. goto out;
  4235. do_interrupted:
  4236. err = sock_intr_errno(*timeo_p);
  4237. goto out;
  4238. do_nonblock:
  4239. err = -EAGAIN;
  4240. goto out;
  4241. }
  4242. /* If socket sndbuf has changed, wake up all per association waiters. */
  4243. void sctp_write_space(struct sock *sk)
  4244. {
  4245. struct sctp_association *asoc;
  4246. struct list_head *pos;
  4247. /* Wake up the tasks in each wait queue. */
  4248. list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
  4249. asoc = list_entry(pos, struct sctp_association, asocs);
  4250. __sctp_write_space(asoc);
  4251. }
  4252. }
  4253. /* Is there any sndbuf space available on the socket?
  4254. *
  4255. * Note that wmem_queued is the sum of the send buffers on all of the
  4256. * associations on the same socket. For a UDP-style socket with
  4257. * multiple associations, it is possible for it to be "unwriteable"
  4258. * prematurely. I assume that this is acceptable because
  4259. * a premature "unwriteable" is better than an accidental "writeable" which
  4260. * would cause an unwanted block under certain circumstances. For the 1-1
  4261. * UDP-style sockets or TCP-style sockets, this code should work.
  4262. * - Daisy
  4263. */
  4264. static int sctp_writeable(struct sock *sk)
  4265. {
  4266. int amt = 0;
  4267. amt = sk->sk_sndbuf - sk->sk_wmem_queued;
  4268. if (amt < 0)
  4269. amt = 0;
  4270. return amt;
  4271. }
  4272. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  4273. * returns immediately with EINPROGRESS.
  4274. */
  4275. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  4276. {
  4277. struct sock *sk = asoc->base.sk;
  4278. int err = 0;
  4279. long current_timeo = *timeo_p;
  4280. DEFINE_WAIT(wait);
  4281. SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
  4282. (long)(*timeo_p));
  4283. /* Increment the association's refcnt. */
  4284. sctp_association_hold(asoc);
  4285. for (;;) {
  4286. prepare_to_wait_exclusive(&asoc->wait, &wait,
  4287. TASK_INTERRUPTIBLE);
  4288. if (!*timeo_p)
  4289. goto do_nonblock;
  4290. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4291. break;
  4292. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  4293. asoc->base.dead)
  4294. goto do_error;
  4295. if (signal_pending(current))
  4296. goto do_interrupted;
  4297. if (sctp_state(asoc, ESTABLISHED))
  4298. break;
  4299. /* Let another process have a go. Since we are going
  4300. * to sleep anyway.
  4301. */
  4302. sctp_release_sock(sk);
  4303. current_timeo = schedule_timeout(current_timeo);
  4304. sctp_lock_sock(sk);
  4305. *timeo_p = current_timeo;
  4306. }
  4307. out:
  4308. finish_wait(&asoc->wait, &wait);
  4309. /* Release the association's refcnt. */
  4310. sctp_association_put(asoc);
  4311. return err;
  4312. do_error:
  4313. if (asoc->init_err_counter + 1 >= asoc->max_init_attempts)
  4314. err = -ETIMEDOUT;
  4315. else
  4316. err = -ECONNREFUSED;
  4317. goto out;
  4318. do_interrupted:
  4319. err = sock_intr_errno(*timeo_p);
  4320. goto out;
  4321. do_nonblock:
  4322. err = -EINPROGRESS;
  4323. goto out;
  4324. }
  4325. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  4326. {
  4327. struct sctp_endpoint *ep;
  4328. int err = 0;
  4329. DEFINE_WAIT(wait);
  4330. ep = sctp_sk(sk)->ep;
  4331. for (;;) {
  4332. prepare_to_wait_exclusive(sk->sk_sleep, &wait,
  4333. TASK_INTERRUPTIBLE);
  4334. if (list_empty(&ep->asocs)) {
  4335. sctp_release_sock(sk);
  4336. timeo = schedule_timeout(timeo);
  4337. sctp_lock_sock(sk);
  4338. }
  4339. err = -EINVAL;
  4340. if (!sctp_sstate(sk, LISTENING))
  4341. break;
  4342. err = 0;
  4343. if (!list_empty(&ep->asocs))
  4344. break;
  4345. err = sock_intr_errno(timeo);
  4346. if (signal_pending(current))
  4347. break;
  4348. err = -EAGAIN;
  4349. if (!timeo)
  4350. break;
  4351. }
  4352. finish_wait(sk->sk_sleep, &wait);
  4353. return err;
  4354. }
  4355. void sctp_wait_for_close(struct sock *sk, long timeout)
  4356. {
  4357. DEFINE_WAIT(wait);
  4358. do {
  4359. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  4360. if (list_empty(&sctp_sk(sk)->ep->asocs))
  4361. break;
  4362. sctp_release_sock(sk);
  4363. timeout = schedule_timeout(timeout);
  4364. sctp_lock_sock(sk);
  4365. } while (!signal_pending(current) && timeout);
  4366. finish_wait(sk->sk_sleep, &wait);
  4367. }
  4368. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  4369. * and its messages to the newsk.
  4370. */
  4371. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  4372. struct sctp_association *assoc,
  4373. sctp_socket_type_t type)
  4374. {
  4375. struct sctp_sock *oldsp = sctp_sk(oldsk);
  4376. struct sctp_sock *newsp = sctp_sk(newsk);
  4377. struct sctp_bind_bucket *pp; /* hash list port iterator */
  4378. struct sctp_endpoint *newep = newsp->ep;
  4379. struct sk_buff *skb, *tmp;
  4380. struct sctp_ulpevent *event;
  4381. int flags = 0;
  4382. /* Migrate socket buffer sizes and all the socket level options to the
  4383. * new socket.
  4384. */
  4385. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  4386. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  4387. /* Brute force copy old sctp opt. */
  4388. inet_sk_copy_descendant(newsk, oldsk);
  4389. /* Restore the ep value that was overwritten with the above structure
  4390. * copy.
  4391. */
  4392. newsp->ep = newep;
  4393. newsp->hmac = NULL;
  4394. /* Hook this new socket in to the bind_hash list. */
  4395. pp = sctp_sk(oldsk)->bind_hash;
  4396. sk_add_bind_node(newsk, &pp->owner);
  4397. sctp_sk(newsk)->bind_hash = pp;
  4398. inet_sk(newsk)->num = inet_sk(oldsk)->num;
  4399. /* Copy the bind_addr list from the original endpoint to the new
  4400. * endpoint so that we can handle restarts properly
  4401. */
  4402. if (assoc->peer.ipv4_address)
  4403. flags |= SCTP_ADDR4_PEERSUPP;
  4404. if (assoc->peer.ipv6_address)
  4405. flags |= SCTP_ADDR6_PEERSUPP;
  4406. sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
  4407. &oldsp->ep->base.bind_addr,
  4408. SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
  4409. /* Move any messages in the old socket's receive queue that are for the
  4410. * peeled off association to the new socket's receive queue.
  4411. */
  4412. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  4413. event = sctp_skb2event(skb);
  4414. if (event->asoc == assoc) {
  4415. sock_rfree(skb);
  4416. __skb_unlink(skb, &oldsk->sk_receive_queue);
  4417. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  4418. skb_set_owner_r(skb, newsk);
  4419. }
  4420. }
  4421. /* Clean up any messages pending delivery due to partial
  4422. * delivery. Three cases:
  4423. * 1) No partial deliver; no work.
  4424. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  4425. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  4426. */
  4427. skb_queue_head_init(&newsp->pd_lobby);
  4428. sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
  4429. if (sctp_sk(oldsk)->pd_mode) {
  4430. struct sk_buff_head *queue;
  4431. /* Decide which queue to move pd_lobby skbs to. */
  4432. if (assoc->ulpq.pd_mode) {
  4433. queue = &newsp->pd_lobby;
  4434. } else
  4435. queue = &newsk->sk_receive_queue;
  4436. /* Walk through the pd_lobby, looking for skbs that
  4437. * need moved to the new socket.
  4438. */
  4439. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  4440. event = sctp_skb2event(skb);
  4441. if (event->asoc == assoc) {
  4442. sock_rfree(skb);
  4443. __skb_unlink(skb, &oldsp->pd_lobby);
  4444. __skb_queue_tail(queue, skb);
  4445. skb_set_owner_r(skb, newsk);
  4446. }
  4447. }
  4448. /* Clear up any skbs waiting for the partial
  4449. * delivery to finish.
  4450. */
  4451. if (assoc->ulpq.pd_mode)
  4452. sctp_clear_pd(oldsk);
  4453. }
  4454. /* Set the type of socket to indicate that it is peeled off from the
  4455. * original UDP-style socket or created with the accept() call on a
  4456. * TCP-style socket..
  4457. */
  4458. newsp->type = type;
  4459. /* Migrate the association to the new socket. */
  4460. sctp_assoc_migrate(assoc, newsk);
  4461. /* If the association on the newsk is already closed before accept()
  4462. * is called, set RCV_SHUTDOWN flag.
  4463. */
  4464. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
  4465. newsk->sk_shutdown |= RCV_SHUTDOWN;
  4466. newsk->sk_state = SCTP_SS_ESTABLISHED;
  4467. }
  4468. /* This proto struct describes the ULP interface for SCTP. */
  4469. struct proto sctp_prot = {
  4470. .name = "SCTP",
  4471. .owner = THIS_MODULE,
  4472. .close = sctp_close,
  4473. .connect = sctp_connect,
  4474. .disconnect = sctp_disconnect,
  4475. .accept = sctp_accept,
  4476. .ioctl = sctp_ioctl,
  4477. .init = sctp_init_sock,
  4478. .destroy = sctp_destroy_sock,
  4479. .shutdown = sctp_shutdown,
  4480. .setsockopt = sctp_setsockopt,
  4481. .getsockopt = sctp_getsockopt,
  4482. .sendmsg = sctp_sendmsg,
  4483. .recvmsg = sctp_recvmsg,
  4484. .bind = sctp_bind,
  4485. .backlog_rcv = sctp_backlog_rcv,
  4486. .hash = sctp_hash,
  4487. .unhash = sctp_unhash,
  4488. .get_port = sctp_get_port,
  4489. .obj_size = sizeof(struct sctp_sock),
  4490. };
  4491. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  4492. struct proto sctpv6_prot = {
  4493. .name = "SCTPv6",
  4494. .owner = THIS_MODULE,
  4495. .close = sctp_close,
  4496. .connect = sctp_connect,
  4497. .disconnect = sctp_disconnect,
  4498. .accept = sctp_accept,
  4499. .ioctl = sctp_ioctl,
  4500. .init = sctp_init_sock,
  4501. .destroy = sctp_destroy_sock,
  4502. .shutdown = sctp_shutdown,
  4503. .setsockopt = sctp_setsockopt,
  4504. .getsockopt = sctp_getsockopt,
  4505. .sendmsg = sctp_sendmsg,
  4506. .recvmsg = sctp_recvmsg,
  4507. .bind = sctp_bind,
  4508. .backlog_rcv = sctp_backlog_rcv,
  4509. .hash = sctp_hash,
  4510. .unhash = sctp_unhash,
  4511. .get_port = sctp_get_port,
  4512. .obj_size = sizeof(struct sctp6_sock),
  4513. };
  4514. #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */