socket.c 139 KB

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