socket.c 166 KB

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