socket.c 200 KB

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