socket.c 200 KB

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