socket.c 146 KB

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