socket.c 164 KB

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