socket.c 200 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083
  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 void sctp_destruct_sock(struct sock *sk);
  92. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  93. union sctp_addr *addr, int len);
  94. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  95. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  96. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  97. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  98. static int sctp_send_asconf(struct sctp_association *asoc,
  99. struct sctp_chunk *chunk);
  100. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  101. static int sctp_autobind(struct sock *sk);
  102. static void sctp_sock_migrate(struct sock *, struct sock *,
  103. struct sctp_association *, sctp_socket_type_t);
  104. extern struct kmem_cache *sctp_bucket_cachep;
  105. extern long sysctl_sctp_mem[3];
  106. extern int sysctl_sctp_rmem[3];
  107. extern int sysctl_sctp_wmem[3];
  108. static int sctp_memory_pressure;
  109. static atomic_long_t sctp_memory_allocated;
  110. struct percpu_counter sctp_sockets_allocated;
  111. static void sctp_enter_memory_pressure(struct sock *sk)
  112. {
  113. sctp_memory_pressure = 1;
  114. }
  115. /* Get the sndbuf space available at the time on the association. */
  116. static inline int sctp_wspace(struct sctp_association *asoc)
  117. {
  118. int amt;
  119. if (asoc->ep->sndbuf_policy)
  120. amt = asoc->sndbuf_used;
  121. else
  122. amt = sk_wmem_alloc_get(asoc->base.sk);
  123. if (amt >= asoc->base.sk->sk_sndbuf) {
  124. if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
  125. amt = 0;
  126. else {
  127. amt = sk_stream_wspace(asoc->base.sk);
  128. if (amt < 0)
  129. amt = 0;
  130. }
  131. } else {
  132. amt = asoc->base.sk->sk_sndbuf - amt;
  133. }
  134. return amt;
  135. }
  136. /* Increment the used sndbuf space count of the corresponding association by
  137. * the size of the outgoing data chunk.
  138. * Also, set the skb destructor for sndbuf accounting later.
  139. *
  140. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  141. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  142. * destructor in the data chunk skb for the purpose of the sndbuf space
  143. * tracking.
  144. */
  145. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  146. {
  147. struct sctp_association *asoc = chunk->asoc;
  148. struct sock *sk = asoc->base.sk;
  149. /* The sndbuf space is tracked per association. */
  150. sctp_association_hold(asoc);
  151. skb_set_owner_w(chunk->skb, sk);
  152. chunk->skb->destructor = sctp_wfree;
  153. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  154. *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
  155. asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
  156. sizeof(struct sk_buff) +
  157. sizeof(struct sctp_chunk);
  158. atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  159. sk->sk_wmem_queued += chunk->skb->truesize;
  160. sk_mem_charge(sk, chunk->skb->truesize);
  161. }
  162. /* Verify that this is a valid address. */
  163. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  164. int len)
  165. {
  166. struct sctp_af *af;
  167. /* Verify basic sockaddr. */
  168. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  169. if (!af)
  170. return -EINVAL;
  171. /* Is this a valid SCTP address? */
  172. if (!af->addr_valid(addr, sctp_sk(sk), NULL))
  173. return -EINVAL;
  174. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  175. return -EINVAL;
  176. return 0;
  177. }
  178. /* Look up the association by its id. If this is not a UDP-style
  179. * socket, the ID field is always ignored.
  180. */
  181. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  182. {
  183. struct sctp_association *asoc = NULL;
  184. /* If this is not a UDP-style socket, assoc id should be ignored. */
  185. if (!sctp_style(sk, UDP)) {
  186. /* Return NULL if the socket state is not ESTABLISHED. It
  187. * could be a TCP-style listening socket or a socket which
  188. * hasn't yet called connect() to establish an association.
  189. */
  190. if (!sctp_sstate(sk, ESTABLISHED))
  191. return NULL;
  192. /* Get the first and the only association from the list. */
  193. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  194. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  195. struct sctp_association, asocs);
  196. return asoc;
  197. }
  198. /* Otherwise this is a UDP-style socket. */
  199. if (!id || (id == (sctp_assoc_t)-1))
  200. return NULL;
  201. spin_lock_bh(&sctp_assocs_id_lock);
  202. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  203. spin_unlock_bh(&sctp_assocs_id_lock);
  204. if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
  205. return NULL;
  206. return asoc;
  207. }
  208. /* Look up the transport from an address and an assoc id. If both address and
  209. * id are specified, the associations matching the address and the id should be
  210. * the same.
  211. */
  212. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  213. struct sockaddr_storage *addr,
  214. sctp_assoc_t id)
  215. {
  216. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  217. struct sctp_transport *transport;
  218. union sctp_addr *laddr = (union sctp_addr *)addr;
  219. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  220. laddr,
  221. &transport);
  222. if (!addr_asoc)
  223. return NULL;
  224. id_asoc = sctp_id2assoc(sk, id);
  225. if (id_asoc && (id_asoc != addr_asoc))
  226. return NULL;
  227. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  228. (union sctp_addr *)addr);
  229. return transport;
  230. }
  231. /* API 3.1.2 bind() - UDP Style Syntax
  232. * The syntax of bind() is,
  233. *
  234. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  235. *
  236. * sd - the socket descriptor returned by socket().
  237. * addr - the address structure (struct sockaddr_in or struct
  238. * sockaddr_in6 [RFC 2553]),
  239. * addr_len - the size of the address structure.
  240. */
  241. static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
  242. {
  243. int retval = 0;
  244. sctp_lock_sock(sk);
  245. pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
  246. addr, addr_len);
  247. /* Disallow binding twice. */
  248. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  249. retval = sctp_do_bind(sk, (union sctp_addr *)addr,
  250. addr_len);
  251. else
  252. retval = -EINVAL;
  253. sctp_release_sock(sk);
  254. return retval;
  255. }
  256. static long sctp_get_port_local(struct sock *, union sctp_addr *);
  257. /* Verify this is a valid sockaddr. */
  258. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  259. union sctp_addr *addr, int len)
  260. {
  261. struct sctp_af *af;
  262. /* Check minimum size. */
  263. if (len < sizeof (struct sockaddr))
  264. return NULL;
  265. /* V4 mapped address are really of AF_INET family */
  266. if (addr->sa.sa_family == AF_INET6 &&
  267. ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
  268. if (!opt->pf->af_supported(AF_INET, opt))
  269. return NULL;
  270. } else {
  271. /* Does this PF support this AF? */
  272. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  273. return NULL;
  274. }
  275. /* If we get this far, af is valid. */
  276. af = sctp_get_af_specific(addr->sa.sa_family);
  277. if (len < af->sockaddr_len)
  278. return NULL;
  279. return af;
  280. }
  281. /* Bind a local address either to an endpoint or to an association. */
  282. static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  283. {
  284. struct net *net = sock_net(sk);
  285. struct sctp_sock *sp = sctp_sk(sk);
  286. struct sctp_endpoint *ep = sp->ep;
  287. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  288. struct sctp_af *af;
  289. unsigned short snum;
  290. int ret = 0;
  291. /* Common sockaddr verification. */
  292. af = sctp_sockaddr_af(sp, addr, len);
  293. if (!af) {
  294. pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
  295. __func__, sk, addr, len);
  296. return -EINVAL;
  297. }
  298. snum = ntohs(addr->v4.sin_port);
  299. pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
  300. __func__, sk, &addr->sa, bp->port, snum, len);
  301. /* PF specific bind() address verification. */
  302. if (!sp->pf->bind_verify(sp, addr))
  303. return -EADDRNOTAVAIL;
  304. /* We must either be unbound, or bind to the same port.
  305. * It's OK to allow 0 ports if we are already bound.
  306. * We'll just inhert an already bound port in this case
  307. */
  308. if (bp->port) {
  309. if (!snum)
  310. snum = bp->port;
  311. else if (snum != bp->port) {
  312. pr_debug("%s: new port %d doesn't match existing port "
  313. "%d\n", __func__, snum, bp->port);
  314. return -EINVAL;
  315. }
  316. }
  317. if (snum && snum < PROT_SOCK &&
  318. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  319. return -EACCES;
  320. /* See if the address matches any of the addresses we may have
  321. * already bound before checking against other endpoints.
  322. */
  323. if (sctp_bind_addr_match(bp, addr, sp))
  324. return -EINVAL;
  325. /* Make sure we are allowed to bind here.
  326. * The function sctp_get_port_local() does duplicate address
  327. * detection.
  328. */
  329. addr->v4.sin_port = htons(snum);
  330. if ((ret = sctp_get_port_local(sk, addr))) {
  331. return -EADDRINUSE;
  332. }
  333. /* Refresh ephemeral port. */
  334. if (!bp->port)
  335. bp->port = inet_sk(sk)->inet_num;
  336. /* Add the address to the bind address list.
  337. * Use GFP_ATOMIC since BHs will be disabled.
  338. */
  339. ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC);
  340. /* Copy back into socket for getsockname() use. */
  341. if (!ret) {
  342. inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
  343. af->to_sk_saddr(addr, sk);
  344. }
  345. return ret;
  346. }
  347. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  348. *
  349. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  350. * at any one time. If a sender, after sending an ASCONF chunk, decides
  351. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  352. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  353. * subsequent ASCONF. Note this restriction binds each side, so at any
  354. * time two ASCONF may be in-transit on any given association (one sent
  355. * from each endpoint).
  356. */
  357. static int sctp_send_asconf(struct sctp_association *asoc,
  358. struct sctp_chunk *chunk)
  359. {
  360. struct net *net = sock_net(asoc->base.sk);
  361. int retval = 0;
  362. /* If there is an outstanding ASCONF chunk, queue it for later
  363. * transmission.
  364. */
  365. if (asoc->addip_last_asconf) {
  366. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  367. goto out;
  368. }
  369. /* Hold the chunk until an ASCONF_ACK is received. */
  370. sctp_chunk_hold(chunk);
  371. retval = sctp_primitive_ASCONF(net, asoc, chunk);
  372. if (retval)
  373. sctp_chunk_free(chunk);
  374. else
  375. asoc->addip_last_asconf = chunk;
  376. out:
  377. return retval;
  378. }
  379. /* Add a list of addresses as bind addresses to local endpoint or
  380. * association.
  381. *
  382. * Basically run through each address specified in the addrs/addrcnt
  383. * array/length pair, determine if it is IPv6 or IPv4 and call
  384. * sctp_do_bind() on it.
  385. *
  386. * If any of them fails, then the operation will be reversed and the
  387. * ones that were added will be removed.
  388. *
  389. * Only sctp_setsockopt_bindx() is supposed to call this function.
  390. */
  391. static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  392. {
  393. int cnt;
  394. int retval = 0;
  395. void *addr_buf;
  396. struct sockaddr *sa_addr;
  397. struct sctp_af *af;
  398. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
  399. addrs, addrcnt);
  400. addr_buf = addrs;
  401. for (cnt = 0; cnt < addrcnt; cnt++) {
  402. /* The list may contain either IPv4 or IPv6 address;
  403. * determine the address length for walking thru the list.
  404. */
  405. sa_addr = addr_buf;
  406. af = sctp_get_af_specific(sa_addr->sa_family);
  407. if (!af) {
  408. retval = -EINVAL;
  409. goto err_bindx_add;
  410. }
  411. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  412. af->sockaddr_len);
  413. addr_buf += af->sockaddr_len;
  414. err_bindx_add:
  415. if (retval < 0) {
  416. /* Failed. Cleanup the ones that have been added */
  417. if (cnt > 0)
  418. sctp_bindx_rem(sk, addrs, cnt);
  419. return retval;
  420. }
  421. }
  422. return retval;
  423. }
  424. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  425. * associations that are part of the endpoint indicating that a list of local
  426. * addresses are added to the endpoint.
  427. *
  428. * If any of the addresses is already in the bind address list of the
  429. * association, we do not send the chunk for that association. But it will not
  430. * affect other associations.
  431. *
  432. * Only sctp_setsockopt_bindx() is supposed to call this function.
  433. */
  434. static int sctp_send_asconf_add_ip(struct sock *sk,
  435. struct sockaddr *addrs,
  436. int addrcnt)
  437. {
  438. struct net *net = sock_net(sk);
  439. struct sctp_sock *sp;
  440. struct sctp_endpoint *ep;
  441. struct sctp_association *asoc;
  442. struct sctp_bind_addr *bp;
  443. struct sctp_chunk *chunk;
  444. struct sctp_sockaddr_entry *laddr;
  445. union sctp_addr *addr;
  446. union sctp_addr saveaddr;
  447. void *addr_buf;
  448. struct sctp_af *af;
  449. struct list_head *p;
  450. int i;
  451. int retval = 0;
  452. if (!net->sctp.addip_enable)
  453. return retval;
  454. sp = sctp_sk(sk);
  455. ep = sp->ep;
  456. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  457. __func__, sk, addrs, addrcnt);
  458. list_for_each_entry(asoc, &ep->asocs, asocs) {
  459. if (!asoc->peer.asconf_capable)
  460. continue;
  461. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  462. continue;
  463. if (!sctp_state(asoc, ESTABLISHED))
  464. continue;
  465. /* Check if any address in the packed array of addresses is
  466. * in the bind address list of the association. If so,
  467. * do not send the asconf chunk to its peer, but continue with
  468. * other associations.
  469. */
  470. addr_buf = addrs;
  471. for (i = 0; i < addrcnt; i++) {
  472. addr = addr_buf;
  473. af = sctp_get_af_specific(addr->v4.sin_family);
  474. if (!af) {
  475. retval = -EINVAL;
  476. goto out;
  477. }
  478. if (sctp_assoc_lookup_laddr(asoc, addr))
  479. break;
  480. addr_buf += af->sockaddr_len;
  481. }
  482. if (i < addrcnt)
  483. continue;
  484. /* Use the first valid address in bind addr list of
  485. * association as Address Parameter of ASCONF CHUNK.
  486. */
  487. bp = &asoc->base.bind_addr;
  488. p = bp->address_list.next;
  489. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  490. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  491. addrcnt, SCTP_PARAM_ADD_IP);
  492. if (!chunk) {
  493. retval = -ENOMEM;
  494. goto out;
  495. }
  496. /* Add the new addresses to the bind address list with
  497. * use_as_src set to 0.
  498. */
  499. addr_buf = addrs;
  500. for (i = 0; i < addrcnt; i++) {
  501. addr = addr_buf;
  502. af = sctp_get_af_specific(addr->v4.sin_family);
  503. memcpy(&saveaddr, addr, af->sockaddr_len);
  504. retval = sctp_add_bind_addr(bp, &saveaddr,
  505. SCTP_ADDR_NEW, GFP_ATOMIC);
  506. addr_buf += af->sockaddr_len;
  507. }
  508. if (asoc->src_out_of_asoc_ok) {
  509. struct sctp_transport *trans;
  510. list_for_each_entry(trans,
  511. &asoc->peer.transport_addr_list, transports) {
  512. /* Clear the source and route cache */
  513. dst_release(trans->dst);
  514. trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
  515. 2*asoc->pathmtu, 4380));
  516. trans->ssthresh = asoc->peer.i.a_rwnd;
  517. trans->rto = asoc->rto_initial;
  518. sctp_max_rto(asoc, trans);
  519. trans->rtt = trans->srtt = trans->rttvar = 0;
  520. sctp_transport_route(trans, NULL,
  521. sctp_sk(asoc->base.sk));
  522. }
  523. }
  524. retval = sctp_send_asconf(asoc, chunk);
  525. }
  526. out:
  527. return retval;
  528. }
  529. /* Remove a list of addresses from bind addresses list. Do not remove the
  530. * last address.
  531. *
  532. * Basically run through each address specified in the addrs/addrcnt
  533. * array/length pair, determine if it is IPv6 or IPv4 and call
  534. * sctp_del_bind() on it.
  535. *
  536. * If any of them fails, then the operation will be reversed and the
  537. * ones that were removed will be added back.
  538. *
  539. * At least one address has to be left; if only one address is
  540. * available, the operation will return -EBUSY.
  541. *
  542. * Only sctp_setsockopt_bindx() is supposed to call this function.
  543. */
  544. static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  545. {
  546. struct sctp_sock *sp = sctp_sk(sk);
  547. struct sctp_endpoint *ep = sp->ep;
  548. int cnt;
  549. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  550. int retval = 0;
  551. void *addr_buf;
  552. union sctp_addr *sa_addr;
  553. struct sctp_af *af;
  554. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  555. __func__, sk, addrs, addrcnt);
  556. addr_buf = addrs;
  557. for (cnt = 0; cnt < addrcnt; cnt++) {
  558. /* If the bind address list is empty or if there is only one
  559. * bind address, there is nothing more to be removed (we need
  560. * at least one address here).
  561. */
  562. if (list_empty(&bp->address_list) ||
  563. (sctp_list_single_entry(&bp->address_list))) {
  564. retval = -EBUSY;
  565. goto err_bindx_rem;
  566. }
  567. sa_addr = addr_buf;
  568. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  569. if (!af) {
  570. retval = -EINVAL;
  571. goto err_bindx_rem;
  572. }
  573. if (!af->addr_valid(sa_addr, sp, NULL)) {
  574. retval = -EADDRNOTAVAIL;
  575. goto err_bindx_rem;
  576. }
  577. if (sa_addr->v4.sin_port &&
  578. sa_addr->v4.sin_port != htons(bp->port)) {
  579. retval = -EINVAL;
  580. goto err_bindx_rem;
  581. }
  582. if (!sa_addr->v4.sin_port)
  583. sa_addr->v4.sin_port = htons(bp->port);
  584. /* FIXME - There is probably a need to check if sk->sk_saddr and
  585. * sk->sk_rcv_addr are currently set to one of the addresses to
  586. * be removed. This is something which needs to be looked into
  587. * when we are fixing the outstanding issues with multi-homing
  588. * socket routing and failover schemes. Refer to comments in
  589. * sctp_do_bind(). -daisy
  590. */
  591. retval = sctp_del_bind_addr(bp, sa_addr);
  592. addr_buf += af->sockaddr_len;
  593. err_bindx_rem:
  594. if (retval < 0) {
  595. /* Failed. Add the ones that has been removed back */
  596. if (cnt > 0)
  597. sctp_bindx_add(sk, addrs, cnt);
  598. return retval;
  599. }
  600. }
  601. return retval;
  602. }
  603. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  604. * the associations that are part of the endpoint indicating that a list of
  605. * local addresses are removed from the endpoint.
  606. *
  607. * If any of the addresses is already in the bind address list of the
  608. * association, we do not send the chunk for that association. But it will not
  609. * affect other associations.
  610. *
  611. * Only sctp_setsockopt_bindx() is supposed to call this function.
  612. */
  613. static int sctp_send_asconf_del_ip(struct sock *sk,
  614. struct sockaddr *addrs,
  615. int addrcnt)
  616. {
  617. struct net *net = sock_net(sk);
  618. struct sctp_sock *sp;
  619. struct sctp_endpoint *ep;
  620. struct sctp_association *asoc;
  621. struct sctp_transport *transport;
  622. struct sctp_bind_addr *bp;
  623. struct sctp_chunk *chunk;
  624. union sctp_addr *laddr;
  625. void *addr_buf;
  626. struct sctp_af *af;
  627. struct sctp_sockaddr_entry *saddr;
  628. int i;
  629. int retval = 0;
  630. int stored = 0;
  631. chunk = NULL;
  632. if (!net->sctp.addip_enable)
  633. return retval;
  634. sp = sctp_sk(sk);
  635. ep = sp->ep;
  636. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  637. __func__, sk, addrs, addrcnt);
  638. list_for_each_entry(asoc, &ep->asocs, asocs) {
  639. if (!asoc->peer.asconf_capable)
  640. continue;
  641. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  642. continue;
  643. if (!sctp_state(asoc, ESTABLISHED))
  644. continue;
  645. /* Check if any address in the packed array of addresses is
  646. * not present in the bind address list of the association.
  647. * If so, do not send the asconf chunk to its peer, but
  648. * continue with other associations.
  649. */
  650. addr_buf = addrs;
  651. for (i = 0; i < addrcnt; i++) {
  652. laddr = addr_buf;
  653. af = sctp_get_af_specific(laddr->v4.sin_family);
  654. if (!af) {
  655. retval = -EINVAL;
  656. goto out;
  657. }
  658. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  659. break;
  660. addr_buf += af->sockaddr_len;
  661. }
  662. if (i < addrcnt)
  663. continue;
  664. /* Find one address in the association's bind address list
  665. * that is not in the packed array of addresses. This is to
  666. * make sure that we do not delete all the addresses in the
  667. * association.
  668. */
  669. bp = &asoc->base.bind_addr;
  670. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  671. addrcnt, sp);
  672. if ((laddr == NULL) && (addrcnt == 1)) {
  673. if (asoc->asconf_addr_del_pending)
  674. continue;
  675. asoc->asconf_addr_del_pending =
  676. kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
  677. if (asoc->asconf_addr_del_pending == NULL) {
  678. retval = -ENOMEM;
  679. goto out;
  680. }
  681. asoc->asconf_addr_del_pending->sa.sa_family =
  682. addrs->sa_family;
  683. asoc->asconf_addr_del_pending->v4.sin_port =
  684. htons(bp->port);
  685. if (addrs->sa_family == AF_INET) {
  686. struct sockaddr_in *sin;
  687. sin = (struct sockaddr_in *)addrs;
  688. asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
  689. } else if (addrs->sa_family == AF_INET6) {
  690. struct sockaddr_in6 *sin6;
  691. sin6 = (struct sockaddr_in6 *)addrs;
  692. asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
  693. }
  694. pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
  695. __func__, asoc, &asoc->asconf_addr_del_pending->sa,
  696. asoc->asconf_addr_del_pending);
  697. asoc->src_out_of_asoc_ok = 1;
  698. stored = 1;
  699. goto skip_mkasconf;
  700. }
  701. /* We do not need RCU protection throughout this loop
  702. * because this is done under a socket lock from the
  703. * setsockopt call.
  704. */
  705. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  706. SCTP_PARAM_DEL_IP);
  707. if (!chunk) {
  708. retval = -ENOMEM;
  709. goto out;
  710. }
  711. skip_mkasconf:
  712. /* Reset use_as_src flag for the addresses in the bind address
  713. * list that are to be deleted.
  714. */
  715. addr_buf = addrs;
  716. for (i = 0; i < addrcnt; i++) {
  717. laddr = addr_buf;
  718. af = sctp_get_af_specific(laddr->v4.sin_family);
  719. list_for_each_entry(saddr, &bp->address_list, list) {
  720. if (sctp_cmp_addr_exact(&saddr->a, laddr))
  721. saddr->state = SCTP_ADDR_DEL;
  722. }
  723. addr_buf += af->sockaddr_len;
  724. }
  725. /* Update the route and saddr entries for all the transports
  726. * as some of the addresses in the bind address list are
  727. * about to be deleted and cannot be used as source addresses.
  728. */
  729. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  730. transports) {
  731. dst_release(transport->dst);
  732. sctp_transport_route(transport, NULL,
  733. sctp_sk(asoc->base.sk));
  734. }
  735. if (stored)
  736. /* We don't need to transmit ASCONF */
  737. continue;
  738. retval = sctp_send_asconf(asoc, chunk);
  739. }
  740. out:
  741. return retval;
  742. }
  743. /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
  744. int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
  745. {
  746. struct sock *sk = sctp_opt2sk(sp);
  747. union sctp_addr *addr;
  748. struct sctp_af *af;
  749. /* It is safe to write port space in caller. */
  750. addr = &addrw->a;
  751. addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
  752. af = sctp_get_af_specific(addr->sa.sa_family);
  753. if (!af)
  754. return -EINVAL;
  755. if (sctp_verify_addr(sk, addr, af->sockaddr_len))
  756. return -EINVAL;
  757. if (addrw->state == SCTP_ADDR_NEW)
  758. return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
  759. else
  760. return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
  761. }
  762. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  763. *
  764. * API 8.1
  765. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  766. * int flags);
  767. *
  768. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  769. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  770. * or IPv6 addresses.
  771. *
  772. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  773. * Section 3.1.2 for this usage.
  774. *
  775. * addrs is a pointer to an array of one or more socket addresses. Each
  776. * address is contained in its appropriate structure (i.e. struct
  777. * sockaddr_in or struct sockaddr_in6) the family of the address type
  778. * must be used to distinguish the address length (note that this
  779. * representation is termed a "packed array" of addresses). The caller
  780. * specifies the number of addresses in the array with addrcnt.
  781. *
  782. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  783. * -1, and sets errno to the appropriate error code.
  784. *
  785. * For SCTP, the port given in each socket address must be the same, or
  786. * sctp_bindx() will fail, setting errno to EINVAL.
  787. *
  788. * The flags parameter is formed from the bitwise OR of zero or more of
  789. * the following currently defined flags:
  790. *
  791. * SCTP_BINDX_ADD_ADDR
  792. *
  793. * SCTP_BINDX_REM_ADDR
  794. *
  795. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  796. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  797. * addresses from the association. The two flags are mutually exclusive;
  798. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  799. * not remove all addresses from an association; sctp_bindx() will
  800. * reject such an attempt with EINVAL.
  801. *
  802. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  803. * additional addresses with an endpoint after calling bind(). Or use
  804. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  805. * socket is associated with so that no new association accepted will be
  806. * associated with those addresses. If the endpoint supports dynamic
  807. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  808. * endpoint to send the appropriate message to the peer to change the
  809. * peers address lists.
  810. *
  811. * Adding and removing addresses from a connected association is
  812. * optional functionality. Implementations that do not support this
  813. * functionality should return EOPNOTSUPP.
  814. *
  815. * Basically do nothing but copying the addresses from user to kernel
  816. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  817. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  818. * from userspace.
  819. *
  820. * We don't use copy_from_user() for optimization: we first do the
  821. * sanity checks (buffer size -fast- and access check-healthy
  822. * pointer); if all of those succeed, then we can alloc the memory
  823. * (expensive operation) needed to copy the data to kernel. Then we do
  824. * the copying without checking the user space area
  825. * (__copy_from_user()).
  826. *
  827. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  828. * it.
  829. *
  830. * sk The sk of the socket
  831. * addrs The pointer to the addresses in user land
  832. * addrssize Size of the addrs buffer
  833. * op Operation to perform (add or remove, see the flags of
  834. * sctp_bindx)
  835. *
  836. * Returns 0 if ok, <0 errno code on error.
  837. */
  838. static int sctp_setsockopt_bindx(struct sock* sk,
  839. struct sockaddr __user *addrs,
  840. int addrs_size, int op)
  841. {
  842. struct sockaddr *kaddrs;
  843. int err;
  844. int addrcnt = 0;
  845. int walk_size = 0;
  846. struct sockaddr *sa_addr;
  847. void *addr_buf;
  848. struct sctp_af *af;
  849. pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
  850. __func__, sk, addrs, addrs_size, op);
  851. if (unlikely(addrs_size <= 0))
  852. return -EINVAL;
  853. /* Check the user passed a healthy pointer. */
  854. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  855. return -EFAULT;
  856. /* Alloc space for the address array in kernel memory. */
  857. kaddrs = kmalloc(addrs_size, GFP_KERNEL);
  858. if (unlikely(!kaddrs))
  859. return -ENOMEM;
  860. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  861. kfree(kaddrs);
  862. return -EFAULT;
  863. }
  864. /* Walk through the addrs buffer and count the number of addresses. */
  865. addr_buf = kaddrs;
  866. while (walk_size < addrs_size) {
  867. if (walk_size + sizeof(sa_family_t) > addrs_size) {
  868. kfree(kaddrs);
  869. return -EINVAL;
  870. }
  871. sa_addr = addr_buf;
  872. af = sctp_get_af_specific(sa_addr->sa_family);
  873. /* If the address family is not supported or if this address
  874. * causes the address buffer to overflow return EINVAL.
  875. */
  876. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  877. kfree(kaddrs);
  878. return -EINVAL;
  879. }
  880. addrcnt++;
  881. addr_buf += af->sockaddr_len;
  882. walk_size += af->sockaddr_len;
  883. }
  884. /* Do the work. */
  885. switch (op) {
  886. case SCTP_BINDX_ADD_ADDR:
  887. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  888. if (err)
  889. goto out;
  890. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  891. break;
  892. case SCTP_BINDX_REM_ADDR:
  893. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  894. if (err)
  895. goto out;
  896. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  897. break;
  898. default:
  899. err = -EINVAL;
  900. break;
  901. }
  902. out:
  903. kfree(kaddrs);
  904. return err;
  905. }
  906. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  907. *
  908. * Common routine for handling connect() and sctp_connectx().
  909. * Connect will come in with just a single address.
  910. */
  911. static int __sctp_connect(struct sock* sk,
  912. struct sockaddr *kaddrs,
  913. int addrs_size,
  914. sctp_assoc_t *assoc_id)
  915. {
  916. struct net *net = sock_net(sk);
  917. struct sctp_sock *sp;
  918. struct sctp_endpoint *ep;
  919. struct sctp_association *asoc = NULL;
  920. struct sctp_association *asoc2;
  921. struct sctp_transport *transport;
  922. union sctp_addr to;
  923. struct sctp_af *af;
  924. sctp_scope_t scope;
  925. long timeo;
  926. int err = 0;
  927. int addrcnt = 0;
  928. int walk_size = 0;
  929. union sctp_addr *sa_addr = NULL;
  930. void *addr_buf;
  931. unsigned short port;
  932. unsigned int f_flags = 0;
  933. sp = sctp_sk(sk);
  934. ep = sp->ep;
  935. /* connect() cannot be done on a socket that is already in ESTABLISHED
  936. * state - UDP-style peeled off socket or a TCP-style socket that
  937. * is already connected.
  938. * It cannot be done even on a TCP-style listening socket.
  939. */
  940. if (sctp_sstate(sk, ESTABLISHED) ||
  941. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
  942. err = -EISCONN;
  943. goto out_free;
  944. }
  945. /* Walk through the addrs buffer and count the number of addresses. */
  946. addr_buf = kaddrs;
  947. while (walk_size < addrs_size) {
  948. if (walk_size + sizeof(sa_family_t) > addrs_size) {
  949. err = -EINVAL;
  950. goto out_free;
  951. }
  952. sa_addr = addr_buf;
  953. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  954. /* If the address family is not supported or if this address
  955. * causes the address buffer to overflow return EINVAL.
  956. */
  957. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  958. err = -EINVAL;
  959. goto out_free;
  960. }
  961. port = ntohs(sa_addr->v4.sin_port);
  962. /* Save current address so we can work with it */
  963. memcpy(&to, sa_addr, af->sockaddr_len);
  964. err = sctp_verify_addr(sk, &to, af->sockaddr_len);
  965. if (err)
  966. goto out_free;
  967. /* Make sure the destination port is correctly set
  968. * in all addresses.
  969. */
  970. if (asoc && asoc->peer.port && asoc->peer.port != port) {
  971. err = -EINVAL;
  972. goto out_free;
  973. }
  974. /* Check if there already is a matching association on the
  975. * endpoint (other than the one created here).
  976. */
  977. asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  978. if (asoc2 && asoc2 != asoc) {
  979. if (asoc2->state >= SCTP_STATE_ESTABLISHED)
  980. err = -EISCONN;
  981. else
  982. err = -EALREADY;
  983. goto out_free;
  984. }
  985. /* If we could not find a matching association on the endpoint,
  986. * make sure that there is no peeled-off association matching
  987. * the peer address even on another socket.
  988. */
  989. if (sctp_endpoint_is_peeled_off(ep, &to)) {
  990. err = -EADDRNOTAVAIL;
  991. goto out_free;
  992. }
  993. if (!asoc) {
  994. /* If a bind() or sctp_bindx() is not called prior to
  995. * an sctp_connectx() call, the system picks an
  996. * ephemeral port and will choose an address set
  997. * equivalent to binding with a wildcard address.
  998. */
  999. if (!ep->base.bind_addr.port) {
  1000. if (sctp_autobind(sk)) {
  1001. err = -EAGAIN;
  1002. goto out_free;
  1003. }
  1004. } else {
  1005. /*
  1006. * If an unprivileged user inherits a 1-many
  1007. * style socket with open associations on a
  1008. * privileged port, it MAY be permitted to
  1009. * accept new associations, but it SHOULD NOT
  1010. * be permitted to open new associations.
  1011. */
  1012. if (ep->base.bind_addr.port < PROT_SOCK &&
  1013. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
  1014. err = -EACCES;
  1015. goto out_free;
  1016. }
  1017. }
  1018. scope = sctp_scope(&to);
  1019. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1020. if (!asoc) {
  1021. err = -ENOMEM;
  1022. goto out_free;
  1023. }
  1024. err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
  1025. GFP_KERNEL);
  1026. if (err < 0) {
  1027. goto out_free;
  1028. }
  1029. }
  1030. /* Prime the peer's transport structures. */
  1031. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
  1032. SCTP_UNKNOWN);
  1033. if (!transport) {
  1034. err = -ENOMEM;
  1035. goto out_free;
  1036. }
  1037. addrcnt++;
  1038. addr_buf += af->sockaddr_len;
  1039. walk_size += af->sockaddr_len;
  1040. }
  1041. /* In case the user of sctp_connectx() wants an association
  1042. * id back, assign one now.
  1043. */
  1044. if (assoc_id) {
  1045. err = sctp_assoc_set_id(asoc, GFP_KERNEL);
  1046. if (err < 0)
  1047. goto out_free;
  1048. }
  1049. err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
  1050. if (err < 0) {
  1051. goto out_free;
  1052. }
  1053. /* Initialize sk's dport and daddr for getpeername() */
  1054. inet_sk(sk)->inet_dport = htons(asoc->peer.port);
  1055. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  1056. af->to_sk_daddr(sa_addr, sk);
  1057. sk->sk_err = 0;
  1058. /* in-kernel sockets don't generally have a file allocated to them
  1059. * if all they do is call sock_create_kern().
  1060. */
  1061. if (sk->sk_socket->file)
  1062. f_flags = sk->sk_socket->file->f_flags;
  1063. timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
  1064. err = sctp_wait_for_connect(asoc, &timeo);
  1065. if ((err == 0 || err == -EINPROGRESS) && assoc_id)
  1066. *assoc_id = asoc->assoc_id;
  1067. /* Don't free association on exit. */
  1068. asoc = NULL;
  1069. out_free:
  1070. pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
  1071. __func__, asoc, kaddrs, err);
  1072. if (asoc) {
  1073. /* sctp_primitive_ASSOCIATE may have added this association
  1074. * To the hash table, try to unhash it, just in case, its a noop
  1075. * if it wasn't hashed so we're safe
  1076. */
  1077. sctp_unhash_established(asoc);
  1078. sctp_association_free(asoc);
  1079. }
  1080. return err;
  1081. }
  1082. /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
  1083. *
  1084. * API 8.9
  1085. * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
  1086. * sctp_assoc_t *asoc);
  1087. *
  1088. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  1089. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  1090. * or IPv6 addresses.
  1091. *
  1092. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  1093. * Section 3.1.2 for this usage.
  1094. *
  1095. * addrs is a pointer to an array of one or more socket addresses. Each
  1096. * address is contained in its appropriate structure (i.e. struct
  1097. * sockaddr_in or struct sockaddr_in6) the family of the address type
  1098. * must be used to distengish the address length (note that this
  1099. * representation is termed a "packed array" of addresses). The caller
  1100. * specifies the number of addresses in the array with addrcnt.
  1101. *
  1102. * On success, sctp_connectx() returns 0. It also sets the assoc_id to
  1103. * the association id of the new association. On failure, sctp_connectx()
  1104. * returns -1, and sets errno to the appropriate error code. The assoc_id
  1105. * is not touched by the kernel.
  1106. *
  1107. * For SCTP, the port given in each socket address must be the same, or
  1108. * sctp_connectx() will fail, setting errno to EINVAL.
  1109. *
  1110. * An application can use sctp_connectx to initiate an association with
  1111. * an endpoint that is multi-homed. Much like sctp_bindx() this call
  1112. * allows a caller to specify multiple addresses at which a peer can be
  1113. * reached. The way the SCTP stack uses the list of addresses to set up
  1114. * the association is implementation dependent. This function only
  1115. * specifies that the stack will try to make use of all the addresses in
  1116. * the list when needed.
  1117. *
  1118. * Note that the list of addresses passed in is only used for setting up
  1119. * the association. It does not necessarily equal the set of addresses
  1120. * the peer uses for the resulting association. If the caller wants to
  1121. * find out the set of peer addresses, it must use sctp_getpaddrs() to
  1122. * retrieve them after the association has been set up.
  1123. *
  1124. * Basically do nothing but copying the addresses from user to kernel
  1125. * land and invoking either sctp_connectx(). This is used for tunneling
  1126. * the sctp_connectx() request through sctp_setsockopt() from userspace.
  1127. *
  1128. * We don't use copy_from_user() for optimization: we first do the
  1129. * sanity checks (buffer size -fast- and access check-healthy
  1130. * pointer); if all of those succeed, then we can alloc the memory
  1131. * (expensive operation) needed to copy the data to kernel. Then we do
  1132. * the copying without checking the user space area
  1133. * (__copy_from_user()).
  1134. *
  1135. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  1136. * it.
  1137. *
  1138. * sk The sk of the socket
  1139. * addrs The pointer to the addresses in user land
  1140. * addrssize Size of the addrs buffer
  1141. *
  1142. * Returns >=0 if ok, <0 errno code on error.
  1143. */
  1144. static int __sctp_setsockopt_connectx(struct sock* sk,
  1145. struct sockaddr __user *addrs,
  1146. int addrs_size,
  1147. sctp_assoc_t *assoc_id)
  1148. {
  1149. int err = 0;
  1150. struct sockaddr *kaddrs;
  1151. pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
  1152. __func__, sk, addrs, addrs_size);
  1153. if (unlikely(addrs_size <= 0))
  1154. return -EINVAL;
  1155. /* Check the user passed a healthy pointer. */
  1156. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  1157. return -EFAULT;
  1158. /* Alloc space for the address array in kernel memory. */
  1159. kaddrs = kmalloc(addrs_size, GFP_KERNEL);
  1160. if (unlikely(!kaddrs))
  1161. return -ENOMEM;
  1162. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  1163. err = -EFAULT;
  1164. } else {
  1165. err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
  1166. }
  1167. kfree(kaddrs);
  1168. return err;
  1169. }
  1170. /*
  1171. * This is an older interface. It's kept for backward compatibility
  1172. * to the option that doesn't provide association id.
  1173. */
  1174. static int sctp_setsockopt_connectx_old(struct sock* sk,
  1175. struct sockaddr __user *addrs,
  1176. int addrs_size)
  1177. {
  1178. return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
  1179. }
  1180. /*
  1181. * New interface for the API. The since the API is done with a socket
  1182. * option, to make it simple we feed back the association id is as a return
  1183. * indication to the call. Error is always negative and association id is
  1184. * always positive.
  1185. */
  1186. static int sctp_setsockopt_connectx(struct sock* sk,
  1187. struct sockaddr __user *addrs,
  1188. int addrs_size)
  1189. {
  1190. sctp_assoc_t assoc_id = 0;
  1191. int err = 0;
  1192. err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
  1193. if (err)
  1194. return err;
  1195. else
  1196. return assoc_id;
  1197. }
  1198. /*
  1199. * New (hopefully final) interface for the API.
  1200. * We use the sctp_getaddrs_old structure so that use-space library
  1201. * can avoid any unnecessary allocations. The only defferent part
  1202. * is that we store the actual length of the address buffer into the
  1203. * addrs_num structure member. That way we can re-use the existing
  1204. * code.
  1205. */
  1206. static int sctp_getsockopt_connectx3(struct sock* sk, int len,
  1207. char __user *optval,
  1208. int __user *optlen)
  1209. {
  1210. struct sctp_getaddrs_old param;
  1211. sctp_assoc_t assoc_id = 0;
  1212. int err = 0;
  1213. if (len < sizeof(param))
  1214. return -EINVAL;
  1215. if (copy_from_user(&param, optval, sizeof(param)))
  1216. return -EFAULT;
  1217. err = __sctp_setsockopt_connectx(sk,
  1218. (struct sockaddr __user *)param.addrs,
  1219. param.addr_num, &assoc_id);
  1220. if (err == 0 || err == -EINPROGRESS) {
  1221. if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
  1222. return -EFAULT;
  1223. if (put_user(sizeof(assoc_id), optlen))
  1224. return -EFAULT;
  1225. }
  1226. return err;
  1227. }
  1228. /* API 3.1.4 close() - UDP Style Syntax
  1229. * Applications use close() to perform graceful shutdown (as described in
  1230. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  1231. * by a UDP-style socket.
  1232. *
  1233. * The syntax is
  1234. *
  1235. * ret = close(int sd);
  1236. *
  1237. * sd - the socket descriptor of the associations to be closed.
  1238. *
  1239. * To gracefully shutdown a specific association represented by the
  1240. * UDP-style socket, an application should use the sendmsg() call,
  1241. * passing no user data, but including the appropriate flag in the
  1242. * ancillary data (see Section xxxx).
  1243. *
  1244. * If sd in the close() call is a branched-off socket representing only
  1245. * one association, the shutdown is performed on that association only.
  1246. *
  1247. * 4.1.6 close() - TCP Style Syntax
  1248. *
  1249. * Applications use close() to gracefully close down an association.
  1250. *
  1251. * The syntax is:
  1252. *
  1253. * int close(int sd);
  1254. *
  1255. * sd - the socket descriptor of the association to be closed.
  1256. *
  1257. * After an application calls close() on a socket descriptor, no further
  1258. * socket operations will succeed on that descriptor.
  1259. *
  1260. * API 7.1.4 SO_LINGER
  1261. *
  1262. * An application using the TCP-style socket can use this option to
  1263. * perform the SCTP ABORT primitive. The linger option structure is:
  1264. *
  1265. * struct linger {
  1266. * int l_onoff; // option on/off
  1267. * int l_linger; // linger time
  1268. * };
  1269. *
  1270. * To enable the option, set l_onoff to 1. If the l_linger value is set
  1271. * to 0, calling close() is the same as the ABORT primitive. If the
  1272. * value is set to a negative value, the setsockopt() call will return
  1273. * an error. If the value is set to a positive value linger_time, the
  1274. * close() can be blocked for at most linger_time ms. If the graceful
  1275. * shutdown phase does not finish during this period, close() will
  1276. * return but the graceful shutdown phase continues in the system.
  1277. */
  1278. static void sctp_close(struct sock *sk, long timeout)
  1279. {
  1280. struct net *net = sock_net(sk);
  1281. struct sctp_endpoint *ep;
  1282. struct sctp_association *asoc;
  1283. struct list_head *pos, *temp;
  1284. unsigned int data_was_unread;
  1285. pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
  1286. sctp_lock_sock(sk);
  1287. sk->sk_shutdown = SHUTDOWN_MASK;
  1288. sk->sk_state = SCTP_SS_CLOSING;
  1289. ep = sctp_sk(sk)->ep;
  1290. /* Clean up any skbs sitting on the receive queue. */
  1291. data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  1292. data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  1293. /* Walk all associations on an endpoint. */
  1294. list_for_each_safe(pos, temp, &ep->asocs) {
  1295. asoc = list_entry(pos, struct sctp_association, asocs);
  1296. if (sctp_style(sk, TCP)) {
  1297. /* A closed association can still be in the list if
  1298. * it belongs to a TCP-style listening socket that is
  1299. * not yet accepted. If so, free it. If not, send an
  1300. * ABORT or SHUTDOWN based on the linger options.
  1301. */
  1302. if (sctp_state(asoc, CLOSED)) {
  1303. sctp_unhash_established(asoc);
  1304. sctp_association_free(asoc);
  1305. continue;
  1306. }
  1307. }
  1308. if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
  1309. !skb_queue_empty(&asoc->ulpq.reasm) ||
  1310. (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
  1311. struct sctp_chunk *chunk;
  1312. chunk = sctp_make_abort_user(asoc, NULL, 0);
  1313. if (chunk)
  1314. sctp_primitive_ABORT(net, asoc, chunk);
  1315. } else
  1316. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1317. }
  1318. /* On a TCP-style socket, block for at most linger_time if set. */
  1319. if (sctp_style(sk, TCP) && timeout)
  1320. sctp_wait_for_close(sk, timeout);
  1321. /* This will run the backlog queue. */
  1322. sctp_release_sock(sk);
  1323. /* Supposedly, no process has access to the socket, but
  1324. * the net layers still may.
  1325. */
  1326. sctp_local_bh_disable();
  1327. sctp_bh_lock_sock(sk);
  1328. /* Hold the sock, since sk_common_release() will put sock_put()
  1329. * and we have just a little more cleanup.
  1330. */
  1331. sock_hold(sk);
  1332. sk_common_release(sk);
  1333. sctp_bh_unlock_sock(sk);
  1334. sctp_local_bh_enable();
  1335. sock_put(sk);
  1336. SCTP_DBG_OBJCNT_DEC(sock);
  1337. }
  1338. /* Handle EPIPE error. */
  1339. static int sctp_error(struct sock *sk, int flags, int err)
  1340. {
  1341. if (err == -EPIPE)
  1342. err = sock_error(sk) ? : -EPIPE;
  1343. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  1344. send_sig(SIGPIPE, current, 0);
  1345. return err;
  1346. }
  1347. /* API 3.1.3 sendmsg() - UDP Style Syntax
  1348. *
  1349. * An application uses sendmsg() and recvmsg() calls to transmit data to
  1350. * and receive data from its peer.
  1351. *
  1352. * ssize_t sendmsg(int socket, const struct msghdr *message,
  1353. * int flags);
  1354. *
  1355. * socket - the socket descriptor of the endpoint.
  1356. * message - pointer to the msghdr structure which contains a single
  1357. * user message and possibly some ancillary data.
  1358. *
  1359. * See Section 5 for complete description of the data
  1360. * structures.
  1361. *
  1362. * flags - flags sent or received with the user message, see Section
  1363. * 5 for complete description of the flags.
  1364. *
  1365. * Note: This function could use a rewrite especially when explicit
  1366. * connect support comes in.
  1367. */
  1368. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  1369. static int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
  1370. static int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
  1371. struct msghdr *msg, size_t msg_len)
  1372. {
  1373. struct net *net = sock_net(sk);
  1374. struct sctp_sock *sp;
  1375. struct sctp_endpoint *ep;
  1376. struct sctp_association *new_asoc=NULL, *asoc=NULL;
  1377. struct sctp_transport *transport, *chunk_tp;
  1378. struct sctp_chunk *chunk;
  1379. union sctp_addr to;
  1380. struct sockaddr *msg_name = NULL;
  1381. struct sctp_sndrcvinfo default_sinfo;
  1382. struct sctp_sndrcvinfo *sinfo;
  1383. struct sctp_initmsg *sinit;
  1384. sctp_assoc_t associd = 0;
  1385. sctp_cmsgs_t cmsgs = { NULL };
  1386. int err;
  1387. sctp_scope_t scope;
  1388. long timeo;
  1389. __u16 sinfo_flags = 0;
  1390. struct sctp_datamsg *datamsg;
  1391. int msg_flags = msg->msg_flags;
  1392. err = 0;
  1393. sp = sctp_sk(sk);
  1394. ep = sp->ep;
  1395. pr_debug("%s: sk:%p, msg:%p, msg_len:%zu ep:%p\n", __func__, sk,
  1396. msg, msg_len, ep);
  1397. /* We cannot send a message over a TCP-style listening socket. */
  1398. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
  1399. err = -EPIPE;
  1400. goto out_nounlock;
  1401. }
  1402. /* Parse out the SCTP CMSGs. */
  1403. err = sctp_msghdr_parse(msg, &cmsgs);
  1404. if (err) {
  1405. pr_debug("%s: msghdr parse err:%x\n", __func__, err);
  1406. goto out_nounlock;
  1407. }
  1408. /* Fetch the destination address for this packet. This
  1409. * address only selects the association--it is not necessarily
  1410. * the address we will send to.
  1411. * For a peeled-off socket, msg_name is ignored.
  1412. */
  1413. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  1414. int msg_namelen = msg->msg_namelen;
  1415. err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
  1416. msg_namelen);
  1417. if (err)
  1418. return err;
  1419. if (msg_namelen > sizeof(to))
  1420. msg_namelen = sizeof(to);
  1421. memcpy(&to, msg->msg_name, msg_namelen);
  1422. msg_name = msg->msg_name;
  1423. }
  1424. sinfo = cmsgs.info;
  1425. sinit = cmsgs.init;
  1426. /* Did the user specify SNDRCVINFO? */
  1427. if (sinfo) {
  1428. sinfo_flags = sinfo->sinfo_flags;
  1429. associd = sinfo->sinfo_assoc_id;
  1430. }
  1431. pr_debug("%s: msg_len:%zu, sinfo_flags:0x%x\n", __func__,
  1432. msg_len, sinfo_flags);
  1433. /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
  1434. if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
  1435. err = -EINVAL;
  1436. goto out_nounlock;
  1437. }
  1438. /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
  1439. * length messages when SCTP_EOF|SCTP_ABORT is not set.
  1440. * If SCTP_ABORT is set, the message length could be non zero with
  1441. * the msg_iov set to the user abort reason.
  1442. */
  1443. if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
  1444. (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
  1445. err = -EINVAL;
  1446. goto out_nounlock;
  1447. }
  1448. /* If SCTP_ADDR_OVER is set, there must be an address
  1449. * specified in msg_name.
  1450. */
  1451. if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
  1452. err = -EINVAL;
  1453. goto out_nounlock;
  1454. }
  1455. transport = NULL;
  1456. pr_debug("%s: about to look up association\n", __func__);
  1457. sctp_lock_sock(sk);
  1458. /* If a msg_name has been specified, assume this is to be used. */
  1459. if (msg_name) {
  1460. /* Look for a matching association on the endpoint. */
  1461. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  1462. if (!asoc) {
  1463. /* If we could not find a matching association on the
  1464. * endpoint, make sure that it is not a TCP-style
  1465. * socket that already has an association or there is
  1466. * no peeled-off association on another socket.
  1467. */
  1468. if ((sctp_style(sk, TCP) &&
  1469. sctp_sstate(sk, ESTABLISHED)) ||
  1470. sctp_endpoint_is_peeled_off(ep, &to)) {
  1471. err = -EADDRNOTAVAIL;
  1472. goto out_unlock;
  1473. }
  1474. }
  1475. } else {
  1476. asoc = sctp_id2assoc(sk, associd);
  1477. if (!asoc) {
  1478. err = -EPIPE;
  1479. goto out_unlock;
  1480. }
  1481. }
  1482. if (asoc) {
  1483. pr_debug("%s: just looked up association:%p\n", __func__, asoc);
  1484. /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
  1485. * socket that has an association in CLOSED state. This can
  1486. * happen when an accepted socket has an association that is
  1487. * already CLOSED.
  1488. */
  1489. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
  1490. err = -EPIPE;
  1491. goto out_unlock;
  1492. }
  1493. if (sinfo_flags & SCTP_EOF) {
  1494. pr_debug("%s: shutting down association:%p\n",
  1495. __func__, asoc);
  1496. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1497. err = 0;
  1498. goto out_unlock;
  1499. }
  1500. if (sinfo_flags & SCTP_ABORT) {
  1501. chunk = sctp_make_abort_user(asoc, msg, msg_len);
  1502. if (!chunk) {
  1503. err = -ENOMEM;
  1504. goto out_unlock;
  1505. }
  1506. pr_debug("%s: aborting association:%p\n",
  1507. __func__, asoc);
  1508. sctp_primitive_ABORT(net, asoc, chunk);
  1509. err = 0;
  1510. goto out_unlock;
  1511. }
  1512. }
  1513. /* Do we need to create the association? */
  1514. if (!asoc) {
  1515. pr_debug("%s: there is no association yet\n", __func__);
  1516. if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
  1517. err = -EINVAL;
  1518. goto out_unlock;
  1519. }
  1520. /* Check for invalid stream against the stream counts,
  1521. * either the default or the user specified stream counts.
  1522. */
  1523. if (sinfo) {
  1524. if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
  1525. /* Check against the defaults. */
  1526. if (sinfo->sinfo_stream >=
  1527. sp->initmsg.sinit_num_ostreams) {
  1528. err = -EINVAL;
  1529. goto out_unlock;
  1530. }
  1531. } else {
  1532. /* Check against the requested. */
  1533. if (sinfo->sinfo_stream >=
  1534. sinit->sinit_num_ostreams) {
  1535. err = -EINVAL;
  1536. goto out_unlock;
  1537. }
  1538. }
  1539. }
  1540. /*
  1541. * API 3.1.2 bind() - UDP Style Syntax
  1542. * If a bind() or sctp_bindx() is not called prior to a
  1543. * sendmsg() call that initiates a new association, the
  1544. * system picks an ephemeral port and will choose an address
  1545. * set equivalent to binding with a wildcard address.
  1546. */
  1547. if (!ep->base.bind_addr.port) {
  1548. if (sctp_autobind(sk)) {
  1549. err = -EAGAIN;
  1550. goto out_unlock;
  1551. }
  1552. } else {
  1553. /*
  1554. * If an unprivileged user inherits a one-to-many
  1555. * style socket with open associations on a privileged
  1556. * port, it MAY be permitted to accept new associations,
  1557. * but it SHOULD NOT be permitted to open new
  1558. * associations.
  1559. */
  1560. if (ep->base.bind_addr.port < PROT_SOCK &&
  1561. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
  1562. err = -EACCES;
  1563. goto out_unlock;
  1564. }
  1565. }
  1566. scope = sctp_scope(&to);
  1567. new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1568. if (!new_asoc) {
  1569. err = -ENOMEM;
  1570. goto out_unlock;
  1571. }
  1572. asoc = new_asoc;
  1573. err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
  1574. if (err < 0) {
  1575. err = -ENOMEM;
  1576. goto out_free;
  1577. }
  1578. /* If the SCTP_INIT ancillary data is specified, set all
  1579. * the association init values accordingly.
  1580. */
  1581. if (sinit) {
  1582. if (sinit->sinit_num_ostreams) {
  1583. asoc->c.sinit_num_ostreams =
  1584. sinit->sinit_num_ostreams;
  1585. }
  1586. if (sinit->sinit_max_instreams) {
  1587. asoc->c.sinit_max_instreams =
  1588. sinit->sinit_max_instreams;
  1589. }
  1590. if (sinit->sinit_max_attempts) {
  1591. asoc->max_init_attempts
  1592. = sinit->sinit_max_attempts;
  1593. }
  1594. if (sinit->sinit_max_init_timeo) {
  1595. asoc->max_init_timeo =
  1596. msecs_to_jiffies(sinit->sinit_max_init_timeo);
  1597. }
  1598. }
  1599. /* Prime the peer's transport structures. */
  1600. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
  1601. if (!transport) {
  1602. err = -ENOMEM;
  1603. goto out_free;
  1604. }
  1605. }
  1606. /* ASSERT: we have a valid association at this point. */
  1607. pr_debug("%s: we have a valid association\n", __func__);
  1608. if (!sinfo) {
  1609. /* If the user didn't specify SNDRCVINFO, make up one with
  1610. * some defaults.
  1611. */
  1612. memset(&default_sinfo, 0, sizeof(default_sinfo));
  1613. default_sinfo.sinfo_stream = asoc->default_stream;
  1614. default_sinfo.sinfo_flags = asoc->default_flags;
  1615. default_sinfo.sinfo_ppid = asoc->default_ppid;
  1616. default_sinfo.sinfo_context = asoc->default_context;
  1617. default_sinfo.sinfo_timetolive = asoc->default_timetolive;
  1618. default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
  1619. sinfo = &default_sinfo;
  1620. }
  1621. /* API 7.1.7, the sndbuf size per association bounds the
  1622. * maximum size of data that can be sent in a single send call.
  1623. */
  1624. if (msg_len > sk->sk_sndbuf) {
  1625. err = -EMSGSIZE;
  1626. goto out_free;
  1627. }
  1628. if (asoc->pmtu_pending)
  1629. sctp_assoc_pending_pmtu(sk, asoc);
  1630. /* If fragmentation is disabled and the message length exceeds the
  1631. * association fragmentation point, return EMSGSIZE. The I-D
  1632. * does not specify what this error is, but this looks like
  1633. * a great fit.
  1634. */
  1635. if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
  1636. err = -EMSGSIZE;
  1637. goto out_free;
  1638. }
  1639. /* Check for invalid stream. */
  1640. if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
  1641. err = -EINVAL;
  1642. goto out_free;
  1643. }
  1644. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1645. if (!sctp_wspace(asoc)) {
  1646. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
  1647. if (err)
  1648. goto out_free;
  1649. }
  1650. /* If an address is passed with the sendto/sendmsg call, it is used
  1651. * to override the primary destination address in the TCP model, or
  1652. * when SCTP_ADDR_OVER flag is set in the UDP model.
  1653. */
  1654. if ((sctp_style(sk, TCP) && msg_name) ||
  1655. (sinfo_flags & SCTP_ADDR_OVER)) {
  1656. chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
  1657. if (!chunk_tp) {
  1658. err = -EINVAL;
  1659. goto out_free;
  1660. }
  1661. } else
  1662. chunk_tp = NULL;
  1663. /* Auto-connect, if we aren't connected already. */
  1664. if (sctp_state(asoc, CLOSED)) {
  1665. err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
  1666. if (err < 0)
  1667. goto out_free;
  1668. pr_debug("%s: we associated primitively\n", __func__);
  1669. }
  1670. /* Break the message into multiple chunks of maximum size. */
  1671. datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
  1672. if (IS_ERR(datamsg)) {
  1673. err = PTR_ERR(datamsg);
  1674. goto out_free;
  1675. }
  1676. /* Now send the (possibly) fragmented message. */
  1677. list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
  1678. sctp_chunk_hold(chunk);
  1679. /* Do accounting for the write space. */
  1680. sctp_set_owner_w(chunk);
  1681. chunk->transport = chunk_tp;
  1682. }
  1683. /* Send it to the lower layers. Note: all chunks
  1684. * must either fail or succeed. The lower layer
  1685. * works that way today. Keep it that way or this
  1686. * breaks.
  1687. */
  1688. err = sctp_primitive_SEND(net, asoc, datamsg);
  1689. /* Did the lower layer accept the chunk? */
  1690. if (err) {
  1691. sctp_datamsg_free(datamsg);
  1692. goto out_free;
  1693. }
  1694. pr_debug("%s: we sent primitively\n", __func__);
  1695. sctp_datamsg_put(datamsg);
  1696. err = msg_len;
  1697. /* If we are already past ASSOCIATE, the lower
  1698. * layers are responsible for association cleanup.
  1699. */
  1700. goto out_unlock;
  1701. out_free:
  1702. if (new_asoc) {
  1703. sctp_unhash_established(asoc);
  1704. sctp_association_free(asoc);
  1705. }
  1706. out_unlock:
  1707. sctp_release_sock(sk);
  1708. out_nounlock:
  1709. return sctp_error(sk, msg_flags, err);
  1710. #if 0
  1711. do_sock_err:
  1712. if (msg_len)
  1713. err = msg_len;
  1714. else
  1715. err = sock_error(sk);
  1716. goto out;
  1717. do_interrupted:
  1718. if (msg_len)
  1719. err = msg_len;
  1720. goto out;
  1721. #endif /* 0 */
  1722. }
  1723. /* This is an extended version of skb_pull() that removes the data from the
  1724. * start of a skb even when data is spread across the list of skb's in the
  1725. * frag_list. len specifies the total amount of data that needs to be removed.
  1726. * when 'len' bytes could be removed from the skb, it returns 0.
  1727. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1728. * could not be removed.
  1729. */
  1730. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1731. {
  1732. struct sk_buff *list;
  1733. int skb_len = skb_headlen(skb);
  1734. int rlen;
  1735. if (len <= skb_len) {
  1736. __skb_pull(skb, len);
  1737. return 0;
  1738. }
  1739. len -= skb_len;
  1740. __skb_pull(skb, skb_len);
  1741. skb_walk_frags(skb, list) {
  1742. rlen = sctp_skb_pull(list, len);
  1743. skb->len -= (len-rlen);
  1744. skb->data_len -= (len-rlen);
  1745. if (!rlen)
  1746. return 0;
  1747. len = rlen;
  1748. }
  1749. return len;
  1750. }
  1751. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1752. *
  1753. * ssize_t recvmsg(int socket, struct msghdr *message,
  1754. * int flags);
  1755. *
  1756. * socket - the socket descriptor of the endpoint.
  1757. * message - pointer to the msghdr structure which contains a single
  1758. * user message and possibly some ancillary data.
  1759. *
  1760. * See Section 5 for complete description of the data
  1761. * structures.
  1762. *
  1763. * flags - flags sent or received with the user message, see Section
  1764. * 5 for complete description of the flags.
  1765. */
  1766. static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
  1767. static int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
  1768. struct msghdr *msg, size_t len, int noblock,
  1769. int flags, int *addr_len)
  1770. {
  1771. struct sctp_ulpevent *event = NULL;
  1772. struct sctp_sock *sp = sctp_sk(sk);
  1773. struct sk_buff *skb;
  1774. int copied;
  1775. int err = 0;
  1776. int skb_len;
  1777. pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
  1778. "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
  1779. addr_len);
  1780. sctp_lock_sock(sk);
  1781. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
  1782. err = -ENOTCONN;
  1783. goto out;
  1784. }
  1785. skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
  1786. if (!skb)
  1787. goto out;
  1788. /* Get the total length of the skb including any skb's in the
  1789. * frag_list.
  1790. */
  1791. skb_len = skb->len;
  1792. copied = skb_len;
  1793. if (copied > len)
  1794. copied = len;
  1795. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1796. event = sctp_skb2event(skb);
  1797. if (err)
  1798. goto out_free;
  1799. sock_recv_ts_and_drops(msg, sk, skb);
  1800. if (sctp_ulpevent_is_notification(event)) {
  1801. msg->msg_flags |= MSG_NOTIFICATION;
  1802. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1803. } else {
  1804. sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
  1805. }
  1806. /* Check if we allow SCTP_SNDRCVINFO. */
  1807. if (sp->subscribe.sctp_data_io_event)
  1808. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1809. #if 0
  1810. /* FIXME: we should be calling IP/IPv6 layers. */
  1811. if (sk->sk_protinfo.af_inet.cmsg_flags)
  1812. ip_cmsg_recv(msg, skb);
  1813. #endif
  1814. err = copied;
  1815. /* If skb's length exceeds the user's buffer, update the skb and
  1816. * push it back to the receive_queue so that the next call to
  1817. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1818. */
  1819. if (skb_len > copied) {
  1820. msg->msg_flags &= ~MSG_EOR;
  1821. if (flags & MSG_PEEK)
  1822. goto out_free;
  1823. sctp_skb_pull(skb, copied);
  1824. skb_queue_head(&sk->sk_receive_queue, skb);
  1825. /* When only partial message is copied to the user, increase
  1826. * rwnd by that amount. If all the data in the skb is read,
  1827. * rwnd is updated when the event is freed.
  1828. */
  1829. if (!sctp_ulpevent_is_notification(event))
  1830. sctp_assoc_rwnd_increase(event->asoc, copied);
  1831. goto out;
  1832. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1833. (event->msg_flags & MSG_EOR))
  1834. msg->msg_flags |= MSG_EOR;
  1835. else
  1836. msg->msg_flags &= ~MSG_EOR;
  1837. out_free:
  1838. if (flags & MSG_PEEK) {
  1839. /* Release the skb reference acquired after peeking the skb in
  1840. * sctp_skb_recv_datagram().
  1841. */
  1842. kfree_skb(skb);
  1843. } else {
  1844. /* Free the event which includes releasing the reference to
  1845. * the owner of the skb, freeing the skb and updating the
  1846. * rwnd.
  1847. */
  1848. sctp_ulpevent_free(event);
  1849. }
  1850. out:
  1851. sctp_release_sock(sk);
  1852. return err;
  1853. }
  1854. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1855. *
  1856. * This option is a on/off flag. If enabled no SCTP message
  1857. * fragmentation will be performed. Instead if a message being sent
  1858. * exceeds the current PMTU size, the message will NOT be sent and
  1859. * instead a error will be indicated to the user.
  1860. */
  1861. static int sctp_setsockopt_disable_fragments(struct sock *sk,
  1862. char __user *optval,
  1863. unsigned int optlen)
  1864. {
  1865. int val;
  1866. if (optlen < sizeof(int))
  1867. return -EINVAL;
  1868. if (get_user(val, (int __user *)optval))
  1869. return -EFAULT;
  1870. sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
  1871. return 0;
  1872. }
  1873. static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
  1874. unsigned int optlen)
  1875. {
  1876. struct sctp_association *asoc;
  1877. struct sctp_ulpevent *event;
  1878. if (optlen > sizeof(struct sctp_event_subscribe))
  1879. return -EINVAL;
  1880. if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
  1881. return -EFAULT;
  1882. /*
  1883. * At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
  1884. * if there is no data to be sent or retransmit, the stack will
  1885. * immediately send up this notification.
  1886. */
  1887. if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
  1888. &sctp_sk(sk)->subscribe)) {
  1889. asoc = sctp_id2assoc(sk, 0);
  1890. if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
  1891. event = sctp_ulpevent_make_sender_dry_event(asoc,
  1892. GFP_ATOMIC);
  1893. if (!event)
  1894. return -ENOMEM;
  1895. sctp_ulpq_tail_event(&asoc->ulpq, event);
  1896. }
  1897. }
  1898. return 0;
  1899. }
  1900. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1901. *
  1902. * This socket option is applicable to the UDP-style socket only. When
  1903. * set it will cause associations that are idle for more than the
  1904. * specified number of seconds to automatically close. An association
  1905. * being idle is defined an association that has NOT sent or received
  1906. * user data. The special value of '0' indicates that no automatic
  1907. * close of any associations should be performed. The option expects an
  1908. * integer defining the number of seconds of idle time before an
  1909. * association is closed.
  1910. */
  1911. static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
  1912. unsigned int optlen)
  1913. {
  1914. struct sctp_sock *sp = sctp_sk(sk);
  1915. /* Applicable to UDP-style socket only */
  1916. if (sctp_style(sk, TCP))
  1917. return -EOPNOTSUPP;
  1918. if (optlen != sizeof(int))
  1919. return -EINVAL;
  1920. if (copy_from_user(&sp->autoclose, optval, optlen))
  1921. return -EFAULT;
  1922. return 0;
  1923. }
  1924. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1925. *
  1926. * Applications can enable or disable heartbeats for any peer address of
  1927. * an association, modify an address's heartbeat interval, force a
  1928. * heartbeat to be sent immediately, and adjust the address's maximum
  1929. * number of retransmissions sent before an address is considered
  1930. * unreachable. The following structure is used to access and modify an
  1931. * address's parameters:
  1932. *
  1933. * struct sctp_paddrparams {
  1934. * sctp_assoc_t spp_assoc_id;
  1935. * struct sockaddr_storage spp_address;
  1936. * uint32_t spp_hbinterval;
  1937. * uint16_t spp_pathmaxrxt;
  1938. * uint32_t spp_pathmtu;
  1939. * uint32_t spp_sackdelay;
  1940. * uint32_t spp_flags;
  1941. * };
  1942. *
  1943. * spp_assoc_id - (one-to-many style socket) This is filled in the
  1944. * application, and identifies the association for
  1945. * this query.
  1946. * spp_address - This specifies which address is of interest.
  1947. * spp_hbinterval - This contains the value of the heartbeat interval,
  1948. * in milliseconds. If a value of zero
  1949. * is present in this field then no changes are to
  1950. * be made to this parameter.
  1951. * spp_pathmaxrxt - This contains the maximum number of
  1952. * retransmissions before this address shall be
  1953. * considered unreachable. If a value of zero
  1954. * is present in this field then no changes are to
  1955. * be made to this parameter.
  1956. * spp_pathmtu - When Path MTU discovery is disabled the value
  1957. * specified here will be the "fixed" path mtu.
  1958. * Note that if the spp_address field is empty
  1959. * then all associations on this address will
  1960. * have this fixed path mtu set upon them.
  1961. *
  1962. * spp_sackdelay - When delayed sack is enabled, this value specifies
  1963. * the number of milliseconds that sacks will be delayed
  1964. * for. This value will apply to all addresses of an
  1965. * association if the spp_address field is empty. Note
  1966. * also, that if delayed sack is enabled and this
  1967. * value is set to 0, no change is made to the last
  1968. * recorded delayed sack timer value.
  1969. *
  1970. * spp_flags - These flags are used to control various features
  1971. * on an association. The flag field may contain
  1972. * zero or more of the following options.
  1973. *
  1974. * SPP_HB_ENABLE - Enable heartbeats on the
  1975. * specified address. Note that if the address
  1976. * field is empty all addresses for the association
  1977. * have heartbeats enabled upon them.
  1978. *
  1979. * SPP_HB_DISABLE - Disable heartbeats on the
  1980. * speicifed address. Note that if the address
  1981. * field is empty all addresses for the association
  1982. * will have their heartbeats disabled. Note also
  1983. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  1984. * mutually exclusive, only one of these two should
  1985. * be specified. Enabling both fields will have
  1986. * undetermined results.
  1987. *
  1988. * SPP_HB_DEMAND - Request a user initiated heartbeat
  1989. * to be made immediately.
  1990. *
  1991. * SPP_HB_TIME_IS_ZERO - Specify's that the time for
  1992. * heartbeat delayis to be set to the value of 0
  1993. * milliseconds.
  1994. *
  1995. * SPP_PMTUD_ENABLE - This field will enable PMTU
  1996. * discovery upon the specified address. Note that
  1997. * if the address feild is empty then all addresses
  1998. * on the association are effected.
  1999. *
  2000. * SPP_PMTUD_DISABLE - This field will disable PMTU
  2001. * discovery upon the specified address. Note that
  2002. * if the address feild is empty then all addresses
  2003. * on the association are effected. Not also that
  2004. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  2005. * exclusive. Enabling both will have undetermined
  2006. * results.
  2007. *
  2008. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  2009. * on delayed sack. The time specified in spp_sackdelay
  2010. * is used to specify the sack delay for this address. Note
  2011. * that if spp_address is empty then all addresses will
  2012. * enable delayed sack and take on the sack delay
  2013. * value specified in spp_sackdelay.
  2014. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  2015. * off delayed sack. If the spp_address field is blank then
  2016. * delayed sack is disabled for the entire association. Note
  2017. * also that this field is mutually exclusive to
  2018. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  2019. * results.
  2020. */
  2021. static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
  2022. struct sctp_transport *trans,
  2023. struct sctp_association *asoc,
  2024. struct sctp_sock *sp,
  2025. int hb_change,
  2026. int pmtud_change,
  2027. int sackdelay_change)
  2028. {
  2029. int error;
  2030. if (params->spp_flags & SPP_HB_DEMAND && trans) {
  2031. struct net *net = sock_net(trans->asoc->base.sk);
  2032. error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
  2033. if (error)
  2034. return error;
  2035. }
  2036. /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
  2037. * this field is ignored. Note also that a value of zero indicates
  2038. * the current setting should be left unchanged.
  2039. */
  2040. if (params->spp_flags & SPP_HB_ENABLE) {
  2041. /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
  2042. * set. This lets us use 0 value when this flag
  2043. * is set.
  2044. */
  2045. if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
  2046. params->spp_hbinterval = 0;
  2047. if (params->spp_hbinterval ||
  2048. (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
  2049. if (trans) {
  2050. trans->hbinterval =
  2051. msecs_to_jiffies(params->spp_hbinterval);
  2052. } else if (asoc) {
  2053. asoc->hbinterval =
  2054. msecs_to_jiffies(params->spp_hbinterval);
  2055. } else {
  2056. sp->hbinterval = params->spp_hbinterval;
  2057. }
  2058. }
  2059. }
  2060. if (hb_change) {
  2061. if (trans) {
  2062. trans->param_flags =
  2063. (trans->param_flags & ~SPP_HB) | hb_change;
  2064. } else if (asoc) {
  2065. asoc->param_flags =
  2066. (asoc->param_flags & ~SPP_HB) | hb_change;
  2067. } else {
  2068. sp->param_flags =
  2069. (sp->param_flags & ~SPP_HB) | hb_change;
  2070. }
  2071. }
  2072. /* When Path MTU discovery is disabled the value specified here will
  2073. * be the "fixed" path mtu (i.e. the value of the spp_flags field must
  2074. * include the flag SPP_PMTUD_DISABLE for this field to have any
  2075. * effect).
  2076. */
  2077. if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
  2078. if (trans) {
  2079. trans->pathmtu = params->spp_pathmtu;
  2080. sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
  2081. } else if (asoc) {
  2082. asoc->pathmtu = params->spp_pathmtu;
  2083. sctp_frag_point(asoc, params->spp_pathmtu);
  2084. } else {
  2085. sp->pathmtu = params->spp_pathmtu;
  2086. }
  2087. }
  2088. if (pmtud_change) {
  2089. if (trans) {
  2090. int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
  2091. (params->spp_flags & SPP_PMTUD_ENABLE);
  2092. trans->param_flags =
  2093. (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
  2094. if (update) {
  2095. sctp_transport_pmtu(trans, sctp_opt2sk(sp));
  2096. sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
  2097. }
  2098. } else if (asoc) {
  2099. asoc->param_flags =
  2100. (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
  2101. } else {
  2102. sp->param_flags =
  2103. (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
  2104. }
  2105. }
  2106. /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
  2107. * value of this field is ignored. Note also that a value of zero
  2108. * indicates the current setting should be left unchanged.
  2109. */
  2110. if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
  2111. if (trans) {
  2112. trans->sackdelay =
  2113. msecs_to_jiffies(params->spp_sackdelay);
  2114. } else if (asoc) {
  2115. asoc->sackdelay =
  2116. msecs_to_jiffies(params->spp_sackdelay);
  2117. } else {
  2118. sp->sackdelay = params->spp_sackdelay;
  2119. }
  2120. }
  2121. if (sackdelay_change) {
  2122. if (trans) {
  2123. trans->param_flags =
  2124. (trans->param_flags & ~SPP_SACKDELAY) |
  2125. sackdelay_change;
  2126. } else if (asoc) {
  2127. asoc->param_flags =
  2128. (asoc->param_flags & ~SPP_SACKDELAY) |
  2129. sackdelay_change;
  2130. } else {
  2131. sp->param_flags =
  2132. (sp->param_flags & ~SPP_SACKDELAY) |
  2133. sackdelay_change;
  2134. }
  2135. }
  2136. /* Note that a value of zero indicates the current setting should be
  2137. left unchanged.
  2138. */
  2139. if (params->spp_pathmaxrxt) {
  2140. if (trans) {
  2141. trans->pathmaxrxt = params->spp_pathmaxrxt;
  2142. } else if (asoc) {
  2143. asoc->pathmaxrxt = params->spp_pathmaxrxt;
  2144. } else {
  2145. sp->pathmaxrxt = params->spp_pathmaxrxt;
  2146. }
  2147. }
  2148. return 0;
  2149. }
  2150. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  2151. char __user *optval,
  2152. unsigned int optlen)
  2153. {
  2154. struct sctp_paddrparams params;
  2155. struct sctp_transport *trans = NULL;
  2156. struct sctp_association *asoc = NULL;
  2157. struct sctp_sock *sp = sctp_sk(sk);
  2158. int error;
  2159. int hb_change, pmtud_change, sackdelay_change;
  2160. if (optlen != sizeof(struct sctp_paddrparams))
  2161. return - EINVAL;
  2162. if (copy_from_user(&params, optval, optlen))
  2163. return -EFAULT;
  2164. /* Validate flags and value parameters. */
  2165. hb_change = params.spp_flags & SPP_HB;
  2166. pmtud_change = params.spp_flags & SPP_PMTUD;
  2167. sackdelay_change = params.spp_flags & SPP_SACKDELAY;
  2168. if (hb_change == SPP_HB ||
  2169. pmtud_change == SPP_PMTUD ||
  2170. sackdelay_change == SPP_SACKDELAY ||
  2171. params.spp_sackdelay > 500 ||
  2172. (params.spp_pathmtu &&
  2173. params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
  2174. return -EINVAL;
  2175. /* If an address other than INADDR_ANY is specified, and
  2176. * no transport is found, then the request is invalid.
  2177. */
  2178. if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
  2179. trans = sctp_addr_id2transport(sk, &params.spp_address,
  2180. params.spp_assoc_id);
  2181. if (!trans)
  2182. return -EINVAL;
  2183. }
  2184. /* Get association, if assoc_id != 0 and the socket is a one
  2185. * to many style socket, and an association was not found, then
  2186. * the id was invalid.
  2187. */
  2188. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  2189. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
  2190. return -EINVAL;
  2191. /* Heartbeat demand can only be sent on a transport or
  2192. * association, but not a socket.
  2193. */
  2194. if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
  2195. return -EINVAL;
  2196. /* Process parameters. */
  2197. error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2198. hb_change, pmtud_change,
  2199. sackdelay_change);
  2200. if (error)
  2201. return error;
  2202. /* If changes are for association, also apply parameters to each
  2203. * transport.
  2204. */
  2205. if (!trans && asoc) {
  2206. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2207. transports) {
  2208. sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2209. hb_change, pmtud_change,
  2210. sackdelay_change);
  2211. }
  2212. }
  2213. return 0;
  2214. }
  2215. /*
  2216. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  2217. *
  2218. * This option will effect the way delayed acks are performed. This
  2219. * option allows you to get or set the delayed ack time, in
  2220. * milliseconds. It also allows changing the delayed ack frequency.
  2221. * Changing the frequency to 1 disables the delayed sack algorithm. If
  2222. * the assoc_id is 0, then this sets or gets the endpoints default
  2223. * values. If the assoc_id field is non-zero, then the set or get
  2224. * effects the specified association for the one to many model (the
  2225. * assoc_id field is ignored by the one to one model). Note that if
  2226. * sack_delay or sack_freq are 0 when setting this option, then the
  2227. * current values will remain unchanged.
  2228. *
  2229. * struct sctp_sack_info {
  2230. * sctp_assoc_t sack_assoc_id;
  2231. * uint32_t sack_delay;
  2232. * uint32_t sack_freq;
  2233. * };
  2234. *
  2235. * sack_assoc_id - This parameter, indicates which association the user
  2236. * is performing an action upon. Note that if this field's value is
  2237. * zero then the endpoints default value is changed (effecting future
  2238. * associations only).
  2239. *
  2240. * sack_delay - This parameter contains the number of milliseconds that
  2241. * the user is requesting the delayed ACK timer be set to. Note that
  2242. * this value is defined in the standard to be between 200 and 500
  2243. * milliseconds.
  2244. *
  2245. * sack_freq - This parameter contains the number of packets that must
  2246. * be received before a sack is sent without waiting for the delay
  2247. * timer to expire. The default value for this is 2, setting this
  2248. * value to 1 will disable the delayed sack algorithm.
  2249. */
  2250. static int sctp_setsockopt_delayed_ack(struct sock *sk,
  2251. char __user *optval, unsigned int optlen)
  2252. {
  2253. struct sctp_sack_info params;
  2254. struct sctp_transport *trans = NULL;
  2255. struct sctp_association *asoc = NULL;
  2256. struct sctp_sock *sp = sctp_sk(sk);
  2257. if (optlen == sizeof(struct sctp_sack_info)) {
  2258. if (copy_from_user(&params, optval, optlen))
  2259. return -EFAULT;
  2260. if (params.sack_delay == 0 && params.sack_freq == 0)
  2261. return 0;
  2262. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2263. pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
  2264. pr_warn("Use struct sctp_sack_info instead\n");
  2265. if (copy_from_user(&params, optval, optlen))
  2266. return -EFAULT;
  2267. if (params.sack_delay == 0)
  2268. params.sack_freq = 1;
  2269. else
  2270. params.sack_freq = 0;
  2271. } else
  2272. return - EINVAL;
  2273. /* Validate value parameter. */
  2274. if (params.sack_delay > 500)
  2275. return -EINVAL;
  2276. /* Get association, if sack_assoc_id != 0 and the socket is a one
  2277. * to many style socket, and an association was not found, then
  2278. * the id was invalid.
  2279. */
  2280. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  2281. if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
  2282. return -EINVAL;
  2283. if (params.sack_delay) {
  2284. if (asoc) {
  2285. asoc->sackdelay =
  2286. msecs_to_jiffies(params.sack_delay);
  2287. asoc->param_flags =
  2288. (asoc->param_flags & ~SPP_SACKDELAY) |
  2289. SPP_SACKDELAY_ENABLE;
  2290. } else {
  2291. sp->sackdelay = params.sack_delay;
  2292. sp->param_flags =
  2293. (sp->param_flags & ~SPP_SACKDELAY) |
  2294. SPP_SACKDELAY_ENABLE;
  2295. }
  2296. }
  2297. if (params.sack_freq == 1) {
  2298. if (asoc) {
  2299. asoc->param_flags =
  2300. (asoc->param_flags & ~SPP_SACKDELAY) |
  2301. SPP_SACKDELAY_DISABLE;
  2302. } else {
  2303. sp->param_flags =
  2304. (sp->param_flags & ~SPP_SACKDELAY) |
  2305. SPP_SACKDELAY_DISABLE;
  2306. }
  2307. } else if (params.sack_freq > 1) {
  2308. if (asoc) {
  2309. asoc->sackfreq = params.sack_freq;
  2310. asoc->param_flags =
  2311. (asoc->param_flags & ~SPP_SACKDELAY) |
  2312. SPP_SACKDELAY_ENABLE;
  2313. } else {
  2314. sp->sackfreq = params.sack_freq;
  2315. sp->param_flags =
  2316. (sp->param_flags & ~SPP_SACKDELAY) |
  2317. SPP_SACKDELAY_ENABLE;
  2318. }
  2319. }
  2320. /* If change is for association, also apply to each transport. */
  2321. if (asoc) {
  2322. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2323. transports) {
  2324. if (params.sack_delay) {
  2325. trans->sackdelay =
  2326. msecs_to_jiffies(params.sack_delay);
  2327. trans->param_flags =
  2328. (trans->param_flags & ~SPP_SACKDELAY) |
  2329. SPP_SACKDELAY_ENABLE;
  2330. }
  2331. if (params.sack_freq == 1) {
  2332. trans->param_flags =
  2333. (trans->param_flags & ~SPP_SACKDELAY) |
  2334. SPP_SACKDELAY_DISABLE;
  2335. } else if (params.sack_freq > 1) {
  2336. trans->sackfreq = params.sack_freq;
  2337. trans->param_flags =
  2338. (trans->param_flags & ~SPP_SACKDELAY) |
  2339. SPP_SACKDELAY_ENABLE;
  2340. }
  2341. }
  2342. }
  2343. return 0;
  2344. }
  2345. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2346. *
  2347. * Applications can specify protocol parameters for the default association
  2348. * initialization. The option name argument to setsockopt() and getsockopt()
  2349. * is SCTP_INITMSG.
  2350. *
  2351. * Setting initialization parameters is effective only on an unconnected
  2352. * socket (for UDP-style sockets only future associations are effected
  2353. * by the change). With TCP-style sockets, this option is inherited by
  2354. * sockets derived from a listener socket.
  2355. */
  2356. static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
  2357. {
  2358. struct sctp_initmsg sinit;
  2359. struct sctp_sock *sp = sctp_sk(sk);
  2360. if (optlen != sizeof(struct sctp_initmsg))
  2361. return -EINVAL;
  2362. if (copy_from_user(&sinit, optval, optlen))
  2363. return -EFAULT;
  2364. if (sinit.sinit_num_ostreams)
  2365. sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
  2366. if (sinit.sinit_max_instreams)
  2367. sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
  2368. if (sinit.sinit_max_attempts)
  2369. sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
  2370. if (sinit.sinit_max_init_timeo)
  2371. sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
  2372. return 0;
  2373. }
  2374. /*
  2375. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2376. *
  2377. * Applications that wish to use the sendto() system call may wish to
  2378. * specify a default set of parameters that would normally be supplied
  2379. * through the inclusion of ancillary data. This socket option allows
  2380. * such an application to set the default sctp_sndrcvinfo structure.
  2381. * The application that wishes to use this socket option simply passes
  2382. * in to this call the sctp_sndrcvinfo structure defined in Section
  2383. * 5.2.2) The input parameters accepted by this call include
  2384. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2385. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2386. * to this call if the caller is using the UDP model.
  2387. */
  2388. static int sctp_setsockopt_default_send_param(struct sock *sk,
  2389. char __user *optval,
  2390. unsigned int optlen)
  2391. {
  2392. struct sctp_sndrcvinfo info;
  2393. struct sctp_association *asoc;
  2394. struct sctp_sock *sp = sctp_sk(sk);
  2395. if (optlen != sizeof(struct sctp_sndrcvinfo))
  2396. return -EINVAL;
  2397. if (copy_from_user(&info, optval, optlen))
  2398. return -EFAULT;
  2399. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  2400. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  2401. return -EINVAL;
  2402. if (asoc) {
  2403. asoc->default_stream = info.sinfo_stream;
  2404. asoc->default_flags = info.sinfo_flags;
  2405. asoc->default_ppid = info.sinfo_ppid;
  2406. asoc->default_context = info.sinfo_context;
  2407. asoc->default_timetolive = info.sinfo_timetolive;
  2408. } else {
  2409. sp->default_stream = info.sinfo_stream;
  2410. sp->default_flags = info.sinfo_flags;
  2411. sp->default_ppid = info.sinfo_ppid;
  2412. sp->default_context = info.sinfo_context;
  2413. sp->default_timetolive = info.sinfo_timetolive;
  2414. }
  2415. return 0;
  2416. }
  2417. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2418. *
  2419. * Requests that the local SCTP stack use the enclosed peer address as
  2420. * the association primary. The enclosed address must be one of the
  2421. * association peer's addresses.
  2422. */
  2423. static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
  2424. unsigned int optlen)
  2425. {
  2426. struct sctp_prim prim;
  2427. struct sctp_transport *trans;
  2428. if (optlen != sizeof(struct sctp_prim))
  2429. return -EINVAL;
  2430. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  2431. return -EFAULT;
  2432. trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
  2433. if (!trans)
  2434. return -EINVAL;
  2435. sctp_assoc_set_primary(trans->asoc, trans);
  2436. return 0;
  2437. }
  2438. /*
  2439. * 7.1.5 SCTP_NODELAY
  2440. *
  2441. * Turn on/off any Nagle-like algorithm. This means that packets are
  2442. * generally sent as soon as possible and no unnecessary delays are
  2443. * introduced, at the cost of more packets in the network. Expects an
  2444. * integer boolean flag.
  2445. */
  2446. static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
  2447. unsigned int optlen)
  2448. {
  2449. int val;
  2450. if (optlen < sizeof(int))
  2451. return -EINVAL;
  2452. if (get_user(val, (int __user *)optval))
  2453. return -EFAULT;
  2454. sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
  2455. return 0;
  2456. }
  2457. /*
  2458. *
  2459. * 7.1.1 SCTP_RTOINFO
  2460. *
  2461. * The protocol parameters used to initialize and bound retransmission
  2462. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2463. * and modify these parameters.
  2464. * All parameters are time values, in milliseconds. A value of 0, when
  2465. * modifying the parameters, indicates that the current value should not
  2466. * be changed.
  2467. *
  2468. */
  2469. static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
  2470. {
  2471. struct sctp_rtoinfo rtoinfo;
  2472. struct sctp_association *asoc;
  2473. if (optlen != sizeof (struct sctp_rtoinfo))
  2474. return -EINVAL;
  2475. if (copy_from_user(&rtoinfo, optval, optlen))
  2476. return -EFAULT;
  2477. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  2478. /* Set the values to the specific association */
  2479. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  2480. return -EINVAL;
  2481. if (asoc) {
  2482. if (rtoinfo.srto_initial != 0)
  2483. asoc->rto_initial =
  2484. msecs_to_jiffies(rtoinfo.srto_initial);
  2485. if (rtoinfo.srto_max != 0)
  2486. asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
  2487. if (rtoinfo.srto_min != 0)
  2488. asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
  2489. } else {
  2490. /* If there is no association or the association-id = 0
  2491. * set the values to the endpoint.
  2492. */
  2493. struct sctp_sock *sp = sctp_sk(sk);
  2494. if (rtoinfo.srto_initial != 0)
  2495. sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
  2496. if (rtoinfo.srto_max != 0)
  2497. sp->rtoinfo.srto_max = rtoinfo.srto_max;
  2498. if (rtoinfo.srto_min != 0)
  2499. sp->rtoinfo.srto_min = rtoinfo.srto_min;
  2500. }
  2501. return 0;
  2502. }
  2503. /*
  2504. *
  2505. * 7.1.2 SCTP_ASSOCINFO
  2506. *
  2507. * This option is used to tune the maximum retransmission attempts
  2508. * of the association.
  2509. * Returns an error if the new association retransmission value is
  2510. * greater than the sum of the retransmission value of the peer.
  2511. * See [SCTP] for more information.
  2512. *
  2513. */
  2514. static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
  2515. {
  2516. struct sctp_assocparams assocparams;
  2517. struct sctp_association *asoc;
  2518. if (optlen != sizeof(struct sctp_assocparams))
  2519. return -EINVAL;
  2520. if (copy_from_user(&assocparams, optval, optlen))
  2521. return -EFAULT;
  2522. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  2523. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  2524. return -EINVAL;
  2525. /* Set the values to the specific association */
  2526. if (asoc) {
  2527. if (assocparams.sasoc_asocmaxrxt != 0) {
  2528. __u32 path_sum = 0;
  2529. int paths = 0;
  2530. struct sctp_transport *peer_addr;
  2531. list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
  2532. transports) {
  2533. path_sum += peer_addr->pathmaxrxt;
  2534. paths++;
  2535. }
  2536. /* Only validate asocmaxrxt if we have more than
  2537. * one path/transport. We do this because path
  2538. * retransmissions are only counted when we have more
  2539. * then one path.
  2540. */
  2541. if (paths > 1 &&
  2542. assocparams.sasoc_asocmaxrxt > path_sum)
  2543. return -EINVAL;
  2544. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  2545. }
  2546. if (assocparams.sasoc_cookie_life != 0)
  2547. asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
  2548. } else {
  2549. /* Set the values to the endpoint */
  2550. struct sctp_sock *sp = sctp_sk(sk);
  2551. if (assocparams.sasoc_asocmaxrxt != 0)
  2552. sp->assocparams.sasoc_asocmaxrxt =
  2553. assocparams.sasoc_asocmaxrxt;
  2554. if (assocparams.sasoc_cookie_life != 0)
  2555. sp->assocparams.sasoc_cookie_life =
  2556. assocparams.sasoc_cookie_life;
  2557. }
  2558. return 0;
  2559. }
  2560. /*
  2561. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  2562. *
  2563. * This socket option is a boolean flag which turns on or off mapped V4
  2564. * addresses. If this option is turned on and the socket is type
  2565. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  2566. * If this option is turned off, then no mapping will be done of V4
  2567. * addresses and a user will receive both PF_INET6 and PF_INET type
  2568. * addresses on the socket.
  2569. */
  2570. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
  2571. {
  2572. int val;
  2573. struct sctp_sock *sp = sctp_sk(sk);
  2574. if (optlen < sizeof(int))
  2575. return -EINVAL;
  2576. if (get_user(val, (int __user *)optval))
  2577. return -EFAULT;
  2578. if (val)
  2579. sp->v4mapped = 1;
  2580. else
  2581. sp->v4mapped = 0;
  2582. return 0;
  2583. }
  2584. /*
  2585. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  2586. * This option will get or set the maximum size to put in any outgoing
  2587. * SCTP DATA chunk. If a message is larger than this size it will be
  2588. * fragmented by SCTP into the specified size. Note that the underlying
  2589. * SCTP implementation may fragment into smaller sized chunks when the
  2590. * PMTU of the underlying association is smaller than the value set by
  2591. * the user. The default value for this option is '0' which indicates
  2592. * the user is NOT limiting fragmentation and only the PMTU will effect
  2593. * SCTP's choice of DATA chunk size. Note also that values set larger
  2594. * than the maximum size of an IP datagram will effectively let SCTP
  2595. * control fragmentation (i.e. the same as setting this option to 0).
  2596. *
  2597. * The following structure is used to access and modify this parameter:
  2598. *
  2599. * struct sctp_assoc_value {
  2600. * sctp_assoc_t assoc_id;
  2601. * uint32_t assoc_value;
  2602. * };
  2603. *
  2604. * assoc_id: This parameter is ignored for one-to-one style sockets.
  2605. * For one-to-many style sockets this parameter indicates which
  2606. * association the user is performing an action upon. Note that if
  2607. * this field's value is zero then the endpoints default value is
  2608. * changed (effecting future associations only).
  2609. * assoc_value: This parameter specifies the maximum size in bytes.
  2610. */
  2611. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
  2612. {
  2613. struct sctp_assoc_value params;
  2614. struct sctp_association *asoc;
  2615. struct sctp_sock *sp = sctp_sk(sk);
  2616. int val;
  2617. if (optlen == sizeof(int)) {
  2618. pr_warn("Use of int in maxseg socket option deprecated\n");
  2619. pr_warn("Use struct sctp_assoc_value instead\n");
  2620. if (copy_from_user(&val, optval, optlen))
  2621. return -EFAULT;
  2622. params.assoc_id = 0;
  2623. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2624. if (copy_from_user(&params, optval, optlen))
  2625. return -EFAULT;
  2626. val = params.assoc_value;
  2627. } else
  2628. return -EINVAL;
  2629. if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
  2630. return -EINVAL;
  2631. asoc = sctp_id2assoc(sk, params.assoc_id);
  2632. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  2633. return -EINVAL;
  2634. if (asoc) {
  2635. if (val == 0) {
  2636. val = asoc->pathmtu;
  2637. val -= sp->pf->af->net_header_len;
  2638. val -= sizeof(struct sctphdr) +
  2639. sizeof(struct sctp_data_chunk);
  2640. }
  2641. asoc->user_frag = val;
  2642. asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
  2643. } else {
  2644. sp->user_frag = val;
  2645. }
  2646. return 0;
  2647. }
  2648. /*
  2649. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2650. *
  2651. * Requests that the peer mark the enclosed address as the association
  2652. * primary. The enclosed address must be one of the association's
  2653. * locally bound addresses. The following structure is used to make a
  2654. * set primary request:
  2655. */
  2656. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  2657. unsigned int optlen)
  2658. {
  2659. struct net *net = sock_net(sk);
  2660. struct sctp_sock *sp;
  2661. struct sctp_association *asoc = NULL;
  2662. struct sctp_setpeerprim prim;
  2663. struct sctp_chunk *chunk;
  2664. struct sctp_af *af;
  2665. int err;
  2666. sp = sctp_sk(sk);
  2667. if (!net->sctp.addip_enable)
  2668. return -EPERM;
  2669. if (optlen != sizeof(struct sctp_setpeerprim))
  2670. return -EINVAL;
  2671. if (copy_from_user(&prim, optval, optlen))
  2672. return -EFAULT;
  2673. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  2674. if (!asoc)
  2675. return -EINVAL;
  2676. if (!asoc->peer.asconf_capable)
  2677. return -EPERM;
  2678. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2679. return -EPERM;
  2680. if (!sctp_state(asoc, ESTABLISHED))
  2681. return -ENOTCONN;
  2682. af = sctp_get_af_specific(prim.sspp_addr.ss_family);
  2683. if (!af)
  2684. return -EINVAL;
  2685. if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
  2686. return -EADDRNOTAVAIL;
  2687. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  2688. return -EADDRNOTAVAIL;
  2689. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2690. chunk = sctp_make_asconf_set_prim(asoc,
  2691. (union sctp_addr *)&prim.sspp_addr);
  2692. if (!chunk)
  2693. return -ENOMEM;
  2694. err = sctp_send_asconf(asoc, chunk);
  2695. pr_debug("%s: we set peer primary addr primitively\n", __func__);
  2696. return err;
  2697. }
  2698. static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
  2699. unsigned int optlen)
  2700. {
  2701. struct sctp_setadaptation adaptation;
  2702. if (optlen != sizeof(struct sctp_setadaptation))
  2703. return -EINVAL;
  2704. if (copy_from_user(&adaptation, optval, optlen))
  2705. return -EFAULT;
  2706. sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
  2707. return 0;
  2708. }
  2709. /*
  2710. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  2711. *
  2712. * The context field in the sctp_sndrcvinfo structure is normally only
  2713. * used when a failed message is retrieved holding the value that was
  2714. * sent down on the actual send call. This option allows the setting of
  2715. * a default context on an association basis that will be received on
  2716. * reading messages from the peer. This is especially helpful in the
  2717. * one-2-many model for an application to keep some reference to an
  2718. * internal state machine that is processing messages on the
  2719. * association. Note that the setting of this value only effects
  2720. * received messages from the peer and does not effect the value that is
  2721. * saved with outbound messages.
  2722. */
  2723. static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
  2724. unsigned int optlen)
  2725. {
  2726. struct sctp_assoc_value params;
  2727. struct sctp_sock *sp;
  2728. struct sctp_association *asoc;
  2729. if (optlen != sizeof(struct sctp_assoc_value))
  2730. return -EINVAL;
  2731. if (copy_from_user(&params, optval, optlen))
  2732. return -EFAULT;
  2733. sp = sctp_sk(sk);
  2734. if (params.assoc_id != 0) {
  2735. asoc = sctp_id2assoc(sk, params.assoc_id);
  2736. if (!asoc)
  2737. return -EINVAL;
  2738. asoc->default_rcv_context = params.assoc_value;
  2739. } else {
  2740. sp->default_rcv_context = params.assoc_value;
  2741. }
  2742. return 0;
  2743. }
  2744. /*
  2745. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  2746. *
  2747. * This options will at a minimum specify if the implementation is doing
  2748. * fragmented interleave. Fragmented interleave, for a one to many
  2749. * socket, is when subsequent calls to receive a message may return
  2750. * parts of messages from different associations. Some implementations
  2751. * may allow you to turn this value on or off. If so, when turned off,
  2752. * no fragment interleave will occur (which will cause a head of line
  2753. * blocking amongst multiple associations sharing the same one to many
  2754. * socket). When this option is turned on, then each receive call may
  2755. * come from a different association (thus the user must receive data
  2756. * with the extended calls (e.g. sctp_recvmsg) to keep track of which
  2757. * association each receive belongs to.
  2758. *
  2759. * This option takes a boolean value. A non-zero value indicates that
  2760. * fragmented interleave is on. A value of zero indicates that
  2761. * fragmented interleave is off.
  2762. *
  2763. * Note that it is important that an implementation that allows this
  2764. * option to be turned on, have it off by default. Otherwise an unaware
  2765. * application using the one to many model may become confused and act
  2766. * incorrectly.
  2767. */
  2768. static int sctp_setsockopt_fragment_interleave(struct sock *sk,
  2769. char __user *optval,
  2770. unsigned int optlen)
  2771. {
  2772. int val;
  2773. if (optlen != sizeof(int))
  2774. return -EINVAL;
  2775. if (get_user(val, (int __user *)optval))
  2776. return -EFAULT;
  2777. sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
  2778. return 0;
  2779. }
  2780. /*
  2781. * 8.1.21. Set or Get the SCTP Partial Delivery Point
  2782. * (SCTP_PARTIAL_DELIVERY_POINT)
  2783. *
  2784. * This option will set or get the SCTP partial delivery point. This
  2785. * point is the size of a message where the partial delivery API will be
  2786. * invoked to help free up rwnd space for the peer. Setting this to a
  2787. * lower value will cause partial deliveries to happen more often. The
  2788. * calls argument is an integer that sets or gets the partial delivery
  2789. * point. Note also that the call will fail if the user attempts to set
  2790. * this value larger than the socket receive buffer size.
  2791. *
  2792. * Note that any single message having a length smaller than or equal to
  2793. * the SCTP partial delivery point will be delivered in one single read
  2794. * call as long as the user provided buffer is large enough to hold the
  2795. * message.
  2796. */
  2797. static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
  2798. char __user *optval,
  2799. unsigned int optlen)
  2800. {
  2801. u32 val;
  2802. if (optlen != sizeof(u32))
  2803. return -EINVAL;
  2804. if (get_user(val, (int __user *)optval))
  2805. return -EFAULT;
  2806. /* Note: We double the receive buffer from what the user sets
  2807. * it to be, also initial rwnd is based on rcvbuf/2.
  2808. */
  2809. if (val > (sk->sk_rcvbuf >> 1))
  2810. return -EINVAL;
  2811. sctp_sk(sk)->pd_point = val;
  2812. return 0; /* is this the right error code? */
  2813. }
  2814. /*
  2815. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  2816. *
  2817. * This option will allow a user to change the maximum burst of packets
  2818. * that can be emitted by this association. Note that the default value
  2819. * is 4, and some implementations may restrict this setting so that it
  2820. * can only be lowered.
  2821. *
  2822. * NOTE: This text doesn't seem right. Do this on a socket basis with
  2823. * future associations inheriting the socket value.
  2824. */
  2825. static int sctp_setsockopt_maxburst(struct sock *sk,
  2826. char __user *optval,
  2827. unsigned int optlen)
  2828. {
  2829. struct sctp_assoc_value params;
  2830. struct sctp_sock *sp;
  2831. struct sctp_association *asoc;
  2832. int val;
  2833. int assoc_id = 0;
  2834. if (optlen == sizeof(int)) {
  2835. pr_warn("Use of int in max_burst socket option deprecated\n");
  2836. pr_warn("Use struct sctp_assoc_value instead\n");
  2837. if (copy_from_user(&val, optval, optlen))
  2838. return -EFAULT;
  2839. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2840. if (copy_from_user(&params, optval, optlen))
  2841. return -EFAULT;
  2842. val = params.assoc_value;
  2843. assoc_id = params.assoc_id;
  2844. } else
  2845. return -EINVAL;
  2846. sp = sctp_sk(sk);
  2847. if (assoc_id != 0) {
  2848. asoc = sctp_id2assoc(sk, assoc_id);
  2849. if (!asoc)
  2850. return -EINVAL;
  2851. asoc->max_burst = val;
  2852. } else
  2853. sp->max_burst = val;
  2854. return 0;
  2855. }
  2856. /*
  2857. * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
  2858. *
  2859. * This set option adds a chunk type that the user is requesting to be
  2860. * received only in an authenticated way. Changes to the list of chunks
  2861. * will only effect future associations on the socket.
  2862. */
  2863. static int sctp_setsockopt_auth_chunk(struct sock *sk,
  2864. char __user *optval,
  2865. unsigned int optlen)
  2866. {
  2867. struct net *net = sock_net(sk);
  2868. struct sctp_authchunk val;
  2869. if (!net->sctp.auth_enable)
  2870. return -EACCES;
  2871. if (optlen != sizeof(struct sctp_authchunk))
  2872. return -EINVAL;
  2873. if (copy_from_user(&val, optval, optlen))
  2874. return -EFAULT;
  2875. switch (val.sauth_chunk) {
  2876. case SCTP_CID_INIT:
  2877. case SCTP_CID_INIT_ACK:
  2878. case SCTP_CID_SHUTDOWN_COMPLETE:
  2879. case SCTP_CID_AUTH:
  2880. return -EINVAL;
  2881. }
  2882. /* add this chunk id to the endpoint */
  2883. return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
  2884. }
  2885. /*
  2886. * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
  2887. *
  2888. * This option gets or sets the list of HMAC algorithms that the local
  2889. * endpoint requires the peer to use.
  2890. */
  2891. static int sctp_setsockopt_hmac_ident(struct sock *sk,
  2892. char __user *optval,
  2893. unsigned int optlen)
  2894. {
  2895. struct net *net = sock_net(sk);
  2896. struct sctp_hmacalgo *hmacs;
  2897. u32 idents;
  2898. int err;
  2899. if (!net->sctp.auth_enable)
  2900. return -EACCES;
  2901. if (optlen < sizeof(struct sctp_hmacalgo))
  2902. return -EINVAL;
  2903. hmacs= memdup_user(optval, optlen);
  2904. if (IS_ERR(hmacs))
  2905. return PTR_ERR(hmacs);
  2906. idents = hmacs->shmac_num_idents;
  2907. if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
  2908. (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
  2909. err = -EINVAL;
  2910. goto out;
  2911. }
  2912. err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
  2913. out:
  2914. kfree(hmacs);
  2915. return err;
  2916. }
  2917. /*
  2918. * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
  2919. *
  2920. * This option will set a shared secret key which is used to build an
  2921. * association shared key.
  2922. */
  2923. static int sctp_setsockopt_auth_key(struct sock *sk,
  2924. char __user *optval,
  2925. unsigned int optlen)
  2926. {
  2927. struct net *net = sock_net(sk);
  2928. struct sctp_authkey *authkey;
  2929. struct sctp_association *asoc;
  2930. int ret;
  2931. if (!net->sctp.auth_enable)
  2932. return -EACCES;
  2933. if (optlen <= sizeof(struct sctp_authkey))
  2934. return -EINVAL;
  2935. authkey= memdup_user(optval, optlen);
  2936. if (IS_ERR(authkey))
  2937. return PTR_ERR(authkey);
  2938. if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
  2939. ret = -EINVAL;
  2940. goto out;
  2941. }
  2942. asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
  2943. if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
  2944. ret = -EINVAL;
  2945. goto out;
  2946. }
  2947. ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
  2948. out:
  2949. kzfree(authkey);
  2950. return ret;
  2951. }
  2952. /*
  2953. * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
  2954. *
  2955. * This option will get or set the active shared key to be used to build
  2956. * the association shared key.
  2957. */
  2958. static int sctp_setsockopt_active_key(struct sock *sk,
  2959. char __user *optval,
  2960. unsigned int optlen)
  2961. {
  2962. struct net *net = sock_net(sk);
  2963. struct sctp_authkeyid val;
  2964. struct sctp_association *asoc;
  2965. if (!net->sctp.auth_enable)
  2966. return -EACCES;
  2967. if (optlen != sizeof(struct sctp_authkeyid))
  2968. return -EINVAL;
  2969. if (copy_from_user(&val, optval, optlen))
  2970. return -EFAULT;
  2971. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  2972. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  2973. return -EINVAL;
  2974. return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
  2975. val.scact_keynumber);
  2976. }
  2977. /*
  2978. * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
  2979. *
  2980. * This set option will delete a shared secret key from use.
  2981. */
  2982. static int sctp_setsockopt_del_key(struct sock *sk,
  2983. char __user *optval,
  2984. unsigned int optlen)
  2985. {
  2986. struct net *net = sock_net(sk);
  2987. struct sctp_authkeyid val;
  2988. struct sctp_association *asoc;
  2989. if (!net->sctp.auth_enable)
  2990. return -EACCES;
  2991. if (optlen != sizeof(struct sctp_authkeyid))
  2992. return -EINVAL;
  2993. if (copy_from_user(&val, optval, optlen))
  2994. return -EFAULT;
  2995. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  2996. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  2997. return -EINVAL;
  2998. return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
  2999. val.scact_keynumber);
  3000. }
  3001. /*
  3002. * 8.1.23 SCTP_AUTO_ASCONF
  3003. *
  3004. * This option will enable or disable the use of the automatic generation of
  3005. * ASCONF chunks to add and delete addresses to an existing association. Note
  3006. * that this option has two caveats namely: a) it only affects sockets that
  3007. * are bound to all addresses available to the SCTP stack, and b) the system
  3008. * administrator may have an overriding control that turns the ASCONF feature
  3009. * off no matter what setting the socket option may have.
  3010. * This option expects an integer boolean flag, where a non-zero value turns on
  3011. * the option, and a zero value turns off the option.
  3012. * Note. In this implementation, socket operation overrides default parameter
  3013. * being set by sysctl as well as FreeBSD implementation
  3014. */
  3015. static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
  3016. unsigned int optlen)
  3017. {
  3018. int val;
  3019. struct sctp_sock *sp = sctp_sk(sk);
  3020. if (optlen < sizeof(int))
  3021. return -EINVAL;
  3022. if (get_user(val, (int __user *)optval))
  3023. return -EFAULT;
  3024. if (!sctp_is_ep_boundall(sk) && val)
  3025. return -EINVAL;
  3026. if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
  3027. return 0;
  3028. if (val == 0 && sp->do_auto_asconf) {
  3029. list_del(&sp->auto_asconf_list);
  3030. sp->do_auto_asconf = 0;
  3031. } else if (val && !sp->do_auto_asconf) {
  3032. list_add_tail(&sp->auto_asconf_list,
  3033. &sock_net(sk)->sctp.auto_asconf_splist);
  3034. sp->do_auto_asconf = 1;
  3035. }
  3036. return 0;
  3037. }
  3038. /*
  3039. * SCTP_PEER_ADDR_THLDS
  3040. *
  3041. * This option allows us to alter the partially failed threshold for one or all
  3042. * transports in an association. See Section 6.1 of:
  3043. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  3044. */
  3045. static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
  3046. char __user *optval,
  3047. unsigned int optlen)
  3048. {
  3049. struct sctp_paddrthlds val;
  3050. struct sctp_transport *trans;
  3051. struct sctp_association *asoc;
  3052. if (optlen < sizeof(struct sctp_paddrthlds))
  3053. return -EINVAL;
  3054. if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
  3055. sizeof(struct sctp_paddrthlds)))
  3056. return -EFAULT;
  3057. if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
  3058. asoc = sctp_id2assoc(sk, val.spt_assoc_id);
  3059. if (!asoc)
  3060. return -ENOENT;
  3061. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  3062. transports) {
  3063. if (val.spt_pathmaxrxt)
  3064. trans->pathmaxrxt = val.spt_pathmaxrxt;
  3065. trans->pf_retrans = val.spt_pathpfthld;
  3066. }
  3067. if (val.spt_pathmaxrxt)
  3068. asoc->pathmaxrxt = val.spt_pathmaxrxt;
  3069. asoc->pf_retrans = val.spt_pathpfthld;
  3070. } else {
  3071. trans = sctp_addr_id2transport(sk, &val.spt_address,
  3072. val.spt_assoc_id);
  3073. if (!trans)
  3074. return -ENOENT;
  3075. if (val.spt_pathmaxrxt)
  3076. trans->pathmaxrxt = val.spt_pathmaxrxt;
  3077. trans->pf_retrans = val.spt_pathpfthld;
  3078. }
  3079. return 0;
  3080. }
  3081. /* API 6.2 setsockopt(), getsockopt()
  3082. *
  3083. * Applications use setsockopt() and getsockopt() to set or retrieve
  3084. * socket options. Socket options are used to change the default
  3085. * behavior of sockets calls. They are described in Section 7.
  3086. *
  3087. * The syntax is:
  3088. *
  3089. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  3090. * int __user *optlen);
  3091. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  3092. * int optlen);
  3093. *
  3094. * sd - the socket descript.
  3095. * level - set to IPPROTO_SCTP for all SCTP options.
  3096. * optname - the option name.
  3097. * optval - the buffer to store the value of the option.
  3098. * optlen - the size of the buffer.
  3099. */
  3100. static int sctp_setsockopt(struct sock *sk, int level, int optname,
  3101. char __user *optval, unsigned int optlen)
  3102. {
  3103. int retval = 0;
  3104. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  3105. /* I can hardly begin to describe how wrong this is. This is
  3106. * so broken as to be worse than useless. The API draft
  3107. * REALLY is NOT helpful here... I am not convinced that the
  3108. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  3109. * are at all well-founded.
  3110. */
  3111. if (level != SOL_SCTP) {
  3112. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3113. retval = af->setsockopt(sk, level, optname, optval, optlen);
  3114. goto out_nounlock;
  3115. }
  3116. sctp_lock_sock(sk);
  3117. switch (optname) {
  3118. case SCTP_SOCKOPT_BINDX_ADD:
  3119. /* 'optlen' is the size of the addresses buffer. */
  3120. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  3121. optlen, SCTP_BINDX_ADD_ADDR);
  3122. break;
  3123. case SCTP_SOCKOPT_BINDX_REM:
  3124. /* 'optlen' is the size of the addresses buffer. */
  3125. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  3126. optlen, SCTP_BINDX_REM_ADDR);
  3127. break;
  3128. case SCTP_SOCKOPT_CONNECTX_OLD:
  3129. /* 'optlen' is the size of the addresses buffer. */
  3130. retval = sctp_setsockopt_connectx_old(sk,
  3131. (struct sockaddr __user *)optval,
  3132. optlen);
  3133. break;
  3134. case SCTP_SOCKOPT_CONNECTX:
  3135. /* 'optlen' is the size of the addresses buffer. */
  3136. retval = sctp_setsockopt_connectx(sk,
  3137. (struct sockaddr __user *)optval,
  3138. optlen);
  3139. break;
  3140. case SCTP_DISABLE_FRAGMENTS:
  3141. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  3142. break;
  3143. case SCTP_EVENTS:
  3144. retval = sctp_setsockopt_events(sk, optval, optlen);
  3145. break;
  3146. case SCTP_AUTOCLOSE:
  3147. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  3148. break;
  3149. case SCTP_PEER_ADDR_PARAMS:
  3150. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  3151. break;
  3152. case SCTP_DELAYED_SACK:
  3153. retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
  3154. break;
  3155. case SCTP_PARTIAL_DELIVERY_POINT:
  3156. retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
  3157. break;
  3158. case SCTP_INITMSG:
  3159. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  3160. break;
  3161. case SCTP_DEFAULT_SEND_PARAM:
  3162. retval = sctp_setsockopt_default_send_param(sk, optval,
  3163. optlen);
  3164. break;
  3165. case SCTP_PRIMARY_ADDR:
  3166. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  3167. break;
  3168. case SCTP_SET_PEER_PRIMARY_ADDR:
  3169. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  3170. break;
  3171. case SCTP_NODELAY:
  3172. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  3173. break;
  3174. case SCTP_RTOINFO:
  3175. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  3176. break;
  3177. case SCTP_ASSOCINFO:
  3178. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  3179. break;
  3180. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3181. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  3182. break;
  3183. case SCTP_MAXSEG:
  3184. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  3185. break;
  3186. case SCTP_ADAPTATION_LAYER:
  3187. retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
  3188. break;
  3189. case SCTP_CONTEXT:
  3190. retval = sctp_setsockopt_context(sk, optval, optlen);
  3191. break;
  3192. case SCTP_FRAGMENT_INTERLEAVE:
  3193. retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
  3194. break;
  3195. case SCTP_MAX_BURST:
  3196. retval = sctp_setsockopt_maxburst(sk, optval, optlen);
  3197. break;
  3198. case SCTP_AUTH_CHUNK:
  3199. retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
  3200. break;
  3201. case SCTP_HMAC_IDENT:
  3202. retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
  3203. break;
  3204. case SCTP_AUTH_KEY:
  3205. retval = sctp_setsockopt_auth_key(sk, optval, optlen);
  3206. break;
  3207. case SCTP_AUTH_ACTIVE_KEY:
  3208. retval = sctp_setsockopt_active_key(sk, optval, optlen);
  3209. break;
  3210. case SCTP_AUTH_DELETE_KEY:
  3211. retval = sctp_setsockopt_del_key(sk, optval, optlen);
  3212. break;
  3213. case SCTP_AUTO_ASCONF:
  3214. retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
  3215. break;
  3216. case SCTP_PEER_ADDR_THLDS:
  3217. retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
  3218. break;
  3219. default:
  3220. retval = -ENOPROTOOPT;
  3221. break;
  3222. }
  3223. sctp_release_sock(sk);
  3224. out_nounlock:
  3225. return retval;
  3226. }
  3227. /* API 3.1.6 connect() - UDP Style Syntax
  3228. *
  3229. * An application may use the connect() call in the UDP model to initiate an
  3230. * association without sending data.
  3231. *
  3232. * The syntax is:
  3233. *
  3234. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  3235. *
  3236. * sd: the socket descriptor to have a new association added to.
  3237. *
  3238. * nam: the address structure (either struct sockaddr_in or struct
  3239. * sockaddr_in6 defined in RFC2553 [7]).
  3240. *
  3241. * len: the size of the address.
  3242. */
  3243. static int sctp_connect(struct sock *sk, struct sockaddr *addr,
  3244. int addr_len)
  3245. {
  3246. int err = 0;
  3247. struct sctp_af *af;
  3248. sctp_lock_sock(sk);
  3249. pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
  3250. addr, addr_len);
  3251. /* Validate addr_len before calling common connect/connectx routine. */
  3252. af = sctp_get_af_specific(addr->sa_family);
  3253. if (!af || addr_len < af->sockaddr_len) {
  3254. err = -EINVAL;
  3255. } else {
  3256. /* Pass correct addr len to common routine (so it knows there
  3257. * is only one address being passed.
  3258. */
  3259. err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
  3260. }
  3261. sctp_release_sock(sk);
  3262. return err;
  3263. }
  3264. /* FIXME: Write comments. */
  3265. static int sctp_disconnect(struct sock *sk, int flags)
  3266. {
  3267. return -EOPNOTSUPP; /* STUB */
  3268. }
  3269. /* 4.1.4 accept() - TCP Style Syntax
  3270. *
  3271. * Applications use accept() call to remove an established SCTP
  3272. * association from the accept queue of the endpoint. A new socket
  3273. * descriptor will be returned from accept() to represent the newly
  3274. * formed association.
  3275. */
  3276. static struct sock *sctp_accept(struct sock *sk, int flags, int *err)
  3277. {
  3278. struct sctp_sock *sp;
  3279. struct sctp_endpoint *ep;
  3280. struct sock *newsk = NULL;
  3281. struct sctp_association *asoc;
  3282. long timeo;
  3283. int error = 0;
  3284. sctp_lock_sock(sk);
  3285. sp = sctp_sk(sk);
  3286. ep = sp->ep;
  3287. if (!sctp_style(sk, TCP)) {
  3288. error = -EOPNOTSUPP;
  3289. goto out;
  3290. }
  3291. if (!sctp_sstate(sk, LISTENING)) {
  3292. error = -EINVAL;
  3293. goto out;
  3294. }
  3295. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  3296. error = sctp_wait_for_accept(sk, timeo);
  3297. if (error)
  3298. goto out;
  3299. /* We treat the list of associations on the endpoint as the accept
  3300. * queue and pick the first association on the list.
  3301. */
  3302. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  3303. newsk = sp->pf->create_accept_sk(sk, asoc);
  3304. if (!newsk) {
  3305. error = -ENOMEM;
  3306. goto out;
  3307. }
  3308. /* Populate the fields of the newsk from the oldsk and migrate the
  3309. * asoc to the newsk.
  3310. */
  3311. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  3312. out:
  3313. sctp_release_sock(sk);
  3314. *err = error;
  3315. return newsk;
  3316. }
  3317. /* The SCTP ioctl handler. */
  3318. static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  3319. {
  3320. int rc = -ENOTCONN;
  3321. sctp_lock_sock(sk);
  3322. /*
  3323. * SEQPACKET-style sockets in LISTENING state are valid, for
  3324. * SCTP, so only discard TCP-style sockets in LISTENING state.
  3325. */
  3326. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  3327. goto out;
  3328. switch (cmd) {
  3329. case SIOCINQ: {
  3330. struct sk_buff *skb;
  3331. unsigned int amount = 0;
  3332. skb = skb_peek(&sk->sk_receive_queue);
  3333. if (skb != NULL) {
  3334. /*
  3335. * We will only return the amount of this packet since
  3336. * that is all that will be read.
  3337. */
  3338. amount = skb->len;
  3339. }
  3340. rc = put_user(amount, (int __user *)arg);
  3341. break;
  3342. }
  3343. default:
  3344. rc = -ENOIOCTLCMD;
  3345. break;
  3346. }
  3347. out:
  3348. sctp_release_sock(sk);
  3349. return rc;
  3350. }
  3351. /* This is the function which gets called during socket creation to
  3352. * initialized the SCTP-specific portion of the sock.
  3353. * The sock structure should already be zero-filled memory.
  3354. */
  3355. static int sctp_init_sock(struct sock *sk)
  3356. {
  3357. struct net *net = sock_net(sk);
  3358. struct sctp_sock *sp;
  3359. pr_debug("%s: sk:%p\n", __func__, sk);
  3360. sp = sctp_sk(sk);
  3361. /* Initialize the SCTP per socket area. */
  3362. switch (sk->sk_type) {
  3363. case SOCK_SEQPACKET:
  3364. sp->type = SCTP_SOCKET_UDP;
  3365. break;
  3366. case SOCK_STREAM:
  3367. sp->type = SCTP_SOCKET_TCP;
  3368. break;
  3369. default:
  3370. return -ESOCKTNOSUPPORT;
  3371. }
  3372. /* Initialize default send parameters. These parameters can be
  3373. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  3374. */
  3375. sp->default_stream = 0;
  3376. sp->default_ppid = 0;
  3377. sp->default_flags = 0;
  3378. sp->default_context = 0;
  3379. sp->default_timetolive = 0;
  3380. sp->default_rcv_context = 0;
  3381. sp->max_burst = net->sctp.max_burst;
  3382. sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
  3383. /* Initialize default setup parameters. These parameters
  3384. * can be modified with the SCTP_INITMSG socket option or
  3385. * overridden by the SCTP_INIT CMSG.
  3386. */
  3387. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  3388. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  3389. sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
  3390. sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
  3391. /* Initialize default RTO related parameters. These parameters can
  3392. * be modified for with the SCTP_RTOINFO socket option.
  3393. */
  3394. sp->rtoinfo.srto_initial = net->sctp.rto_initial;
  3395. sp->rtoinfo.srto_max = net->sctp.rto_max;
  3396. sp->rtoinfo.srto_min = net->sctp.rto_min;
  3397. /* Initialize default association related parameters. These parameters
  3398. * can be modified with the SCTP_ASSOCINFO socket option.
  3399. */
  3400. sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
  3401. sp->assocparams.sasoc_number_peer_destinations = 0;
  3402. sp->assocparams.sasoc_peer_rwnd = 0;
  3403. sp->assocparams.sasoc_local_rwnd = 0;
  3404. sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
  3405. /* Initialize default event subscriptions. By default, all the
  3406. * options are off.
  3407. */
  3408. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  3409. /* Default Peer Address Parameters. These defaults can
  3410. * be modified via SCTP_PEER_ADDR_PARAMS
  3411. */
  3412. sp->hbinterval = net->sctp.hb_interval;
  3413. sp->pathmaxrxt = net->sctp.max_retrans_path;
  3414. sp->pathmtu = 0; // allow default discovery
  3415. sp->sackdelay = net->sctp.sack_timeout;
  3416. sp->sackfreq = 2;
  3417. sp->param_flags = SPP_HB_ENABLE |
  3418. SPP_PMTUD_ENABLE |
  3419. SPP_SACKDELAY_ENABLE;
  3420. /* If enabled no SCTP message fragmentation will be performed.
  3421. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  3422. */
  3423. sp->disable_fragments = 0;
  3424. /* Enable Nagle algorithm by default. */
  3425. sp->nodelay = 0;
  3426. /* Enable by default. */
  3427. sp->v4mapped = 1;
  3428. /* Auto-close idle associations after the configured
  3429. * number of seconds. A value of 0 disables this
  3430. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  3431. * for UDP-style sockets only.
  3432. */
  3433. sp->autoclose = 0;
  3434. /* User specified fragmentation limit. */
  3435. sp->user_frag = 0;
  3436. sp->adaptation_ind = 0;
  3437. sp->pf = sctp_get_pf_specific(sk->sk_family);
  3438. /* Control variables for partial data delivery. */
  3439. atomic_set(&sp->pd_mode, 0);
  3440. skb_queue_head_init(&sp->pd_lobby);
  3441. sp->frag_interleave = 0;
  3442. /* Create a per socket endpoint structure. Even if we
  3443. * change the data structure relationships, this may still
  3444. * be useful for storing pre-connect address information.
  3445. */
  3446. sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
  3447. if (!sp->ep)
  3448. return -ENOMEM;
  3449. sp->hmac = NULL;
  3450. sk->sk_destruct = sctp_destruct_sock;
  3451. SCTP_DBG_OBJCNT_INC(sock);
  3452. local_bh_disable();
  3453. percpu_counter_inc(&sctp_sockets_allocated);
  3454. sock_prot_inuse_add(net, sk->sk_prot, 1);
  3455. if (net->sctp.default_auto_asconf) {
  3456. list_add_tail(&sp->auto_asconf_list,
  3457. &net->sctp.auto_asconf_splist);
  3458. sp->do_auto_asconf = 1;
  3459. } else
  3460. sp->do_auto_asconf = 0;
  3461. local_bh_enable();
  3462. return 0;
  3463. }
  3464. /* Cleanup any SCTP per socket resources. */
  3465. static void sctp_destroy_sock(struct sock *sk)
  3466. {
  3467. struct sctp_sock *sp;
  3468. pr_debug("%s: sk:%p\n", __func__, sk);
  3469. /* Release our hold on the endpoint. */
  3470. sp = sctp_sk(sk);
  3471. /* This could happen during socket init, thus we bail out
  3472. * early, since the rest of the below is not setup either.
  3473. */
  3474. if (sp->ep == NULL)
  3475. return;
  3476. if (sp->do_auto_asconf) {
  3477. sp->do_auto_asconf = 0;
  3478. list_del(&sp->auto_asconf_list);
  3479. }
  3480. sctp_endpoint_free(sp->ep);
  3481. local_bh_disable();
  3482. percpu_counter_dec(&sctp_sockets_allocated);
  3483. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  3484. local_bh_enable();
  3485. }
  3486. /* Triggered when there are no references on the socket anymore */
  3487. static void sctp_destruct_sock(struct sock *sk)
  3488. {
  3489. struct sctp_sock *sp = sctp_sk(sk);
  3490. /* Free up the HMAC transform. */
  3491. crypto_free_hash(sp->hmac);
  3492. inet_sock_destruct(sk);
  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. pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
  3583. __func__, 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_flags(0);
  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. pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
  3761. 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. pr_debug("%s: failed no transport\n", __func__);
  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. pr_debug("%s: failed no association\n", __func__);
  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. pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
  4921. if (copy_to_user(optval, &sas, len))
  4922. return -EFAULT;
  4923. return 0;
  4924. }
  4925. static int sctp_getsockopt(struct sock *sk, int level, int optname,
  4926. char __user *optval, int __user *optlen)
  4927. {
  4928. int retval = 0;
  4929. int len;
  4930. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  4931. /* I can hardly begin to describe how wrong this is. This is
  4932. * so broken as to be worse than useless. The API draft
  4933. * REALLY is NOT helpful here... I am not convinced that the
  4934. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  4935. * are at all well-founded.
  4936. */
  4937. if (level != SOL_SCTP) {
  4938. struct sctp_af *af = sctp_sk(sk)->pf->af;
  4939. retval = af->getsockopt(sk, level, optname, optval, optlen);
  4940. return retval;
  4941. }
  4942. if (get_user(len, optlen))
  4943. return -EFAULT;
  4944. sctp_lock_sock(sk);
  4945. switch (optname) {
  4946. case SCTP_STATUS:
  4947. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  4948. break;
  4949. case SCTP_DISABLE_FRAGMENTS:
  4950. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  4951. optlen);
  4952. break;
  4953. case SCTP_EVENTS:
  4954. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  4955. break;
  4956. case SCTP_AUTOCLOSE:
  4957. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  4958. break;
  4959. case SCTP_SOCKOPT_PEELOFF:
  4960. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  4961. break;
  4962. case SCTP_PEER_ADDR_PARAMS:
  4963. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  4964. optlen);
  4965. break;
  4966. case SCTP_DELAYED_SACK:
  4967. retval = sctp_getsockopt_delayed_ack(sk, len, optval,
  4968. optlen);
  4969. break;
  4970. case SCTP_INITMSG:
  4971. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  4972. break;
  4973. case SCTP_GET_PEER_ADDRS:
  4974. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  4975. optlen);
  4976. break;
  4977. case SCTP_GET_LOCAL_ADDRS:
  4978. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  4979. optlen);
  4980. break;
  4981. case SCTP_SOCKOPT_CONNECTX3:
  4982. retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
  4983. break;
  4984. case SCTP_DEFAULT_SEND_PARAM:
  4985. retval = sctp_getsockopt_default_send_param(sk, len,
  4986. optval, optlen);
  4987. break;
  4988. case SCTP_PRIMARY_ADDR:
  4989. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  4990. break;
  4991. case SCTP_NODELAY:
  4992. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  4993. break;
  4994. case SCTP_RTOINFO:
  4995. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  4996. break;
  4997. case SCTP_ASSOCINFO:
  4998. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  4999. break;
  5000. case SCTP_I_WANT_MAPPED_V4_ADDR:
  5001. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  5002. break;
  5003. case SCTP_MAXSEG:
  5004. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  5005. break;
  5006. case SCTP_GET_PEER_ADDR_INFO:
  5007. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  5008. optlen);
  5009. break;
  5010. case SCTP_ADAPTATION_LAYER:
  5011. retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
  5012. optlen);
  5013. break;
  5014. case SCTP_CONTEXT:
  5015. retval = sctp_getsockopt_context(sk, len, optval, optlen);
  5016. break;
  5017. case SCTP_FRAGMENT_INTERLEAVE:
  5018. retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
  5019. optlen);
  5020. break;
  5021. case SCTP_PARTIAL_DELIVERY_POINT:
  5022. retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
  5023. optlen);
  5024. break;
  5025. case SCTP_MAX_BURST:
  5026. retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
  5027. break;
  5028. case SCTP_AUTH_KEY:
  5029. case SCTP_AUTH_CHUNK:
  5030. case SCTP_AUTH_DELETE_KEY:
  5031. retval = -EOPNOTSUPP;
  5032. break;
  5033. case SCTP_HMAC_IDENT:
  5034. retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
  5035. break;
  5036. case SCTP_AUTH_ACTIVE_KEY:
  5037. retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
  5038. break;
  5039. case SCTP_PEER_AUTH_CHUNKS:
  5040. retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
  5041. optlen);
  5042. break;
  5043. case SCTP_LOCAL_AUTH_CHUNKS:
  5044. retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
  5045. optlen);
  5046. break;
  5047. case SCTP_GET_ASSOC_NUMBER:
  5048. retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
  5049. break;
  5050. case SCTP_GET_ASSOC_ID_LIST:
  5051. retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
  5052. break;
  5053. case SCTP_AUTO_ASCONF:
  5054. retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
  5055. break;
  5056. case SCTP_PEER_ADDR_THLDS:
  5057. retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
  5058. break;
  5059. case SCTP_GET_ASSOC_STATS:
  5060. retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
  5061. break;
  5062. default:
  5063. retval = -ENOPROTOOPT;
  5064. break;
  5065. }
  5066. sctp_release_sock(sk);
  5067. return retval;
  5068. }
  5069. static void sctp_hash(struct sock *sk)
  5070. {
  5071. /* STUB */
  5072. }
  5073. static void sctp_unhash(struct sock *sk)
  5074. {
  5075. /* STUB */
  5076. }
  5077. /* Check if port is acceptable. Possibly find first available port.
  5078. *
  5079. * The port hash table (contained in the 'global' SCTP protocol storage
  5080. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  5081. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  5082. * list (the list number is the port number hashed out, so as you
  5083. * would expect from a hash function, all the ports in a given list have
  5084. * such a number that hashes out to the same list number; you were
  5085. * expecting that, right?); so each list has a set of ports, with a
  5086. * link to the socket (struct sock) that uses it, the port number and
  5087. * a fastreuse flag (FIXME: NPI ipg).
  5088. */
  5089. static struct sctp_bind_bucket *sctp_bucket_create(
  5090. struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
  5091. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  5092. {
  5093. struct sctp_bind_hashbucket *head; /* hash list */
  5094. struct sctp_bind_bucket *pp;
  5095. unsigned short snum;
  5096. int ret;
  5097. snum = ntohs(addr->v4.sin_port);
  5098. pr_debug("%s: begins, snum:%d\n", __func__, snum);
  5099. sctp_local_bh_disable();
  5100. if (snum == 0) {
  5101. /* Search for an available port. */
  5102. int low, high, remaining, index;
  5103. unsigned int rover;
  5104. inet_get_local_port_range(&low, &high);
  5105. remaining = (high - low) + 1;
  5106. rover = net_random() % remaining + low;
  5107. do {
  5108. rover++;
  5109. if ((rover < low) || (rover > high))
  5110. rover = low;
  5111. if (inet_is_reserved_local_port(rover))
  5112. continue;
  5113. index = sctp_phashfn(sock_net(sk), rover);
  5114. head = &sctp_port_hashtable[index];
  5115. sctp_spin_lock(&head->lock);
  5116. sctp_for_each_hentry(pp, &head->chain)
  5117. if ((pp->port == rover) &&
  5118. net_eq(sock_net(sk), pp->net))
  5119. goto next;
  5120. break;
  5121. next:
  5122. sctp_spin_unlock(&head->lock);
  5123. } while (--remaining > 0);
  5124. /* Exhausted local port range during search? */
  5125. ret = 1;
  5126. if (remaining <= 0)
  5127. goto fail;
  5128. /* OK, here is the one we will use. HEAD (the port
  5129. * hash table list entry) is non-NULL and we hold it's
  5130. * mutex.
  5131. */
  5132. snum = rover;
  5133. } else {
  5134. /* We are given an specific port number; we verify
  5135. * that it is not being used. If it is used, we will
  5136. * exahust the search in the hash list corresponding
  5137. * to the port number (snum) - we detect that with the
  5138. * port iterator, pp being NULL.
  5139. */
  5140. head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
  5141. sctp_spin_lock(&head->lock);
  5142. sctp_for_each_hentry(pp, &head->chain) {
  5143. if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
  5144. goto pp_found;
  5145. }
  5146. }
  5147. pp = NULL;
  5148. goto pp_not_found;
  5149. pp_found:
  5150. if (!hlist_empty(&pp->owner)) {
  5151. /* We had a port hash table hit - there is an
  5152. * available port (pp != NULL) and it is being
  5153. * used by other socket (pp->owner not empty); that other
  5154. * socket is going to be sk2.
  5155. */
  5156. int reuse = sk->sk_reuse;
  5157. struct sock *sk2;
  5158. pr_debug("%s: found a possible match\n", __func__);
  5159. if (pp->fastreuse && sk->sk_reuse &&
  5160. sk->sk_state != SCTP_SS_LISTENING)
  5161. goto success;
  5162. /* Run through the list of sockets bound to the port
  5163. * (pp->port) [via the pointers bind_next and
  5164. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  5165. * we get the endpoint they describe and run through
  5166. * the endpoint's list of IP (v4 or v6) addresses,
  5167. * comparing each of the addresses with the address of
  5168. * the socket sk. If we find a match, then that means
  5169. * that this port/socket (sk) combination are already
  5170. * in an endpoint.
  5171. */
  5172. sk_for_each_bound(sk2, &pp->owner) {
  5173. struct sctp_endpoint *ep2;
  5174. ep2 = sctp_sk(sk2)->ep;
  5175. if (sk == sk2 ||
  5176. (reuse && sk2->sk_reuse &&
  5177. sk2->sk_state != SCTP_SS_LISTENING))
  5178. continue;
  5179. if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
  5180. sctp_sk(sk2), sctp_sk(sk))) {
  5181. ret = (long)sk2;
  5182. goto fail_unlock;
  5183. }
  5184. }
  5185. pr_debug("%s: found a match\n", __func__);
  5186. }
  5187. pp_not_found:
  5188. /* If there was a hash table miss, create a new port. */
  5189. ret = 1;
  5190. if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
  5191. goto fail_unlock;
  5192. /* In either case (hit or miss), make sure fastreuse is 1 only
  5193. * if sk->sk_reuse is too (that is, if the caller requested
  5194. * SO_REUSEADDR on this socket -sk-).
  5195. */
  5196. if (hlist_empty(&pp->owner)) {
  5197. if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
  5198. pp->fastreuse = 1;
  5199. else
  5200. pp->fastreuse = 0;
  5201. } else if (pp->fastreuse &&
  5202. (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
  5203. pp->fastreuse = 0;
  5204. /* We are set, so fill up all the data in the hash table
  5205. * entry, tie the socket list information with the rest of the
  5206. * sockets FIXME: Blurry, NPI (ipg).
  5207. */
  5208. success:
  5209. if (!sctp_sk(sk)->bind_hash) {
  5210. inet_sk(sk)->inet_num = snum;
  5211. sk_add_bind_node(sk, &pp->owner);
  5212. sctp_sk(sk)->bind_hash = pp;
  5213. }
  5214. ret = 0;
  5215. fail_unlock:
  5216. sctp_spin_unlock(&head->lock);
  5217. fail:
  5218. sctp_local_bh_enable();
  5219. return ret;
  5220. }
  5221. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  5222. * port is requested.
  5223. */
  5224. static int sctp_get_port(struct sock *sk, unsigned short snum)
  5225. {
  5226. union sctp_addr addr;
  5227. struct sctp_af *af = sctp_sk(sk)->pf->af;
  5228. /* Set up a dummy address struct from the sk. */
  5229. af->from_sk(&addr, sk);
  5230. addr.v4.sin_port = htons(snum);
  5231. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  5232. return !!sctp_get_port_local(sk, &addr);
  5233. }
  5234. /*
  5235. * Move a socket to LISTENING state.
  5236. */
  5237. static int sctp_listen_start(struct sock *sk, int backlog)
  5238. {
  5239. struct sctp_sock *sp = sctp_sk(sk);
  5240. struct sctp_endpoint *ep = sp->ep;
  5241. struct crypto_hash *tfm = NULL;
  5242. char alg[32];
  5243. /* Allocate HMAC for generating cookie. */
  5244. if (!sp->hmac && sp->sctp_hmac_alg) {
  5245. sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
  5246. tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
  5247. if (IS_ERR(tfm)) {
  5248. net_info_ratelimited("failed to load transform for %s: %ld\n",
  5249. sp->sctp_hmac_alg, PTR_ERR(tfm));
  5250. return -ENOSYS;
  5251. }
  5252. sctp_sk(sk)->hmac = tfm;
  5253. }
  5254. /*
  5255. * If a bind() or sctp_bindx() is not called prior to a listen()
  5256. * call that allows new associations to be accepted, the system
  5257. * picks an ephemeral port and will choose an address set equivalent
  5258. * to binding with a wildcard address.
  5259. *
  5260. * This is not currently spelled out in the SCTP sockets
  5261. * extensions draft, but follows the practice as seen in TCP
  5262. * sockets.
  5263. *
  5264. */
  5265. sk->sk_state = SCTP_SS_LISTENING;
  5266. if (!ep->base.bind_addr.port) {
  5267. if (sctp_autobind(sk))
  5268. return -EAGAIN;
  5269. } else {
  5270. if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
  5271. sk->sk_state = SCTP_SS_CLOSED;
  5272. return -EADDRINUSE;
  5273. }
  5274. }
  5275. sk->sk_max_ack_backlog = backlog;
  5276. sctp_hash_endpoint(ep);
  5277. return 0;
  5278. }
  5279. /*
  5280. * 4.1.3 / 5.1.3 listen()
  5281. *
  5282. * By default, new associations are not accepted for UDP style sockets.
  5283. * An application uses listen() to mark a socket as being able to
  5284. * accept new associations.
  5285. *
  5286. * On TCP style sockets, applications use listen() to ready the SCTP
  5287. * endpoint for accepting inbound associations.
  5288. *
  5289. * On both types of endpoints a backlog of '0' disables listening.
  5290. *
  5291. * Move a socket to LISTENING state.
  5292. */
  5293. int sctp_inet_listen(struct socket *sock, int backlog)
  5294. {
  5295. struct sock *sk = sock->sk;
  5296. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5297. int err = -EINVAL;
  5298. if (unlikely(backlog < 0))
  5299. return err;
  5300. sctp_lock_sock(sk);
  5301. /* Peeled-off sockets are not allowed to listen(). */
  5302. if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
  5303. goto out;
  5304. if (sock->state != SS_UNCONNECTED)
  5305. goto out;
  5306. /* If backlog is zero, disable listening. */
  5307. if (!backlog) {
  5308. if (sctp_sstate(sk, CLOSED))
  5309. goto out;
  5310. err = 0;
  5311. sctp_unhash_endpoint(ep);
  5312. sk->sk_state = SCTP_SS_CLOSED;
  5313. if (sk->sk_reuse)
  5314. sctp_sk(sk)->bind_hash->fastreuse = 1;
  5315. goto out;
  5316. }
  5317. /* If we are already listening, just update the backlog */
  5318. if (sctp_sstate(sk, LISTENING))
  5319. sk->sk_max_ack_backlog = backlog;
  5320. else {
  5321. err = sctp_listen_start(sk, backlog);
  5322. if (err)
  5323. goto out;
  5324. }
  5325. err = 0;
  5326. out:
  5327. sctp_release_sock(sk);
  5328. return err;
  5329. }
  5330. /*
  5331. * This function is done by modeling the current datagram_poll() and the
  5332. * tcp_poll(). Note that, based on these implementations, we don't
  5333. * lock the socket in this function, even though it seems that,
  5334. * ideally, locking or some other mechanisms can be used to ensure
  5335. * the integrity of the counters (sndbuf and wmem_alloc) used
  5336. * in this place. We assume that we don't need locks either until proven
  5337. * otherwise.
  5338. *
  5339. * Another thing to note is that we include the Async I/O support
  5340. * here, again, by modeling the current TCP/UDP code. We don't have
  5341. * a good way to test with it yet.
  5342. */
  5343. unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  5344. {
  5345. struct sock *sk = sock->sk;
  5346. struct sctp_sock *sp = sctp_sk(sk);
  5347. unsigned int mask;
  5348. poll_wait(file, sk_sleep(sk), wait);
  5349. /* A TCP-style listening socket becomes readable when the accept queue
  5350. * is not empty.
  5351. */
  5352. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  5353. return (!list_empty(&sp->ep->asocs)) ?
  5354. (POLLIN | POLLRDNORM) : 0;
  5355. mask = 0;
  5356. /* Is there any exceptional events? */
  5357. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  5358. mask |= POLLERR |
  5359. sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0;
  5360. if (sk->sk_shutdown & RCV_SHUTDOWN)
  5361. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  5362. if (sk->sk_shutdown == SHUTDOWN_MASK)
  5363. mask |= POLLHUP;
  5364. /* Is it readable? Reconsider this code with TCP-style support. */
  5365. if (!skb_queue_empty(&sk->sk_receive_queue))
  5366. mask |= POLLIN | POLLRDNORM;
  5367. /* The association is either gone or not ready. */
  5368. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  5369. return mask;
  5370. /* Is it writable? */
  5371. if (sctp_writeable(sk)) {
  5372. mask |= POLLOUT | POLLWRNORM;
  5373. } else {
  5374. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  5375. /*
  5376. * Since the socket is not locked, the buffer
  5377. * might be made available after the writeable check and
  5378. * before the bit is set. This could cause a lost I/O
  5379. * signal. tcp_poll() has a race breaker for this race
  5380. * condition. Based on their implementation, we put
  5381. * in the following code to cover it as well.
  5382. */
  5383. if (sctp_writeable(sk))
  5384. mask |= POLLOUT | POLLWRNORM;
  5385. }
  5386. return mask;
  5387. }
  5388. /********************************************************************
  5389. * 2nd Level Abstractions
  5390. ********************************************************************/
  5391. static struct sctp_bind_bucket *sctp_bucket_create(
  5392. struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
  5393. {
  5394. struct sctp_bind_bucket *pp;
  5395. pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
  5396. if (pp) {
  5397. SCTP_DBG_OBJCNT_INC(bind_bucket);
  5398. pp->port = snum;
  5399. pp->fastreuse = 0;
  5400. INIT_HLIST_HEAD(&pp->owner);
  5401. pp->net = net;
  5402. hlist_add_head(&pp->node, &head->chain);
  5403. }
  5404. return pp;
  5405. }
  5406. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  5407. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  5408. {
  5409. if (pp && hlist_empty(&pp->owner)) {
  5410. __hlist_del(&pp->node);
  5411. kmem_cache_free(sctp_bucket_cachep, pp);
  5412. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  5413. }
  5414. }
  5415. /* Release this socket's reference to a local port. */
  5416. static inline void __sctp_put_port(struct sock *sk)
  5417. {
  5418. struct sctp_bind_hashbucket *head =
  5419. &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
  5420. inet_sk(sk)->inet_num)];
  5421. struct sctp_bind_bucket *pp;
  5422. sctp_spin_lock(&head->lock);
  5423. pp = sctp_sk(sk)->bind_hash;
  5424. __sk_del_bind_node(sk);
  5425. sctp_sk(sk)->bind_hash = NULL;
  5426. inet_sk(sk)->inet_num = 0;
  5427. sctp_bucket_destroy(pp);
  5428. sctp_spin_unlock(&head->lock);
  5429. }
  5430. void sctp_put_port(struct sock *sk)
  5431. {
  5432. sctp_local_bh_disable();
  5433. __sctp_put_port(sk);
  5434. sctp_local_bh_enable();
  5435. }
  5436. /*
  5437. * The system picks an ephemeral port and choose an address set equivalent
  5438. * to binding with a wildcard address.
  5439. * One of those addresses will be the primary address for the association.
  5440. * This automatically enables the multihoming capability of SCTP.
  5441. */
  5442. static int sctp_autobind(struct sock *sk)
  5443. {
  5444. union sctp_addr autoaddr;
  5445. struct sctp_af *af;
  5446. __be16 port;
  5447. /* Initialize a local sockaddr structure to INADDR_ANY. */
  5448. af = sctp_sk(sk)->pf->af;
  5449. port = htons(inet_sk(sk)->inet_num);
  5450. af->inaddr_any(&autoaddr, port);
  5451. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  5452. }
  5453. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  5454. *
  5455. * From RFC 2292
  5456. * 4.2 The cmsghdr Structure *
  5457. *
  5458. * When ancillary data is sent or received, any number of ancillary data
  5459. * objects can be specified by the msg_control and msg_controllen members of
  5460. * the msghdr structure, because each object is preceded by
  5461. * a cmsghdr structure defining the object's length (the cmsg_len member).
  5462. * Historically Berkeley-derived implementations have passed only one object
  5463. * at a time, but this API allows multiple objects to be
  5464. * passed in a single call to sendmsg() or recvmsg(). The following example
  5465. * shows two ancillary data objects in a control buffer.
  5466. *
  5467. * |<--------------------------- msg_controllen -------------------------->|
  5468. * | |
  5469. *
  5470. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  5471. *
  5472. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  5473. * | | |
  5474. *
  5475. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  5476. *
  5477. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  5478. * | | | | |
  5479. *
  5480. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  5481. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  5482. *
  5483. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  5484. *
  5485. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  5486. * ^
  5487. * |
  5488. *
  5489. * msg_control
  5490. * points here
  5491. */
  5492. static int sctp_msghdr_parse(const struct msghdr *msg, sctp_cmsgs_t *cmsgs)
  5493. {
  5494. struct cmsghdr *cmsg;
  5495. struct msghdr *my_msg = (struct msghdr *)msg;
  5496. for (cmsg = CMSG_FIRSTHDR(msg);
  5497. cmsg != NULL;
  5498. cmsg = CMSG_NXTHDR(my_msg, cmsg)) {
  5499. if (!CMSG_OK(my_msg, cmsg))
  5500. return -EINVAL;
  5501. /* Should we parse this header or ignore? */
  5502. if (cmsg->cmsg_level != IPPROTO_SCTP)
  5503. continue;
  5504. /* Strictly check lengths following example in SCM code. */
  5505. switch (cmsg->cmsg_type) {
  5506. case SCTP_INIT:
  5507. /* SCTP Socket API Extension
  5508. * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
  5509. *
  5510. * This cmsghdr structure provides information for
  5511. * initializing new SCTP associations with sendmsg().
  5512. * The SCTP_INITMSG socket option uses this same data
  5513. * structure. This structure is not used for
  5514. * recvmsg().
  5515. *
  5516. * cmsg_level cmsg_type cmsg_data[]
  5517. * ------------ ------------ ----------------------
  5518. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  5519. */
  5520. if (cmsg->cmsg_len !=
  5521. CMSG_LEN(sizeof(struct sctp_initmsg)))
  5522. return -EINVAL;
  5523. cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
  5524. break;
  5525. case SCTP_SNDRCV:
  5526. /* SCTP Socket API Extension
  5527. * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
  5528. *
  5529. * This cmsghdr structure specifies SCTP options for
  5530. * sendmsg() and describes SCTP header information
  5531. * about a received message through recvmsg().
  5532. *
  5533. * cmsg_level cmsg_type cmsg_data[]
  5534. * ------------ ------------ ----------------------
  5535. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  5536. */
  5537. if (cmsg->cmsg_len !=
  5538. CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  5539. return -EINVAL;
  5540. cmsgs->info =
  5541. (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  5542. /* Minimally, validate the sinfo_flags. */
  5543. if (cmsgs->info->sinfo_flags &
  5544. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  5545. SCTP_ABORT | SCTP_EOF))
  5546. return -EINVAL;
  5547. break;
  5548. default:
  5549. return -EINVAL;
  5550. }
  5551. }
  5552. return 0;
  5553. }
  5554. /*
  5555. * Wait for a packet..
  5556. * Note: This function is the same function as in core/datagram.c
  5557. * with a few modifications to make lksctp work.
  5558. */
  5559. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
  5560. {
  5561. int error;
  5562. DEFINE_WAIT(wait);
  5563. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  5564. /* Socket errors? */
  5565. error = sock_error(sk);
  5566. if (error)
  5567. goto out;
  5568. if (!skb_queue_empty(&sk->sk_receive_queue))
  5569. goto ready;
  5570. /* Socket shut down? */
  5571. if (sk->sk_shutdown & RCV_SHUTDOWN)
  5572. goto out;
  5573. /* Sequenced packets can come disconnected. If so we report the
  5574. * problem.
  5575. */
  5576. error = -ENOTCONN;
  5577. /* Is there a good reason to think that we may receive some data? */
  5578. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  5579. goto out;
  5580. /* Handle signals. */
  5581. if (signal_pending(current))
  5582. goto interrupted;
  5583. /* Let another process have a go. Since we are going to sleep
  5584. * anyway. Note: This may cause odd behaviors if the message
  5585. * does not fit in the user's buffer, but this seems to be the
  5586. * only way to honor MSG_DONTWAIT realistically.
  5587. */
  5588. sctp_release_sock(sk);
  5589. *timeo_p = schedule_timeout(*timeo_p);
  5590. sctp_lock_sock(sk);
  5591. ready:
  5592. finish_wait(sk_sleep(sk), &wait);
  5593. return 0;
  5594. interrupted:
  5595. error = sock_intr_errno(*timeo_p);
  5596. out:
  5597. finish_wait(sk_sleep(sk), &wait);
  5598. *err = error;
  5599. return error;
  5600. }
  5601. /* Receive a datagram.
  5602. * Note: This is pretty much the same routine as in core/datagram.c
  5603. * with a few changes to make lksctp work.
  5604. */
  5605. static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  5606. int noblock, int *err)
  5607. {
  5608. int error;
  5609. struct sk_buff *skb;
  5610. long timeo;
  5611. timeo = sock_rcvtimeo(sk, noblock);
  5612. pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
  5613. MAX_SCHEDULE_TIMEOUT);
  5614. do {
  5615. /* Again only user level code calls this function,
  5616. * so nothing interrupt level
  5617. * will suddenly eat the receive_queue.
  5618. *
  5619. * Look at current nfs client by the way...
  5620. * However, this function was correct in any case. 8)
  5621. */
  5622. if (flags & MSG_PEEK) {
  5623. spin_lock_bh(&sk->sk_receive_queue.lock);
  5624. skb = skb_peek(&sk->sk_receive_queue);
  5625. if (skb)
  5626. atomic_inc(&skb->users);
  5627. spin_unlock_bh(&sk->sk_receive_queue.lock);
  5628. } else {
  5629. skb = skb_dequeue(&sk->sk_receive_queue);
  5630. }
  5631. if (skb)
  5632. return skb;
  5633. /* Caller is allowed not to check sk->sk_err before calling. */
  5634. error = sock_error(sk);
  5635. if (error)
  5636. goto no_packet;
  5637. if (sk->sk_shutdown & RCV_SHUTDOWN)
  5638. break;
  5639. /* User doesn't want to wait. */
  5640. error = -EAGAIN;
  5641. if (!timeo)
  5642. goto no_packet;
  5643. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  5644. return NULL;
  5645. no_packet:
  5646. *err = error;
  5647. return NULL;
  5648. }
  5649. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  5650. static void __sctp_write_space(struct sctp_association *asoc)
  5651. {
  5652. struct sock *sk = asoc->base.sk;
  5653. struct socket *sock = sk->sk_socket;
  5654. if ((sctp_wspace(asoc) > 0) && sock) {
  5655. if (waitqueue_active(&asoc->wait))
  5656. wake_up_interruptible(&asoc->wait);
  5657. if (sctp_writeable(sk)) {
  5658. wait_queue_head_t *wq = sk_sleep(sk);
  5659. if (wq && waitqueue_active(wq))
  5660. wake_up_interruptible(wq);
  5661. /* Note that we try to include the Async I/O support
  5662. * here by modeling from the current TCP/UDP code.
  5663. * We have not tested with it yet.
  5664. */
  5665. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  5666. sock_wake_async(sock,
  5667. SOCK_WAKE_SPACE, POLL_OUT);
  5668. }
  5669. }
  5670. }
  5671. /* Do accounting for the sndbuf space.
  5672. * Decrement the used sndbuf space of the corresponding association by the
  5673. * data size which was just transmitted(freed).
  5674. */
  5675. static void sctp_wfree(struct sk_buff *skb)
  5676. {
  5677. struct sctp_association *asoc;
  5678. struct sctp_chunk *chunk;
  5679. struct sock *sk;
  5680. /* Get the saved chunk pointer. */
  5681. chunk = *((struct sctp_chunk **)(skb->cb));
  5682. asoc = chunk->asoc;
  5683. sk = asoc->base.sk;
  5684. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  5685. sizeof(struct sk_buff) +
  5686. sizeof(struct sctp_chunk);
  5687. atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  5688. /*
  5689. * This undoes what is done via sctp_set_owner_w and sk_mem_charge
  5690. */
  5691. sk->sk_wmem_queued -= skb->truesize;
  5692. sk_mem_uncharge(sk, skb->truesize);
  5693. sock_wfree(skb);
  5694. __sctp_write_space(asoc);
  5695. sctp_association_put(asoc);
  5696. }
  5697. /* Do accounting for the receive space on the socket.
  5698. * Accounting for the association is done in ulpevent.c
  5699. * We set this as a destructor for the cloned data skbs so that
  5700. * accounting is done at the correct time.
  5701. */
  5702. void sctp_sock_rfree(struct sk_buff *skb)
  5703. {
  5704. struct sock *sk = skb->sk;
  5705. struct sctp_ulpevent *event = sctp_skb2event(skb);
  5706. atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
  5707. /*
  5708. * Mimic the behavior of sock_rfree
  5709. */
  5710. sk_mem_uncharge(sk, event->rmem_len);
  5711. }
  5712. /* Helper function to wait for space in the sndbuf. */
  5713. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  5714. size_t msg_len)
  5715. {
  5716. struct sock *sk = asoc->base.sk;
  5717. int err = 0;
  5718. long current_timeo = *timeo_p;
  5719. DEFINE_WAIT(wait);
  5720. pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
  5721. *timeo_p, msg_len);
  5722. /* Increment the association's refcnt. */
  5723. sctp_association_hold(asoc);
  5724. /* Wait on the association specific sndbuf space. */
  5725. for (;;) {
  5726. prepare_to_wait_exclusive(&asoc->wait, &wait,
  5727. TASK_INTERRUPTIBLE);
  5728. if (!*timeo_p)
  5729. goto do_nonblock;
  5730. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  5731. asoc->base.dead)
  5732. goto do_error;
  5733. if (signal_pending(current))
  5734. goto do_interrupted;
  5735. if (msg_len <= sctp_wspace(asoc))
  5736. break;
  5737. /* Let another process have a go. Since we are going
  5738. * to sleep anyway.
  5739. */
  5740. sctp_release_sock(sk);
  5741. current_timeo = schedule_timeout(current_timeo);
  5742. BUG_ON(sk != asoc->base.sk);
  5743. sctp_lock_sock(sk);
  5744. *timeo_p = current_timeo;
  5745. }
  5746. out:
  5747. finish_wait(&asoc->wait, &wait);
  5748. /* Release the association's refcnt. */
  5749. sctp_association_put(asoc);
  5750. return err;
  5751. do_error:
  5752. err = -EPIPE;
  5753. goto out;
  5754. do_interrupted:
  5755. err = sock_intr_errno(*timeo_p);
  5756. goto out;
  5757. do_nonblock:
  5758. err = -EAGAIN;
  5759. goto out;
  5760. }
  5761. void sctp_data_ready(struct sock *sk, int len)
  5762. {
  5763. struct socket_wq *wq;
  5764. rcu_read_lock();
  5765. wq = rcu_dereference(sk->sk_wq);
  5766. if (wq_has_sleeper(wq))
  5767. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  5768. POLLRDNORM | POLLRDBAND);
  5769. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  5770. rcu_read_unlock();
  5771. }
  5772. /* If socket sndbuf has changed, wake up all per association waiters. */
  5773. void sctp_write_space(struct sock *sk)
  5774. {
  5775. struct sctp_association *asoc;
  5776. /* Wake up the tasks in each wait queue. */
  5777. list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
  5778. __sctp_write_space(asoc);
  5779. }
  5780. }
  5781. /* Is there any sndbuf space available on the socket?
  5782. *
  5783. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  5784. * associations on the same socket. For a UDP-style socket with
  5785. * multiple associations, it is possible for it to be "unwriteable"
  5786. * prematurely. I assume that this is acceptable because
  5787. * a premature "unwriteable" is better than an accidental "writeable" which
  5788. * would cause an unwanted block under certain circumstances. For the 1-1
  5789. * UDP-style sockets or TCP-style sockets, this code should work.
  5790. * - Daisy
  5791. */
  5792. static int sctp_writeable(struct sock *sk)
  5793. {
  5794. int amt = 0;
  5795. amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  5796. if (amt < 0)
  5797. amt = 0;
  5798. return amt;
  5799. }
  5800. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  5801. * returns immediately with EINPROGRESS.
  5802. */
  5803. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  5804. {
  5805. struct sock *sk = asoc->base.sk;
  5806. int err = 0;
  5807. long current_timeo = *timeo_p;
  5808. DEFINE_WAIT(wait);
  5809. pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
  5810. /* Increment the association's refcnt. */
  5811. sctp_association_hold(asoc);
  5812. for (;;) {
  5813. prepare_to_wait_exclusive(&asoc->wait, &wait,
  5814. TASK_INTERRUPTIBLE);
  5815. if (!*timeo_p)
  5816. goto do_nonblock;
  5817. if (sk->sk_shutdown & RCV_SHUTDOWN)
  5818. break;
  5819. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  5820. asoc->base.dead)
  5821. goto do_error;
  5822. if (signal_pending(current))
  5823. goto do_interrupted;
  5824. if (sctp_state(asoc, ESTABLISHED))
  5825. break;
  5826. /* Let another process have a go. Since we are going
  5827. * to sleep anyway.
  5828. */
  5829. sctp_release_sock(sk);
  5830. current_timeo = schedule_timeout(current_timeo);
  5831. sctp_lock_sock(sk);
  5832. *timeo_p = current_timeo;
  5833. }
  5834. out:
  5835. finish_wait(&asoc->wait, &wait);
  5836. /* Release the association's refcnt. */
  5837. sctp_association_put(asoc);
  5838. return err;
  5839. do_error:
  5840. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  5841. err = -ETIMEDOUT;
  5842. else
  5843. err = -ECONNREFUSED;
  5844. goto out;
  5845. do_interrupted:
  5846. err = sock_intr_errno(*timeo_p);
  5847. goto out;
  5848. do_nonblock:
  5849. err = -EINPROGRESS;
  5850. goto out;
  5851. }
  5852. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  5853. {
  5854. struct sctp_endpoint *ep;
  5855. int err = 0;
  5856. DEFINE_WAIT(wait);
  5857. ep = sctp_sk(sk)->ep;
  5858. for (;;) {
  5859. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  5860. TASK_INTERRUPTIBLE);
  5861. if (list_empty(&ep->asocs)) {
  5862. sctp_release_sock(sk);
  5863. timeo = schedule_timeout(timeo);
  5864. sctp_lock_sock(sk);
  5865. }
  5866. err = -EINVAL;
  5867. if (!sctp_sstate(sk, LISTENING))
  5868. break;
  5869. err = 0;
  5870. if (!list_empty(&ep->asocs))
  5871. break;
  5872. err = sock_intr_errno(timeo);
  5873. if (signal_pending(current))
  5874. break;
  5875. err = -EAGAIN;
  5876. if (!timeo)
  5877. break;
  5878. }
  5879. finish_wait(sk_sleep(sk), &wait);
  5880. return err;
  5881. }
  5882. static void sctp_wait_for_close(struct sock *sk, long timeout)
  5883. {
  5884. DEFINE_WAIT(wait);
  5885. do {
  5886. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  5887. if (list_empty(&sctp_sk(sk)->ep->asocs))
  5888. break;
  5889. sctp_release_sock(sk);
  5890. timeout = schedule_timeout(timeout);
  5891. sctp_lock_sock(sk);
  5892. } while (!signal_pending(current) && timeout);
  5893. finish_wait(sk_sleep(sk), &wait);
  5894. }
  5895. static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
  5896. {
  5897. struct sk_buff *frag;
  5898. if (!skb->data_len)
  5899. goto done;
  5900. /* Don't forget the fragments. */
  5901. skb_walk_frags(skb, frag)
  5902. sctp_skb_set_owner_r_frag(frag, sk);
  5903. done:
  5904. sctp_skb_set_owner_r(skb, sk);
  5905. }
  5906. void sctp_copy_sock(struct sock *newsk, struct sock *sk,
  5907. struct sctp_association *asoc)
  5908. {
  5909. struct inet_sock *inet = inet_sk(sk);
  5910. struct inet_sock *newinet;
  5911. newsk->sk_type = sk->sk_type;
  5912. newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
  5913. newsk->sk_flags = sk->sk_flags;
  5914. newsk->sk_no_check = sk->sk_no_check;
  5915. newsk->sk_reuse = sk->sk_reuse;
  5916. newsk->sk_shutdown = sk->sk_shutdown;
  5917. newsk->sk_destruct = sctp_destruct_sock;
  5918. newsk->sk_family = sk->sk_family;
  5919. newsk->sk_protocol = IPPROTO_SCTP;
  5920. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  5921. newsk->sk_sndbuf = sk->sk_sndbuf;
  5922. newsk->sk_rcvbuf = sk->sk_rcvbuf;
  5923. newsk->sk_lingertime = sk->sk_lingertime;
  5924. newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
  5925. newsk->sk_sndtimeo = sk->sk_sndtimeo;
  5926. newinet = inet_sk(newsk);
  5927. /* Initialize sk's sport, dport, rcv_saddr and daddr for
  5928. * getsockname() and getpeername()
  5929. */
  5930. newinet->inet_sport = inet->inet_sport;
  5931. newinet->inet_saddr = inet->inet_saddr;
  5932. newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
  5933. newinet->inet_dport = htons(asoc->peer.port);
  5934. newinet->pmtudisc = inet->pmtudisc;
  5935. newinet->inet_id = asoc->next_tsn ^ jiffies;
  5936. newinet->uc_ttl = inet->uc_ttl;
  5937. newinet->mc_loop = 1;
  5938. newinet->mc_ttl = 1;
  5939. newinet->mc_index = 0;
  5940. newinet->mc_list = NULL;
  5941. }
  5942. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  5943. * and its messages to the newsk.
  5944. */
  5945. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  5946. struct sctp_association *assoc,
  5947. sctp_socket_type_t type)
  5948. {
  5949. struct sctp_sock *oldsp = sctp_sk(oldsk);
  5950. struct sctp_sock *newsp = sctp_sk(newsk);
  5951. struct sctp_bind_bucket *pp; /* hash list port iterator */
  5952. struct sctp_endpoint *newep = newsp->ep;
  5953. struct sk_buff *skb, *tmp;
  5954. struct sctp_ulpevent *event;
  5955. struct sctp_bind_hashbucket *head;
  5956. struct list_head tmplist;
  5957. /* Migrate socket buffer sizes and all the socket level options to the
  5958. * new socket.
  5959. */
  5960. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  5961. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  5962. /* Brute force copy old sctp opt. */
  5963. if (oldsp->do_auto_asconf) {
  5964. memcpy(&tmplist, &newsp->auto_asconf_list, sizeof(tmplist));
  5965. inet_sk_copy_descendant(newsk, oldsk);
  5966. memcpy(&newsp->auto_asconf_list, &tmplist, sizeof(tmplist));
  5967. } else
  5968. inet_sk_copy_descendant(newsk, oldsk);
  5969. /* Restore the ep value that was overwritten with the above structure
  5970. * copy.
  5971. */
  5972. newsp->ep = newep;
  5973. newsp->hmac = NULL;
  5974. /* Hook this new socket in to the bind_hash list. */
  5975. head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
  5976. inet_sk(oldsk)->inet_num)];
  5977. sctp_local_bh_disable();
  5978. sctp_spin_lock(&head->lock);
  5979. pp = sctp_sk(oldsk)->bind_hash;
  5980. sk_add_bind_node(newsk, &pp->owner);
  5981. sctp_sk(newsk)->bind_hash = pp;
  5982. inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
  5983. sctp_spin_unlock(&head->lock);
  5984. sctp_local_bh_enable();
  5985. /* Copy the bind_addr list from the original endpoint to the new
  5986. * endpoint so that we can handle restarts properly
  5987. */
  5988. sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
  5989. &oldsp->ep->base.bind_addr, GFP_KERNEL);
  5990. /* Move any messages in the old socket's receive queue that are for the
  5991. * peeled off association to the new socket's receive queue.
  5992. */
  5993. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  5994. event = sctp_skb2event(skb);
  5995. if (event->asoc == assoc) {
  5996. __skb_unlink(skb, &oldsk->sk_receive_queue);
  5997. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  5998. sctp_skb_set_owner_r_frag(skb, newsk);
  5999. }
  6000. }
  6001. /* Clean up any messages pending delivery due to partial
  6002. * delivery. Three cases:
  6003. * 1) No partial deliver; no work.
  6004. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  6005. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  6006. */
  6007. skb_queue_head_init(&newsp->pd_lobby);
  6008. atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
  6009. if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
  6010. struct sk_buff_head *queue;
  6011. /* Decide which queue to move pd_lobby skbs to. */
  6012. if (assoc->ulpq.pd_mode) {
  6013. queue = &newsp->pd_lobby;
  6014. } else
  6015. queue = &newsk->sk_receive_queue;
  6016. /* Walk through the pd_lobby, looking for skbs that
  6017. * need moved to the new socket.
  6018. */
  6019. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  6020. event = sctp_skb2event(skb);
  6021. if (event->asoc == assoc) {
  6022. __skb_unlink(skb, &oldsp->pd_lobby);
  6023. __skb_queue_tail(queue, skb);
  6024. sctp_skb_set_owner_r_frag(skb, newsk);
  6025. }
  6026. }
  6027. /* Clear up any skbs waiting for the partial
  6028. * delivery to finish.
  6029. */
  6030. if (assoc->ulpq.pd_mode)
  6031. sctp_clear_pd(oldsk, NULL);
  6032. }
  6033. sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
  6034. sctp_skb_set_owner_r_frag(skb, newsk);
  6035. sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
  6036. sctp_skb_set_owner_r_frag(skb, newsk);
  6037. /* Set the type of socket to indicate that it is peeled off from the
  6038. * original UDP-style socket or created with the accept() call on a
  6039. * TCP-style socket..
  6040. */
  6041. newsp->type = type;
  6042. /* Mark the new socket "in-use" by the user so that any packets
  6043. * that may arrive on the association after we've moved it are
  6044. * queued to the backlog. This prevents a potential race between
  6045. * backlog processing on the old socket and new-packet processing
  6046. * on the new socket.
  6047. *
  6048. * The caller has just allocated newsk so we can guarantee that other
  6049. * paths won't try to lock it and then oldsk.
  6050. */
  6051. lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
  6052. sctp_assoc_migrate(assoc, newsk);
  6053. /* If the association on the newsk is already closed before accept()
  6054. * is called, set RCV_SHUTDOWN flag.
  6055. */
  6056. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
  6057. newsk->sk_shutdown |= RCV_SHUTDOWN;
  6058. newsk->sk_state = SCTP_SS_ESTABLISHED;
  6059. sctp_release_sock(newsk);
  6060. }
  6061. /* This proto struct describes the ULP interface for SCTP. */
  6062. struct proto sctp_prot = {
  6063. .name = "SCTP",
  6064. .owner = THIS_MODULE,
  6065. .close = sctp_close,
  6066. .connect = sctp_connect,
  6067. .disconnect = sctp_disconnect,
  6068. .accept = sctp_accept,
  6069. .ioctl = sctp_ioctl,
  6070. .init = sctp_init_sock,
  6071. .destroy = sctp_destroy_sock,
  6072. .shutdown = sctp_shutdown,
  6073. .setsockopt = sctp_setsockopt,
  6074. .getsockopt = sctp_getsockopt,
  6075. .sendmsg = sctp_sendmsg,
  6076. .recvmsg = sctp_recvmsg,
  6077. .bind = sctp_bind,
  6078. .backlog_rcv = sctp_backlog_rcv,
  6079. .hash = sctp_hash,
  6080. .unhash = sctp_unhash,
  6081. .get_port = sctp_get_port,
  6082. .obj_size = sizeof(struct sctp_sock),
  6083. .sysctl_mem = sysctl_sctp_mem,
  6084. .sysctl_rmem = sysctl_sctp_rmem,
  6085. .sysctl_wmem = sysctl_sctp_wmem,
  6086. .memory_pressure = &sctp_memory_pressure,
  6087. .enter_memory_pressure = sctp_enter_memory_pressure,
  6088. .memory_allocated = &sctp_memory_allocated,
  6089. .sockets_allocated = &sctp_sockets_allocated,
  6090. };
  6091. #if IS_ENABLED(CONFIG_IPV6)
  6092. struct proto sctpv6_prot = {
  6093. .name = "SCTPv6",
  6094. .owner = THIS_MODULE,
  6095. .close = sctp_close,
  6096. .connect = sctp_connect,
  6097. .disconnect = sctp_disconnect,
  6098. .accept = sctp_accept,
  6099. .ioctl = sctp_ioctl,
  6100. .init = sctp_init_sock,
  6101. .destroy = sctp_destroy_sock,
  6102. .shutdown = sctp_shutdown,
  6103. .setsockopt = sctp_setsockopt,
  6104. .getsockopt = sctp_getsockopt,
  6105. .sendmsg = sctp_sendmsg,
  6106. .recvmsg = sctp_recvmsg,
  6107. .bind = sctp_bind,
  6108. .backlog_rcv = sctp_backlog_rcv,
  6109. .hash = sctp_hash,
  6110. .unhash = sctp_unhash,
  6111. .get_port = sctp_get_port,
  6112. .obj_size = sizeof(struct sctp6_sock),
  6113. .sysctl_mem = sysctl_sctp_mem,
  6114. .sysctl_rmem = sysctl_sctp_rmem,
  6115. .sysctl_wmem = sysctl_sctp_wmem,
  6116. .memory_pressure = &sctp_memory_pressure,
  6117. .enter_memory_pressure = sctp_enter_memory_pressure,
  6118. .memory_allocated = &sctp_memory_allocated,
  6119. .sockets_allocated = &sctp_sockets_allocated,
  6120. };
  6121. #endif /* IS_ENABLED(CONFIG_IPV6) */