socket.c 173 KB

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