socket.c 186 KB

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