socket.c 182 KB

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