socket.c 133 KB

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