socket.c 259 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright IBM Corp. 2001, 2004
  4. * Copyright (c) 1999-2000 Cisco, Inc.
  5. * Copyright (c) 1999-2001 Motorola, Inc.
  6. * Copyright (c) 2001-2003 Intel Corp.
  7. * Copyright (c) 2001-2002 Nokia, Inc.
  8. * Copyright (c) 2001 La Monte H.P. Yarroll
  9. *
  10. * This file is part of the SCTP kernel implementation
  11. *
  12. * These functions interface with the sockets layer to implement the
  13. * SCTP Extensions for the Sockets API.
  14. *
  15. * Note that the descriptions from the specification are USER level
  16. * functions--this file is the functions which populate the struct proto
  17. * for SCTP which is the BOTTOM of the sockets interface.
  18. *
  19. * Please send any bug reports or fixes you make to the
  20. * email address(es):
  21. * lksctp developers <linux-sctp@vger.kernel.org>
  22. *
  23. * Written or modified by:
  24. * La Monte H.P. Yarroll <piggy@acm.org>
  25. * Narasimha Budihal <narsi@refcode.org>
  26. * Karl Knutson <karl@athena.chicago.il.us>
  27. * Jon Grimm <jgrimm@us.ibm.com>
  28. * Xingang Guo <xingang.guo@intel.com>
  29. * Daisy Chang <daisyc@us.ibm.com>
  30. * Sridhar Samudrala <samudrala@us.ibm.com>
  31. * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
  32. * Ardelle Fan <ardelle.fan@intel.com>
  33. * Ryan Layer <rmlayer@us.ibm.com>
  34. * Anup Pemmaiah <pemmaiah@cc.usu.edu>
  35. * Kevin Gao <kevin.gao@intel.com>
  36. */
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <crypto/hash.h>
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/wait.h>
  42. #include <linux/time.h>
  43. #include <linux/sched/signal.h>
  44. #include <linux/ip.h>
  45. #include <linux/capability.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/poll.h>
  48. #include <linux/init.h>
  49. #include <linux/slab.h>
  50. #include <linux/file.h>
  51. #include <linux/compat.h>
  52. #include <linux/rhashtable.h>
  53. #include <net/ip.h>
  54. #include <net/icmp.h>
  55. #include <net/route.h>
  56. #include <net/ipv6.h>
  57. #include <net/inet_common.h>
  58. #include <net/busy_poll.h>
  59. #include <linux/socket.h> /* for sa_family_t */
  60. #include <linux/export.h>
  61. #include <net/sock.h>
  62. #include <net/sctp/sctp.h>
  63. #include <net/sctp/sm.h>
  64. #include <net/sctp/stream_sched.h>
  65. /* Forward declarations for internal helper functions. */
  66. static bool sctp_writeable(struct sock *sk);
  67. static void sctp_wfree(struct sk_buff *skb);
  68. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  69. size_t msg_len);
  70. static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
  71. static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  72. static int sctp_wait_for_accept(struct sock *sk, long timeo);
  73. static void sctp_wait_for_close(struct sock *sk, long timeo);
  74. static void sctp_destruct_sock(struct sock *sk);
  75. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  76. union sctp_addr *addr, int len);
  77. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  78. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  79. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  80. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  81. static int sctp_send_asconf(struct sctp_association *asoc,
  82. struct sctp_chunk *chunk);
  83. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  84. static int sctp_autobind(struct sock *sk);
  85. static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  86. struct sctp_association *assoc,
  87. enum sctp_socket_type type);
  88. static unsigned long sctp_memory_pressure;
  89. static atomic_long_t sctp_memory_allocated;
  90. struct percpu_counter sctp_sockets_allocated;
  91. static void sctp_enter_memory_pressure(struct sock *sk)
  92. {
  93. sctp_memory_pressure = 1;
  94. }
  95. /* Get the sndbuf space available at the time on the association. */
  96. static inline int sctp_wspace(struct sctp_association *asoc)
  97. {
  98. struct sock *sk = asoc->base.sk;
  99. return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
  100. : sk_stream_wspace(sk);
  101. }
  102. /* Increment the used sndbuf space count of the corresponding association by
  103. * the size of the outgoing data chunk.
  104. * Also, set the skb destructor for sndbuf accounting later.
  105. *
  106. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  107. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  108. * destructor in the data chunk skb for the purpose of the sndbuf space
  109. * tracking.
  110. */
  111. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  112. {
  113. struct sctp_association *asoc = chunk->asoc;
  114. struct sock *sk = asoc->base.sk;
  115. /* The sndbuf space is tracked per association. */
  116. sctp_association_hold(asoc);
  117. if (chunk->shkey)
  118. sctp_auth_shkey_hold(chunk->shkey);
  119. skb_set_owner_w(chunk->skb, sk);
  120. chunk->skb->destructor = sctp_wfree;
  121. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  122. skb_shinfo(chunk->skb)->destructor_arg = chunk;
  123. refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  124. asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
  125. sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
  126. sk_mem_charge(sk, chunk->skb->truesize);
  127. }
  128. static void sctp_clear_owner_w(struct sctp_chunk *chunk)
  129. {
  130. skb_orphan(chunk->skb);
  131. }
  132. #define traverse_and_process() \
  133. do { \
  134. msg = chunk->msg; \
  135. if (msg == prev_msg) \
  136. continue; \
  137. list_for_each_entry(c, &msg->chunks, frag_list) { \
  138. if ((clear && asoc->base.sk == c->skb->sk) || \
  139. (!clear && asoc->base.sk != c->skb->sk)) \
  140. cb(c); \
  141. } \
  142. prev_msg = msg; \
  143. } while (0)
  144. static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
  145. bool clear,
  146. void (*cb)(struct sctp_chunk *))
  147. {
  148. struct sctp_datamsg *msg, *prev_msg = NULL;
  149. struct sctp_outq *q = &asoc->outqueue;
  150. struct sctp_chunk *chunk, *c;
  151. struct sctp_transport *t;
  152. list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
  153. list_for_each_entry(chunk, &t->transmitted, transmitted_list)
  154. traverse_and_process();
  155. list_for_each_entry(chunk, &q->retransmit, transmitted_list)
  156. traverse_and_process();
  157. list_for_each_entry(chunk, &q->sacked, transmitted_list)
  158. traverse_and_process();
  159. list_for_each_entry(chunk, &q->abandoned, transmitted_list)
  160. traverse_and_process();
  161. list_for_each_entry(chunk, &q->out_chunk_list, list)
  162. traverse_and_process();
  163. }
  164. static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
  165. void (*cb)(struct sk_buff *, struct sock *))
  166. {
  167. struct sk_buff *skb, *tmp;
  168. sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
  169. cb(skb, sk);
  170. sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
  171. cb(skb, sk);
  172. sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
  173. cb(skb, sk);
  174. }
  175. /* Verify that this is a valid address. */
  176. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  177. int len)
  178. {
  179. struct sctp_af *af;
  180. /* Verify basic sockaddr. */
  181. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  182. if (!af)
  183. return -EINVAL;
  184. /* Is this a valid SCTP address? */
  185. if (!af->addr_valid(addr, sctp_sk(sk), NULL))
  186. return -EINVAL;
  187. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  188. return -EINVAL;
  189. return 0;
  190. }
  191. /* Look up the association by its id. If this is not a UDP-style
  192. * socket, the ID field is always ignored.
  193. */
  194. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  195. {
  196. struct sctp_association *asoc = NULL;
  197. /* If this is not a UDP-style socket, assoc id should be ignored. */
  198. if (!sctp_style(sk, UDP)) {
  199. /* Return NULL if the socket state is not ESTABLISHED. It
  200. * could be a TCP-style listening socket or a socket which
  201. * hasn't yet called connect() to establish an association.
  202. */
  203. if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
  204. return NULL;
  205. /* Get the first and the only association from the list. */
  206. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  207. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  208. struct sctp_association, asocs);
  209. return asoc;
  210. }
  211. /* Otherwise this is a UDP-style socket. */
  212. if (id <= SCTP_ALL_ASSOC)
  213. return NULL;
  214. spin_lock_bh(&sctp_assocs_id_lock);
  215. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  216. if (asoc && (asoc->base.sk != sk || asoc->base.dead))
  217. asoc = NULL;
  218. spin_unlock_bh(&sctp_assocs_id_lock);
  219. return asoc;
  220. }
  221. /* Look up the transport from an address and an assoc id. If both address and
  222. * id are specified, the associations matching the address and the id should be
  223. * the same.
  224. */
  225. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  226. struct sockaddr_storage *addr,
  227. sctp_assoc_t id)
  228. {
  229. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  230. struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
  231. union sctp_addr *laddr = (union sctp_addr *)addr;
  232. struct sctp_transport *transport;
  233. if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
  234. return NULL;
  235. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  236. laddr,
  237. &transport);
  238. if (!addr_asoc)
  239. return NULL;
  240. id_asoc = sctp_id2assoc(sk, id);
  241. if (id_asoc && (id_asoc != addr_asoc))
  242. return NULL;
  243. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
  244. (union sctp_addr *)addr);
  245. return transport;
  246. }
  247. /* API 3.1.2 bind() - UDP Style Syntax
  248. * The syntax of bind() is,
  249. *
  250. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  251. *
  252. * sd - the socket descriptor returned by socket().
  253. * addr - the address structure (struct sockaddr_in or struct
  254. * sockaddr_in6 [RFC 2553]),
  255. * addr_len - the size of the address structure.
  256. */
  257. static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
  258. {
  259. int retval = 0;
  260. lock_sock(sk);
  261. pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
  262. addr, addr_len);
  263. /* Disallow binding twice. */
  264. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  265. retval = sctp_do_bind(sk, (union sctp_addr *)addr,
  266. addr_len);
  267. else
  268. retval = -EINVAL;
  269. release_sock(sk);
  270. return retval;
  271. }
  272. static int sctp_get_port_local(struct sock *, union sctp_addr *);
  273. /* Verify this is a valid sockaddr. */
  274. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  275. union sctp_addr *addr, int len)
  276. {
  277. struct sctp_af *af;
  278. /* Check minimum size. */
  279. if (len < sizeof (struct sockaddr))
  280. return NULL;
  281. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  282. return NULL;
  283. if (addr->sa.sa_family == AF_INET6) {
  284. if (len < SIN6_LEN_RFC2133)
  285. return NULL;
  286. /* V4 mapped address are really of AF_INET family */
  287. if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
  288. !opt->pf->af_supported(AF_INET, opt))
  289. return NULL;
  290. }
  291. /* If we get this far, af is valid. */
  292. af = sctp_get_af_specific(addr->sa.sa_family);
  293. if (len < af->sockaddr_len)
  294. return NULL;
  295. return af;
  296. }
  297. static void sctp_auto_asconf_init(struct sctp_sock *sp)
  298. {
  299. struct net *net = sock_net(&sp->inet.sk);
  300. if (net->sctp.default_auto_asconf) {
  301. spin_lock(&net->sctp.addr_wq_lock);
  302. list_add_tail(&sp->auto_asconf_list, &net->sctp.auto_asconf_splist);
  303. spin_unlock(&net->sctp.addr_wq_lock);
  304. sp->do_auto_asconf = 1;
  305. }
  306. }
  307. /* Bind a local address either to an endpoint or to an association. */
  308. static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  309. {
  310. struct net *net = sock_net(sk);
  311. struct sctp_sock *sp = sctp_sk(sk);
  312. struct sctp_endpoint *ep = sp->ep;
  313. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  314. struct sctp_af *af;
  315. unsigned short snum;
  316. int ret = 0;
  317. /* Common sockaddr verification. */
  318. af = sctp_sockaddr_af(sp, addr, len);
  319. if (!af) {
  320. pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
  321. __func__, sk, addr, len);
  322. return -EINVAL;
  323. }
  324. snum = ntohs(addr->v4.sin_port);
  325. pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
  326. __func__, sk, &addr->sa, bp->port, snum, len);
  327. /* PF specific bind() address verification. */
  328. if (!sp->pf->bind_verify(sp, addr))
  329. return -EADDRNOTAVAIL;
  330. /* We must either be unbound, or bind to the same port.
  331. * It's OK to allow 0 ports if we are already bound.
  332. * We'll just inhert an already bound port in this case
  333. */
  334. if (bp->port) {
  335. if (!snum)
  336. snum = bp->port;
  337. else if (snum != bp->port) {
  338. pr_debug("%s: new port %d doesn't match existing port "
  339. "%d\n", __func__, snum, bp->port);
  340. return -EINVAL;
  341. }
  342. }
  343. if (snum && inet_is_local_unbindable_port(net, snum))
  344. return -EPERM;
  345. if (snum && inet_port_requires_bind_service(net, snum) &&
  346. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  347. return -EACCES;
  348. /* See if the address matches any of the addresses we may have
  349. * already bound before checking against other endpoints.
  350. */
  351. if (sctp_bind_addr_match(bp, addr, sp))
  352. return -EINVAL;
  353. /* Make sure we are allowed to bind here.
  354. * The function sctp_get_port_local() does duplicate address
  355. * detection.
  356. */
  357. addr->v4.sin_port = htons(snum);
  358. if (sctp_get_port_local(sk, addr))
  359. return -EADDRINUSE;
  360. /* Refresh ephemeral port. */
  361. if (!bp->port) {
  362. bp->port = inet_sk(sk)->inet_num;
  363. sctp_auto_asconf_init(sp);
  364. }
  365. /* Add the address to the bind address list.
  366. * Use GFP_ATOMIC since BHs will be disabled.
  367. */
  368. ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
  369. SCTP_ADDR_SRC, GFP_ATOMIC);
  370. if (ret) {
  371. sctp_put_port(sk);
  372. return ret;
  373. }
  374. /* Copy back into socket for getsockname() use. */
  375. inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
  376. sp->pf->to_sk_saddr(addr, sk);
  377. return ret;
  378. }
  379. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  380. *
  381. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  382. * at any one time. If a sender, after sending an ASCONF chunk, decides
  383. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  384. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  385. * subsequent ASCONF. Note this restriction binds each side, so at any
  386. * time two ASCONF may be in-transit on any given association (one sent
  387. * from each endpoint).
  388. */
  389. static int sctp_send_asconf(struct sctp_association *asoc,
  390. struct sctp_chunk *chunk)
  391. {
  392. int retval = 0;
  393. /* If there is an outstanding ASCONF chunk, queue it for later
  394. * transmission.
  395. */
  396. if (asoc->addip_last_asconf) {
  397. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  398. goto out;
  399. }
  400. /* Hold the chunk until an ASCONF_ACK is received. */
  401. sctp_chunk_hold(chunk);
  402. retval = sctp_primitive_ASCONF(asoc->base.net, asoc, chunk);
  403. if (retval)
  404. sctp_chunk_free(chunk);
  405. else
  406. asoc->addip_last_asconf = chunk;
  407. out:
  408. return retval;
  409. }
  410. /* Add a list of addresses as bind addresses to local endpoint or
  411. * association.
  412. *
  413. * Basically run through each address specified in the addrs/addrcnt
  414. * array/length pair, determine if it is IPv6 or IPv4 and call
  415. * sctp_do_bind() on it.
  416. *
  417. * If any of them fails, then the operation will be reversed and the
  418. * ones that were added will be removed.
  419. *
  420. * Only sctp_setsockopt_bindx() is supposed to call this function.
  421. */
  422. static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  423. {
  424. int cnt;
  425. int retval = 0;
  426. void *addr_buf;
  427. struct sockaddr *sa_addr;
  428. struct sctp_af *af;
  429. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
  430. addrs, addrcnt);
  431. addr_buf = addrs;
  432. for (cnt = 0; cnt < addrcnt; cnt++) {
  433. /* The list may contain either IPv4 or IPv6 address;
  434. * determine the address length for walking thru the list.
  435. */
  436. sa_addr = addr_buf;
  437. af = sctp_get_af_specific(sa_addr->sa_family);
  438. if (!af) {
  439. retval = -EINVAL;
  440. goto err_bindx_add;
  441. }
  442. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  443. af->sockaddr_len);
  444. addr_buf += af->sockaddr_len;
  445. err_bindx_add:
  446. if (retval < 0) {
  447. /* Failed. Cleanup the ones that have been added */
  448. if (cnt > 0)
  449. sctp_bindx_rem(sk, addrs, cnt);
  450. return retval;
  451. }
  452. }
  453. return retval;
  454. }
  455. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  456. * associations that are part of the endpoint indicating that a list of local
  457. * addresses are added to the endpoint.
  458. *
  459. * If any of the addresses is already in the bind address list of the
  460. * association, we do not send the chunk for that association. But it will not
  461. * affect other associations.
  462. *
  463. * Only sctp_setsockopt_bindx() is supposed to call this function.
  464. */
  465. static int sctp_send_asconf_add_ip(struct sock *sk,
  466. struct sockaddr *addrs,
  467. int addrcnt)
  468. {
  469. struct sctp_sock *sp;
  470. struct sctp_endpoint *ep;
  471. struct sctp_association *asoc;
  472. struct sctp_bind_addr *bp;
  473. struct sctp_chunk *chunk;
  474. struct sctp_sockaddr_entry *laddr;
  475. union sctp_addr *addr;
  476. union sctp_addr saveaddr;
  477. void *addr_buf;
  478. struct sctp_af *af;
  479. struct list_head *p;
  480. int i;
  481. int retval = 0;
  482. sp = sctp_sk(sk);
  483. ep = sp->ep;
  484. if (!ep->asconf_enable)
  485. return retval;
  486. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  487. __func__, sk, addrs, addrcnt);
  488. list_for_each_entry(asoc, &ep->asocs, asocs) {
  489. if (!asoc->peer.asconf_capable)
  490. continue;
  491. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  492. continue;
  493. if (!sctp_state(asoc, ESTABLISHED))
  494. continue;
  495. /* Check if any address in the packed array of addresses is
  496. * in the bind address list of the association. If so,
  497. * do not send the asconf chunk to its peer, but continue with
  498. * other associations.
  499. */
  500. addr_buf = addrs;
  501. for (i = 0; i < addrcnt; i++) {
  502. addr = addr_buf;
  503. af = sctp_get_af_specific(addr->v4.sin_family);
  504. if (!af) {
  505. retval = -EINVAL;
  506. goto out;
  507. }
  508. if (sctp_assoc_lookup_laddr(asoc, addr))
  509. break;
  510. addr_buf += af->sockaddr_len;
  511. }
  512. if (i < addrcnt)
  513. continue;
  514. /* Use the first valid address in bind addr list of
  515. * association as Address Parameter of ASCONF CHUNK.
  516. */
  517. bp = &asoc->base.bind_addr;
  518. p = bp->address_list.next;
  519. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  520. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  521. addrcnt, SCTP_PARAM_ADD_IP);
  522. if (!chunk) {
  523. retval = -ENOMEM;
  524. goto out;
  525. }
  526. /* Add the new addresses to the bind address list with
  527. * use_as_src set to 0.
  528. */
  529. addr_buf = addrs;
  530. for (i = 0; i < addrcnt; i++) {
  531. addr = addr_buf;
  532. af = sctp_get_af_specific(addr->v4.sin_family);
  533. memcpy(&saveaddr, addr, af->sockaddr_len);
  534. retval = sctp_add_bind_addr(bp, &saveaddr,
  535. sizeof(saveaddr),
  536. SCTP_ADDR_NEW, GFP_ATOMIC);
  537. addr_buf += af->sockaddr_len;
  538. }
  539. if (asoc->src_out_of_asoc_ok) {
  540. struct sctp_transport *trans;
  541. list_for_each_entry(trans,
  542. &asoc->peer.transport_addr_list, transports) {
  543. trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
  544. 2*asoc->pathmtu, 4380));
  545. trans->ssthresh = asoc->peer.i.a_rwnd;
  546. trans->rto = asoc->rto_initial;
  547. sctp_max_rto(asoc, trans);
  548. trans->rtt = trans->srtt = trans->rttvar = 0;
  549. /* Clear the source and route cache */
  550. sctp_transport_route(trans, NULL,
  551. sctp_sk(asoc->base.sk));
  552. }
  553. }
  554. retval = sctp_send_asconf(asoc, chunk);
  555. }
  556. out:
  557. return retval;
  558. }
  559. /* Remove a list of addresses from bind addresses list. Do not remove the
  560. * last address.
  561. *
  562. * Basically run through each address specified in the addrs/addrcnt
  563. * array/length pair, determine if it is IPv6 or IPv4 and call
  564. * sctp_del_bind() on it.
  565. *
  566. * If any of them fails, then the operation will be reversed and the
  567. * ones that were removed will be added back.
  568. *
  569. * At least one address has to be left; if only one address is
  570. * available, the operation will return -EBUSY.
  571. *
  572. * Only sctp_setsockopt_bindx() is supposed to call this function.
  573. */
  574. static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  575. {
  576. struct sctp_sock *sp = sctp_sk(sk);
  577. struct sctp_endpoint *ep = sp->ep;
  578. int cnt;
  579. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  580. int retval = 0;
  581. void *addr_buf;
  582. union sctp_addr *sa_addr;
  583. struct sctp_af *af;
  584. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  585. __func__, sk, addrs, addrcnt);
  586. addr_buf = addrs;
  587. for (cnt = 0; cnt < addrcnt; cnt++) {
  588. /* If the bind address list is empty or if there is only one
  589. * bind address, there is nothing more to be removed (we need
  590. * at least one address here).
  591. */
  592. if (list_empty(&bp->address_list) ||
  593. (sctp_list_single_entry(&bp->address_list))) {
  594. retval = -EBUSY;
  595. goto err_bindx_rem;
  596. }
  597. sa_addr = addr_buf;
  598. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  599. if (!af) {
  600. retval = -EINVAL;
  601. goto err_bindx_rem;
  602. }
  603. if (!af->addr_valid(sa_addr, sp, NULL)) {
  604. retval = -EADDRNOTAVAIL;
  605. goto err_bindx_rem;
  606. }
  607. if (sa_addr->v4.sin_port &&
  608. sa_addr->v4.sin_port != htons(bp->port)) {
  609. retval = -EINVAL;
  610. goto err_bindx_rem;
  611. }
  612. if (!sa_addr->v4.sin_port)
  613. sa_addr->v4.sin_port = htons(bp->port);
  614. /* FIXME - There is probably a need to check if sk->sk_saddr and
  615. * sk->sk_rcv_addr are currently set to one of the addresses to
  616. * be removed. This is something which needs to be looked into
  617. * when we are fixing the outstanding issues with multi-homing
  618. * socket routing and failover schemes. Refer to comments in
  619. * sctp_do_bind(). -daisy
  620. */
  621. retval = sctp_del_bind_addr(bp, sa_addr);
  622. addr_buf += af->sockaddr_len;
  623. err_bindx_rem:
  624. if (retval < 0) {
  625. /* Failed. Add the ones that has been removed back */
  626. if (cnt > 0)
  627. sctp_bindx_add(sk, addrs, cnt);
  628. return retval;
  629. }
  630. }
  631. return retval;
  632. }
  633. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  634. * the associations that are part of the endpoint indicating that a list of
  635. * local addresses are removed from the endpoint.
  636. *
  637. * If any of the addresses is already in the bind address list of the
  638. * association, we do not send the chunk for that association. But it will not
  639. * affect other associations.
  640. *
  641. * Only sctp_setsockopt_bindx() is supposed to call this function.
  642. */
  643. static int sctp_send_asconf_del_ip(struct sock *sk,
  644. struct sockaddr *addrs,
  645. int addrcnt)
  646. {
  647. struct sctp_sock *sp;
  648. struct sctp_endpoint *ep;
  649. struct sctp_association *asoc;
  650. struct sctp_transport *transport;
  651. struct sctp_bind_addr *bp;
  652. struct sctp_chunk *chunk;
  653. union sctp_addr *laddr;
  654. void *addr_buf;
  655. struct sctp_af *af;
  656. struct sctp_sockaddr_entry *saddr;
  657. int i;
  658. int retval = 0;
  659. int stored = 0;
  660. chunk = NULL;
  661. sp = sctp_sk(sk);
  662. ep = sp->ep;
  663. if (!ep->asconf_enable)
  664. return retval;
  665. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  666. __func__, sk, addrs, addrcnt);
  667. list_for_each_entry(asoc, &ep->asocs, asocs) {
  668. if (!asoc->peer.asconf_capable)
  669. continue;
  670. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  671. continue;
  672. if (!sctp_state(asoc, ESTABLISHED))
  673. continue;
  674. /* Check if any address in the packed array of addresses is
  675. * not present in the bind address list of the association.
  676. * If so, do not send the asconf chunk to its peer, but
  677. * continue with other associations.
  678. */
  679. addr_buf = addrs;
  680. for (i = 0; i < addrcnt; i++) {
  681. laddr = addr_buf;
  682. af = sctp_get_af_specific(laddr->v4.sin_family);
  683. if (!af) {
  684. retval = -EINVAL;
  685. goto out;
  686. }
  687. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  688. break;
  689. addr_buf += af->sockaddr_len;
  690. }
  691. if (i < addrcnt)
  692. continue;
  693. /* Find one address in the association's bind address list
  694. * that is not in the packed array of addresses. This is to
  695. * make sure that we do not delete all the addresses in the
  696. * association.
  697. */
  698. bp = &asoc->base.bind_addr;
  699. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  700. addrcnt, sp);
  701. if ((laddr == NULL) && (addrcnt == 1)) {
  702. if (asoc->asconf_addr_del_pending)
  703. continue;
  704. asoc->asconf_addr_del_pending =
  705. kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
  706. if (asoc->asconf_addr_del_pending == NULL) {
  707. retval = -ENOMEM;
  708. goto out;
  709. }
  710. asoc->asconf_addr_del_pending->sa.sa_family =
  711. addrs->sa_family;
  712. asoc->asconf_addr_del_pending->v4.sin_port =
  713. htons(bp->port);
  714. if (addrs->sa_family == AF_INET) {
  715. struct sockaddr_in *sin;
  716. sin = (struct sockaddr_in *)addrs;
  717. asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
  718. } else if (addrs->sa_family == AF_INET6) {
  719. struct sockaddr_in6 *sin6;
  720. sin6 = (struct sockaddr_in6 *)addrs;
  721. asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
  722. }
  723. pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
  724. __func__, asoc, &asoc->asconf_addr_del_pending->sa,
  725. asoc->asconf_addr_del_pending);
  726. asoc->src_out_of_asoc_ok = 1;
  727. stored = 1;
  728. goto skip_mkasconf;
  729. }
  730. if (laddr == NULL)
  731. return -EINVAL;
  732. /* We do not need RCU protection throughout this loop
  733. * because this is done under a socket lock from the
  734. * setsockopt call.
  735. */
  736. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  737. SCTP_PARAM_DEL_IP);
  738. if (!chunk) {
  739. retval = -ENOMEM;
  740. goto out;
  741. }
  742. skip_mkasconf:
  743. /* Reset use_as_src flag for the addresses in the bind address
  744. * list that are to be deleted.
  745. */
  746. addr_buf = addrs;
  747. for (i = 0; i < addrcnt; i++) {
  748. laddr = addr_buf;
  749. af = sctp_get_af_specific(laddr->v4.sin_family);
  750. list_for_each_entry(saddr, &bp->address_list, list) {
  751. if (sctp_cmp_addr_exact(&saddr->a, laddr))
  752. saddr->state = SCTP_ADDR_DEL;
  753. }
  754. addr_buf += af->sockaddr_len;
  755. }
  756. /* Update the route and saddr entries for all the transports
  757. * as some of the addresses in the bind address list are
  758. * about to be deleted and cannot be used as source addresses.
  759. */
  760. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  761. transports) {
  762. sctp_transport_route(transport, NULL,
  763. sctp_sk(asoc->base.sk));
  764. }
  765. if (stored)
  766. /* We don't need to transmit ASCONF */
  767. continue;
  768. retval = sctp_send_asconf(asoc, chunk);
  769. }
  770. out:
  771. return retval;
  772. }
  773. /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
  774. int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
  775. {
  776. struct sock *sk = sctp_opt2sk(sp);
  777. union sctp_addr *addr;
  778. struct sctp_af *af;
  779. /* It is safe to write port space in caller. */
  780. addr = &addrw->a;
  781. addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
  782. af = sctp_get_af_specific(addr->sa.sa_family);
  783. if (!af)
  784. return -EINVAL;
  785. if (sctp_verify_addr(sk, addr, af->sockaddr_len))
  786. return -EINVAL;
  787. if (addrw->state == SCTP_ADDR_NEW)
  788. return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
  789. else
  790. return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
  791. }
  792. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  793. *
  794. * API 8.1
  795. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  796. * int flags);
  797. *
  798. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  799. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  800. * or IPv6 addresses.
  801. *
  802. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  803. * Section 3.1.2 for this usage.
  804. *
  805. * addrs is a pointer to an array of one or more socket addresses. Each
  806. * address is contained in its appropriate structure (i.e. struct
  807. * sockaddr_in or struct sockaddr_in6) the family of the address type
  808. * must be used to distinguish the address length (note that this
  809. * representation is termed a "packed array" of addresses). The caller
  810. * specifies the number of addresses in the array with addrcnt.
  811. *
  812. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  813. * -1, and sets errno to the appropriate error code.
  814. *
  815. * For SCTP, the port given in each socket address must be the same, or
  816. * sctp_bindx() will fail, setting errno to EINVAL.
  817. *
  818. * The flags parameter is formed from the bitwise OR of zero or more of
  819. * the following currently defined flags:
  820. *
  821. * SCTP_BINDX_ADD_ADDR
  822. *
  823. * SCTP_BINDX_REM_ADDR
  824. *
  825. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  826. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  827. * addresses from the association. The two flags are mutually exclusive;
  828. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  829. * not remove all addresses from an association; sctp_bindx() will
  830. * reject such an attempt with EINVAL.
  831. *
  832. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  833. * additional addresses with an endpoint after calling bind(). Or use
  834. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  835. * socket is associated with so that no new association accepted will be
  836. * associated with those addresses. If the endpoint supports dynamic
  837. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  838. * endpoint to send the appropriate message to the peer to change the
  839. * peers address lists.
  840. *
  841. * Adding and removing addresses from a connected association is
  842. * optional functionality. Implementations that do not support this
  843. * functionality should return EOPNOTSUPP.
  844. *
  845. * Basically do nothing but copying the addresses from user to kernel
  846. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  847. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  848. * from userspace.
  849. *
  850. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  851. * it.
  852. *
  853. * sk The sk of the socket
  854. * addrs The pointer to the addresses
  855. * addrssize Size of the addrs buffer
  856. * op Operation to perform (add or remove, see the flags of
  857. * sctp_bindx)
  858. *
  859. * Returns 0 if ok, <0 errno code on error.
  860. */
  861. static int sctp_setsockopt_bindx(struct sock *sk, struct sockaddr *addrs,
  862. int addrs_size, int op)
  863. {
  864. int err;
  865. int addrcnt = 0;
  866. int walk_size = 0;
  867. struct sockaddr *sa_addr;
  868. void *addr_buf = addrs;
  869. struct sctp_af *af;
  870. pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
  871. __func__, sk, addr_buf, addrs_size, op);
  872. if (unlikely(addrs_size <= 0))
  873. return -EINVAL;
  874. /* Walk through the addrs buffer and count the number of addresses. */
  875. while (walk_size < addrs_size) {
  876. if (walk_size + sizeof(sa_family_t) > addrs_size)
  877. return -EINVAL;
  878. sa_addr = addr_buf;
  879. af = sctp_get_af_specific(sa_addr->sa_family);
  880. /* If the address family is not supported or if this address
  881. * causes the address buffer to overflow return EINVAL.
  882. */
  883. if (!af || (walk_size + af->sockaddr_len) > addrs_size)
  884. return -EINVAL;
  885. addrcnt++;
  886. addr_buf += af->sockaddr_len;
  887. walk_size += af->sockaddr_len;
  888. }
  889. /* Do the work. */
  890. switch (op) {
  891. case SCTP_BINDX_ADD_ADDR:
  892. /* Allow security module to validate bindx addresses. */
  893. err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
  894. addrs, addrs_size);
  895. if (err)
  896. return err;
  897. err = sctp_bindx_add(sk, addrs, addrcnt);
  898. if (err)
  899. return err;
  900. return sctp_send_asconf_add_ip(sk, addrs, addrcnt);
  901. case SCTP_BINDX_REM_ADDR:
  902. err = sctp_bindx_rem(sk, addrs, addrcnt);
  903. if (err)
  904. return err;
  905. return sctp_send_asconf_del_ip(sk, addrs, addrcnt);
  906. default:
  907. return -EINVAL;
  908. }
  909. }
  910. static int sctp_bind_add(struct sock *sk, struct sockaddr *addrs,
  911. int addrlen)
  912. {
  913. int err;
  914. lock_sock(sk);
  915. err = sctp_setsockopt_bindx(sk, addrs, addrlen, SCTP_BINDX_ADD_ADDR);
  916. release_sock(sk);
  917. return err;
  918. }
  919. static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
  920. const union sctp_addr *daddr,
  921. const struct sctp_initmsg *init,
  922. struct sctp_transport **tp)
  923. {
  924. struct sctp_association *asoc;
  925. struct sock *sk = ep->base.sk;
  926. struct net *net = sock_net(sk);
  927. enum sctp_scope scope;
  928. int err;
  929. if (sctp_endpoint_is_peeled_off(ep, daddr))
  930. return -EADDRNOTAVAIL;
  931. if (!ep->base.bind_addr.port) {
  932. if (sctp_autobind(sk))
  933. return -EAGAIN;
  934. } else {
  935. if (inet_is_local_unbindable_port(net, ep->base.bind_addr.port))
  936. return -EPERM;
  937. if (inet_port_requires_bind_service(net, ep->base.bind_addr.port) &&
  938. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  939. return -EACCES;
  940. }
  941. scope = sctp_scope(daddr);
  942. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  943. if (!asoc)
  944. return -ENOMEM;
  945. err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
  946. if (err < 0)
  947. goto free;
  948. *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
  949. if (!*tp) {
  950. err = -ENOMEM;
  951. goto free;
  952. }
  953. if (!init)
  954. return 0;
  955. if (init->sinit_num_ostreams) {
  956. __u16 outcnt = init->sinit_num_ostreams;
  957. asoc->c.sinit_num_ostreams = outcnt;
  958. /* outcnt has been changed, need to re-init stream */
  959. err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL);
  960. if (err)
  961. goto free;
  962. }
  963. if (init->sinit_max_instreams)
  964. asoc->c.sinit_max_instreams = init->sinit_max_instreams;
  965. if (init->sinit_max_attempts)
  966. asoc->max_init_attempts = init->sinit_max_attempts;
  967. if (init->sinit_max_init_timeo)
  968. asoc->max_init_timeo =
  969. msecs_to_jiffies(init->sinit_max_init_timeo);
  970. return 0;
  971. free:
  972. sctp_association_free(asoc);
  973. return err;
  974. }
  975. static int sctp_connect_add_peer(struct sctp_association *asoc,
  976. union sctp_addr *daddr, int addr_len)
  977. {
  978. struct sctp_endpoint *ep = asoc->ep;
  979. struct sctp_association *old;
  980. struct sctp_transport *t;
  981. int err;
  982. err = sctp_verify_addr(ep->base.sk, daddr, addr_len);
  983. if (err)
  984. return err;
  985. old = sctp_endpoint_lookup_assoc(ep, daddr, &t);
  986. if (old && old != asoc)
  987. return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
  988. : -EALREADY;
  989. if (sctp_endpoint_is_peeled_off(ep, daddr))
  990. return -EADDRNOTAVAIL;
  991. t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
  992. if (!t)
  993. return -ENOMEM;
  994. return 0;
  995. }
  996. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  997. *
  998. * Common routine for handling connect() and sctp_connectx().
  999. * Connect will come in with just a single address.
  1000. */
  1001. static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,
  1002. int addrs_size, int flags, sctp_assoc_t *assoc_id)
  1003. {
  1004. struct sctp_sock *sp = sctp_sk(sk);
  1005. struct sctp_endpoint *ep = sp->ep;
  1006. struct sctp_transport *transport;
  1007. struct sctp_association *asoc;
  1008. void *addr_buf = kaddrs;
  1009. union sctp_addr *daddr;
  1010. struct sctp_af *af;
  1011. int walk_size, err;
  1012. long timeo;
  1013. if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
  1014. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)))
  1015. return -EISCONN;
  1016. daddr = addr_buf;
  1017. af = sctp_get_af_specific(daddr->sa.sa_family);
  1018. if (!af || af->sockaddr_len > addrs_size)
  1019. return -EINVAL;
  1020. err = sctp_verify_addr(sk, daddr, af->sockaddr_len);
  1021. if (err)
  1022. return err;
  1023. asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
  1024. if (asoc)
  1025. return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
  1026. : -EALREADY;
  1027. err = sctp_connect_new_asoc(ep, daddr, NULL, &transport);
  1028. if (err)
  1029. return err;
  1030. asoc = transport->asoc;
  1031. addr_buf += af->sockaddr_len;
  1032. walk_size = af->sockaddr_len;
  1033. while (walk_size < addrs_size) {
  1034. err = -EINVAL;
  1035. if (walk_size + sizeof(sa_family_t) > addrs_size)
  1036. goto out_free;
  1037. daddr = addr_buf;
  1038. af = sctp_get_af_specific(daddr->sa.sa_family);
  1039. if (!af || af->sockaddr_len + walk_size > addrs_size)
  1040. goto out_free;
  1041. if (asoc->peer.port != ntohs(daddr->v4.sin_port))
  1042. goto out_free;
  1043. err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len);
  1044. if (err)
  1045. goto out_free;
  1046. addr_buf += af->sockaddr_len;
  1047. walk_size += af->sockaddr_len;
  1048. }
  1049. /* In case the user of sctp_connectx() wants an association
  1050. * id back, assign one now.
  1051. */
  1052. if (assoc_id) {
  1053. err = sctp_assoc_set_id(asoc, GFP_KERNEL);
  1054. if (err < 0)
  1055. goto out_free;
  1056. }
  1057. err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL);
  1058. if (err < 0)
  1059. goto out_free;
  1060. /* Initialize sk's dport and daddr for getpeername() */
  1061. inet_sk(sk)->inet_dport = htons(asoc->peer.port);
  1062. sp->pf->to_sk_daddr(daddr, sk);
  1063. sk->sk_err = 0;
  1064. if (assoc_id)
  1065. *assoc_id = asoc->assoc_id;
  1066. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  1067. return sctp_wait_for_connect(asoc, &timeo);
  1068. out_free:
  1069. pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
  1070. __func__, asoc, kaddrs, err);
  1071. sctp_association_free(asoc);
  1072. return err;
  1073. }
  1074. /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
  1075. *
  1076. * API 8.9
  1077. * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
  1078. * sctp_assoc_t *asoc);
  1079. *
  1080. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  1081. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  1082. * or IPv6 addresses.
  1083. *
  1084. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  1085. * Section 3.1.2 for this usage.
  1086. *
  1087. * addrs is a pointer to an array of one or more socket addresses. Each
  1088. * address is contained in its appropriate structure (i.e. struct
  1089. * sockaddr_in or struct sockaddr_in6) the family of the address type
  1090. * must be used to distengish the address length (note that this
  1091. * representation is termed a "packed array" of addresses). The caller
  1092. * specifies the number of addresses in the array with addrcnt.
  1093. *
  1094. * On success, sctp_connectx() returns 0. It also sets the assoc_id to
  1095. * the association id of the new association. On failure, sctp_connectx()
  1096. * returns -1, and sets errno to the appropriate error code. The assoc_id
  1097. * is not touched by the kernel.
  1098. *
  1099. * For SCTP, the port given in each socket address must be the same, or
  1100. * sctp_connectx() will fail, setting errno to EINVAL.
  1101. *
  1102. * An application can use sctp_connectx to initiate an association with
  1103. * an endpoint that is multi-homed. Much like sctp_bindx() this call
  1104. * allows a caller to specify multiple addresses at which a peer can be
  1105. * reached. The way the SCTP stack uses the list of addresses to set up
  1106. * the association is implementation dependent. This function only
  1107. * specifies that the stack will try to make use of all the addresses in
  1108. * the list when needed.
  1109. *
  1110. * Note that the list of addresses passed in is only used for setting up
  1111. * the association. It does not necessarily equal the set of addresses
  1112. * the peer uses for the resulting association. If the caller wants to
  1113. * find out the set of peer addresses, it must use sctp_getpaddrs() to
  1114. * retrieve them after the association has been set up.
  1115. *
  1116. * Basically do nothing but copying the addresses from user to kernel
  1117. * land and invoking either sctp_connectx(). This is used for tunneling
  1118. * the sctp_connectx() request through sctp_setsockopt() from userspace.
  1119. *
  1120. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  1121. * it.
  1122. *
  1123. * sk The sk of the socket
  1124. * addrs The pointer to the addresses
  1125. * addrssize Size of the addrs buffer
  1126. *
  1127. * Returns >=0 if ok, <0 errno code on error.
  1128. */
  1129. static int __sctp_setsockopt_connectx(struct sock *sk, struct sockaddr *kaddrs,
  1130. int addrs_size, sctp_assoc_t *assoc_id)
  1131. {
  1132. int err = 0, flags = 0;
  1133. pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
  1134. __func__, sk, kaddrs, addrs_size);
  1135. /* make sure the 1st addr's sa_family is accessible later */
  1136. if (unlikely(addrs_size < sizeof(sa_family_t)))
  1137. return -EINVAL;
  1138. /* Allow security module to validate connectx addresses. */
  1139. err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
  1140. (struct sockaddr *)kaddrs,
  1141. addrs_size);
  1142. if (err)
  1143. return err;
  1144. /* in-kernel sockets don't generally have a file allocated to them
  1145. * if all they do is call sock_create_kern().
  1146. */
  1147. if (sk->sk_socket->file)
  1148. flags = sk->sk_socket->file->f_flags;
  1149. return __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
  1150. }
  1151. /*
  1152. * This is an older interface. It's kept for backward compatibility
  1153. * to the option that doesn't provide association id.
  1154. */
  1155. static int sctp_setsockopt_connectx_old(struct sock *sk,
  1156. struct sockaddr *kaddrs,
  1157. int addrs_size)
  1158. {
  1159. return __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, NULL);
  1160. }
  1161. /*
  1162. * New interface for the API. The since the API is done with a socket
  1163. * option, to make it simple we feed back the association id is as a return
  1164. * indication to the call. Error is always negative and association id is
  1165. * always positive.
  1166. */
  1167. static int sctp_setsockopt_connectx(struct sock *sk,
  1168. struct sockaddr *kaddrs,
  1169. int addrs_size)
  1170. {
  1171. sctp_assoc_t assoc_id = 0;
  1172. int err = 0;
  1173. err = __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, &assoc_id);
  1174. if (err)
  1175. return err;
  1176. else
  1177. return assoc_id;
  1178. }
  1179. /*
  1180. * New (hopefully final) interface for the API.
  1181. * We use the sctp_getaddrs_old structure so that use-space library
  1182. * can avoid any unnecessary allocations. The only different part
  1183. * is that we store the actual length of the address buffer into the
  1184. * addrs_num structure member. That way we can re-use the existing
  1185. * code.
  1186. */
  1187. #ifdef CONFIG_COMPAT
  1188. struct compat_sctp_getaddrs_old {
  1189. sctp_assoc_t assoc_id;
  1190. s32 addr_num;
  1191. compat_uptr_t addrs; /* struct sockaddr * */
  1192. };
  1193. #endif
  1194. static int sctp_getsockopt_connectx3(struct sock *sk, int len,
  1195. char __user *optval,
  1196. int __user *optlen)
  1197. {
  1198. struct sctp_getaddrs_old param;
  1199. sctp_assoc_t assoc_id = 0;
  1200. struct sockaddr *kaddrs;
  1201. int err = 0;
  1202. #ifdef CONFIG_COMPAT
  1203. if (in_compat_syscall()) {
  1204. struct compat_sctp_getaddrs_old param32;
  1205. if (len < sizeof(param32))
  1206. return -EINVAL;
  1207. if (copy_from_user(&param32, optval, sizeof(param32)))
  1208. return -EFAULT;
  1209. param.assoc_id = param32.assoc_id;
  1210. param.addr_num = param32.addr_num;
  1211. param.addrs = compat_ptr(param32.addrs);
  1212. } else
  1213. #endif
  1214. {
  1215. if (len < sizeof(param))
  1216. return -EINVAL;
  1217. if (copy_from_user(&param, optval, sizeof(param)))
  1218. return -EFAULT;
  1219. }
  1220. kaddrs = memdup_user(param.addrs, param.addr_num);
  1221. if (IS_ERR(kaddrs))
  1222. return PTR_ERR(kaddrs);
  1223. err = __sctp_setsockopt_connectx(sk, kaddrs, param.addr_num, &assoc_id);
  1224. kfree(kaddrs);
  1225. if (err == 0 || err == -EINPROGRESS) {
  1226. if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
  1227. return -EFAULT;
  1228. if (put_user(sizeof(assoc_id), optlen))
  1229. return -EFAULT;
  1230. }
  1231. return err;
  1232. }
  1233. /* API 3.1.4 close() - UDP Style Syntax
  1234. * Applications use close() to perform graceful shutdown (as described in
  1235. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  1236. * by a UDP-style socket.
  1237. *
  1238. * The syntax is
  1239. *
  1240. * ret = close(int sd);
  1241. *
  1242. * sd - the socket descriptor of the associations to be closed.
  1243. *
  1244. * To gracefully shutdown a specific association represented by the
  1245. * UDP-style socket, an application should use the sendmsg() call,
  1246. * passing no user data, but including the appropriate flag in the
  1247. * ancillary data (see Section xxxx).
  1248. *
  1249. * If sd in the close() call is a branched-off socket representing only
  1250. * one association, the shutdown is performed on that association only.
  1251. *
  1252. * 4.1.6 close() - TCP Style Syntax
  1253. *
  1254. * Applications use close() to gracefully close down an association.
  1255. *
  1256. * The syntax is:
  1257. *
  1258. * int close(int sd);
  1259. *
  1260. * sd - the socket descriptor of the association to be closed.
  1261. *
  1262. * After an application calls close() on a socket descriptor, no further
  1263. * socket operations will succeed on that descriptor.
  1264. *
  1265. * API 7.1.4 SO_LINGER
  1266. *
  1267. * An application using the TCP-style socket can use this option to
  1268. * perform the SCTP ABORT primitive. The linger option structure is:
  1269. *
  1270. * struct linger {
  1271. * int l_onoff; // option on/off
  1272. * int l_linger; // linger time
  1273. * };
  1274. *
  1275. * To enable the option, set l_onoff to 1. If the l_linger value is set
  1276. * to 0, calling close() is the same as the ABORT primitive. If the
  1277. * value is set to a negative value, the setsockopt() call will return
  1278. * an error. If the value is set to a positive value linger_time, the
  1279. * close() can be blocked for at most linger_time ms. If the graceful
  1280. * shutdown phase does not finish during this period, close() will
  1281. * return but the graceful shutdown phase continues in the system.
  1282. */
  1283. static void sctp_close(struct sock *sk, long timeout)
  1284. {
  1285. struct net *net = sock_net(sk);
  1286. struct sctp_endpoint *ep;
  1287. struct sctp_association *asoc;
  1288. struct list_head *pos, *temp;
  1289. unsigned int data_was_unread;
  1290. pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
  1291. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  1292. sk->sk_shutdown = SHUTDOWN_MASK;
  1293. inet_sk_set_state(sk, SCTP_SS_CLOSING);
  1294. ep = sctp_sk(sk)->ep;
  1295. /* Clean up any skbs sitting on the receive queue. */
  1296. data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  1297. data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  1298. /* Walk all associations on an endpoint. */
  1299. list_for_each_safe(pos, temp, &ep->asocs) {
  1300. asoc = list_entry(pos, struct sctp_association, asocs);
  1301. if (sctp_style(sk, TCP)) {
  1302. /* A closed association can still be in the list if
  1303. * it belongs to a TCP-style listening socket that is
  1304. * not yet accepted. If so, free it. If not, send an
  1305. * ABORT or SHUTDOWN based on the linger options.
  1306. */
  1307. if (sctp_state(asoc, CLOSED)) {
  1308. sctp_association_free(asoc);
  1309. continue;
  1310. }
  1311. }
  1312. if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
  1313. !skb_queue_empty(&asoc->ulpq.reasm) ||
  1314. !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
  1315. (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
  1316. struct sctp_chunk *chunk;
  1317. chunk = sctp_make_abort_user(asoc, NULL, 0);
  1318. sctp_primitive_ABORT(net, asoc, chunk);
  1319. } else
  1320. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1321. }
  1322. /* On a TCP-style socket, block for at most linger_time if set. */
  1323. if (sctp_style(sk, TCP) && timeout)
  1324. sctp_wait_for_close(sk, timeout);
  1325. /* This will run the backlog queue. */
  1326. release_sock(sk);
  1327. /* Supposedly, no process has access to the socket, but
  1328. * the net layers still may.
  1329. * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
  1330. * held and that should be grabbed before socket lock.
  1331. */
  1332. spin_lock_bh(&net->sctp.addr_wq_lock);
  1333. bh_lock_sock_nested(sk);
  1334. /* Hold the sock, since sk_common_release() will put sock_put()
  1335. * and we have just a little more cleanup.
  1336. */
  1337. sock_hold(sk);
  1338. sk_common_release(sk);
  1339. bh_unlock_sock(sk);
  1340. spin_unlock_bh(&net->sctp.addr_wq_lock);
  1341. sock_put(sk);
  1342. SCTP_DBG_OBJCNT_DEC(sock);
  1343. }
  1344. /* Handle EPIPE error. */
  1345. static int sctp_error(struct sock *sk, int flags, int err)
  1346. {
  1347. if (err == -EPIPE)
  1348. err = sock_error(sk) ? : -EPIPE;
  1349. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  1350. send_sig(SIGPIPE, current, 0);
  1351. return err;
  1352. }
  1353. /* API 3.1.3 sendmsg() - UDP Style Syntax
  1354. *
  1355. * An application uses sendmsg() and recvmsg() calls to transmit data to
  1356. * and receive data from its peer.
  1357. *
  1358. * ssize_t sendmsg(int socket, const struct msghdr *message,
  1359. * int flags);
  1360. *
  1361. * socket - the socket descriptor of the endpoint.
  1362. * message - pointer to the msghdr structure which contains a single
  1363. * user message and possibly some ancillary data.
  1364. *
  1365. * See Section 5 for complete description of the data
  1366. * structures.
  1367. *
  1368. * flags - flags sent or received with the user message, see Section
  1369. * 5 for complete description of the flags.
  1370. *
  1371. * Note: This function could use a rewrite especially when explicit
  1372. * connect support comes in.
  1373. */
  1374. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  1375. static int sctp_msghdr_parse(const struct msghdr *msg,
  1376. struct sctp_cmsgs *cmsgs);
  1377. static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
  1378. struct sctp_sndrcvinfo *srinfo,
  1379. const struct msghdr *msg, size_t msg_len)
  1380. {
  1381. __u16 sflags;
  1382. int err;
  1383. if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
  1384. return -EPIPE;
  1385. if (msg_len > sk->sk_sndbuf)
  1386. return -EMSGSIZE;
  1387. memset(cmsgs, 0, sizeof(*cmsgs));
  1388. err = sctp_msghdr_parse(msg, cmsgs);
  1389. if (err) {
  1390. pr_debug("%s: msghdr parse err:%x\n", __func__, err);
  1391. return err;
  1392. }
  1393. memset(srinfo, 0, sizeof(*srinfo));
  1394. if (cmsgs->srinfo) {
  1395. srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
  1396. srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
  1397. srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
  1398. srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
  1399. srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
  1400. srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
  1401. }
  1402. if (cmsgs->sinfo) {
  1403. srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
  1404. srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
  1405. srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
  1406. srinfo->sinfo_context = cmsgs->sinfo->snd_context;
  1407. srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
  1408. }
  1409. if (cmsgs->prinfo) {
  1410. srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
  1411. SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
  1412. cmsgs->prinfo->pr_policy);
  1413. }
  1414. sflags = srinfo->sinfo_flags;
  1415. if (!sflags && msg_len)
  1416. return 0;
  1417. if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
  1418. return -EINVAL;
  1419. if (((sflags & SCTP_EOF) && msg_len > 0) ||
  1420. (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
  1421. return -EINVAL;
  1422. if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
  1423. return -EINVAL;
  1424. return 0;
  1425. }
  1426. static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
  1427. struct sctp_cmsgs *cmsgs,
  1428. union sctp_addr *daddr,
  1429. struct sctp_transport **tp)
  1430. {
  1431. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  1432. struct sctp_association *asoc;
  1433. struct cmsghdr *cmsg;
  1434. __be32 flowinfo = 0;
  1435. struct sctp_af *af;
  1436. int err;
  1437. *tp = NULL;
  1438. if (sflags & (SCTP_EOF | SCTP_ABORT))
  1439. return -EINVAL;
  1440. if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
  1441. sctp_sstate(sk, CLOSING)))
  1442. return -EADDRNOTAVAIL;
  1443. /* Label connection socket for first association 1-to-many
  1444. * style for client sequence socket()->sendmsg(). This
  1445. * needs to be done before sctp_assoc_add_peer() as that will
  1446. * set up the initial packet that needs to account for any
  1447. * security ip options (CIPSO/CALIPSO) added to the packet.
  1448. */
  1449. af = sctp_get_af_specific(daddr->sa.sa_family);
  1450. if (!af)
  1451. return -EINVAL;
  1452. err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
  1453. (struct sockaddr *)daddr,
  1454. af->sockaddr_len);
  1455. if (err < 0)
  1456. return err;
  1457. err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp);
  1458. if (err)
  1459. return err;
  1460. asoc = (*tp)->asoc;
  1461. if (!cmsgs->addrs_msg)
  1462. return 0;
  1463. if (daddr->sa.sa_family == AF_INET6)
  1464. flowinfo = daddr->v6.sin6_flowinfo;
  1465. /* sendv addr list parse */
  1466. for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
  1467. union sctp_addr _daddr;
  1468. int dlen;
  1469. if (cmsg->cmsg_level != IPPROTO_SCTP ||
  1470. (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
  1471. cmsg->cmsg_type != SCTP_DSTADDRV6))
  1472. continue;
  1473. daddr = &_daddr;
  1474. memset(daddr, 0, sizeof(*daddr));
  1475. dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1476. if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
  1477. if (dlen < sizeof(struct in_addr)) {
  1478. err = -EINVAL;
  1479. goto free;
  1480. }
  1481. dlen = sizeof(struct in_addr);
  1482. daddr->v4.sin_family = AF_INET;
  1483. daddr->v4.sin_port = htons(asoc->peer.port);
  1484. memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
  1485. } else {
  1486. if (dlen < sizeof(struct in6_addr)) {
  1487. err = -EINVAL;
  1488. goto free;
  1489. }
  1490. dlen = sizeof(struct in6_addr);
  1491. daddr->v6.sin6_flowinfo = flowinfo;
  1492. daddr->v6.sin6_family = AF_INET6;
  1493. daddr->v6.sin6_port = htons(asoc->peer.port);
  1494. memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
  1495. }
  1496. err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr));
  1497. if (err)
  1498. goto free;
  1499. }
  1500. return 0;
  1501. free:
  1502. sctp_association_free(asoc);
  1503. return err;
  1504. }
  1505. static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
  1506. __u16 sflags, struct msghdr *msg,
  1507. size_t msg_len)
  1508. {
  1509. struct sock *sk = asoc->base.sk;
  1510. struct net *net = sock_net(sk);
  1511. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
  1512. return -EPIPE;
  1513. if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
  1514. !sctp_state(asoc, ESTABLISHED))
  1515. return 0;
  1516. if (sflags & SCTP_EOF) {
  1517. pr_debug("%s: shutting down association:%p\n", __func__, asoc);
  1518. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1519. return 0;
  1520. }
  1521. if (sflags & SCTP_ABORT) {
  1522. struct sctp_chunk *chunk;
  1523. chunk = sctp_make_abort_user(asoc, msg, msg_len);
  1524. if (!chunk)
  1525. return -ENOMEM;
  1526. pr_debug("%s: aborting association:%p\n", __func__, asoc);
  1527. sctp_primitive_ABORT(net, asoc, chunk);
  1528. iov_iter_revert(&msg->msg_iter, msg_len);
  1529. return 0;
  1530. }
  1531. return 1;
  1532. }
  1533. static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
  1534. struct msghdr *msg, size_t msg_len,
  1535. struct sctp_transport *transport,
  1536. struct sctp_sndrcvinfo *sinfo)
  1537. {
  1538. struct sock *sk = asoc->base.sk;
  1539. struct sctp_sock *sp = sctp_sk(sk);
  1540. struct net *net = sock_net(sk);
  1541. struct sctp_datamsg *datamsg;
  1542. bool wait_connect = false;
  1543. struct sctp_chunk *chunk;
  1544. long timeo;
  1545. int err;
  1546. if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
  1547. err = -EINVAL;
  1548. goto err;
  1549. }
  1550. if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
  1551. err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
  1552. if (err)
  1553. goto err;
  1554. }
  1555. if (sp->disable_fragments && msg_len > asoc->frag_point) {
  1556. err = -EMSGSIZE;
  1557. goto err;
  1558. }
  1559. if (asoc->pmtu_pending) {
  1560. if (sp->param_flags & SPP_PMTUD_ENABLE)
  1561. sctp_assoc_sync_pmtu(asoc);
  1562. asoc->pmtu_pending = 0;
  1563. }
  1564. if (sctp_wspace(asoc) < (int)msg_len)
  1565. sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
  1566. if (sk_under_memory_pressure(sk))
  1567. sk_mem_reclaim(sk);
  1568. if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
  1569. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1570. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
  1571. if (err)
  1572. goto err;
  1573. }
  1574. if (sctp_state(asoc, CLOSED)) {
  1575. err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
  1576. if (err)
  1577. goto err;
  1578. if (asoc->ep->intl_enable) {
  1579. timeo = sock_sndtimeo(sk, 0);
  1580. err = sctp_wait_for_connect(asoc, &timeo);
  1581. if (err) {
  1582. err = -ESRCH;
  1583. goto err;
  1584. }
  1585. } else {
  1586. wait_connect = true;
  1587. }
  1588. pr_debug("%s: we associated primitively\n", __func__);
  1589. }
  1590. datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
  1591. if (IS_ERR(datamsg)) {
  1592. err = PTR_ERR(datamsg);
  1593. goto err;
  1594. }
  1595. asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
  1596. list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
  1597. sctp_chunk_hold(chunk);
  1598. sctp_set_owner_w(chunk);
  1599. chunk->transport = transport;
  1600. }
  1601. err = sctp_primitive_SEND(net, asoc, datamsg);
  1602. if (err) {
  1603. sctp_datamsg_free(datamsg);
  1604. goto err;
  1605. }
  1606. pr_debug("%s: we sent primitively\n", __func__);
  1607. sctp_datamsg_put(datamsg);
  1608. if (unlikely(wait_connect)) {
  1609. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1610. sctp_wait_for_connect(asoc, &timeo);
  1611. }
  1612. err = msg_len;
  1613. err:
  1614. return err;
  1615. }
  1616. static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
  1617. const struct msghdr *msg,
  1618. struct sctp_cmsgs *cmsgs)
  1619. {
  1620. union sctp_addr *daddr = NULL;
  1621. int err;
  1622. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  1623. int len = msg->msg_namelen;
  1624. if (len > sizeof(*daddr))
  1625. len = sizeof(*daddr);
  1626. daddr = (union sctp_addr *)msg->msg_name;
  1627. err = sctp_verify_addr(sk, daddr, len);
  1628. if (err)
  1629. return ERR_PTR(err);
  1630. }
  1631. return daddr;
  1632. }
  1633. static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
  1634. struct sctp_sndrcvinfo *sinfo,
  1635. struct sctp_cmsgs *cmsgs)
  1636. {
  1637. if (!cmsgs->srinfo && !cmsgs->sinfo) {
  1638. sinfo->sinfo_stream = asoc->default_stream;
  1639. sinfo->sinfo_ppid = asoc->default_ppid;
  1640. sinfo->sinfo_context = asoc->default_context;
  1641. sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
  1642. if (!cmsgs->prinfo)
  1643. sinfo->sinfo_flags = asoc->default_flags;
  1644. }
  1645. if (!cmsgs->srinfo && !cmsgs->prinfo)
  1646. sinfo->sinfo_timetolive = asoc->default_timetolive;
  1647. if (cmsgs->authinfo) {
  1648. /* Reuse sinfo_tsn to indicate that authinfo was set and
  1649. * sinfo_ssn to save the keyid on tx path.
  1650. */
  1651. sinfo->sinfo_tsn = 1;
  1652. sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
  1653. }
  1654. }
  1655. static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
  1656. {
  1657. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  1658. struct sctp_transport *transport = NULL;
  1659. struct sctp_sndrcvinfo _sinfo, *sinfo;
  1660. struct sctp_association *asoc, *tmp;
  1661. struct sctp_cmsgs cmsgs;
  1662. union sctp_addr *daddr;
  1663. bool new = false;
  1664. __u16 sflags;
  1665. int err;
  1666. /* Parse and get snd_info */
  1667. err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
  1668. if (err)
  1669. goto out;
  1670. sinfo = &_sinfo;
  1671. sflags = sinfo->sinfo_flags;
  1672. /* Get daddr from msg */
  1673. daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
  1674. if (IS_ERR(daddr)) {
  1675. err = PTR_ERR(daddr);
  1676. goto out;
  1677. }
  1678. lock_sock(sk);
  1679. /* SCTP_SENDALL process */
  1680. if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
  1681. list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
  1682. err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
  1683. msg_len);
  1684. if (err == 0)
  1685. continue;
  1686. if (err < 0)
  1687. goto out_unlock;
  1688. sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
  1689. err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
  1690. NULL, sinfo);
  1691. if (err < 0)
  1692. goto out_unlock;
  1693. iov_iter_revert(&msg->msg_iter, err);
  1694. }
  1695. goto out_unlock;
  1696. }
  1697. /* Get and check or create asoc */
  1698. if (daddr) {
  1699. asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
  1700. if (asoc) {
  1701. err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
  1702. msg_len);
  1703. if (err <= 0)
  1704. goto out_unlock;
  1705. } else {
  1706. err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
  1707. &transport);
  1708. if (err)
  1709. goto out_unlock;
  1710. asoc = transport->asoc;
  1711. new = true;
  1712. }
  1713. if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
  1714. transport = NULL;
  1715. } else {
  1716. asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
  1717. if (!asoc) {
  1718. err = -EPIPE;
  1719. goto out_unlock;
  1720. }
  1721. err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
  1722. if (err <= 0)
  1723. goto out_unlock;
  1724. }
  1725. /* Update snd_info with the asoc */
  1726. sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
  1727. /* Send msg to the asoc */
  1728. err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
  1729. if (err < 0 && err != -ESRCH && new)
  1730. sctp_association_free(asoc);
  1731. out_unlock:
  1732. release_sock(sk);
  1733. out:
  1734. return sctp_error(sk, msg->msg_flags, err);
  1735. }
  1736. /* This is an extended version of skb_pull() that removes the data from the
  1737. * start of a skb even when data is spread across the list of skb's in the
  1738. * frag_list. len specifies the total amount of data that needs to be removed.
  1739. * when 'len' bytes could be removed from the skb, it returns 0.
  1740. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1741. * could not be removed.
  1742. */
  1743. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1744. {
  1745. struct sk_buff *list;
  1746. int skb_len = skb_headlen(skb);
  1747. int rlen;
  1748. if (len <= skb_len) {
  1749. __skb_pull(skb, len);
  1750. return 0;
  1751. }
  1752. len -= skb_len;
  1753. __skb_pull(skb, skb_len);
  1754. skb_walk_frags(skb, list) {
  1755. rlen = sctp_skb_pull(list, len);
  1756. skb->len -= (len-rlen);
  1757. skb->data_len -= (len-rlen);
  1758. if (!rlen)
  1759. return 0;
  1760. len = rlen;
  1761. }
  1762. return len;
  1763. }
  1764. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1765. *
  1766. * ssize_t recvmsg(int socket, struct msghdr *message,
  1767. * int flags);
  1768. *
  1769. * socket - the socket descriptor of the endpoint.
  1770. * message - pointer to the msghdr structure which contains a single
  1771. * user message and possibly some ancillary data.
  1772. *
  1773. * See Section 5 for complete description of the data
  1774. * structures.
  1775. *
  1776. * flags - flags sent or received with the user message, see Section
  1777. * 5 for complete description of the flags.
  1778. */
  1779. static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  1780. int noblock, int flags, int *addr_len)
  1781. {
  1782. struct sctp_ulpevent *event = NULL;
  1783. struct sctp_sock *sp = sctp_sk(sk);
  1784. struct sk_buff *skb, *head_skb;
  1785. int copied;
  1786. int err = 0;
  1787. int skb_len;
  1788. pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
  1789. "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
  1790. addr_len);
  1791. lock_sock(sk);
  1792. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
  1793. !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
  1794. err = -ENOTCONN;
  1795. goto out;
  1796. }
  1797. skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
  1798. if (!skb)
  1799. goto out;
  1800. /* Get the total length of the skb including any skb's in the
  1801. * frag_list.
  1802. */
  1803. skb_len = skb->len;
  1804. copied = skb_len;
  1805. if (copied > len)
  1806. copied = len;
  1807. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  1808. event = sctp_skb2event(skb);
  1809. if (err)
  1810. goto out_free;
  1811. if (event->chunk && event->chunk->head_skb)
  1812. head_skb = event->chunk->head_skb;
  1813. else
  1814. head_skb = skb;
  1815. sock_recv_ts_and_drops(msg, sk, head_skb);
  1816. if (sctp_ulpevent_is_notification(event)) {
  1817. msg->msg_flags |= MSG_NOTIFICATION;
  1818. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1819. } else {
  1820. sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
  1821. }
  1822. /* Check if we allow SCTP_NXTINFO. */
  1823. if (sp->recvnxtinfo)
  1824. sctp_ulpevent_read_nxtinfo(event, msg, sk);
  1825. /* Check if we allow SCTP_RCVINFO. */
  1826. if (sp->recvrcvinfo)
  1827. sctp_ulpevent_read_rcvinfo(event, msg);
  1828. /* Check if we allow SCTP_SNDRCVINFO. */
  1829. if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
  1830. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1831. err = copied;
  1832. /* If skb's length exceeds the user's buffer, update the skb and
  1833. * push it back to the receive_queue so that the next call to
  1834. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1835. */
  1836. if (skb_len > copied) {
  1837. msg->msg_flags &= ~MSG_EOR;
  1838. if (flags & MSG_PEEK)
  1839. goto out_free;
  1840. sctp_skb_pull(skb, copied);
  1841. skb_queue_head(&sk->sk_receive_queue, skb);
  1842. /* When only partial message is copied to the user, increase
  1843. * rwnd by that amount. If all the data in the skb is read,
  1844. * rwnd is updated when the event is freed.
  1845. */
  1846. if (!sctp_ulpevent_is_notification(event))
  1847. sctp_assoc_rwnd_increase(event->asoc, copied);
  1848. goto out;
  1849. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1850. (event->msg_flags & MSG_EOR))
  1851. msg->msg_flags |= MSG_EOR;
  1852. else
  1853. msg->msg_flags &= ~MSG_EOR;
  1854. out_free:
  1855. if (flags & MSG_PEEK) {
  1856. /* Release the skb reference acquired after peeking the skb in
  1857. * sctp_skb_recv_datagram().
  1858. */
  1859. kfree_skb(skb);
  1860. } else {
  1861. /* Free the event which includes releasing the reference to
  1862. * the owner of the skb, freeing the skb and updating the
  1863. * rwnd.
  1864. */
  1865. sctp_ulpevent_free(event);
  1866. }
  1867. out:
  1868. release_sock(sk);
  1869. return err;
  1870. }
  1871. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1872. *
  1873. * This option is a on/off flag. If enabled no SCTP message
  1874. * fragmentation will be performed. Instead if a message being sent
  1875. * exceeds the current PMTU size, the message will NOT be sent and
  1876. * instead a error will be indicated to the user.
  1877. */
  1878. static int sctp_setsockopt_disable_fragments(struct sock *sk, int *val,
  1879. unsigned int optlen)
  1880. {
  1881. if (optlen < sizeof(int))
  1882. return -EINVAL;
  1883. sctp_sk(sk)->disable_fragments = (*val == 0) ? 0 : 1;
  1884. return 0;
  1885. }
  1886. static int sctp_setsockopt_events(struct sock *sk, __u8 *sn_type,
  1887. unsigned int optlen)
  1888. {
  1889. struct sctp_sock *sp = sctp_sk(sk);
  1890. struct sctp_association *asoc;
  1891. int i;
  1892. if (optlen > sizeof(struct sctp_event_subscribe))
  1893. return -EINVAL;
  1894. for (i = 0; i < optlen; i++)
  1895. sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
  1896. sn_type[i]);
  1897. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  1898. asoc->subscribe = sctp_sk(sk)->subscribe;
  1899. /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
  1900. * if there is no data to be sent or retransmit, the stack will
  1901. * immediately send up this notification.
  1902. */
  1903. if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
  1904. struct sctp_ulpevent *event;
  1905. asoc = sctp_id2assoc(sk, 0);
  1906. if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
  1907. event = sctp_ulpevent_make_sender_dry_event(asoc,
  1908. GFP_USER | __GFP_NOWARN);
  1909. if (!event)
  1910. return -ENOMEM;
  1911. asoc->stream.si->enqueue_event(&asoc->ulpq, event);
  1912. }
  1913. }
  1914. return 0;
  1915. }
  1916. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1917. *
  1918. * This socket option is applicable to the UDP-style socket only. When
  1919. * set it will cause associations that are idle for more than the
  1920. * specified number of seconds to automatically close. An association
  1921. * being idle is defined an association that has NOT sent or received
  1922. * user data. The special value of '0' indicates that no automatic
  1923. * close of any associations should be performed. The option expects an
  1924. * integer defining the number of seconds of idle time before an
  1925. * association is closed.
  1926. */
  1927. static int sctp_setsockopt_autoclose(struct sock *sk, u32 *optval,
  1928. unsigned int optlen)
  1929. {
  1930. struct sctp_sock *sp = sctp_sk(sk);
  1931. struct net *net = sock_net(sk);
  1932. /* Applicable to UDP-style socket only */
  1933. if (sctp_style(sk, TCP))
  1934. return -EOPNOTSUPP;
  1935. if (optlen != sizeof(int))
  1936. return -EINVAL;
  1937. sp->autoclose = *optval;
  1938. if (sp->autoclose > net->sctp.max_autoclose)
  1939. sp->autoclose = net->sctp.max_autoclose;
  1940. return 0;
  1941. }
  1942. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1943. *
  1944. * Applications can enable or disable heartbeats for any peer address of
  1945. * an association, modify an address's heartbeat interval, force a
  1946. * heartbeat to be sent immediately, and adjust the address's maximum
  1947. * number of retransmissions sent before an address is considered
  1948. * unreachable. The following structure is used to access and modify an
  1949. * address's parameters:
  1950. *
  1951. * struct sctp_paddrparams {
  1952. * sctp_assoc_t spp_assoc_id;
  1953. * struct sockaddr_storage spp_address;
  1954. * uint32_t spp_hbinterval;
  1955. * uint16_t spp_pathmaxrxt;
  1956. * uint32_t spp_pathmtu;
  1957. * uint32_t spp_sackdelay;
  1958. * uint32_t spp_flags;
  1959. * uint32_t spp_ipv6_flowlabel;
  1960. * uint8_t spp_dscp;
  1961. * };
  1962. *
  1963. * spp_assoc_id - (one-to-many style socket) This is filled in the
  1964. * application, and identifies the association for
  1965. * this query.
  1966. * spp_address - This specifies which address is of interest.
  1967. * spp_hbinterval - This contains the value of the heartbeat interval,
  1968. * in milliseconds. If a value of zero
  1969. * is present in this field then no changes are to
  1970. * be made to this parameter.
  1971. * spp_pathmaxrxt - This contains the maximum number of
  1972. * retransmissions before this address shall be
  1973. * considered unreachable. If a value of zero
  1974. * is present in this field then no changes are to
  1975. * be made to this parameter.
  1976. * spp_pathmtu - When Path MTU discovery is disabled the value
  1977. * specified here will be the "fixed" path mtu.
  1978. * Note that if the spp_address field is empty
  1979. * then all associations on this address will
  1980. * have this fixed path mtu set upon them.
  1981. *
  1982. * spp_sackdelay - When delayed sack is enabled, this value specifies
  1983. * the number of milliseconds that sacks will be delayed
  1984. * for. This value will apply to all addresses of an
  1985. * association if the spp_address field is empty. Note
  1986. * also, that if delayed sack is enabled and this
  1987. * value is set to 0, no change is made to the last
  1988. * recorded delayed sack timer value.
  1989. *
  1990. * spp_flags - These flags are used to control various features
  1991. * on an association. The flag field may contain
  1992. * zero or more of the following options.
  1993. *
  1994. * SPP_HB_ENABLE - Enable heartbeats on the
  1995. * specified address. Note that if the address
  1996. * field is empty all addresses for the association
  1997. * have heartbeats enabled upon them.
  1998. *
  1999. * SPP_HB_DISABLE - Disable heartbeats on the
  2000. * speicifed address. Note that if the address
  2001. * field is empty all addresses for the association
  2002. * will have their heartbeats disabled. Note also
  2003. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  2004. * mutually exclusive, only one of these two should
  2005. * be specified. Enabling both fields will have
  2006. * undetermined results.
  2007. *
  2008. * SPP_HB_DEMAND - Request a user initiated heartbeat
  2009. * to be made immediately.
  2010. *
  2011. * SPP_HB_TIME_IS_ZERO - Specify's that the time for
  2012. * heartbeat delayis to be set to the value of 0
  2013. * milliseconds.
  2014. *
  2015. * SPP_PMTUD_ENABLE - This field will enable PMTU
  2016. * discovery upon the specified address. Note that
  2017. * if the address feild is empty then all addresses
  2018. * on the association are effected.
  2019. *
  2020. * SPP_PMTUD_DISABLE - This field will disable PMTU
  2021. * discovery upon the specified address. Note that
  2022. * if the address feild is empty then all addresses
  2023. * on the association are effected. Not also that
  2024. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  2025. * exclusive. Enabling both will have undetermined
  2026. * results.
  2027. *
  2028. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  2029. * on delayed sack. The time specified in spp_sackdelay
  2030. * is used to specify the sack delay for this address. Note
  2031. * that if spp_address is empty then all addresses will
  2032. * enable delayed sack and take on the sack delay
  2033. * value specified in spp_sackdelay.
  2034. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  2035. * off delayed sack. If the spp_address field is blank then
  2036. * delayed sack is disabled for the entire association. Note
  2037. * also that this field is mutually exclusive to
  2038. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  2039. * results.
  2040. *
  2041. * SPP_IPV6_FLOWLABEL: Setting this flag enables the
  2042. * setting of the IPV6 flow label value. The value is
  2043. * contained in the spp_ipv6_flowlabel field.
  2044. * Upon retrieval, this flag will be set to indicate that
  2045. * the spp_ipv6_flowlabel field has a valid value returned.
  2046. * If a specific destination address is set (in the
  2047. * spp_address field), then the value returned is that of
  2048. * the address. If just an association is specified (and
  2049. * no address), then the association's default flow label
  2050. * is returned. If neither an association nor a destination
  2051. * is specified, then the socket's default flow label is
  2052. * returned. For non-IPv6 sockets, this flag will be left
  2053. * cleared.
  2054. *
  2055. * SPP_DSCP: Setting this flag enables the setting of the
  2056. * Differentiated Services Code Point (DSCP) value
  2057. * associated with either the association or a specific
  2058. * address. The value is obtained in the spp_dscp field.
  2059. * Upon retrieval, this flag will be set to indicate that
  2060. * the spp_dscp field has a valid value returned. If a
  2061. * specific destination address is set when called (in the
  2062. * spp_address field), then that specific destination
  2063. * address's DSCP value is returned. If just an association
  2064. * is specified, then the association's default DSCP is
  2065. * returned. If neither an association nor a destination is
  2066. * specified, then the socket's default DSCP is returned.
  2067. *
  2068. * spp_ipv6_flowlabel
  2069. * - This field is used in conjunction with the
  2070. * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
  2071. * The 20 least significant bits are used for the flow
  2072. * label. This setting has precedence over any IPv6-layer
  2073. * setting.
  2074. *
  2075. * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
  2076. * and contains the DSCP. The 6 most significant bits are
  2077. * used for the DSCP. This setting has precedence over any
  2078. * IPv4- or IPv6- layer setting.
  2079. */
  2080. static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
  2081. struct sctp_transport *trans,
  2082. struct sctp_association *asoc,
  2083. struct sctp_sock *sp,
  2084. int hb_change,
  2085. int pmtud_change,
  2086. int sackdelay_change)
  2087. {
  2088. int error;
  2089. if (params->spp_flags & SPP_HB_DEMAND && trans) {
  2090. error = sctp_primitive_REQUESTHEARTBEAT(trans->asoc->base.net,
  2091. trans->asoc, trans);
  2092. if (error)
  2093. return error;
  2094. }
  2095. /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
  2096. * this field is ignored. Note also that a value of zero indicates
  2097. * the current setting should be left unchanged.
  2098. */
  2099. if (params->spp_flags & SPP_HB_ENABLE) {
  2100. /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
  2101. * set. This lets us use 0 value when this flag
  2102. * is set.
  2103. */
  2104. if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
  2105. params->spp_hbinterval = 0;
  2106. if (params->spp_hbinterval ||
  2107. (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
  2108. if (trans) {
  2109. trans->hbinterval =
  2110. msecs_to_jiffies(params->spp_hbinterval);
  2111. } else if (asoc) {
  2112. asoc->hbinterval =
  2113. msecs_to_jiffies(params->spp_hbinterval);
  2114. } else {
  2115. sp->hbinterval = params->spp_hbinterval;
  2116. }
  2117. }
  2118. }
  2119. if (hb_change) {
  2120. if (trans) {
  2121. trans->param_flags =
  2122. (trans->param_flags & ~SPP_HB) | hb_change;
  2123. } else if (asoc) {
  2124. asoc->param_flags =
  2125. (asoc->param_flags & ~SPP_HB) | hb_change;
  2126. } else {
  2127. sp->param_flags =
  2128. (sp->param_flags & ~SPP_HB) | hb_change;
  2129. }
  2130. }
  2131. /* When Path MTU discovery is disabled the value specified here will
  2132. * be the "fixed" path mtu (i.e. the value of the spp_flags field must
  2133. * include the flag SPP_PMTUD_DISABLE for this field to have any
  2134. * effect).
  2135. */
  2136. if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
  2137. if (trans) {
  2138. trans->pathmtu = params->spp_pathmtu;
  2139. sctp_assoc_sync_pmtu(asoc);
  2140. } else if (asoc) {
  2141. sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
  2142. } else {
  2143. sp->pathmtu = params->spp_pathmtu;
  2144. }
  2145. }
  2146. if (pmtud_change) {
  2147. if (trans) {
  2148. int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
  2149. (params->spp_flags & SPP_PMTUD_ENABLE);
  2150. trans->param_flags =
  2151. (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
  2152. if (update) {
  2153. sctp_transport_pmtu(trans, sctp_opt2sk(sp));
  2154. sctp_assoc_sync_pmtu(asoc);
  2155. }
  2156. } else if (asoc) {
  2157. asoc->param_flags =
  2158. (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
  2159. } else {
  2160. sp->param_flags =
  2161. (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
  2162. }
  2163. }
  2164. /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
  2165. * value of this field is ignored. Note also that a value of zero
  2166. * indicates the current setting should be left unchanged.
  2167. */
  2168. if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
  2169. if (trans) {
  2170. trans->sackdelay =
  2171. msecs_to_jiffies(params->spp_sackdelay);
  2172. } else if (asoc) {
  2173. asoc->sackdelay =
  2174. msecs_to_jiffies(params->spp_sackdelay);
  2175. } else {
  2176. sp->sackdelay = params->spp_sackdelay;
  2177. }
  2178. }
  2179. if (sackdelay_change) {
  2180. if (trans) {
  2181. trans->param_flags =
  2182. (trans->param_flags & ~SPP_SACKDELAY) |
  2183. sackdelay_change;
  2184. } else if (asoc) {
  2185. asoc->param_flags =
  2186. (asoc->param_flags & ~SPP_SACKDELAY) |
  2187. sackdelay_change;
  2188. } else {
  2189. sp->param_flags =
  2190. (sp->param_flags & ~SPP_SACKDELAY) |
  2191. sackdelay_change;
  2192. }
  2193. }
  2194. /* Note that a value of zero indicates the current setting should be
  2195. left unchanged.
  2196. */
  2197. if (params->spp_pathmaxrxt) {
  2198. if (trans) {
  2199. trans->pathmaxrxt = params->spp_pathmaxrxt;
  2200. } else if (asoc) {
  2201. asoc->pathmaxrxt = params->spp_pathmaxrxt;
  2202. } else {
  2203. sp->pathmaxrxt = params->spp_pathmaxrxt;
  2204. }
  2205. }
  2206. if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
  2207. if (trans) {
  2208. if (trans->ipaddr.sa.sa_family == AF_INET6) {
  2209. trans->flowlabel = params->spp_ipv6_flowlabel &
  2210. SCTP_FLOWLABEL_VAL_MASK;
  2211. trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2212. }
  2213. } else if (asoc) {
  2214. struct sctp_transport *t;
  2215. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  2216. transports) {
  2217. if (t->ipaddr.sa.sa_family != AF_INET6)
  2218. continue;
  2219. t->flowlabel = params->spp_ipv6_flowlabel &
  2220. SCTP_FLOWLABEL_VAL_MASK;
  2221. t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2222. }
  2223. asoc->flowlabel = params->spp_ipv6_flowlabel &
  2224. SCTP_FLOWLABEL_VAL_MASK;
  2225. asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2226. } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
  2227. sp->flowlabel = params->spp_ipv6_flowlabel &
  2228. SCTP_FLOWLABEL_VAL_MASK;
  2229. sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2230. }
  2231. }
  2232. if (params->spp_flags & SPP_DSCP) {
  2233. if (trans) {
  2234. trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
  2235. trans->dscp |= SCTP_DSCP_SET_MASK;
  2236. } else if (asoc) {
  2237. struct sctp_transport *t;
  2238. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  2239. transports) {
  2240. t->dscp = params->spp_dscp &
  2241. SCTP_DSCP_VAL_MASK;
  2242. t->dscp |= SCTP_DSCP_SET_MASK;
  2243. }
  2244. asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
  2245. asoc->dscp |= SCTP_DSCP_SET_MASK;
  2246. } else {
  2247. sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
  2248. sp->dscp |= SCTP_DSCP_SET_MASK;
  2249. }
  2250. }
  2251. return 0;
  2252. }
  2253. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  2254. struct sctp_paddrparams *params,
  2255. unsigned int optlen)
  2256. {
  2257. struct sctp_transport *trans = NULL;
  2258. struct sctp_association *asoc = NULL;
  2259. struct sctp_sock *sp = sctp_sk(sk);
  2260. int error;
  2261. int hb_change, pmtud_change, sackdelay_change;
  2262. if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
  2263. spp_ipv6_flowlabel), 4)) {
  2264. if (params->spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
  2265. return -EINVAL;
  2266. } else if (optlen != sizeof(*params)) {
  2267. return -EINVAL;
  2268. }
  2269. /* Validate flags and value parameters. */
  2270. hb_change = params->spp_flags & SPP_HB;
  2271. pmtud_change = params->spp_flags & SPP_PMTUD;
  2272. sackdelay_change = params->spp_flags & SPP_SACKDELAY;
  2273. if (hb_change == SPP_HB ||
  2274. pmtud_change == SPP_PMTUD ||
  2275. sackdelay_change == SPP_SACKDELAY ||
  2276. params->spp_sackdelay > 500 ||
  2277. (params->spp_pathmtu &&
  2278. params->spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
  2279. return -EINVAL;
  2280. /* If an address other than INADDR_ANY is specified, and
  2281. * no transport is found, then the request is invalid.
  2282. */
  2283. if (!sctp_is_any(sk, (union sctp_addr *)&params->spp_address)) {
  2284. trans = sctp_addr_id2transport(sk, &params->spp_address,
  2285. params->spp_assoc_id);
  2286. if (!trans)
  2287. return -EINVAL;
  2288. }
  2289. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  2290. * socket is a one to many style socket, and an association
  2291. * was not found, then the id was invalid.
  2292. */
  2293. asoc = sctp_id2assoc(sk, params->spp_assoc_id);
  2294. if (!asoc && params->spp_assoc_id != SCTP_FUTURE_ASSOC &&
  2295. sctp_style(sk, UDP))
  2296. return -EINVAL;
  2297. /* Heartbeat demand can only be sent on a transport or
  2298. * association, but not a socket.
  2299. */
  2300. if (params->spp_flags & SPP_HB_DEMAND && !trans && !asoc)
  2301. return -EINVAL;
  2302. /* Process parameters. */
  2303. error = sctp_apply_peer_addr_params(params, trans, asoc, sp,
  2304. hb_change, pmtud_change,
  2305. sackdelay_change);
  2306. if (error)
  2307. return error;
  2308. /* If changes are for association, also apply parameters to each
  2309. * transport.
  2310. */
  2311. if (!trans && asoc) {
  2312. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2313. transports) {
  2314. sctp_apply_peer_addr_params(params, trans, asoc, sp,
  2315. hb_change, pmtud_change,
  2316. sackdelay_change);
  2317. }
  2318. }
  2319. return 0;
  2320. }
  2321. static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
  2322. {
  2323. return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
  2324. }
  2325. static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
  2326. {
  2327. return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
  2328. }
  2329. static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
  2330. struct sctp_association *asoc)
  2331. {
  2332. struct sctp_transport *trans;
  2333. if (params->sack_delay) {
  2334. asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
  2335. asoc->param_flags =
  2336. sctp_spp_sackdelay_enable(asoc->param_flags);
  2337. }
  2338. if (params->sack_freq == 1) {
  2339. asoc->param_flags =
  2340. sctp_spp_sackdelay_disable(asoc->param_flags);
  2341. } else if (params->sack_freq > 1) {
  2342. asoc->sackfreq = params->sack_freq;
  2343. asoc->param_flags =
  2344. sctp_spp_sackdelay_enable(asoc->param_flags);
  2345. }
  2346. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2347. transports) {
  2348. if (params->sack_delay) {
  2349. trans->sackdelay = msecs_to_jiffies(params->sack_delay);
  2350. trans->param_flags =
  2351. sctp_spp_sackdelay_enable(trans->param_flags);
  2352. }
  2353. if (params->sack_freq == 1) {
  2354. trans->param_flags =
  2355. sctp_spp_sackdelay_disable(trans->param_flags);
  2356. } else if (params->sack_freq > 1) {
  2357. trans->sackfreq = params->sack_freq;
  2358. trans->param_flags =
  2359. sctp_spp_sackdelay_enable(trans->param_flags);
  2360. }
  2361. }
  2362. }
  2363. /*
  2364. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  2365. *
  2366. * This option will effect the way delayed acks are performed. This
  2367. * option allows you to get or set the delayed ack time, in
  2368. * milliseconds. It also allows changing the delayed ack frequency.
  2369. * Changing the frequency to 1 disables the delayed sack algorithm. If
  2370. * the assoc_id is 0, then this sets or gets the endpoints default
  2371. * values. If the assoc_id field is non-zero, then the set or get
  2372. * effects the specified association for the one to many model (the
  2373. * assoc_id field is ignored by the one to one model). Note that if
  2374. * sack_delay or sack_freq are 0 when setting this option, then the
  2375. * current values will remain unchanged.
  2376. *
  2377. * struct sctp_sack_info {
  2378. * sctp_assoc_t sack_assoc_id;
  2379. * uint32_t sack_delay;
  2380. * uint32_t sack_freq;
  2381. * };
  2382. *
  2383. * sack_assoc_id - This parameter, indicates which association the user
  2384. * is performing an action upon. Note that if this field's value is
  2385. * zero then the endpoints default value is changed (effecting future
  2386. * associations only).
  2387. *
  2388. * sack_delay - This parameter contains the number of milliseconds that
  2389. * the user is requesting the delayed ACK timer be set to. Note that
  2390. * this value is defined in the standard to be between 200 and 500
  2391. * milliseconds.
  2392. *
  2393. * sack_freq - This parameter contains the number of packets that must
  2394. * be received before a sack is sent without waiting for the delay
  2395. * timer to expire. The default value for this is 2, setting this
  2396. * value to 1 will disable the delayed sack algorithm.
  2397. */
  2398. static int __sctp_setsockopt_delayed_ack(struct sock *sk,
  2399. struct sctp_sack_info *params)
  2400. {
  2401. struct sctp_sock *sp = sctp_sk(sk);
  2402. struct sctp_association *asoc;
  2403. /* Validate value parameter. */
  2404. if (params->sack_delay > 500)
  2405. return -EINVAL;
  2406. /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
  2407. * socket is a one to many style socket, and an association
  2408. * was not found, then the id was invalid.
  2409. */
  2410. asoc = sctp_id2assoc(sk, params->sack_assoc_id);
  2411. if (!asoc && params->sack_assoc_id > SCTP_ALL_ASSOC &&
  2412. sctp_style(sk, UDP))
  2413. return -EINVAL;
  2414. if (asoc) {
  2415. sctp_apply_asoc_delayed_ack(params, asoc);
  2416. return 0;
  2417. }
  2418. if (sctp_style(sk, TCP))
  2419. params->sack_assoc_id = SCTP_FUTURE_ASSOC;
  2420. if (params->sack_assoc_id == SCTP_FUTURE_ASSOC ||
  2421. params->sack_assoc_id == SCTP_ALL_ASSOC) {
  2422. if (params->sack_delay) {
  2423. sp->sackdelay = params->sack_delay;
  2424. sp->param_flags =
  2425. sctp_spp_sackdelay_enable(sp->param_flags);
  2426. }
  2427. if (params->sack_freq == 1) {
  2428. sp->param_flags =
  2429. sctp_spp_sackdelay_disable(sp->param_flags);
  2430. } else if (params->sack_freq > 1) {
  2431. sp->sackfreq = params->sack_freq;
  2432. sp->param_flags =
  2433. sctp_spp_sackdelay_enable(sp->param_flags);
  2434. }
  2435. }
  2436. if (params->sack_assoc_id == SCTP_CURRENT_ASSOC ||
  2437. params->sack_assoc_id == SCTP_ALL_ASSOC)
  2438. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  2439. sctp_apply_asoc_delayed_ack(params, asoc);
  2440. return 0;
  2441. }
  2442. static int sctp_setsockopt_delayed_ack(struct sock *sk,
  2443. struct sctp_sack_info *params,
  2444. unsigned int optlen)
  2445. {
  2446. if (optlen == sizeof(struct sctp_assoc_value)) {
  2447. struct sctp_assoc_value *v = (struct sctp_assoc_value *)params;
  2448. struct sctp_sack_info p;
  2449. pr_warn_ratelimited(DEPRECATED
  2450. "%s (pid %d) "
  2451. "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
  2452. "Use struct sctp_sack_info instead\n",
  2453. current->comm, task_pid_nr(current));
  2454. p.sack_assoc_id = v->assoc_id;
  2455. p.sack_delay = v->assoc_value;
  2456. p.sack_freq = v->assoc_value ? 0 : 1;
  2457. return __sctp_setsockopt_delayed_ack(sk, &p);
  2458. }
  2459. if (optlen != sizeof(struct sctp_sack_info))
  2460. return -EINVAL;
  2461. if (params->sack_delay == 0 && params->sack_freq == 0)
  2462. return 0;
  2463. return __sctp_setsockopt_delayed_ack(sk, params);
  2464. }
  2465. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2466. *
  2467. * Applications can specify protocol parameters for the default association
  2468. * initialization. The option name argument to setsockopt() and getsockopt()
  2469. * is SCTP_INITMSG.
  2470. *
  2471. * Setting initialization parameters is effective only on an unconnected
  2472. * socket (for UDP-style sockets only future associations are effected
  2473. * by the change). With TCP-style sockets, this option is inherited by
  2474. * sockets derived from a listener socket.
  2475. */
  2476. static int sctp_setsockopt_initmsg(struct sock *sk, struct sctp_initmsg *sinit,
  2477. unsigned int optlen)
  2478. {
  2479. struct sctp_sock *sp = sctp_sk(sk);
  2480. if (optlen != sizeof(struct sctp_initmsg))
  2481. return -EINVAL;
  2482. if (sinit->sinit_num_ostreams)
  2483. sp->initmsg.sinit_num_ostreams = sinit->sinit_num_ostreams;
  2484. if (sinit->sinit_max_instreams)
  2485. sp->initmsg.sinit_max_instreams = sinit->sinit_max_instreams;
  2486. if (sinit->sinit_max_attempts)
  2487. sp->initmsg.sinit_max_attempts = sinit->sinit_max_attempts;
  2488. if (sinit->sinit_max_init_timeo)
  2489. sp->initmsg.sinit_max_init_timeo = sinit->sinit_max_init_timeo;
  2490. return 0;
  2491. }
  2492. /*
  2493. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2494. *
  2495. * Applications that wish to use the sendto() system call may wish to
  2496. * specify a default set of parameters that would normally be supplied
  2497. * through the inclusion of ancillary data. This socket option allows
  2498. * such an application to set the default sctp_sndrcvinfo structure.
  2499. * The application that wishes to use this socket option simply passes
  2500. * in to this call the sctp_sndrcvinfo structure defined in Section
  2501. * 5.2.2) The input parameters accepted by this call include
  2502. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2503. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2504. * to this call if the caller is using the UDP model.
  2505. */
  2506. static int sctp_setsockopt_default_send_param(struct sock *sk,
  2507. struct sctp_sndrcvinfo *info,
  2508. unsigned int optlen)
  2509. {
  2510. struct sctp_sock *sp = sctp_sk(sk);
  2511. struct sctp_association *asoc;
  2512. if (optlen != sizeof(*info))
  2513. return -EINVAL;
  2514. if (info->sinfo_flags &
  2515. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  2516. SCTP_ABORT | SCTP_EOF))
  2517. return -EINVAL;
  2518. asoc = sctp_id2assoc(sk, info->sinfo_assoc_id);
  2519. if (!asoc && info->sinfo_assoc_id > SCTP_ALL_ASSOC &&
  2520. sctp_style(sk, UDP))
  2521. return -EINVAL;
  2522. if (asoc) {
  2523. asoc->default_stream = info->sinfo_stream;
  2524. asoc->default_flags = info->sinfo_flags;
  2525. asoc->default_ppid = info->sinfo_ppid;
  2526. asoc->default_context = info->sinfo_context;
  2527. asoc->default_timetolive = info->sinfo_timetolive;
  2528. return 0;
  2529. }
  2530. if (sctp_style(sk, TCP))
  2531. info->sinfo_assoc_id = SCTP_FUTURE_ASSOC;
  2532. if (info->sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
  2533. info->sinfo_assoc_id == SCTP_ALL_ASSOC) {
  2534. sp->default_stream = info->sinfo_stream;
  2535. sp->default_flags = info->sinfo_flags;
  2536. sp->default_ppid = info->sinfo_ppid;
  2537. sp->default_context = info->sinfo_context;
  2538. sp->default_timetolive = info->sinfo_timetolive;
  2539. }
  2540. if (info->sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
  2541. info->sinfo_assoc_id == SCTP_ALL_ASSOC) {
  2542. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  2543. asoc->default_stream = info->sinfo_stream;
  2544. asoc->default_flags = info->sinfo_flags;
  2545. asoc->default_ppid = info->sinfo_ppid;
  2546. asoc->default_context = info->sinfo_context;
  2547. asoc->default_timetolive = info->sinfo_timetolive;
  2548. }
  2549. }
  2550. return 0;
  2551. }
  2552. /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
  2553. * (SCTP_DEFAULT_SNDINFO)
  2554. */
  2555. static int sctp_setsockopt_default_sndinfo(struct sock *sk,
  2556. struct sctp_sndinfo *info,
  2557. unsigned int optlen)
  2558. {
  2559. struct sctp_sock *sp = sctp_sk(sk);
  2560. struct sctp_association *asoc;
  2561. if (optlen != sizeof(*info))
  2562. return -EINVAL;
  2563. if (info->snd_flags &
  2564. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  2565. SCTP_ABORT | SCTP_EOF))
  2566. return -EINVAL;
  2567. asoc = sctp_id2assoc(sk, info->snd_assoc_id);
  2568. if (!asoc && info->snd_assoc_id > SCTP_ALL_ASSOC &&
  2569. sctp_style(sk, UDP))
  2570. return -EINVAL;
  2571. if (asoc) {
  2572. asoc->default_stream = info->snd_sid;
  2573. asoc->default_flags = info->snd_flags;
  2574. asoc->default_ppid = info->snd_ppid;
  2575. asoc->default_context = info->snd_context;
  2576. return 0;
  2577. }
  2578. if (sctp_style(sk, TCP))
  2579. info->snd_assoc_id = SCTP_FUTURE_ASSOC;
  2580. if (info->snd_assoc_id == SCTP_FUTURE_ASSOC ||
  2581. info->snd_assoc_id == SCTP_ALL_ASSOC) {
  2582. sp->default_stream = info->snd_sid;
  2583. sp->default_flags = info->snd_flags;
  2584. sp->default_ppid = info->snd_ppid;
  2585. sp->default_context = info->snd_context;
  2586. }
  2587. if (info->snd_assoc_id == SCTP_CURRENT_ASSOC ||
  2588. info->snd_assoc_id == SCTP_ALL_ASSOC) {
  2589. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  2590. asoc->default_stream = info->snd_sid;
  2591. asoc->default_flags = info->snd_flags;
  2592. asoc->default_ppid = info->snd_ppid;
  2593. asoc->default_context = info->snd_context;
  2594. }
  2595. }
  2596. return 0;
  2597. }
  2598. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2599. *
  2600. * Requests that the local SCTP stack use the enclosed peer address as
  2601. * the association primary. The enclosed address must be one of the
  2602. * association peer's addresses.
  2603. */
  2604. static int sctp_setsockopt_primary_addr(struct sock *sk, struct sctp_prim *prim,
  2605. unsigned int optlen)
  2606. {
  2607. struct sctp_transport *trans;
  2608. struct sctp_af *af;
  2609. int err;
  2610. if (optlen != sizeof(struct sctp_prim))
  2611. return -EINVAL;
  2612. /* Allow security module to validate address but need address len. */
  2613. af = sctp_get_af_specific(prim->ssp_addr.ss_family);
  2614. if (!af)
  2615. return -EINVAL;
  2616. err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
  2617. (struct sockaddr *)&prim->ssp_addr,
  2618. af->sockaddr_len);
  2619. if (err)
  2620. return err;
  2621. trans = sctp_addr_id2transport(sk, &prim->ssp_addr, prim->ssp_assoc_id);
  2622. if (!trans)
  2623. return -EINVAL;
  2624. sctp_assoc_set_primary(trans->asoc, trans);
  2625. return 0;
  2626. }
  2627. /*
  2628. * 7.1.5 SCTP_NODELAY
  2629. *
  2630. * Turn on/off any Nagle-like algorithm. This means that packets are
  2631. * generally sent as soon as possible and no unnecessary delays are
  2632. * introduced, at the cost of more packets in the network. Expects an
  2633. * integer boolean flag.
  2634. */
  2635. static int sctp_setsockopt_nodelay(struct sock *sk, int *val,
  2636. unsigned int optlen)
  2637. {
  2638. if (optlen < sizeof(int))
  2639. return -EINVAL;
  2640. sctp_sk(sk)->nodelay = (*val == 0) ? 0 : 1;
  2641. return 0;
  2642. }
  2643. /*
  2644. *
  2645. * 7.1.1 SCTP_RTOINFO
  2646. *
  2647. * The protocol parameters used to initialize and bound retransmission
  2648. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2649. * and modify these parameters.
  2650. * All parameters are time values, in milliseconds. A value of 0, when
  2651. * modifying the parameters, indicates that the current value should not
  2652. * be changed.
  2653. *
  2654. */
  2655. static int sctp_setsockopt_rtoinfo(struct sock *sk,
  2656. struct sctp_rtoinfo *rtoinfo,
  2657. unsigned int optlen)
  2658. {
  2659. struct sctp_association *asoc;
  2660. unsigned long rto_min, rto_max;
  2661. struct sctp_sock *sp = sctp_sk(sk);
  2662. if (optlen != sizeof (struct sctp_rtoinfo))
  2663. return -EINVAL;
  2664. asoc = sctp_id2assoc(sk, rtoinfo->srto_assoc_id);
  2665. /* Set the values to the specific association */
  2666. if (!asoc && rtoinfo->srto_assoc_id != SCTP_FUTURE_ASSOC &&
  2667. sctp_style(sk, UDP))
  2668. return -EINVAL;
  2669. rto_max = rtoinfo->srto_max;
  2670. rto_min = rtoinfo->srto_min;
  2671. if (rto_max)
  2672. rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
  2673. else
  2674. rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
  2675. if (rto_min)
  2676. rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
  2677. else
  2678. rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
  2679. if (rto_min > rto_max)
  2680. return -EINVAL;
  2681. if (asoc) {
  2682. if (rtoinfo->srto_initial != 0)
  2683. asoc->rto_initial =
  2684. msecs_to_jiffies(rtoinfo->srto_initial);
  2685. asoc->rto_max = rto_max;
  2686. asoc->rto_min = rto_min;
  2687. } else {
  2688. /* If there is no association or the association-id = 0
  2689. * set the values to the endpoint.
  2690. */
  2691. if (rtoinfo->srto_initial != 0)
  2692. sp->rtoinfo.srto_initial = rtoinfo->srto_initial;
  2693. sp->rtoinfo.srto_max = rto_max;
  2694. sp->rtoinfo.srto_min = rto_min;
  2695. }
  2696. return 0;
  2697. }
  2698. /*
  2699. *
  2700. * 7.1.2 SCTP_ASSOCINFO
  2701. *
  2702. * This option is used to tune the maximum retransmission attempts
  2703. * of the association.
  2704. * Returns an error if the new association retransmission value is
  2705. * greater than the sum of the retransmission value of the peer.
  2706. * See [SCTP] for more information.
  2707. *
  2708. */
  2709. static int sctp_setsockopt_associnfo(struct sock *sk,
  2710. struct sctp_assocparams *assocparams,
  2711. unsigned int optlen)
  2712. {
  2713. struct sctp_association *asoc;
  2714. if (optlen != sizeof(struct sctp_assocparams))
  2715. return -EINVAL;
  2716. asoc = sctp_id2assoc(sk, assocparams->sasoc_assoc_id);
  2717. if (!asoc && assocparams->sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
  2718. sctp_style(sk, UDP))
  2719. return -EINVAL;
  2720. /* Set the values to the specific association */
  2721. if (asoc) {
  2722. if (assocparams->sasoc_asocmaxrxt != 0) {
  2723. __u32 path_sum = 0;
  2724. int paths = 0;
  2725. struct sctp_transport *peer_addr;
  2726. list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
  2727. transports) {
  2728. path_sum += peer_addr->pathmaxrxt;
  2729. paths++;
  2730. }
  2731. /* Only validate asocmaxrxt if we have more than
  2732. * one path/transport. We do this because path
  2733. * retransmissions are only counted when we have more
  2734. * then one path.
  2735. */
  2736. if (paths > 1 &&
  2737. assocparams->sasoc_asocmaxrxt > path_sum)
  2738. return -EINVAL;
  2739. asoc->max_retrans = assocparams->sasoc_asocmaxrxt;
  2740. }
  2741. if (assocparams->sasoc_cookie_life != 0)
  2742. asoc->cookie_life =
  2743. ms_to_ktime(assocparams->sasoc_cookie_life);
  2744. } else {
  2745. /* Set the values to the endpoint */
  2746. struct sctp_sock *sp = sctp_sk(sk);
  2747. if (assocparams->sasoc_asocmaxrxt != 0)
  2748. sp->assocparams.sasoc_asocmaxrxt =
  2749. assocparams->sasoc_asocmaxrxt;
  2750. if (assocparams->sasoc_cookie_life != 0)
  2751. sp->assocparams.sasoc_cookie_life =
  2752. assocparams->sasoc_cookie_life;
  2753. }
  2754. return 0;
  2755. }
  2756. /*
  2757. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  2758. *
  2759. * This socket option is a boolean flag which turns on or off mapped V4
  2760. * addresses. If this option is turned on and the socket is type
  2761. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  2762. * If this option is turned off, then no mapping will be done of V4
  2763. * addresses and a user will receive both PF_INET6 and PF_INET type
  2764. * addresses on the socket.
  2765. */
  2766. static int sctp_setsockopt_mappedv4(struct sock *sk, int *val,
  2767. unsigned int optlen)
  2768. {
  2769. struct sctp_sock *sp = sctp_sk(sk);
  2770. if (optlen < sizeof(int))
  2771. return -EINVAL;
  2772. if (*val)
  2773. sp->v4mapped = 1;
  2774. else
  2775. sp->v4mapped = 0;
  2776. return 0;
  2777. }
  2778. /*
  2779. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  2780. * This option will get or set the maximum size to put in any outgoing
  2781. * SCTP DATA chunk. If a message is larger than this size it will be
  2782. * fragmented by SCTP into the specified size. Note that the underlying
  2783. * SCTP implementation may fragment into smaller sized chunks when the
  2784. * PMTU of the underlying association is smaller than the value set by
  2785. * the user. The default value for this option is '0' which indicates
  2786. * the user is NOT limiting fragmentation and only the PMTU will effect
  2787. * SCTP's choice of DATA chunk size. Note also that values set larger
  2788. * than the maximum size of an IP datagram will effectively let SCTP
  2789. * control fragmentation (i.e. the same as setting this option to 0).
  2790. *
  2791. * The following structure is used to access and modify this parameter:
  2792. *
  2793. * struct sctp_assoc_value {
  2794. * sctp_assoc_t assoc_id;
  2795. * uint32_t assoc_value;
  2796. * };
  2797. *
  2798. * assoc_id: This parameter is ignored for one-to-one style sockets.
  2799. * For one-to-many style sockets this parameter indicates which
  2800. * association the user is performing an action upon. Note that if
  2801. * this field's value is zero then the endpoints default value is
  2802. * changed (effecting future associations only).
  2803. * assoc_value: This parameter specifies the maximum size in bytes.
  2804. */
  2805. static int sctp_setsockopt_maxseg(struct sock *sk,
  2806. struct sctp_assoc_value *params,
  2807. unsigned int optlen)
  2808. {
  2809. struct sctp_sock *sp = sctp_sk(sk);
  2810. struct sctp_association *asoc;
  2811. sctp_assoc_t assoc_id;
  2812. int val;
  2813. if (optlen == sizeof(int)) {
  2814. pr_warn_ratelimited(DEPRECATED
  2815. "%s (pid %d) "
  2816. "Use of int in maxseg socket option.\n"
  2817. "Use struct sctp_assoc_value instead\n",
  2818. current->comm, task_pid_nr(current));
  2819. assoc_id = SCTP_FUTURE_ASSOC;
  2820. val = *(int *)params;
  2821. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2822. assoc_id = params->assoc_id;
  2823. val = params->assoc_value;
  2824. } else {
  2825. return -EINVAL;
  2826. }
  2827. asoc = sctp_id2assoc(sk, assoc_id);
  2828. if (!asoc && assoc_id != SCTP_FUTURE_ASSOC &&
  2829. sctp_style(sk, UDP))
  2830. return -EINVAL;
  2831. if (val) {
  2832. int min_len, max_len;
  2833. __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
  2834. sizeof(struct sctp_data_chunk);
  2835. min_len = sctp_min_frag_point(sp, datasize);
  2836. max_len = SCTP_MAX_CHUNK_LEN - datasize;
  2837. if (val < min_len || val > max_len)
  2838. return -EINVAL;
  2839. }
  2840. if (asoc) {
  2841. asoc->user_frag = val;
  2842. sctp_assoc_update_frag_point(asoc);
  2843. } else {
  2844. sp->user_frag = val;
  2845. }
  2846. return 0;
  2847. }
  2848. /*
  2849. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2850. *
  2851. * Requests that the peer mark the enclosed address as the association
  2852. * primary. The enclosed address must be one of the association's
  2853. * locally bound addresses. The following structure is used to make a
  2854. * set primary request:
  2855. */
  2856. static int sctp_setsockopt_peer_primary_addr(struct sock *sk,
  2857. struct sctp_setpeerprim *prim,
  2858. unsigned int optlen)
  2859. {
  2860. struct sctp_sock *sp;
  2861. struct sctp_association *asoc = NULL;
  2862. struct sctp_chunk *chunk;
  2863. struct sctp_af *af;
  2864. int err;
  2865. sp = sctp_sk(sk);
  2866. if (!sp->ep->asconf_enable)
  2867. return -EPERM;
  2868. if (optlen != sizeof(struct sctp_setpeerprim))
  2869. return -EINVAL;
  2870. asoc = sctp_id2assoc(sk, prim->sspp_assoc_id);
  2871. if (!asoc)
  2872. return -EINVAL;
  2873. if (!asoc->peer.asconf_capable)
  2874. return -EPERM;
  2875. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2876. return -EPERM;
  2877. if (!sctp_state(asoc, ESTABLISHED))
  2878. return -ENOTCONN;
  2879. af = sctp_get_af_specific(prim->sspp_addr.ss_family);
  2880. if (!af)
  2881. return -EINVAL;
  2882. if (!af->addr_valid((union sctp_addr *)&prim->sspp_addr, sp, NULL))
  2883. return -EADDRNOTAVAIL;
  2884. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim->sspp_addr))
  2885. return -EADDRNOTAVAIL;
  2886. /* Allow security module to validate address. */
  2887. err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
  2888. (struct sockaddr *)&prim->sspp_addr,
  2889. af->sockaddr_len);
  2890. if (err)
  2891. return err;
  2892. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2893. chunk = sctp_make_asconf_set_prim(asoc,
  2894. (union sctp_addr *)&prim->sspp_addr);
  2895. if (!chunk)
  2896. return -ENOMEM;
  2897. err = sctp_send_asconf(asoc, chunk);
  2898. pr_debug("%s: we set peer primary addr primitively\n", __func__);
  2899. return err;
  2900. }
  2901. static int sctp_setsockopt_adaptation_layer(struct sock *sk,
  2902. struct sctp_setadaptation *adapt,
  2903. unsigned int optlen)
  2904. {
  2905. if (optlen != sizeof(struct sctp_setadaptation))
  2906. return -EINVAL;
  2907. sctp_sk(sk)->adaptation_ind = adapt->ssb_adaptation_ind;
  2908. return 0;
  2909. }
  2910. /*
  2911. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  2912. *
  2913. * The context field in the sctp_sndrcvinfo structure is normally only
  2914. * used when a failed message is retrieved holding the value that was
  2915. * sent down on the actual send call. This option allows the setting of
  2916. * a default context on an association basis that will be received on
  2917. * reading messages from the peer. This is especially helpful in the
  2918. * one-2-many model for an application to keep some reference to an
  2919. * internal state machine that is processing messages on the
  2920. * association. Note that the setting of this value only effects
  2921. * received messages from the peer and does not effect the value that is
  2922. * saved with outbound messages.
  2923. */
  2924. static int sctp_setsockopt_context(struct sock *sk,
  2925. struct sctp_assoc_value *params,
  2926. unsigned int optlen)
  2927. {
  2928. struct sctp_sock *sp = sctp_sk(sk);
  2929. struct sctp_association *asoc;
  2930. if (optlen != sizeof(struct sctp_assoc_value))
  2931. return -EINVAL;
  2932. asoc = sctp_id2assoc(sk, params->assoc_id);
  2933. if (!asoc && params->assoc_id > SCTP_ALL_ASSOC &&
  2934. sctp_style(sk, UDP))
  2935. return -EINVAL;
  2936. if (asoc) {
  2937. asoc->default_rcv_context = params->assoc_value;
  2938. return 0;
  2939. }
  2940. if (sctp_style(sk, TCP))
  2941. params->assoc_id = SCTP_FUTURE_ASSOC;
  2942. if (params->assoc_id == SCTP_FUTURE_ASSOC ||
  2943. params->assoc_id == SCTP_ALL_ASSOC)
  2944. sp->default_rcv_context = params->assoc_value;
  2945. if (params->assoc_id == SCTP_CURRENT_ASSOC ||
  2946. params->assoc_id == SCTP_ALL_ASSOC)
  2947. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  2948. asoc->default_rcv_context = params->assoc_value;
  2949. return 0;
  2950. }
  2951. /*
  2952. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  2953. *
  2954. * This options will at a minimum specify if the implementation is doing
  2955. * fragmented interleave. Fragmented interleave, for a one to many
  2956. * socket, is when subsequent calls to receive a message may return
  2957. * parts of messages from different associations. Some implementations
  2958. * may allow you to turn this value on or off. If so, when turned off,
  2959. * no fragment interleave will occur (which will cause a head of line
  2960. * blocking amongst multiple associations sharing the same one to many
  2961. * socket). When this option is turned on, then each receive call may
  2962. * come from a different association (thus the user must receive data
  2963. * with the extended calls (e.g. sctp_recvmsg) to keep track of which
  2964. * association each receive belongs to.
  2965. *
  2966. * This option takes a boolean value. A non-zero value indicates that
  2967. * fragmented interleave is on. A value of zero indicates that
  2968. * fragmented interleave is off.
  2969. *
  2970. * Note that it is important that an implementation that allows this
  2971. * option to be turned on, have it off by default. Otherwise an unaware
  2972. * application using the one to many model may become confused and act
  2973. * incorrectly.
  2974. */
  2975. static int sctp_setsockopt_fragment_interleave(struct sock *sk, int *val,
  2976. unsigned int optlen)
  2977. {
  2978. if (optlen != sizeof(int))
  2979. return -EINVAL;
  2980. sctp_sk(sk)->frag_interleave = !!*val;
  2981. if (!sctp_sk(sk)->frag_interleave)
  2982. sctp_sk(sk)->ep->intl_enable = 0;
  2983. return 0;
  2984. }
  2985. /*
  2986. * 8.1.21. Set or Get the SCTP Partial Delivery Point
  2987. * (SCTP_PARTIAL_DELIVERY_POINT)
  2988. *
  2989. * This option will set or get the SCTP partial delivery point. This
  2990. * point is the size of a message where the partial delivery API will be
  2991. * invoked to help free up rwnd space for the peer. Setting this to a
  2992. * lower value will cause partial deliveries to happen more often. The
  2993. * calls argument is an integer that sets or gets the partial delivery
  2994. * point. Note also that the call will fail if the user attempts to set
  2995. * this value larger than the socket receive buffer size.
  2996. *
  2997. * Note that any single message having a length smaller than or equal to
  2998. * the SCTP partial delivery point will be delivered in one single read
  2999. * call as long as the user provided buffer is large enough to hold the
  3000. * message.
  3001. */
  3002. static int sctp_setsockopt_partial_delivery_point(struct sock *sk, u32 *val,
  3003. unsigned int optlen)
  3004. {
  3005. if (optlen != sizeof(u32))
  3006. return -EINVAL;
  3007. /* Note: We double the receive buffer from what the user sets
  3008. * it to be, also initial rwnd is based on rcvbuf/2.
  3009. */
  3010. if (*val > (sk->sk_rcvbuf >> 1))
  3011. return -EINVAL;
  3012. sctp_sk(sk)->pd_point = *val;
  3013. return 0; /* is this the right error code? */
  3014. }
  3015. /*
  3016. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  3017. *
  3018. * This option will allow a user to change the maximum burst of packets
  3019. * that can be emitted by this association. Note that the default value
  3020. * is 4, and some implementations may restrict this setting so that it
  3021. * can only be lowered.
  3022. *
  3023. * NOTE: This text doesn't seem right. Do this on a socket basis with
  3024. * future associations inheriting the socket value.
  3025. */
  3026. static int sctp_setsockopt_maxburst(struct sock *sk,
  3027. struct sctp_assoc_value *params,
  3028. unsigned int optlen)
  3029. {
  3030. struct sctp_sock *sp = sctp_sk(sk);
  3031. struct sctp_association *asoc;
  3032. sctp_assoc_t assoc_id;
  3033. u32 assoc_value;
  3034. if (optlen == sizeof(int)) {
  3035. pr_warn_ratelimited(DEPRECATED
  3036. "%s (pid %d) "
  3037. "Use of int in max_burst socket option deprecated.\n"
  3038. "Use struct sctp_assoc_value instead\n",
  3039. current->comm, task_pid_nr(current));
  3040. assoc_id = SCTP_FUTURE_ASSOC;
  3041. assoc_value = *((int *)params);
  3042. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  3043. assoc_id = params->assoc_id;
  3044. assoc_value = params->assoc_value;
  3045. } else
  3046. return -EINVAL;
  3047. asoc = sctp_id2assoc(sk, assoc_id);
  3048. if (!asoc && assoc_id > SCTP_ALL_ASSOC && sctp_style(sk, UDP))
  3049. return -EINVAL;
  3050. if (asoc) {
  3051. asoc->max_burst = assoc_value;
  3052. return 0;
  3053. }
  3054. if (sctp_style(sk, TCP))
  3055. assoc_id = SCTP_FUTURE_ASSOC;
  3056. if (assoc_id == SCTP_FUTURE_ASSOC || assoc_id == SCTP_ALL_ASSOC)
  3057. sp->max_burst = assoc_value;
  3058. if (assoc_id == SCTP_CURRENT_ASSOC || assoc_id == SCTP_ALL_ASSOC)
  3059. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  3060. asoc->max_burst = assoc_value;
  3061. return 0;
  3062. }
  3063. /*
  3064. * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
  3065. *
  3066. * This set option adds a chunk type that the user is requesting to be
  3067. * received only in an authenticated way. Changes to the list of chunks
  3068. * will only effect future associations on the socket.
  3069. */
  3070. static int sctp_setsockopt_auth_chunk(struct sock *sk,
  3071. struct sctp_authchunk *val,
  3072. unsigned int optlen)
  3073. {
  3074. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3075. if (!ep->auth_enable)
  3076. return -EACCES;
  3077. if (optlen != sizeof(struct sctp_authchunk))
  3078. return -EINVAL;
  3079. switch (val->sauth_chunk) {
  3080. case SCTP_CID_INIT:
  3081. case SCTP_CID_INIT_ACK:
  3082. case SCTP_CID_SHUTDOWN_COMPLETE:
  3083. case SCTP_CID_AUTH:
  3084. return -EINVAL;
  3085. }
  3086. /* add this chunk id to the endpoint */
  3087. return sctp_auth_ep_add_chunkid(ep, val->sauth_chunk);
  3088. }
  3089. /*
  3090. * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
  3091. *
  3092. * This option gets or sets the list of HMAC algorithms that the local
  3093. * endpoint requires the peer to use.
  3094. */
  3095. static int sctp_setsockopt_hmac_ident(struct sock *sk,
  3096. struct sctp_hmacalgo *hmacs,
  3097. unsigned int optlen)
  3098. {
  3099. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3100. u32 idents;
  3101. if (!ep->auth_enable)
  3102. return -EACCES;
  3103. if (optlen < sizeof(struct sctp_hmacalgo))
  3104. return -EINVAL;
  3105. optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
  3106. SCTP_AUTH_NUM_HMACS * sizeof(u16));
  3107. idents = hmacs->shmac_num_idents;
  3108. if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
  3109. (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo)))
  3110. return -EINVAL;
  3111. return sctp_auth_ep_set_hmacs(ep, hmacs);
  3112. }
  3113. /*
  3114. * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
  3115. *
  3116. * This option will set a shared secret key which is used to build an
  3117. * association shared key.
  3118. */
  3119. static int sctp_setsockopt_auth_key(struct sock *sk,
  3120. struct sctp_authkey *authkey,
  3121. unsigned int optlen)
  3122. {
  3123. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3124. struct sctp_association *asoc;
  3125. int ret = -EINVAL;
  3126. if (optlen <= sizeof(struct sctp_authkey))
  3127. return -EINVAL;
  3128. /* authkey->sca_keylength is u16, so optlen can't be bigger than
  3129. * this.
  3130. */
  3131. optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
  3132. if (authkey->sca_keylength > optlen - sizeof(*authkey))
  3133. goto out;
  3134. asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
  3135. if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
  3136. sctp_style(sk, UDP))
  3137. goto out;
  3138. if (asoc) {
  3139. ret = sctp_auth_set_key(ep, asoc, authkey);
  3140. goto out;
  3141. }
  3142. if (sctp_style(sk, TCP))
  3143. authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
  3144. if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
  3145. authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
  3146. ret = sctp_auth_set_key(ep, asoc, authkey);
  3147. if (ret)
  3148. goto out;
  3149. }
  3150. ret = 0;
  3151. if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
  3152. authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
  3153. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3154. int res = sctp_auth_set_key(ep, asoc, authkey);
  3155. if (res && !ret)
  3156. ret = res;
  3157. }
  3158. }
  3159. out:
  3160. memzero_explicit(authkey, optlen);
  3161. return ret;
  3162. }
  3163. /*
  3164. * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
  3165. *
  3166. * This option will get or set the active shared key to be used to build
  3167. * the association shared key.
  3168. */
  3169. static int sctp_setsockopt_active_key(struct sock *sk,
  3170. struct sctp_authkeyid *val,
  3171. unsigned int optlen)
  3172. {
  3173. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3174. struct sctp_association *asoc;
  3175. int ret = 0;
  3176. if (optlen != sizeof(struct sctp_authkeyid))
  3177. return -EINVAL;
  3178. asoc = sctp_id2assoc(sk, val->scact_assoc_id);
  3179. if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC &&
  3180. sctp_style(sk, UDP))
  3181. return -EINVAL;
  3182. if (asoc)
  3183. return sctp_auth_set_active_key(ep, asoc, val->scact_keynumber);
  3184. if (sctp_style(sk, TCP))
  3185. val->scact_assoc_id = SCTP_FUTURE_ASSOC;
  3186. if (val->scact_assoc_id == SCTP_FUTURE_ASSOC ||
  3187. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3188. ret = sctp_auth_set_active_key(ep, asoc, val->scact_keynumber);
  3189. if (ret)
  3190. return ret;
  3191. }
  3192. if (val->scact_assoc_id == SCTP_CURRENT_ASSOC ||
  3193. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3194. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3195. int res = sctp_auth_set_active_key(ep, asoc,
  3196. val->scact_keynumber);
  3197. if (res && !ret)
  3198. ret = res;
  3199. }
  3200. }
  3201. return ret;
  3202. }
  3203. /*
  3204. * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
  3205. *
  3206. * This set option will delete a shared secret key from use.
  3207. */
  3208. static int sctp_setsockopt_del_key(struct sock *sk,
  3209. struct sctp_authkeyid *val,
  3210. unsigned int optlen)
  3211. {
  3212. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3213. struct sctp_association *asoc;
  3214. int ret = 0;
  3215. if (optlen != sizeof(struct sctp_authkeyid))
  3216. return -EINVAL;
  3217. asoc = sctp_id2assoc(sk, val->scact_assoc_id);
  3218. if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC &&
  3219. sctp_style(sk, UDP))
  3220. return -EINVAL;
  3221. if (asoc)
  3222. return sctp_auth_del_key_id(ep, asoc, val->scact_keynumber);
  3223. if (sctp_style(sk, TCP))
  3224. val->scact_assoc_id = SCTP_FUTURE_ASSOC;
  3225. if (val->scact_assoc_id == SCTP_FUTURE_ASSOC ||
  3226. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3227. ret = sctp_auth_del_key_id(ep, asoc, val->scact_keynumber);
  3228. if (ret)
  3229. return ret;
  3230. }
  3231. if (val->scact_assoc_id == SCTP_CURRENT_ASSOC ||
  3232. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3233. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3234. int res = sctp_auth_del_key_id(ep, asoc,
  3235. val->scact_keynumber);
  3236. if (res && !ret)
  3237. ret = res;
  3238. }
  3239. }
  3240. return ret;
  3241. }
  3242. /*
  3243. * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
  3244. *
  3245. * This set option will deactivate a shared secret key.
  3246. */
  3247. static int sctp_setsockopt_deactivate_key(struct sock *sk,
  3248. struct sctp_authkeyid *val,
  3249. unsigned int optlen)
  3250. {
  3251. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3252. struct sctp_association *asoc;
  3253. int ret = 0;
  3254. if (optlen != sizeof(struct sctp_authkeyid))
  3255. return -EINVAL;
  3256. asoc = sctp_id2assoc(sk, val->scact_assoc_id);
  3257. if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC &&
  3258. sctp_style(sk, UDP))
  3259. return -EINVAL;
  3260. if (asoc)
  3261. return sctp_auth_deact_key_id(ep, asoc, val->scact_keynumber);
  3262. if (sctp_style(sk, TCP))
  3263. val->scact_assoc_id = SCTP_FUTURE_ASSOC;
  3264. if (val->scact_assoc_id == SCTP_FUTURE_ASSOC ||
  3265. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3266. ret = sctp_auth_deact_key_id(ep, asoc, val->scact_keynumber);
  3267. if (ret)
  3268. return ret;
  3269. }
  3270. if (val->scact_assoc_id == SCTP_CURRENT_ASSOC ||
  3271. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3272. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3273. int res = sctp_auth_deact_key_id(ep, asoc,
  3274. val->scact_keynumber);
  3275. if (res && !ret)
  3276. ret = res;
  3277. }
  3278. }
  3279. return ret;
  3280. }
  3281. /*
  3282. * 8.1.23 SCTP_AUTO_ASCONF
  3283. *
  3284. * This option will enable or disable the use of the automatic generation of
  3285. * ASCONF chunks to add and delete addresses to an existing association. Note
  3286. * that this option has two caveats namely: a) it only affects sockets that
  3287. * are bound to all addresses available to the SCTP stack, and b) the system
  3288. * administrator may have an overriding control that turns the ASCONF feature
  3289. * off no matter what setting the socket option may have.
  3290. * This option expects an integer boolean flag, where a non-zero value turns on
  3291. * the option, and a zero value turns off the option.
  3292. * Note. In this implementation, socket operation overrides default parameter
  3293. * being set by sysctl as well as FreeBSD implementation
  3294. */
  3295. static int sctp_setsockopt_auto_asconf(struct sock *sk, int *val,
  3296. unsigned int optlen)
  3297. {
  3298. struct sctp_sock *sp = sctp_sk(sk);
  3299. if (optlen < sizeof(int))
  3300. return -EINVAL;
  3301. if (!sctp_is_ep_boundall(sk) && *val)
  3302. return -EINVAL;
  3303. if ((*val && sp->do_auto_asconf) || (!*val && !sp->do_auto_asconf))
  3304. return 0;
  3305. spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3306. if (*val == 0 && sp->do_auto_asconf) {
  3307. list_del(&sp->auto_asconf_list);
  3308. sp->do_auto_asconf = 0;
  3309. } else if (*val && !sp->do_auto_asconf) {
  3310. list_add_tail(&sp->auto_asconf_list,
  3311. &sock_net(sk)->sctp.auto_asconf_splist);
  3312. sp->do_auto_asconf = 1;
  3313. }
  3314. spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3315. return 0;
  3316. }
  3317. /*
  3318. * SCTP_PEER_ADDR_THLDS
  3319. *
  3320. * This option allows us to alter the partially failed threshold for one or all
  3321. * transports in an association. See Section 6.1 of:
  3322. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  3323. */
  3324. static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
  3325. struct sctp_paddrthlds_v2 *val,
  3326. unsigned int optlen, bool v2)
  3327. {
  3328. struct sctp_transport *trans;
  3329. struct sctp_association *asoc;
  3330. int len;
  3331. len = v2 ? sizeof(*val) : sizeof(struct sctp_paddrthlds);
  3332. if (optlen < len)
  3333. return -EINVAL;
  3334. if (v2 && val->spt_pathpfthld > val->spt_pathcpthld)
  3335. return -EINVAL;
  3336. if (!sctp_is_any(sk, (const union sctp_addr *)&val->spt_address)) {
  3337. trans = sctp_addr_id2transport(sk, &val->spt_address,
  3338. val->spt_assoc_id);
  3339. if (!trans)
  3340. return -ENOENT;
  3341. if (val->spt_pathmaxrxt)
  3342. trans->pathmaxrxt = val->spt_pathmaxrxt;
  3343. if (v2)
  3344. trans->ps_retrans = val->spt_pathcpthld;
  3345. trans->pf_retrans = val->spt_pathpfthld;
  3346. return 0;
  3347. }
  3348. asoc = sctp_id2assoc(sk, val->spt_assoc_id);
  3349. if (!asoc && val->spt_assoc_id != SCTP_FUTURE_ASSOC &&
  3350. sctp_style(sk, UDP))
  3351. return -EINVAL;
  3352. if (asoc) {
  3353. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  3354. transports) {
  3355. if (val->spt_pathmaxrxt)
  3356. trans->pathmaxrxt = val->spt_pathmaxrxt;
  3357. if (v2)
  3358. trans->ps_retrans = val->spt_pathcpthld;
  3359. trans->pf_retrans = val->spt_pathpfthld;
  3360. }
  3361. if (val->spt_pathmaxrxt)
  3362. asoc->pathmaxrxt = val->spt_pathmaxrxt;
  3363. if (v2)
  3364. asoc->ps_retrans = val->spt_pathcpthld;
  3365. asoc->pf_retrans = val->spt_pathpfthld;
  3366. } else {
  3367. struct sctp_sock *sp = sctp_sk(sk);
  3368. if (val->spt_pathmaxrxt)
  3369. sp->pathmaxrxt = val->spt_pathmaxrxt;
  3370. if (v2)
  3371. sp->ps_retrans = val->spt_pathcpthld;
  3372. sp->pf_retrans = val->spt_pathpfthld;
  3373. }
  3374. return 0;
  3375. }
  3376. static int sctp_setsockopt_recvrcvinfo(struct sock *sk, int *val,
  3377. unsigned int optlen)
  3378. {
  3379. if (optlen < sizeof(int))
  3380. return -EINVAL;
  3381. sctp_sk(sk)->recvrcvinfo = (*val == 0) ? 0 : 1;
  3382. return 0;
  3383. }
  3384. static int sctp_setsockopt_recvnxtinfo(struct sock *sk, int *val,
  3385. unsigned int optlen)
  3386. {
  3387. if (optlen < sizeof(int))
  3388. return -EINVAL;
  3389. sctp_sk(sk)->recvnxtinfo = (*val == 0) ? 0 : 1;
  3390. return 0;
  3391. }
  3392. static int sctp_setsockopt_pr_supported(struct sock *sk,
  3393. struct sctp_assoc_value *params,
  3394. unsigned int optlen)
  3395. {
  3396. struct sctp_association *asoc;
  3397. if (optlen != sizeof(*params))
  3398. return -EINVAL;
  3399. asoc = sctp_id2assoc(sk, params->assoc_id);
  3400. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3401. sctp_style(sk, UDP))
  3402. return -EINVAL;
  3403. sctp_sk(sk)->ep->prsctp_enable = !!params->assoc_value;
  3404. return 0;
  3405. }
  3406. static int sctp_setsockopt_default_prinfo(struct sock *sk,
  3407. struct sctp_default_prinfo *info,
  3408. unsigned int optlen)
  3409. {
  3410. struct sctp_sock *sp = sctp_sk(sk);
  3411. struct sctp_association *asoc;
  3412. int retval = -EINVAL;
  3413. if (optlen != sizeof(*info))
  3414. goto out;
  3415. if (info->pr_policy & ~SCTP_PR_SCTP_MASK)
  3416. goto out;
  3417. if (info->pr_policy == SCTP_PR_SCTP_NONE)
  3418. info->pr_value = 0;
  3419. asoc = sctp_id2assoc(sk, info->pr_assoc_id);
  3420. if (!asoc && info->pr_assoc_id > SCTP_ALL_ASSOC &&
  3421. sctp_style(sk, UDP))
  3422. goto out;
  3423. retval = 0;
  3424. if (asoc) {
  3425. SCTP_PR_SET_POLICY(asoc->default_flags, info->pr_policy);
  3426. asoc->default_timetolive = info->pr_value;
  3427. goto out;
  3428. }
  3429. if (sctp_style(sk, TCP))
  3430. info->pr_assoc_id = SCTP_FUTURE_ASSOC;
  3431. if (info->pr_assoc_id == SCTP_FUTURE_ASSOC ||
  3432. info->pr_assoc_id == SCTP_ALL_ASSOC) {
  3433. SCTP_PR_SET_POLICY(sp->default_flags, info->pr_policy);
  3434. sp->default_timetolive = info->pr_value;
  3435. }
  3436. if (info->pr_assoc_id == SCTP_CURRENT_ASSOC ||
  3437. info->pr_assoc_id == SCTP_ALL_ASSOC) {
  3438. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  3439. SCTP_PR_SET_POLICY(asoc->default_flags,
  3440. info->pr_policy);
  3441. asoc->default_timetolive = info->pr_value;
  3442. }
  3443. }
  3444. out:
  3445. return retval;
  3446. }
  3447. static int sctp_setsockopt_reconfig_supported(struct sock *sk,
  3448. struct sctp_assoc_value *params,
  3449. unsigned int optlen)
  3450. {
  3451. struct sctp_association *asoc;
  3452. int retval = -EINVAL;
  3453. if (optlen != sizeof(*params))
  3454. goto out;
  3455. asoc = sctp_id2assoc(sk, params->assoc_id);
  3456. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3457. sctp_style(sk, UDP))
  3458. goto out;
  3459. sctp_sk(sk)->ep->reconf_enable = !!params->assoc_value;
  3460. retval = 0;
  3461. out:
  3462. return retval;
  3463. }
  3464. static int sctp_setsockopt_enable_strreset(struct sock *sk,
  3465. struct sctp_assoc_value *params,
  3466. unsigned int optlen)
  3467. {
  3468. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3469. struct sctp_association *asoc;
  3470. int retval = -EINVAL;
  3471. if (optlen != sizeof(*params))
  3472. goto out;
  3473. if (params->assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
  3474. goto out;
  3475. asoc = sctp_id2assoc(sk, params->assoc_id);
  3476. if (!asoc && params->assoc_id > SCTP_ALL_ASSOC &&
  3477. sctp_style(sk, UDP))
  3478. goto out;
  3479. retval = 0;
  3480. if (asoc) {
  3481. asoc->strreset_enable = params->assoc_value;
  3482. goto out;
  3483. }
  3484. if (sctp_style(sk, TCP))
  3485. params->assoc_id = SCTP_FUTURE_ASSOC;
  3486. if (params->assoc_id == SCTP_FUTURE_ASSOC ||
  3487. params->assoc_id == SCTP_ALL_ASSOC)
  3488. ep->strreset_enable = params->assoc_value;
  3489. if (params->assoc_id == SCTP_CURRENT_ASSOC ||
  3490. params->assoc_id == SCTP_ALL_ASSOC)
  3491. list_for_each_entry(asoc, &ep->asocs, asocs)
  3492. asoc->strreset_enable = params->assoc_value;
  3493. out:
  3494. return retval;
  3495. }
  3496. static int sctp_setsockopt_reset_streams(struct sock *sk,
  3497. struct sctp_reset_streams *params,
  3498. unsigned int optlen)
  3499. {
  3500. struct sctp_association *asoc;
  3501. if (optlen < sizeof(*params))
  3502. return -EINVAL;
  3503. /* srs_number_streams is u16, so optlen can't be bigger than this. */
  3504. optlen = min_t(unsigned int, optlen, USHRT_MAX +
  3505. sizeof(__u16) * sizeof(*params));
  3506. if (params->srs_number_streams * sizeof(__u16) >
  3507. optlen - sizeof(*params))
  3508. return -EINVAL;
  3509. asoc = sctp_id2assoc(sk, params->srs_assoc_id);
  3510. if (!asoc)
  3511. return -EINVAL;
  3512. return sctp_send_reset_streams(asoc, params);
  3513. }
  3514. static int sctp_setsockopt_reset_assoc(struct sock *sk, sctp_assoc_t *associd,
  3515. unsigned int optlen)
  3516. {
  3517. struct sctp_association *asoc;
  3518. if (optlen != sizeof(*associd))
  3519. return -EINVAL;
  3520. asoc = sctp_id2assoc(sk, *associd);
  3521. if (!asoc)
  3522. return -EINVAL;
  3523. return sctp_send_reset_assoc(asoc);
  3524. }
  3525. static int sctp_setsockopt_add_streams(struct sock *sk,
  3526. struct sctp_add_streams *params,
  3527. unsigned int optlen)
  3528. {
  3529. struct sctp_association *asoc;
  3530. if (optlen != sizeof(*params))
  3531. return -EINVAL;
  3532. asoc = sctp_id2assoc(sk, params->sas_assoc_id);
  3533. if (!asoc)
  3534. return -EINVAL;
  3535. return sctp_send_add_streams(asoc, params);
  3536. }
  3537. static int sctp_setsockopt_scheduler(struct sock *sk,
  3538. struct sctp_assoc_value *params,
  3539. unsigned int optlen)
  3540. {
  3541. struct sctp_sock *sp = sctp_sk(sk);
  3542. struct sctp_association *asoc;
  3543. int retval = 0;
  3544. if (optlen < sizeof(*params))
  3545. return -EINVAL;
  3546. if (params->assoc_value > SCTP_SS_MAX)
  3547. return -EINVAL;
  3548. asoc = sctp_id2assoc(sk, params->assoc_id);
  3549. if (!asoc && params->assoc_id > SCTP_ALL_ASSOC &&
  3550. sctp_style(sk, UDP))
  3551. return -EINVAL;
  3552. if (asoc)
  3553. return sctp_sched_set_sched(asoc, params->assoc_value);
  3554. if (sctp_style(sk, TCP))
  3555. params->assoc_id = SCTP_FUTURE_ASSOC;
  3556. if (params->assoc_id == SCTP_FUTURE_ASSOC ||
  3557. params->assoc_id == SCTP_ALL_ASSOC)
  3558. sp->default_ss = params->assoc_value;
  3559. if (params->assoc_id == SCTP_CURRENT_ASSOC ||
  3560. params->assoc_id == SCTP_ALL_ASSOC) {
  3561. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  3562. int ret = sctp_sched_set_sched(asoc,
  3563. params->assoc_value);
  3564. if (ret && !retval)
  3565. retval = ret;
  3566. }
  3567. }
  3568. return retval;
  3569. }
  3570. static int sctp_setsockopt_scheduler_value(struct sock *sk,
  3571. struct sctp_stream_value *params,
  3572. unsigned int optlen)
  3573. {
  3574. struct sctp_association *asoc;
  3575. int retval = -EINVAL;
  3576. if (optlen < sizeof(*params))
  3577. goto out;
  3578. asoc = sctp_id2assoc(sk, params->assoc_id);
  3579. if (!asoc && params->assoc_id != SCTP_CURRENT_ASSOC &&
  3580. sctp_style(sk, UDP))
  3581. goto out;
  3582. if (asoc) {
  3583. retval = sctp_sched_set_value(asoc, params->stream_id,
  3584. params->stream_value, GFP_KERNEL);
  3585. goto out;
  3586. }
  3587. retval = 0;
  3588. list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
  3589. int ret = sctp_sched_set_value(asoc, params->stream_id,
  3590. params->stream_value,
  3591. GFP_KERNEL);
  3592. if (ret && !retval) /* try to return the 1st error. */
  3593. retval = ret;
  3594. }
  3595. out:
  3596. return retval;
  3597. }
  3598. static int sctp_setsockopt_interleaving_supported(struct sock *sk,
  3599. struct sctp_assoc_value *p,
  3600. unsigned int optlen)
  3601. {
  3602. struct sctp_sock *sp = sctp_sk(sk);
  3603. struct sctp_association *asoc;
  3604. if (optlen < sizeof(*p))
  3605. return -EINVAL;
  3606. asoc = sctp_id2assoc(sk, p->assoc_id);
  3607. if (!asoc && p->assoc_id != SCTP_FUTURE_ASSOC && sctp_style(sk, UDP))
  3608. return -EINVAL;
  3609. if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
  3610. return -EPERM;
  3611. }
  3612. sp->ep->intl_enable = !!p->assoc_value;
  3613. return 0;
  3614. }
  3615. static int sctp_setsockopt_reuse_port(struct sock *sk, int *val,
  3616. unsigned int optlen)
  3617. {
  3618. if (!sctp_style(sk, TCP))
  3619. return -EOPNOTSUPP;
  3620. if (sctp_sk(sk)->ep->base.bind_addr.port)
  3621. return -EFAULT;
  3622. if (optlen < sizeof(int))
  3623. return -EINVAL;
  3624. sctp_sk(sk)->reuse = !!*val;
  3625. return 0;
  3626. }
  3627. static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
  3628. struct sctp_association *asoc)
  3629. {
  3630. struct sctp_ulpevent *event;
  3631. sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
  3632. if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
  3633. if (sctp_outq_is_empty(&asoc->outqueue)) {
  3634. event = sctp_ulpevent_make_sender_dry_event(asoc,
  3635. GFP_USER | __GFP_NOWARN);
  3636. if (!event)
  3637. return -ENOMEM;
  3638. asoc->stream.si->enqueue_event(&asoc->ulpq, event);
  3639. }
  3640. }
  3641. return 0;
  3642. }
  3643. static int sctp_setsockopt_event(struct sock *sk, struct sctp_event *param,
  3644. unsigned int optlen)
  3645. {
  3646. struct sctp_sock *sp = sctp_sk(sk);
  3647. struct sctp_association *asoc;
  3648. int retval = 0;
  3649. if (optlen < sizeof(*param))
  3650. return -EINVAL;
  3651. if (param->se_type < SCTP_SN_TYPE_BASE ||
  3652. param->se_type > SCTP_SN_TYPE_MAX)
  3653. return -EINVAL;
  3654. asoc = sctp_id2assoc(sk, param->se_assoc_id);
  3655. if (!asoc && param->se_assoc_id > SCTP_ALL_ASSOC &&
  3656. sctp_style(sk, UDP))
  3657. return -EINVAL;
  3658. if (asoc)
  3659. return sctp_assoc_ulpevent_type_set(param, asoc);
  3660. if (sctp_style(sk, TCP))
  3661. param->se_assoc_id = SCTP_FUTURE_ASSOC;
  3662. if (param->se_assoc_id == SCTP_FUTURE_ASSOC ||
  3663. param->se_assoc_id == SCTP_ALL_ASSOC)
  3664. sctp_ulpevent_type_set(&sp->subscribe,
  3665. param->se_type, param->se_on);
  3666. if (param->se_assoc_id == SCTP_CURRENT_ASSOC ||
  3667. param->se_assoc_id == SCTP_ALL_ASSOC) {
  3668. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  3669. int ret = sctp_assoc_ulpevent_type_set(param, asoc);
  3670. if (ret && !retval)
  3671. retval = ret;
  3672. }
  3673. }
  3674. return retval;
  3675. }
  3676. static int sctp_setsockopt_asconf_supported(struct sock *sk,
  3677. struct sctp_assoc_value *params,
  3678. unsigned int optlen)
  3679. {
  3680. struct sctp_association *asoc;
  3681. struct sctp_endpoint *ep;
  3682. int retval = -EINVAL;
  3683. if (optlen != sizeof(*params))
  3684. goto out;
  3685. asoc = sctp_id2assoc(sk, params->assoc_id);
  3686. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3687. sctp_style(sk, UDP))
  3688. goto out;
  3689. ep = sctp_sk(sk)->ep;
  3690. ep->asconf_enable = !!params->assoc_value;
  3691. if (ep->asconf_enable && ep->auth_enable) {
  3692. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
  3693. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
  3694. }
  3695. retval = 0;
  3696. out:
  3697. return retval;
  3698. }
  3699. static int sctp_setsockopt_auth_supported(struct sock *sk,
  3700. struct sctp_assoc_value *params,
  3701. unsigned int optlen)
  3702. {
  3703. struct sctp_association *asoc;
  3704. struct sctp_endpoint *ep;
  3705. int retval = -EINVAL;
  3706. if (optlen != sizeof(*params))
  3707. goto out;
  3708. asoc = sctp_id2assoc(sk, params->assoc_id);
  3709. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3710. sctp_style(sk, UDP))
  3711. goto out;
  3712. ep = sctp_sk(sk)->ep;
  3713. if (params->assoc_value) {
  3714. retval = sctp_auth_init(ep, GFP_KERNEL);
  3715. if (retval)
  3716. goto out;
  3717. if (ep->asconf_enable) {
  3718. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
  3719. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
  3720. }
  3721. }
  3722. ep->auth_enable = !!params->assoc_value;
  3723. retval = 0;
  3724. out:
  3725. return retval;
  3726. }
  3727. static int sctp_setsockopt_ecn_supported(struct sock *sk,
  3728. struct sctp_assoc_value *params,
  3729. unsigned int optlen)
  3730. {
  3731. struct sctp_association *asoc;
  3732. int retval = -EINVAL;
  3733. if (optlen != sizeof(*params))
  3734. goto out;
  3735. asoc = sctp_id2assoc(sk, params->assoc_id);
  3736. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3737. sctp_style(sk, UDP))
  3738. goto out;
  3739. sctp_sk(sk)->ep->ecn_enable = !!params->assoc_value;
  3740. retval = 0;
  3741. out:
  3742. return retval;
  3743. }
  3744. static int sctp_setsockopt_pf_expose(struct sock *sk,
  3745. struct sctp_assoc_value *params,
  3746. unsigned int optlen)
  3747. {
  3748. struct sctp_association *asoc;
  3749. int retval = -EINVAL;
  3750. if (optlen != sizeof(*params))
  3751. goto out;
  3752. if (params->assoc_value > SCTP_PF_EXPOSE_MAX)
  3753. goto out;
  3754. asoc = sctp_id2assoc(sk, params->assoc_id);
  3755. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3756. sctp_style(sk, UDP))
  3757. goto out;
  3758. if (asoc)
  3759. asoc->pf_expose = params->assoc_value;
  3760. else
  3761. sctp_sk(sk)->pf_expose = params->assoc_value;
  3762. retval = 0;
  3763. out:
  3764. return retval;
  3765. }
  3766. /* API 6.2 setsockopt(), getsockopt()
  3767. *
  3768. * Applications use setsockopt() and getsockopt() to set or retrieve
  3769. * socket options. Socket options are used to change the default
  3770. * behavior of sockets calls. They are described in Section 7.
  3771. *
  3772. * The syntax is:
  3773. *
  3774. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  3775. * int __user *optlen);
  3776. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  3777. * int optlen);
  3778. *
  3779. * sd - the socket descript.
  3780. * level - set to IPPROTO_SCTP for all SCTP options.
  3781. * optname - the option name.
  3782. * optval - the buffer to store the value of the option.
  3783. * optlen - the size of the buffer.
  3784. */
  3785. static int sctp_setsockopt(struct sock *sk, int level, int optname,
  3786. sockptr_t optval, unsigned int optlen)
  3787. {
  3788. void *kopt = NULL;
  3789. int retval = 0;
  3790. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  3791. /* I can hardly begin to describe how wrong this is. This is
  3792. * so broken as to be worse than useless. The API draft
  3793. * REALLY is NOT helpful here... I am not convinced that the
  3794. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  3795. * are at all well-founded.
  3796. */
  3797. if (level != SOL_SCTP) {
  3798. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3799. return af->setsockopt(sk, level, optname, optval, optlen);
  3800. }
  3801. if (optlen > 0) {
  3802. /* Trim it to the biggest size sctp sockopt may need if necessary */
  3803. optlen = min_t(unsigned int, optlen,
  3804. PAGE_ALIGN(USHRT_MAX +
  3805. sizeof(__u16) * sizeof(struct sctp_reset_streams)));
  3806. kopt = memdup_sockptr(optval, optlen);
  3807. if (IS_ERR(kopt))
  3808. return PTR_ERR(kopt);
  3809. }
  3810. lock_sock(sk);
  3811. switch (optname) {
  3812. case SCTP_SOCKOPT_BINDX_ADD:
  3813. /* 'optlen' is the size of the addresses buffer. */
  3814. retval = sctp_setsockopt_bindx(sk, kopt, optlen,
  3815. SCTP_BINDX_ADD_ADDR);
  3816. break;
  3817. case SCTP_SOCKOPT_BINDX_REM:
  3818. /* 'optlen' is the size of the addresses buffer. */
  3819. retval = sctp_setsockopt_bindx(sk, kopt, optlen,
  3820. SCTP_BINDX_REM_ADDR);
  3821. break;
  3822. case SCTP_SOCKOPT_CONNECTX_OLD:
  3823. /* 'optlen' is the size of the addresses buffer. */
  3824. retval = sctp_setsockopt_connectx_old(sk, kopt, optlen);
  3825. break;
  3826. case SCTP_SOCKOPT_CONNECTX:
  3827. /* 'optlen' is the size of the addresses buffer. */
  3828. retval = sctp_setsockopt_connectx(sk, kopt, optlen);
  3829. break;
  3830. case SCTP_DISABLE_FRAGMENTS:
  3831. retval = sctp_setsockopt_disable_fragments(sk, kopt, optlen);
  3832. break;
  3833. case SCTP_EVENTS:
  3834. retval = sctp_setsockopt_events(sk, kopt, optlen);
  3835. break;
  3836. case SCTP_AUTOCLOSE:
  3837. retval = sctp_setsockopt_autoclose(sk, kopt, optlen);
  3838. break;
  3839. case SCTP_PEER_ADDR_PARAMS:
  3840. retval = sctp_setsockopt_peer_addr_params(sk, kopt, optlen);
  3841. break;
  3842. case SCTP_DELAYED_SACK:
  3843. retval = sctp_setsockopt_delayed_ack(sk, kopt, optlen);
  3844. break;
  3845. case SCTP_PARTIAL_DELIVERY_POINT:
  3846. retval = sctp_setsockopt_partial_delivery_point(sk, kopt, optlen);
  3847. break;
  3848. case SCTP_INITMSG:
  3849. retval = sctp_setsockopt_initmsg(sk, kopt, optlen);
  3850. break;
  3851. case SCTP_DEFAULT_SEND_PARAM:
  3852. retval = sctp_setsockopt_default_send_param(sk, kopt, optlen);
  3853. break;
  3854. case SCTP_DEFAULT_SNDINFO:
  3855. retval = sctp_setsockopt_default_sndinfo(sk, kopt, optlen);
  3856. break;
  3857. case SCTP_PRIMARY_ADDR:
  3858. retval = sctp_setsockopt_primary_addr(sk, kopt, optlen);
  3859. break;
  3860. case SCTP_SET_PEER_PRIMARY_ADDR:
  3861. retval = sctp_setsockopt_peer_primary_addr(sk, kopt, optlen);
  3862. break;
  3863. case SCTP_NODELAY:
  3864. retval = sctp_setsockopt_nodelay(sk, kopt, optlen);
  3865. break;
  3866. case SCTP_RTOINFO:
  3867. retval = sctp_setsockopt_rtoinfo(sk, kopt, optlen);
  3868. break;
  3869. case SCTP_ASSOCINFO:
  3870. retval = sctp_setsockopt_associnfo(sk, kopt, optlen);
  3871. break;
  3872. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3873. retval = sctp_setsockopt_mappedv4(sk, kopt, optlen);
  3874. break;
  3875. case SCTP_MAXSEG:
  3876. retval = sctp_setsockopt_maxseg(sk, kopt, optlen);
  3877. break;
  3878. case SCTP_ADAPTATION_LAYER:
  3879. retval = sctp_setsockopt_adaptation_layer(sk, kopt, optlen);
  3880. break;
  3881. case SCTP_CONTEXT:
  3882. retval = sctp_setsockopt_context(sk, kopt, optlen);
  3883. break;
  3884. case SCTP_FRAGMENT_INTERLEAVE:
  3885. retval = sctp_setsockopt_fragment_interleave(sk, kopt, optlen);
  3886. break;
  3887. case SCTP_MAX_BURST:
  3888. retval = sctp_setsockopt_maxburst(sk, kopt, optlen);
  3889. break;
  3890. case SCTP_AUTH_CHUNK:
  3891. retval = sctp_setsockopt_auth_chunk(sk, kopt, optlen);
  3892. break;
  3893. case SCTP_HMAC_IDENT:
  3894. retval = sctp_setsockopt_hmac_ident(sk, kopt, optlen);
  3895. break;
  3896. case SCTP_AUTH_KEY:
  3897. retval = sctp_setsockopt_auth_key(sk, kopt, optlen);
  3898. break;
  3899. case SCTP_AUTH_ACTIVE_KEY:
  3900. retval = sctp_setsockopt_active_key(sk, kopt, optlen);
  3901. break;
  3902. case SCTP_AUTH_DELETE_KEY:
  3903. retval = sctp_setsockopt_del_key(sk, kopt, optlen);
  3904. break;
  3905. case SCTP_AUTH_DEACTIVATE_KEY:
  3906. retval = sctp_setsockopt_deactivate_key(sk, kopt, optlen);
  3907. break;
  3908. case SCTP_AUTO_ASCONF:
  3909. retval = sctp_setsockopt_auto_asconf(sk, kopt, optlen);
  3910. break;
  3911. case SCTP_PEER_ADDR_THLDS:
  3912. retval = sctp_setsockopt_paddr_thresholds(sk, kopt, optlen,
  3913. false);
  3914. break;
  3915. case SCTP_PEER_ADDR_THLDS_V2:
  3916. retval = sctp_setsockopt_paddr_thresholds(sk, kopt, optlen,
  3917. true);
  3918. break;
  3919. case SCTP_RECVRCVINFO:
  3920. retval = sctp_setsockopt_recvrcvinfo(sk, kopt, optlen);
  3921. break;
  3922. case SCTP_RECVNXTINFO:
  3923. retval = sctp_setsockopt_recvnxtinfo(sk, kopt, optlen);
  3924. break;
  3925. case SCTP_PR_SUPPORTED:
  3926. retval = sctp_setsockopt_pr_supported(sk, kopt, optlen);
  3927. break;
  3928. case SCTP_DEFAULT_PRINFO:
  3929. retval = sctp_setsockopt_default_prinfo(sk, kopt, optlen);
  3930. break;
  3931. case SCTP_RECONFIG_SUPPORTED:
  3932. retval = sctp_setsockopt_reconfig_supported(sk, kopt, optlen);
  3933. break;
  3934. case SCTP_ENABLE_STREAM_RESET:
  3935. retval = sctp_setsockopt_enable_strreset(sk, kopt, optlen);
  3936. break;
  3937. case SCTP_RESET_STREAMS:
  3938. retval = sctp_setsockopt_reset_streams(sk, kopt, optlen);
  3939. break;
  3940. case SCTP_RESET_ASSOC:
  3941. retval = sctp_setsockopt_reset_assoc(sk, kopt, optlen);
  3942. break;
  3943. case SCTP_ADD_STREAMS:
  3944. retval = sctp_setsockopt_add_streams(sk, kopt, optlen);
  3945. break;
  3946. case SCTP_STREAM_SCHEDULER:
  3947. retval = sctp_setsockopt_scheduler(sk, kopt, optlen);
  3948. break;
  3949. case SCTP_STREAM_SCHEDULER_VALUE:
  3950. retval = sctp_setsockopt_scheduler_value(sk, kopt, optlen);
  3951. break;
  3952. case SCTP_INTERLEAVING_SUPPORTED:
  3953. retval = sctp_setsockopt_interleaving_supported(sk, kopt,
  3954. optlen);
  3955. break;
  3956. case SCTP_REUSE_PORT:
  3957. retval = sctp_setsockopt_reuse_port(sk, kopt, optlen);
  3958. break;
  3959. case SCTP_EVENT:
  3960. retval = sctp_setsockopt_event(sk, kopt, optlen);
  3961. break;
  3962. case SCTP_ASCONF_SUPPORTED:
  3963. retval = sctp_setsockopt_asconf_supported(sk, kopt, optlen);
  3964. break;
  3965. case SCTP_AUTH_SUPPORTED:
  3966. retval = sctp_setsockopt_auth_supported(sk, kopt, optlen);
  3967. break;
  3968. case SCTP_ECN_SUPPORTED:
  3969. retval = sctp_setsockopt_ecn_supported(sk, kopt, optlen);
  3970. break;
  3971. case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
  3972. retval = sctp_setsockopt_pf_expose(sk, kopt, optlen);
  3973. break;
  3974. default:
  3975. retval = -ENOPROTOOPT;
  3976. break;
  3977. }
  3978. release_sock(sk);
  3979. kfree(kopt);
  3980. return retval;
  3981. }
  3982. /* API 3.1.6 connect() - UDP Style Syntax
  3983. *
  3984. * An application may use the connect() call in the UDP model to initiate an
  3985. * association without sending data.
  3986. *
  3987. * The syntax is:
  3988. *
  3989. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  3990. *
  3991. * sd: the socket descriptor to have a new association added to.
  3992. *
  3993. * nam: the address structure (either struct sockaddr_in or struct
  3994. * sockaddr_in6 defined in RFC2553 [7]).
  3995. *
  3996. * len: the size of the address.
  3997. */
  3998. static int sctp_connect(struct sock *sk, struct sockaddr *addr,
  3999. int addr_len, int flags)
  4000. {
  4001. struct sctp_af *af;
  4002. int err = -EINVAL;
  4003. lock_sock(sk);
  4004. pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
  4005. addr, addr_len);
  4006. /* Validate addr_len before calling common connect/connectx routine. */
  4007. af = sctp_get_af_specific(addr->sa_family);
  4008. if (af && addr_len >= af->sockaddr_len)
  4009. err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
  4010. release_sock(sk);
  4011. return err;
  4012. }
  4013. int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
  4014. int addr_len, int flags)
  4015. {
  4016. if (addr_len < sizeof(uaddr->sa_family))
  4017. return -EINVAL;
  4018. if (uaddr->sa_family == AF_UNSPEC)
  4019. return -EOPNOTSUPP;
  4020. return sctp_connect(sock->sk, uaddr, addr_len, flags);
  4021. }
  4022. /* FIXME: Write comments. */
  4023. static int sctp_disconnect(struct sock *sk, int flags)
  4024. {
  4025. return -EOPNOTSUPP; /* STUB */
  4026. }
  4027. /* 4.1.4 accept() - TCP Style Syntax
  4028. *
  4029. * Applications use accept() call to remove an established SCTP
  4030. * association from the accept queue of the endpoint. A new socket
  4031. * descriptor will be returned from accept() to represent the newly
  4032. * formed association.
  4033. */
  4034. static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
  4035. {
  4036. struct sctp_sock *sp;
  4037. struct sctp_endpoint *ep;
  4038. struct sock *newsk = NULL;
  4039. struct sctp_association *asoc;
  4040. long timeo;
  4041. int error = 0;
  4042. lock_sock(sk);
  4043. sp = sctp_sk(sk);
  4044. ep = sp->ep;
  4045. if (!sctp_style(sk, TCP)) {
  4046. error = -EOPNOTSUPP;
  4047. goto out;
  4048. }
  4049. if (!sctp_sstate(sk, LISTENING)) {
  4050. error = -EINVAL;
  4051. goto out;
  4052. }
  4053. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  4054. error = sctp_wait_for_accept(sk, timeo);
  4055. if (error)
  4056. goto out;
  4057. /* We treat the list of associations on the endpoint as the accept
  4058. * queue and pick the first association on the list.
  4059. */
  4060. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  4061. newsk = sp->pf->create_accept_sk(sk, asoc, kern);
  4062. if (!newsk) {
  4063. error = -ENOMEM;
  4064. goto out;
  4065. }
  4066. /* Populate the fields of the newsk from the oldsk and migrate the
  4067. * asoc to the newsk.
  4068. */
  4069. error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  4070. if (error) {
  4071. sk_common_release(newsk);
  4072. newsk = NULL;
  4073. }
  4074. out:
  4075. release_sock(sk);
  4076. *err = error;
  4077. return newsk;
  4078. }
  4079. /* The SCTP ioctl handler. */
  4080. static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  4081. {
  4082. int rc = -ENOTCONN;
  4083. lock_sock(sk);
  4084. /*
  4085. * SEQPACKET-style sockets in LISTENING state are valid, for
  4086. * SCTP, so only discard TCP-style sockets in LISTENING state.
  4087. */
  4088. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  4089. goto out;
  4090. switch (cmd) {
  4091. case SIOCINQ: {
  4092. struct sk_buff *skb;
  4093. unsigned int amount = 0;
  4094. skb = skb_peek(&sk->sk_receive_queue);
  4095. if (skb != NULL) {
  4096. /*
  4097. * We will only return the amount of this packet since
  4098. * that is all that will be read.
  4099. */
  4100. amount = skb->len;
  4101. }
  4102. rc = put_user(amount, (int __user *)arg);
  4103. break;
  4104. }
  4105. default:
  4106. rc = -ENOIOCTLCMD;
  4107. break;
  4108. }
  4109. out:
  4110. release_sock(sk);
  4111. return rc;
  4112. }
  4113. /* This is the function which gets called during socket creation to
  4114. * initialized the SCTP-specific portion of the sock.
  4115. * The sock structure should already be zero-filled memory.
  4116. */
  4117. static int sctp_init_sock(struct sock *sk)
  4118. {
  4119. struct net *net = sock_net(sk);
  4120. struct sctp_sock *sp;
  4121. pr_debug("%s: sk:%p\n", __func__, sk);
  4122. sp = sctp_sk(sk);
  4123. /* Initialize the SCTP per socket area. */
  4124. switch (sk->sk_type) {
  4125. case SOCK_SEQPACKET:
  4126. sp->type = SCTP_SOCKET_UDP;
  4127. break;
  4128. case SOCK_STREAM:
  4129. sp->type = SCTP_SOCKET_TCP;
  4130. break;
  4131. default:
  4132. return -ESOCKTNOSUPPORT;
  4133. }
  4134. sk->sk_gso_type = SKB_GSO_SCTP;
  4135. /* Initialize default send parameters. These parameters can be
  4136. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  4137. */
  4138. sp->default_stream = 0;
  4139. sp->default_ppid = 0;
  4140. sp->default_flags = 0;
  4141. sp->default_context = 0;
  4142. sp->default_timetolive = 0;
  4143. sp->default_rcv_context = 0;
  4144. sp->max_burst = net->sctp.max_burst;
  4145. sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
  4146. /* Initialize default setup parameters. These parameters
  4147. * can be modified with the SCTP_INITMSG socket option or
  4148. * overridden by the SCTP_INIT CMSG.
  4149. */
  4150. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  4151. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  4152. sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
  4153. sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
  4154. /* Initialize default RTO related parameters. These parameters can
  4155. * be modified for with the SCTP_RTOINFO socket option.
  4156. */
  4157. sp->rtoinfo.srto_initial = net->sctp.rto_initial;
  4158. sp->rtoinfo.srto_max = net->sctp.rto_max;
  4159. sp->rtoinfo.srto_min = net->sctp.rto_min;
  4160. /* Initialize default association related parameters. These parameters
  4161. * can be modified with the SCTP_ASSOCINFO socket option.
  4162. */
  4163. sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
  4164. sp->assocparams.sasoc_number_peer_destinations = 0;
  4165. sp->assocparams.sasoc_peer_rwnd = 0;
  4166. sp->assocparams.sasoc_local_rwnd = 0;
  4167. sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
  4168. /* Initialize default event subscriptions. By default, all the
  4169. * options are off.
  4170. */
  4171. sp->subscribe = 0;
  4172. /* Default Peer Address Parameters. These defaults can
  4173. * be modified via SCTP_PEER_ADDR_PARAMS
  4174. */
  4175. sp->hbinterval = net->sctp.hb_interval;
  4176. sp->pathmaxrxt = net->sctp.max_retrans_path;
  4177. sp->pf_retrans = net->sctp.pf_retrans;
  4178. sp->ps_retrans = net->sctp.ps_retrans;
  4179. sp->pf_expose = net->sctp.pf_expose;
  4180. sp->pathmtu = 0; /* allow default discovery */
  4181. sp->sackdelay = net->sctp.sack_timeout;
  4182. sp->sackfreq = 2;
  4183. sp->param_flags = SPP_HB_ENABLE |
  4184. SPP_PMTUD_ENABLE |
  4185. SPP_SACKDELAY_ENABLE;
  4186. sp->default_ss = SCTP_SS_DEFAULT;
  4187. /* If enabled no SCTP message fragmentation will be performed.
  4188. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  4189. */
  4190. sp->disable_fragments = 0;
  4191. /* Enable Nagle algorithm by default. */
  4192. sp->nodelay = 0;
  4193. sp->recvrcvinfo = 0;
  4194. sp->recvnxtinfo = 0;
  4195. /* Enable by default. */
  4196. sp->v4mapped = 1;
  4197. /* Auto-close idle associations after the configured
  4198. * number of seconds. A value of 0 disables this
  4199. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  4200. * for UDP-style sockets only.
  4201. */
  4202. sp->autoclose = 0;
  4203. /* User specified fragmentation limit. */
  4204. sp->user_frag = 0;
  4205. sp->adaptation_ind = 0;
  4206. sp->pf = sctp_get_pf_specific(sk->sk_family);
  4207. /* Control variables for partial data delivery. */
  4208. atomic_set(&sp->pd_mode, 0);
  4209. skb_queue_head_init(&sp->pd_lobby);
  4210. sp->frag_interleave = 0;
  4211. /* Create a per socket endpoint structure. Even if we
  4212. * change the data structure relationships, this may still
  4213. * be useful for storing pre-connect address information.
  4214. */
  4215. sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
  4216. if (!sp->ep)
  4217. return -ENOMEM;
  4218. sp->hmac = NULL;
  4219. sk->sk_destruct = sctp_destruct_sock;
  4220. SCTP_DBG_OBJCNT_INC(sock);
  4221. local_bh_disable();
  4222. sk_sockets_allocated_inc(sk);
  4223. sock_prot_inuse_add(net, sk->sk_prot, 1);
  4224. local_bh_enable();
  4225. return 0;
  4226. }
  4227. /* Cleanup any SCTP per socket resources. Must be called with
  4228. * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
  4229. */
  4230. static void sctp_destroy_sock(struct sock *sk)
  4231. {
  4232. struct sctp_sock *sp;
  4233. pr_debug("%s: sk:%p\n", __func__, sk);
  4234. /* Release our hold on the endpoint. */
  4235. sp = sctp_sk(sk);
  4236. /* This could happen during socket init, thus we bail out
  4237. * early, since the rest of the below is not setup either.
  4238. */
  4239. if (sp->ep == NULL)
  4240. return;
  4241. if (sp->do_auto_asconf) {
  4242. sp->do_auto_asconf = 0;
  4243. list_del(&sp->auto_asconf_list);
  4244. }
  4245. sctp_endpoint_free(sp->ep);
  4246. local_bh_disable();
  4247. sk_sockets_allocated_dec(sk);
  4248. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  4249. local_bh_enable();
  4250. }
  4251. /* Triggered when there are no references on the socket anymore */
  4252. static void sctp_destruct_sock(struct sock *sk)
  4253. {
  4254. struct sctp_sock *sp = sctp_sk(sk);
  4255. /* Free up the HMAC transform. */
  4256. crypto_free_shash(sp->hmac);
  4257. inet_sock_destruct(sk);
  4258. }
  4259. /* API 4.1.7 shutdown() - TCP Style Syntax
  4260. * int shutdown(int socket, int how);
  4261. *
  4262. * sd - the socket descriptor of the association to be closed.
  4263. * how - Specifies the type of shutdown. The values are
  4264. * as follows:
  4265. * SHUT_RD
  4266. * Disables further receive operations. No SCTP
  4267. * protocol action is taken.
  4268. * SHUT_WR
  4269. * Disables further send operations, and initiates
  4270. * the SCTP shutdown sequence.
  4271. * SHUT_RDWR
  4272. * Disables further send and receive operations
  4273. * and initiates the SCTP shutdown sequence.
  4274. */
  4275. static void sctp_shutdown(struct sock *sk, int how)
  4276. {
  4277. struct net *net = sock_net(sk);
  4278. struct sctp_endpoint *ep;
  4279. if (!sctp_style(sk, TCP))
  4280. return;
  4281. ep = sctp_sk(sk)->ep;
  4282. if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
  4283. struct sctp_association *asoc;
  4284. inet_sk_set_state(sk, SCTP_SS_CLOSING);
  4285. asoc = list_entry(ep->asocs.next,
  4286. struct sctp_association, asocs);
  4287. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  4288. }
  4289. }
  4290. int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
  4291. struct sctp_info *info)
  4292. {
  4293. struct sctp_transport *prim;
  4294. struct list_head *pos;
  4295. int mask;
  4296. memset(info, 0, sizeof(*info));
  4297. if (!asoc) {
  4298. struct sctp_sock *sp = sctp_sk(sk);
  4299. info->sctpi_s_autoclose = sp->autoclose;
  4300. info->sctpi_s_adaptation_ind = sp->adaptation_ind;
  4301. info->sctpi_s_pd_point = sp->pd_point;
  4302. info->sctpi_s_nodelay = sp->nodelay;
  4303. info->sctpi_s_disable_fragments = sp->disable_fragments;
  4304. info->sctpi_s_v4mapped = sp->v4mapped;
  4305. info->sctpi_s_frag_interleave = sp->frag_interleave;
  4306. info->sctpi_s_type = sp->type;
  4307. return 0;
  4308. }
  4309. info->sctpi_tag = asoc->c.my_vtag;
  4310. info->sctpi_state = asoc->state;
  4311. info->sctpi_rwnd = asoc->a_rwnd;
  4312. info->sctpi_unackdata = asoc->unack_data;
  4313. info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  4314. info->sctpi_instrms = asoc->stream.incnt;
  4315. info->sctpi_outstrms = asoc->stream.outcnt;
  4316. list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
  4317. info->sctpi_inqueue++;
  4318. list_for_each(pos, &asoc->outqueue.out_chunk_list)
  4319. info->sctpi_outqueue++;
  4320. info->sctpi_overall_error = asoc->overall_error_count;
  4321. info->sctpi_max_burst = asoc->max_burst;
  4322. info->sctpi_maxseg = asoc->frag_point;
  4323. info->sctpi_peer_rwnd = asoc->peer.rwnd;
  4324. info->sctpi_peer_tag = asoc->c.peer_vtag;
  4325. mask = asoc->peer.ecn_capable << 1;
  4326. mask = (mask | asoc->peer.ipv4_address) << 1;
  4327. mask = (mask | asoc->peer.ipv6_address) << 1;
  4328. mask = (mask | asoc->peer.hostname_address) << 1;
  4329. mask = (mask | asoc->peer.asconf_capable) << 1;
  4330. mask = (mask | asoc->peer.prsctp_capable) << 1;
  4331. mask = (mask | asoc->peer.auth_capable);
  4332. info->sctpi_peer_capable = mask;
  4333. mask = asoc->peer.sack_needed << 1;
  4334. mask = (mask | asoc->peer.sack_generation) << 1;
  4335. mask = (mask | asoc->peer.zero_window_announced);
  4336. info->sctpi_peer_sack = mask;
  4337. info->sctpi_isacks = asoc->stats.isacks;
  4338. info->sctpi_osacks = asoc->stats.osacks;
  4339. info->sctpi_opackets = asoc->stats.opackets;
  4340. info->sctpi_ipackets = asoc->stats.ipackets;
  4341. info->sctpi_rtxchunks = asoc->stats.rtxchunks;
  4342. info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
  4343. info->sctpi_idupchunks = asoc->stats.idupchunks;
  4344. info->sctpi_gapcnt = asoc->stats.gapcnt;
  4345. info->sctpi_ouodchunks = asoc->stats.ouodchunks;
  4346. info->sctpi_iuodchunks = asoc->stats.iuodchunks;
  4347. info->sctpi_oodchunks = asoc->stats.oodchunks;
  4348. info->sctpi_iodchunks = asoc->stats.iodchunks;
  4349. info->sctpi_octrlchunks = asoc->stats.octrlchunks;
  4350. info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
  4351. prim = asoc->peer.primary_path;
  4352. memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
  4353. info->sctpi_p_state = prim->state;
  4354. info->sctpi_p_cwnd = prim->cwnd;
  4355. info->sctpi_p_srtt = prim->srtt;
  4356. info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
  4357. info->sctpi_p_hbinterval = prim->hbinterval;
  4358. info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
  4359. info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
  4360. info->sctpi_p_ssthresh = prim->ssthresh;
  4361. info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
  4362. info->sctpi_p_flight_size = prim->flight_size;
  4363. info->sctpi_p_error = prim->error_count;
  4364. return 0;
  4365. }
  4366. EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
  4367. /* use callback to avoid exporting the core structure */
  4368. void sctp_transport_walk_start(struct rhashtable_iter *iter) __acquires(RCU)
  4369. {
  4370. rhltable_walk_enter(&sctp_transport_hashtable, iter);
  4371. rhashtable_walk_start(iter);
  4372. }
  4373. void sctp_transport_walk_stop(struct rhashtable_iter *iter) __releases(RCU)
  4374. {
  4375. rhashtable_walk_stop(iter);
  4376. rhashtable_walk_exit(iter);
  4377. }
  4378. struct sctp_transport *sctp_transport_get_next(struct net *net,
  4379. struct rhashtable_iter *iter)
  4380. {
  4381. struct sctp_transport *t;
  4382. t = rhashtable_walk_next(iter);
  4383. for (; t; t = rhashtable_walk_next(iter)) {
  4384. if (IS_ERR(t)) {
  4385. if (PTR_ERR(t) == -EAGAIN)
  4386. continue;
  4387. break;
  4388. }
  4389. if (!sctp_transport_hold(t))
  4390. continue;
  4391. if (net_eq(t->asoc->base.net, net) &&
  4392. t->asoc->peer.primary_path == t)
  4393. break;
  4394. sctp_transport_put(t);
  4395. }
  4396. return t;
  4397. }
  4398. struct sctp_transport *sctp_transport_get_idx(struct net *net,
  4399. struct rhashtable_iter *iter,
  4400. int pos)
  4401. {
  4402. struct sctp_transport *t;
  4403. if (!pos)
  4404. return SEQ_START_TOKEN;
  4405. while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
  4406. if (!--pos)
  4407. break;
  4408. sctp_transport_put(t);
  4409. }
  4410. return t;
  4411. }
  4412. int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
  4413. void *p) {
  4414. int err = 0;
  4415. int hash = 0;
  4416. struct sctp_ep_common *epb;
  4417. struct sctp_hashbucket *head;
  4418. for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
  4419. hash++, head++) {
  4420. read_lock_bh(&head->lock);
  4421. sctp_for_each_hentry(epb, &head->chain) {
  4422. err = cb(sctp_ep(epb), p);
  4423. if (err)
  4424. break;
  4425. }
  4426. read_unlock_bh(&head->lock);
  4427. }
  4428. return err;
  4429. }
  4430. EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
  4431. int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
  4432. struct net *net,
  4433. const union sctp_addr *laddr,
  4434. const union sctp_addr *paddr, void *p)
  4435. {
  4436. struct sctp_transport *transport;
  4437. int err;
  4438. rcu_read_lock();
  4439. transport = sctp_addrs_lookup_transport(net, laddr, paddr);
  4440. rcu_read_unlock();
  4441. if (!transport)
  4442. return -ENOENT;
  4443. err = cb(transport, p);
  4444. sctp_transport_put(transport);
  4445. return err;
  4446. }
  4447. EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
  4448. int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done,
  4449. struct net *net, int *pos, void *p)
  4450. {
  4451. struct rhashtable_iter hti;
  4452. struct sctp_transport *tsp;
  4453. struct sctp_endpoint *ep;
  4454. int ret;
  4455. again:
  4456. ret = 0;
  4457. sctp_transport_walk_start(&hti);
  4458. tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
  4459. for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
  4460. ep = tsp->asoc->ep;
  4461. if (sctp_endpoint_hold(ep)) { /* asoc can be peeled off */
  4462. ret = cb(ep, tsp, p);
  4463. if (ret)
  4464. break;
  4465. sctp_endpoint_put(ep);
  4466. }
  4467. (*pos)++;
  4468. sctp_transport_put(tsp);
  4469. }
  4470. sctp_transport_walk_stop(&hti);
  4471. if (ret) {
  4472. if (cb_done && !cb_done(ep, tsp, p)) {
  4473. (*pos)++;
  4474. sctp_endpoint_put(ep);
  4475. sctp_transport_put(tsp);
  4476. goto again;
  4477. }
  4478. sctp_endpoint_put(ep);
  4479. sctp_transport_put(tsp);
  4480. }
  4481. return ret;
  4482. }
  4483. EXPORT_SYMBOL_GPL(sctp_transport_traverse_process);
  4484. /* 7.2.1 Association Status (SCTP_STATUS)
  4485. * Applications can retrieve current status information about an
  4486. * association, including association state, peer receiver window size,
  4487. * number of unacked data chunks, and number of data chunks pending
  4488. * receipt. This information is read-only.
  4489. */
  4490. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  4491. char __user *optval,
  4492. int __user *optlen)
  4493. {
  4494. struct sctp_status status;
  4495. struct sctp_association *asoc = NULL;
  4496. struct sctp_transport *transport;
  4497. sctp_assoc_t associd;
  4498. int retval = 0;
  4499. if (len < sizeof(status)) {
  4500. retval = -EINVAL;
  4501. goto out;
  4502. }
  4503. len = sizeof(status);
  4504. if (copy_from_user(&status, optval, len)) {
  4505. retval = -EFAULT;
  4506. goto out;
  4507. }
  4508. associd = status.sstat_assoc_id;
  4509. asoc = sctp_id2assoc(sk, associd);
  4510. if (!asoc) {
  4511. retval = -EINVAL;
  4512. goto out;
  4513. }
  4514. transport = asoc->peer.primary_path;
  4515. status.sstat_assoc_id = sctp_assoc2id(asoc);
  4516. status.sstat_state = sctp_assoc_to_state(asoc);
  4517. status.sstat_rwnd = asoc->peer.rwnd;
  4518. status.sstat_unackdata = asoc->unack_data;
  4519. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  4520. status.sstat_instrms = asoc->stream.incnt;
  4521. status.sstat_outstrms = asoc->stream.outcnt;
  4522. status.sstat_fragmentation_point = asoc->frag_point;
  4523. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4524. memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
  4525. transport->af_specific->sockaddr_len);
  4526. /* Map ipv4 address into v4-mapped-on-v6 address. */
  4527. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
  4528. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  4529. status.sstat_primary.spinfo_state = transport->state;
  4530. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  4531. status.sstat_primary.spinfo_srtt = transport->srtt;
  4532. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  4533. status.sstat_primary.spinfo_mtu = transport->pathmtu;
  4534. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  4535. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  4536. if (put_user(len, optlen)) {
  4537. retval = -EFAULT;
  4538. goto out;
  4539. }
  4540. pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
  4541. __func__, len, status.sstat_state, status.sstat_rwnd,
  4542. status.sstat_assoc_id);
  4543. if (copy_to_user(optval, &status, len)) {
  4544. retval = -EFAULT;
  4545. goto out;
  4546. }
  4547. out:
  4548. return retval;
  4549. }
  4550. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  4551. *
  4552. * Applications can retrieve information about a specific peer address
  4553. * of an association, including its reachability state, congestion
  4554. * window, and retransmission timer values. This information is
  4555. * read-only.
  4556. */
  4557. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  4558. char __user *optval,
  4559. int __user *optlen)
  4560. {
  4561. struct sctp_paddrinfo pinfo;
  4562. struct sctp_transport *transport;
  4563. int retval = 0;
  4564. if (len < sizeof(pinfo)) {
  4565. retval = -EINVAL;
  4566. goto out;
  4567. }
  4568. len = sizeof(pinfo);
  4569. if (copy_from_user(&pinfo, optval, len)) {
  4570. retval = -EFAULT;
  4571. goto out;
  4572. }
  4573. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  4574. pinfo.spinfo_assoc_id);
  4575. if (!transport) {
  4576. retval = -EINVAL;
  4577. goto out;
  4578. }
  4579. if (transport->state == SCTP_PF &&
  4580. transport->asoc->pf_expose == SCTP_PF_EXPOSE_DISABLE) {
  4581. retval = -EACCES;
  4582. goto out;
  4583. }
  4584. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4585. pinfo.spinfo_state = transport->state;
  4586. pinfo.spinfo_cwnd = transport->cwnd;
  4587. pinfo.spinfo_srtt = transport->srtt;
  4588. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  4589. pinfo.spinfo_mtu = transport->pathmtu;
  4590. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  4591. pinfo.spinfo_state = SCTP_ACTIVE;
  4592. if (put_user(len, optlen)) {
  4593. retval = -EFAULT;
  4594. goto out;
  4595. }
  4596. if (copy_to_user(optval, &pinfo, len)) {
  4597. retval = -EFAULT;
  4598. goto out;
  4599. }
  4600. out:
  4601. return retval;
  4602. }
  4603. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  4604. *
  4605. * This option is a on/off flag. If enabled no SCTP message
  4606. * fragmentation will be performed. Instead if a message being sent
  4607. * exceeds the current PMTU size, the message will NOT be sent and
  4608. * instead a error will be indicated to the user.
  4609. */
  4610. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  4611. char __user *optval, int __user *optlen)
  4612. {
  4613. int val;
  4614. if (len < sizeof(int))
  4615. return -EINVAL;
  4616. len = sizeof(int);
  4617. val = (sctp_sk(sk)->disable_fragments == 1);
  4618. if (put_user(len, optlen))
  4619. return -EFAULT;
  4620. if (copy_to_user(optval, &val, len))
  4621. return -EFAULT;
  4622. return 0;
  4623. }
  4624. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  4625. *
  4626. * This socket option is used to specify various notifications and
  4627. * ancillary data the user wishes to receive.
  4628. */
  4629. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  4630. int __user *optlen)
  4631. {
  4632. struct sctp_event_subscribe subscribe;
  4633. __u8 *sn_type = (__u8 *)&subscribe;
  4634. int i;
  4635. if (len == 0)
  4636. return -EINVAL;
  4637. if (len > sizeof(struct sctp_event_subscribe))
  4638. len = sizeof(struct sctp_event_subscribe);
  4639. if (put_user(len, optlen))
  4640. return -EFAULT;
  4641. for (i = 0; i < len; i++)
  4642. sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
  4643. SCTP_SN_TYPE_BASE + i);
  4644. if (copy_to_user(optval, &subscribe, len))
  4645. return -EFAULT;
  4646. return 0;
  4647. }
  4648. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  4649. *
  4650. * This socket option is applicable to the UDP-style socket only. When
  4651. * set it will cause associations that are idle for more than the
  4652. * specified number of seconds to automatically close. An association
  4653. * being idle is defined an association that has NOT sent or received
  4654. * user data. The special value of '0' indicates that no automatic
  4655. * close of any associations should be performed. The option expects an
  4656. * integer defining the number of seconds of idle time before an
  4657. * association is closed.
  4658. */
  4659. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4660. {
  4661. /* Applicable to UDP-style socket only */
  4662. if (sctp_style(sk, TCP))
  4663. return -EOPNOTSUPP;
  4664. if (len < sizeof(int))
  4665. return -EINVAL;
  4666. len = sizeof(int);
  4667. if (put_user(len, optlen))
  4668. return -EFAULT;
  4669. if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
  4670. return -EFAULT;
  4671. return 0;
  4672. }
  4673. /* Helper routine to branch off an association to a new socket. */
  4674. int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
  4675. {
  4676. struct sctp_association *asoc = sctp_id2assoc(sk, id);
  4677. struct sctp_sock *sp = sctp_sk(sk);
  4678. struct socket *sock;
  4679. int err = 0;
  4680. /* Do not peel off from one netns to another one. */
  4681. if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
  4682. return -EINVAL;
  4683. if (!asoc)
  4684. return -EINVAL;
  4685. /* An association cannot be branched off from an already peeled-off
  4686. * socket, nor is this supported for tcp style sockets.
  4687. */
  4688. if (!sctp_style(sk, UDP))
  4689. return -EINVAL;
  4690. /* Create a new socket. */
  4691. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  4692. if (err < 0)
  4693. return err;
  4694. sctp_copy_sock(sock->sk, sk, asoc);
  4695. /* Make peeled-off sockets more like 1-1 accepted sockets.
  4696. * Set the daddr and initialize id to something more random and also
  4697. * copy over any ip options.
  4698. */
  4699. sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sock->sk);
  4700. sp->pf->copy_ip_options(sk, sock->sk);
  4701. /* Populate the fields of the newsk from the oldsk and migrate the
  4702. * asoc to the newsk.
  4703. */
  4704. err = sctp_sock_migrate(sk, sock->sk, asoc,
  4705. SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  4706. if (err) {
  4707. sock_release(sock);
  4708. sock = NULL;
  4709. }
  4710. *sockp = sock;
  4711. return err;
  4712. }
  4713. EXPORT_SYMBOL(sctp_do_peeloff);
  4714. static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
  4715. struct file **newfile, unsigned flags)
  4716. {
  4717. struct socket *newsock;
  4718. int retval;
  4719. retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
  4720. if (retval < 0)
  4721. goto out;
  4722. /* Map the socket to an unused fd that can be returned to the user. */
  4723. retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
  4724. if (retval < 0) {
  4725. sock_release(newsock);
  4726. goto out;
  4727. }
  4728. *newfile = sock_alloc_file(newsock, 0, NULL);
  4729. if (IS_ERR(*newfile)) {
  4730. put_unused_fd(retval);
  4731. retval = PTR_ERR(*newfile);
  4732. *newfile = NULL;
  4733. return retval;
  4734. }
  4735. pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
  4736. retval);
  4737. peeloff->sd = retval;
  4738. if (flags & SOCK_NONBLOCK)
  4739. (*newfile)->f_flags |= O_NONBLOCK;
  4740. out:
  4741. return retval;
  4742. }
  4743. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4744. {
  4745. sctp_peeloff_arg_t peeloff;
  4746. struct file *newfile = NULL;
  4747. int retval = 0;
  4748. if (len < sizeof(sctp_peeloff_arg_t))
  4749. return -EINVAL;
  4750. len = sizeof(sctp_peeloff_arg_t);
  4751. if (copy_from_user(&peeloff, optval, len))
  4752. return -EFAULT;
  4753. retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
  4754. if (retval < 0)
  4755. goto out;
  4756. /* Return the fd mapped to the new socket. */
  4757. if (put_user(len, optlen)) {
  4758. fput(newfile);
  4759. put_unused_fd(retval);
  4760. return -EFAULT;
  4761. }
  4762. if (copy_to_user(optval, &peeloff, len)) {
  4763. fput(newfile);
  4764. put_unused_fd(retval);
  4765. return -EFAULT;
  4766. }
  4767. fd_install(retval, newfile);
  4768. out:
  4769. return retval;
  4770. }
  4771. static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
  4772. char __user *optval, int __user *optlen)
  4773. {
  4774. sctp_peeloff_flags_arg_t peeloff;
  4775. struct file *newfile = NULL;
  4776. int retval = 0;
  4777. if (len < sizeof(sctp_peeloff_flags_arg_t))
  4778. return -EINVAL;
  4779. len = sizeof(sctp_peeloff_flags_arg_t);
  4780. if (copy_from_user(&peeloff, optval, len))
  4781. return -EFAULT;
  4782. retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
  4783. &newfile, peeloff.flags);
  4784. if (retval < 0)
  4785. goto out;
  4786. /* Return the fd mapped to the new socket. */
  4787. if (put_user(len, optlen)) {
  4788. fput(newfile);
  4789. put_unused_fd(retval);
  4790. return -EFAULT;
  4791. }
  4792. if (copy_to_user(optval, &peeloff, len)) {
  4793. fput(newfile);
  4794. put_unused_fd(retval);
  4795. return -EFAULT;
  4796. }
  4797. fd_install(retval, newfile);
  4798. out:
  4799. return retval;
  4800. }
  4801. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  4802. *
  4803. * Applications can enable or disable heartbeats for any peer address of
  4804. * an association, modify an address's heartbeat interval, force a
  4805. * heartbeat to be sent immediately, and adjust the address's maximum
  4806. * number of retransmissions sent before an address is considered
  4807. * unreachable. The following structure is used to access and modify an
  4808. * address's parameters:
  4809. *
  4810. * struct sctp_paddrparams {
  4811. * sctp_assoc_t spp_assoc_id;
  4812. * struct sockaddr_storage spp_address;
  4813. * uint32_t spp_hbinterval;
  4814. * uint16_t spp_pathmaxrxt;
  4815. * uint32_t spp_pathmtu;
  4816. * uint32_t spp_sackdelay;
  4817. * uint32_t spp_flags;
  4818. * };
  4819. *
  4820. * spp_assoc_id - (one-to-many style socket) This is filled in the
  4821. * application, and identifies the association for
  4822. * this query.
  4823. * spp_address - This specifies which address is of interest.
  4824. * spp_hbinterval - This contains the value of the heartbeat interval,
  4825. * in milliseconds. If a value of zero
  4826. * is present in this field then no changes are to
  4827. * be made to this parameter.
  4828. * spp_pathmaxrxt - This contains the maximum number of
  4829. * retransmissions before this address shall be
  4830. * considered unreachable. If a value of zero
  4831. * is present in this field then no changes are to
  4832. * be made to this parameter.
  4833. * spp_pathmtu - When Path MTU discovery is disabled the value
  4834. * specified here will be the "fixed" path mtu.
  4835. * Note that if the spp_address field is empty
  4836. * then all associations on this address will
  4837. * have this fixed path mtu set upon them.
  4838. *
  4839. * spp_sackdelay - When delayed sack is enabled, this value specifies
  4840. * the number of milliseconds that sacks will be delayed
  4841. * for. This value will apply to all addresses of an
  4842. * association if the spp_address field is empty. Note
  4843. * also, that if delayed sack is enabled and this
  4844. * value is set to 0, no change is made to the last
  4845. * recorded delayed sack timer value.
  4846. *
  4847. * spp_flags - These flags are used to control various features
  4848. * on an association. The flag field may contain
  4849. * zero or more of the following options.
  4850. *
  4851. * SPP_HB_ENABLE - Enable heartbeats on the
  4852. * specified address. Note that if the address
  4853. * field is empty all addresses for the association
  4854. * have heartbeats enabled upon them.
  4855. *
  4856. * SPP_HB_DISABLE - Disable heartbeats on the
  4857. * speicifed address. Note that if the address
  4858. * field is empty all addresses for the association
  4859. * will have their heartbeats disabled. Note also
  4860. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  4861. * mutually exclusive, only one of these two should
  4862. * be specified. Enabling both fields will have
  4863. * undetermined results.
  4864. *
  4865. * SPP_HB_DEMAND - Request a user initiated heartbeat
  4866. * to be made immediately.
  4867. *
  4868. * SPP_PMTUD_ENABLE - This field will enable PMTU
  4869. * discovery upon the specified address. Note that
  4870. * if the address feild is empty then all addresses
  4871. * on the association are effected.
  4872. *
  4873. * SPP_PMTUD_DISABLE - This field will disable PMTU
  4874. * discovery upon the specified address. Note that
  4875. * if the address feild is empty then all addresses
  4876. * on the association are effected. Not also that
  4877. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  4878. * exclusive. Enabling both will have undetermined
  4879. * results.
  4880. *
  4881. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  4882. * on delayed sack. The time specified in spp_sackdelay
  4883. * is used to specify the sack delay for this address. Note
  4884. * that if spp_address is empty then all addresses will
  4885. * enable delayed sack and take on the sack delay
  4886. * value specified in spp_sackdelay.
  4887. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  4888. * off delayed sack. If the spp_address field is blank then
  4889. * delayed sack is disabled for the entire association. Note
  4890. * also that this field is mutually exclusive to
  4891. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  4892. * results.
  4893. *
  4894. * SPP_IPV6_FLOWLABEL: Setting this flag enables the
  4895. * setting of the IPV6 flow label value. The value is
  4896. * contained in the spp_ipv6_flowlabel field.
  4897. * Upon retrieval, this flag will be set to indicate that
  4898. * the spp_ipv6_flowlabel field has a valid value returned.
  4899. * If a specific destination address is set (in the
  4900. * spp_address field), then the value returned is that of
  4901. * the address. If just an association is specified (and
  4902. * no address), then the association's default flow label
  4903. * is returned. If neither an association nor a destination
  4904. * is specified, then the socket's default flow label is
  4905. * returned. For non-IPv6 sockets, this flag will be left
  4906. * cleared.
  4907. *
  4908. * SPP_DSCP: Setting this flag enables the setting of the
  4909. * Differentiated Services Code Point (DSCP) value
  4910. * associated with either the association or a specific
  4911. * address. The value is obtained in the spp_dscp field.
  4912. * Upon retrieval, this flag will be set to indicate that
  4913. * the spp_dscp field has a valid value returned. If a
  4914. * specific destination address is set when called (in the
  4915. * spp_address field), then that specific destination
  4916. * address's DSCP value is returned. If just an association
  4917. * is specified, then the association's default DSCP is
  4918. * returned. If neither an association nor a destination is
  4919. * specified, then the socket's default DSCP is returned.
  4920. *
  4921. * spp_ipv6_flowlabel
  4922. * - This field is used in conjunction with the
  4923. * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
  4924. * The 20 least significant bits are used for the flow
  4925. * label. This setting has precedence over any IPv6-layer
  4926. * setting.
  4927. *
  4928. * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
  4929. * and contains the DSCP. The 6 most significant bits are
  4930. * used for the DSCP. This setting has precedence over any
  4931. * IPv4- or IPv6- layer setting.
  4932. */
  4933. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  4934. char __user *optval, int __user *optlen)
  4935. {
  4936. struct sctp_paddrparams params;
  4937. struct sctp_transport *trans = NULL;
  4938. struct sctp_association *asoc = NULL;
  4939. struct sctp_sock *sp = sctp_sk(sk);
  4940. if (len >= sizeof(params))
  4941. len = sizeof(params);
  4942. else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
  4943. spp_ipv6_flowlabel), 4))
  4944. len = ALIGN(offsetof(struct sctp_paddrparams,
  4945. spp_ipv6_flowlabel), 4);
  4946. else
  4947. return -EINVAL;
  4948. if (copy_from_user(&params, optval, len))
  4949. return -EFAULT;
  4950. /* If an address other than INADDR_ANY is specified, and
  4951. * no transport is found, then the request is invalid.
  4952. */
  4953. if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
  4954. trans = sctp_addr_id2transport(sk, &params.spp_address,
  4955. params.spp_assoc_id);
  4956. if (!trans) {
  4957. pr_debug("%s: failed no transport\n", __func__);
  4958. return -EINVAL;
  4959. }
  4960. }
  4961. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  4962. * socket is a one to many style socket, and an association
  4963. * was not found, then the id was invalid.
  4964. */
  4965. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  4966. if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
  4967. sctp_style(sk, UDP)) {
  4968. pr_debug("%s: failed no association\n", __func__);
  4969. return -EINVAL;
  4970. }
  4971. if (trans) {
  4972. /* Fetch transport values. */
  4973. params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
  4974. params.spp_pathmtu = trans->pathmtu;
  4975. params.spp_pathmaxrxt = trans->pathmaxrxt;
  4976. params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
  4977. /*draft-11 doesn't say what to return in spp_flags*/
  4978. params.spp_flags = trans->param_flags;
  4979. if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
  4980. params.spp_ipv6_flowlabel = trans->flowlabel &
  4981. SCTP_FLOWLABEL_VAL_MASK;
  4982. params.spp_flags |= SPP_IPV6_FLOWLABEL;
  4983. }
  4984. if (trans->dscp & SCTP_DSCP_SET_MASK) {
  4985. params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
  4986. params.spp_flags |= SPP_DSCP;
  4987. }
  4988. } else if (asoc) {
  4989. /* Fetch association values. */
  4990. params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
  4991. params.spp_pathmtu = asoc->pathmtu;
  4992. params.spp_pathmaxrxt = asoc->pathmaxrxt;
  4993. params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
  4994. /*draft-11 doesn't say what to return in spp_flags*/
  4995. params.spp_flags = asoc->param_flags;
  4996. if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
  4997. params.spp_ipv6_flowlabel = asoc->flowlabel &
  4998. SCTP_FLOWLABEL_VAL_MASK;
  4999. params.spp_flags |= SPP_IPV6_FLOWLABEL;
  5000. }
  5001. if (asoc->dscp & SCTP_DSCP_SET_MASK) {
  5002. params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
  5003. params.spp_flags |= SPP_DSCP;
  5004. }
  5005. } else {
  5006. /* Fetch socket values. */
  5007. params.spp_hbinterval = sp->hbinterval;
  5008. params.spp_pathmtu = sp->pathmtu;
  5009. params.spp_sackdelay = sp->sackdelay;
  5010. params.spp_pathmaxrxt = sp->pathmaxrxt;
  5011. /*draft-11 doesn't say what to return in spp_flags*/
  5012. params.spp_flags = sp->param_flags;
  5013. if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
  5014. params.spp_ipv6_flowlabel = sp->flowlabel &
  5015. SCTP_FLOWLABEL_VAL_MASK;
  5016. params.spp_flags |= SPP_IPV6_FLOWLABEL;
  5017. }
  5018. if (sp->dscp & SCTP_DSCP_SET_MASK) {
  5019. params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
  5020. params.spp_flags |= SPP_DSCP;
  5021. }
  5022. }
  5023. if (copy_to_user(optval, &params, len))
  5024. return -EFAULT;
  5025. if (put_user(len, optlen))
  5026. return -EFAULT;
  5027. return 0;
  5028. }
  5029. /*
  5030. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  5031. *
  5032. * This option will effect the way delayed acks are performed. This
  5033. * option allows you to get or set the delayed ack time, in
  5034. * milliseconds. It also allows changing the delayed ack frequency.
  5035. * Changing the frequency to 1 disables the delayed sack algorithm. If
  5036. * the assoc_id is 0, then this sets or gets the endpoints default
  5037. * values. If the assoc_id field is non-zero, then the set or get
  5038. * effects the specified association for the one to many model (the
  5039. * assoc_id field is ignored by the one to one model). Note that if
  5040. * sack_delay or sack_freq are 0 when setting this option, then the
  5041. * current values will remain unchanged.
  5042. *
  5043. * struct sctp_sack_info {
  5044. * sctp_assoc_t sack_assoc_id;
  5045. * uint32_t sack_delay;
  5046. * uint32_t sack_freq;
  5047. * };
  5048. *
  5049. * sack_assoc_id - This parameter, indicates which association the user
  5050. * is performing an action upon. Note that if this field's value is
  5051. * zero then the endpoints default value is changed (effecting future
  5052. * associations only).
  5053. *
  5054. * sack_delay - This parameter contains the number of milliseconds that
  5055. * the user is requesting the delayed ACK timer be set to. Note that
  5056. * this value is defined in the standard to be between 200 and 500
  5057. * milliseconds.
  5058. *
  5059. * sack_freq - This parameter contains the number of packets that must
  5060. * be received before a sack is sent without waiting for the delay
  5061. * timer to expire. The default value for this is 2, setting this
  5062. * value to 1 will disable the delayed sack algorithm.
  5063. */
  5064. static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
  5065. char __user *optval,
  5066. int __user *optlen)
  5067. {
  5068. struct sctp_sack_info params;
  5069. struct sctp_association *asoc = NULL;
  5070. struct sctp_sock *sp = sctp_sk(sk);
  5071. if (len >= sizeof(struct sctp_sack_info)) {
  5072. len = sizeof(struct sctp_sack_info);
  5073. if (copy_from_user(&params, optval, len))
  5074. return -EFAULT;
  5075. } else if (len == sizeof(struct sctp_assoc_value)) {
  5076. pr_warn_ratelimited(DEPRECATED
  5077. "%s (pid %d) "
  5078. "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
  5079. "Use struct sctp_sack_info instead\n",
  5080. current->comm, task_pid_nr(current));
  5081. if (copy_from_user(&params, optval, len))
  5082. return -EFAULT;
  5083. } else
  5084. return -EINVAL;
  5085. /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
  5086. * socket is a one to many style socket, and an association
  5087. * was not found, then the id was invalid.
  5088. */
  5089. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  5090. if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
  5091. sctp_style(sk, UDP))
  5092. return -EINVAL;
  5093. if (asoc) {
  5094. /* Fetch association values. */
  5095. if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
  5096. params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
  5097. params.sack_freq = asoc->sackfreq;
  5098. } else {
  5099. params.sack_delay = 0;
  5100. params.sack_freq = 1;
  5101. }
  5102. } else {
  5103. /* Fetch socket values. */
  5104. if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
  5105. params.sack_delay = sp->sackdelay;
  5106. params.sack_freq = sp->sackfreq;
  5107. } else {
  5108. params.sack_delay = 0;
  5109. params.sack_freq = 1;
  5110. }
  5111. }
  5112. if (copy_to_user(optval, &params, len))
  5113. return -EFAULT;
  5114. if (put_user(len, optlen))
  5115. return -EFAULT;
  5116. return 0;
  5117. }
  5118. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  5119. *
  5120. * Applications can specify protocol parameters for the default association
  5121. * initialization. The option name argument to setsockopt() and getsockopt()
  5122. * is SCTP_INITMSG.
  5123. *
  5124. * Setting initialization parameters is effective only on an unconnected
  5125. * socket (for UDP-style sockets only future associations are effected
  5126. * by the change). With TCP-style sockets, this option is inherited by
  5127. * sockets derived from a listener socket.
  5128. */
  5129. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  5130. {
  5131. if (len < sizeof(struct sctp_initmsg))
  5132. return -EINVAL;
  5133. len = sizeof(struct sctp_initmsg);
  5134. if (put_user(len, optlen))
  5135. return -EFAULT;
  5136. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  5137. return -EFAULT;
  5138. return 0;
  5139. }
  5140. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  5141. char __user *optval, int __user *optlen)
  5142. {
  5143. struct sctp_association *asoc;
  5144. int cnt = 0;
  5145. struct sctp_getaddrs getaddrs;
  5146. struct sctp_transport *from;
  5147. void __user *to;
  5148. union sctp_addr temp;
  5149. struct sctp_sock *sp = sctp_sk(sk);
  5150. int addrlen;
  5151. size_t space_left;
  5152. int bytes_copied;
  5153. if (len < sizeof(struct sctp_getaddrs))
  5154. return -EINVAL;
  5155. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  5156. return -EFAULT;
  5157. /* For UDP-style sockets, id specifies the association to query. */
  5158. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  5159. if (!asoc)
  5160. return -EINVAL;
  5161. to = optval + offsetof(struct sctp_getaddrs, addrs);
  5162. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  5163. list_for_each_entry(from, &asoc->peer.transport_addr_list,
  5164. transports) {
  5165. memcpy(&temp, &from->ipaddr, sizeof(temp));
  5166. addrlen = sctp_get_pf_specific(sk->sk_family)
  5167. ->addr_to_user(sp, &temp);
  5168. if (space_left < addrlen)
  5169. return -ENOMEM;
  5170. if (copy_to_user(to, &temp, addrlen))
  5171. return -EFAULT;
  5172. to += addrlen;
  5173. cnt++;
  5174. space_left -= addrlen;
  5175. }
  5176. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  5177. return -EFAULT;
  5178. bytes_copied = ((char __user *)to) - optval;
  5179. if (put_user(bytes_copied, optlen))
  5180. return -EFAULT;
  5181. return 0;
  5182. }
  5183. static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
  5184. size_t space_left, int *bytes_copied)
  5185. {
  5186. struct sctp_sockaddr_entry *addr;
  5187. union sctp_addr temp;
  5188. int cnt = 0;
  5189. int addrlen;
  5190. struct net *net = sock_net(sk);
  5191. rcu_read_lock();
  5192. list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
  5193. if (!addr->valid)
  5194. continue;
  5195. if ((PF_INET == sk->sk_family) &&
  5196. (AF_INET6 == addr->a.sa.sa_family))
  5197. continue;
  5198. if ((PF_INET6 == sk->sk_family) &&
  5199. inet_v6_ipv6only(sk) &&
  5200. (AF_INET == addr->a.sa.sa_family))
  5201. continue;
  5202. memcpy(&temp, &addr->a, sizeof(temp));
  5203. if (!temp.v4.sin_port)
  5204. temp.v4.sin_port = htons(port);
  5205. addrlen = sctp_get_pf_specific(sk->sk_family)
  5206. ->addr_to_user(sctp_sk(sk), &temp);
  5207. if (space_left < addrlen) {
  5208. cnt = -ENOMEM;
  5209. break;
  5210. }
  5211. memcpy(to, &temp, addrlen);
  5212. to += addrlen;
  5213. cnt++;
  5214. space_left -= addrlen;
  5215. *bytes_copied += addrlen;
  5216. }
  5217. rcu_read_unlock();
  5218. return cnt;
  5219. }
  5220. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  5221. char __user *optval, int __user *optlen)
  5222. {
  5223. struct sctp_bind_addr *bp;
  5224. struct sctp_association *asoc;
  5225. int cnt = 0;
  5226. struct sctp_getaddrs getaddrs;
  5227. struct sctp_sockaddr_entry *addr;
  5228. void __user *to;
  5229. union sctp_addr temp;
  5230. struct sctp_sock *sp = sctp_sk(sk);
  5231. int addrlen;
  5232. int err = 0;
  5233. size_t space_left;
  5234. int bytes_copied = 0;
  5235. void *addrs;
  5236. void *buf;
  5237. if (len < sizeof(struct sctp_getaddrs))
  5238. return -EINVAL;
  5239. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  5240. return -EFAULT;
  5241. /*
  5242. * For UDP-style sockets, id specifies the association to query.
  5243. * If the id field is set to the value '0' then the locally bound
  5244. * addresses are returned without regard to any particular
  5245. * association.
  5246. */
  5247. if (0 == getaddrs.assoc_id) {
  5248. bp = &sctp_sk(sk)->ep->base.bind_addr;
  5249. } else {
  5250. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  5251. if (!asoc)
  5252. return -EINVAL;
  5253. bp = &asoc->base.bind_addr;
  5254. }
  5255. to = optval + offsetof(struct sctp_getaddrs, addrs);
  5256. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  5257. addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
  5258. if (!addrs)
  5259. return -ENOMEM;
  5260. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  5261. * addresses from the global local address list.
  5262. */
  5263. if (sctp_list_single_entry(&bp->address_list)) {
  5264. addr = list_entry(bp->address_list.next,
  5265. struct sctp_sockaddr_entry, list);
  5266. if (sctp_is_any(sk, &addr->a)) {
  5267. cnt = sctp_copy_laddrs(sk, bp->port, addrs,
  5268. space_left, &bytes_copied);
  5269. if (cnt < 0) {
  5270. err = cnt;
  5271. goto out;
  5272. }
  5273. goto copy_getaddrs;
  5274. }
  5275. }
  5276. buf = addrs;
  5277. /* Protection on the bound address list is not needed since
  5278. * in the socket option context we hold a socket lock and
  5279. * thus the bound address list can't change.
  5280. */
  5281. list_for_each_entry(addr, &bp->address_list, list) {
  5282. memcpy(&temp, &addr->a, sizeof(temp));
  5283. addrlen = sctp_get_pf_specific(sk->sk_family)
  5284. ->addr_to_user(sp, &temp);
  5285. if (space_left < addrlen) {
  5286. err = -ENOMEM; /*fixme: right error?*/
  5287. goto out;
  5288. }
  5289. memcpy(buf, &temp, addrlen);
  5290. buf += addrlen;
  5291. bytes_copied += addrlen;
  5292. cnt++;
  5293. space_left -= addrlen;
  5294. }
  5295. copy_getaddrs:
  5296. if (copy_to_user(to, addrs, bytes_copied)) {
  5297. err = -EFAULT;
  5298. goto out;
  5299. }
  5300. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
  5301. err = -EFAULT;
  5302. goto out;
  5303. }
  5304. /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
  5305. * but we can't change it anymore.
  5306. */
  5307. if (put_user(bytes_copied, optlen))
  5308. err = -EFAULT;
  5309. out:
  5310. kfree(addrs);
  5311. return err;
  5312. }
  5313. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  5314. *
  5315. * Requests that the local SCTP stack use the enclosed peer address as
  5316. * the association primary. The enclosed address must be one of the
  5317. * association peer's addresses.
  5318. */
  5319. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  5320. char __user *optval, int __user *optlen)
  5321. {
  5322. struct sctp_prim prim;
  5323. struct sctp_association *asoc;
  5324. struct sctp_sock *sp = sctp_sk(sk);
  5325. if (len < sizeof(struct sctp_prim))
  5326. return -EINVAL;
  5327. len = sizeof(struct sctp_prim);
  5328. if (copy_from_user(&prim, optval, len))
  5329. return -EFAULT;
  5330. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  5331. if (!asoc)
  5332. return -EINVAL;
  5333. if (!asoc->peer.primary_path)
  5334. return -ENOTCONN;
  5335. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  5336. asoc->peer.primary_path->af_specific->sockaddr_len);
  5337. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
  5338. (union sctp_addr *)&prim.ssp_addr);
  5339. if (put_user(len, optlen))
  5340. return -EFAULT;
  5341. if (copy_to_user(optval, &prim, len))
  5342. return -EFAULT;
  5343. return 0;
  5344. }
  5345. /*
  5346. * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
  5347. *
  5348. * Requests that the local endpoint set the specified Adaptation Layer
  5349. * Indication parameter for all future INIT and INIT-ACK exchanges.
  5350. */
  5351. static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
  5352. char __user *optval, int __user *optlen)
  5353. {
  5354. struct sctp_setadaptation adaptation;
  5355. if (len < sizeof(struct sctp_setadaptation))
  5356. return -EINVAL;
  5357. len = sizeof(struct sctp_setadaptation);
  5358. adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
  5359. if (put_user(len, optlen))
  5360. return -EFAULT;
  5361. if (copy_to_user(optval, &adaptation, len))
  5362. return -EFAULT;
  5363. return 0;
  5364. }
  5365. /*
  5366. *
  5367. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  5368. *
  5369. * Applications that wish to use the sendto() system call may wish to
  5370. * specify a default set of parameters that would normally be supplied
  5371. * through the inclusion of ancillary data. This socket option allows
  5372. * such an application to set the default sctp_sndrcvinfo structure.
  5373. * The application that wishes to use this socket option simply passes
  5374. * in to this call the sctp_sndrcvinfo structure defined in Section
  5375. * 5.2.2) The input parameters accepted by this call include
  5376. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  5377. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  5378. * to this call if the caller is using the UDP model.
  5379. *
  5380. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  5381. */
  5382. static int sctp_getsockopt_default_send_param(struct sock *sk,
  5383. int len, char __user *optval,
  5384. int __user *optlen)
  5385. {
  5386. struct sctp_sock *sp = sctp_sk(sk);
  5387. struct sctp_association *asoc;
  5388. struct sctp_sndrcvinfo info;
  5389. if (len < sizeof(info))
  5390. return -EINVAL;
  5391. len = sizeof(info);
  5392. if (copy_from_user(&info, optval, len))
  5393. return -EFAULT;
  5394. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  5395. if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
  5396. sctp_style(sk, UDP))
  5397. return -EINVAL;
  5398. if (asoc) {
  5399. info.sinfo_stream = asoc->default_stream;
  5400. info.sinfo_flags = asoc->default_flags;
  5401. info.sinfo_ppid = asoc->default_ppid;
  5402. info.sinfo_context = asoc->default_context;
  5403. info.sinfo_timetolive = asoc->default_timetolive;
  5404. } else {
  5405. info.sinfo_stream = sp->default_stream;
  5406. info.sinfo_flags = sp->default_flags;
  5407. info.sinfo_ppid = sp->default_ppid;
  5408. info.sinfo_context = sp->default_context;
  5409. info.sinfo_timetolive = sp->default_timetolive;
  5410. }
  5411. if (put_user(len, optlen))
  5412. return -EFAULT;
  5413. if (copy_to_user(optval, &info, len))
  5414. return -EFAULT;
  5415. return 0;
  5416. }
  5417. /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
  5418. * (SCTP_DEFAULT_SNDINFO)
  5419. */
  5420. static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
  5421. char __user *optval,
  5422. int __user *optlen)
  5423. {
  5424. struct sctp_sock *sp = sctp_sk(sk);
  5425. struct sctp_association *asoc;
  5426. struct sctp_sndinfo info;
  5427. if (len < sizeof(info))
  5428. return -EINVAL;
  5429. len = sizeof(info);
  5430. if (copy_from_user(&info, optval, len))
  5431. return -EFAULT;
  5432. asoc = sctp_id2assoc(sk, info.snd_assoc_id);
  5433. if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
  5434. sctp_style(sk, UDP))
  5435. return -EINVAL;
  5436. if (asoc) {
  5437. info.snd_sid = asoc->default_stream;
  5438. info.snd_flags = asoc->default_flags;
  5439. info.snd_ppid = asoc->default_ppid;
  5440. info.snd_context = asoc->default_context;
  5441. } else {
  5442. info.snd_sid = sp->default_stream;
  5443. info.snd_flags = sp->default_flags;
  5444. info.snd_ppid = sp->default_ppid;
  5445. info.snd_context = sp->default_context;
  5446. }
  5447. if (put_user(len, optlen))
  5448. return -EFAULT;
  5449. if (copy_to_user(optval, &info, len))
  5450. return -EFAULT;
  5451. return 0;
  5452. }
  5453. /*
  5454. *
  5455. * 7.1.5 SCTP_NODELAY
  5456. *
  5457. * Turn on/off any Nagle-like algorithm. This means that packets are
  5458. * generally sent as soon as possible and no unnecessary delays are
  5459. * introduced, at the cost of more packets in the network. Expects an
  5460. * integer boolean flag.
  5461. */
  5462. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  5463. char __user *optval, int __user *optlen)
  5464. {
  5465. int val;
  5466. if (len < sizeof(int))
  5467. return -EINVAL;
  5468. len = sizeof(int);
  5469. val = (sctp_sk(sk)->nodelay == 1);
  5470. if (put_user(len, optlen))
  5471. return -EFAULT;
  5472. if (copy_to_user(optval, &val, len))
  5473. return -EFAULT;
  5474. return 0;
  5475. }
  5476. /*
  5477. *
  5478. * 7.1.1 SCTP_RTOINFO
  5479. *
  5480. * The protocol parameters used to initialize and bound retransmission
  5481. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  5482. * and modify these parameters.
  5483. * All parameters are time values, in milliseconds. A value of 0, when
  5484. * modifying the parameters, indicates that the current value should not
  5485. * be changed.
  5486. *
  5487. */
  5488. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  5489. char __user *optval,
  5490. int __user *optlen) {
  5491. struct sctp_rtoinfo rtoinfo;
  5492. struct sctp_association *asoc;
  5493. if (len < sizeof (struct sctp_rtoinfo))
  5494. return -EINVAL;
  5495. len = sizeof(struct sctp_rtoinfo);
  5496. if (copy_from_user(&rtoinfo, optval, len))
  5497. return -EFAULT;
  5498. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  5499. if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
  5500. sctp_style(sk, UDP))
  5501. return -EINVAL;
  5502. /* Values corresponding to the specific association. */
  5503. if (asoc) {
  5504. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  5505. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  5506. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  5507. } else {
  5508. /* Values corresponding to the endpoint. */
  5509. struct sctp_sock *sp = sctp_sk(sk);
  5510. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  5511. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  5512. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  5513. }
  5514. if (put_user(len, optlen))
  5515. return -EFAULT;
  5516. if (copy_to_user(optval, &rtoinfo, len))
  5517. return -EFAULT;
  5518. return 0;
  5519. }
  5520. /*
  5521. *
  5522. * 7.1.2 SCTP_ASSOCINFO
  5523. *
  5524. * This option is used to tune the maximum retransmission attempts
  5525. * of the association.
  5526. * Returns an error if the new association retransmission value is
  5527. * greater than the sum of the retransmission value of the peer.
  5528. * See [SCTP] for more information.
  5529. *
  5530. */
  5531. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  5532. char __user *optval,
  5533. int __user *optlen)
  5534. {
  5535. struct sctp_assocparams assocparams;
  5536. struct sctp_association *asoc;
  5537. struct list_head *pos;
  5538. int cnt = 0;
  5539. if (len < sizeof (struct sctp_assocparams))
  5540. return -EINVAL;
  5541. len = sizeof(struct sctp_assocparams);
  5542. if (copy_from_user(&assocparams, optval, len))
  5543. return -EFAULT;
  5544. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  5545. if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
  5546. sctp_style(sk, UDP))
  5547. return -EINVAL;
  5548. /* Values correspoinding to the specific association */
  5549. if (asoc) {
  5550. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  5551. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  5552. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  5553. assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
  5554. list_for_each(pos, &asoc->peer.transport_addr_list) {
  5555. cnt++;
  5556. }
  5557. assocparams.sasoc_number_peer_destinations = cnt;
  5558. } else {
  5559. /* Values corresponding to the endpoint */
  5560. struct sctp_sock *sp = sctp_sk(sk);
  5561. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  5562. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  5563. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  5564. assocparams.sasoc_cookie_life =
  5565. sp->assocparams.sasoc_cookie_life;
  5566. assocparams.sasoc_number_peer_destinations =
  5567. sp->assocparams.
  5568. sasoc_number_peer_destinations;
  5569. }
  5570. if (put_user(len, optlen))
  5571. return -EFAULT;
  5572. if (copy_to_user(optval, &assocparams, len))
  5573. return -EFAULT;
  5574. return 0;
  5575. }
  5576. /*
  5577. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  5578. *
  5579. * This socket option is a boolean flag which turns on or off mapped V4
  5580. * addresses. If this option is turned on and the socket is type
  5581. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  5582. * If this option is turned off, then no mapping will be done of V4
  5583. * addresses and a user will receive both PF_INET6 and PF_INET type
  5584. * addresses on the socket.
  5585. */
  5586. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  5587. char __user *optval, int __user *optlen)
  5588. {
  5589. int val;
  5590. struct sctp_sock *sp = sctp_sk(sk);
  5591. if (len < sizeof(int))
  5592. return -EINVAL;
  5593. len = sizeof(int);
  5594. val = sp->v4mapped;
  5595. if (put_user(len, optlen))
  5596. return -EFAULT;
  5597. if (copy_to_user(optval, &val, len))
  5598. return -EFAULT;
  5599. return 0;
  5600. }
  5601. /*
  5602. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  5603. * (chapter and verse is quoted at sctp_setsockopt_context())
  5604. */
  5605. static int sctp_getsockopt_context(struct sock *sk, int len,
  5606. char __user *optval, int __user *optlen)
  5607. {
  5608. struct sctp_assoc_value params;
  5609. struct sctp_association *asoc;
  5610. if (len < sizeof(struct sctp_assoc_value))
  5611. return -EINVAL;
  5612. len = sizeof(struct sctp_assoc_value);
  5613. if (copy_from_user(&params, optval, len))
  5614. return -EFAULT;
  5615. asoc = sctp_id2assoc(sk, params.assoc_id);
  5616. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  5617. sctp_style(sk, UDP))
  5618. return -EINVAL;
  5619. params.assoc_value = asoc ? asoc->default_rcv_context
  5620. : sctp_sk(sk)->default_rcv_context;
  5621. if (put_user(len, optlen))
  5622. return -EFAULT;
  5623. if (copy_to_user(optval, &params, len))
  5624. return -EFAULT;
  5625. return 0;
  5626. }
  5627. /*
  5628. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  5629. * This option will get or set the maximum size to put in any outgoing
  5630. * SCTP DATA chunk. If a message is larger than this size it will be
  5631. * fragmented by SCTP into the specified size. Note that the underlying
  5632. * SCTP implementation may fragment into smaller sized chunks when the
  5633. * PMTU of the underlying association is smaller than the value set by
  5634. * the user. The default value for this option is '0' which indicates
  5635. * the user is NOT limiting fragmentation and only the PMTU will effect
  5636. * SCTP's choice of DATA chunk size. Note also that values set larger
  5637. * than the maximum size of an IP datagram will effectively let SCTP
  5638. * control fragmentation (i.e. the same as setting this option to 0).
  5639. *
  5640. * The following structure is used to access and modify this parameter:
  5641. *
  5642. * struct sctp_assoc_value {
  5643. * sctp_assoc_t assoc_id;
  5644. * uint32_t assoc_value;
  5645. * };
  5646. *
  5647. * assoc_id: This parameter is ignored for one-to-one style sockets.
  5648. * For one-to-many style sockets this parameter indicates which
  5649. * association the user is performing an action upon. Note that if
  5650. * this field's value is zero then the endpoints default value is
  5651. * changed (effecting future associations only).
  5652. * assoc_value: This parameter specifies the maximum size in bytes.
  5653. */
  5654. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  5655. char __user *optval, int __user *optlen)
  5656. {
  5657. struct sctp_assoc_value params;
  5658. struct sctp_association *asoc;
  5659. if (len == sizeof(int)) {
  5660. pr_warn_ratelimited(DEPRECATED
  5661. "%s (pid %d) "
  5662. "Use of int in maxseg socket option.\n"
  5663. "Use struct sctp_assoc_value instead\n",
  5664. current->comm, task_pid_nr(current));
  5665. params.assoc_id = SCTP_FUTURE_ASSOC;
  5666. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5667. len = sizeof(struct sctp_assoc_value);
  5668. if (copy_from_user(&params, optval, len))
  5669. return -EFAULT;
  5670. } else
  5671. return -EINVAL;
  5672. asoc = sctp_id2assoc(sk, params.assoc_id);
  5673. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  5674. sctp_style(sk, UDP))
  5675. return -EINVAL;
  5676. if (asoc)
  5677. params.assoc_value = asoc->frag_point;
  5678. else
  5679. params.assoc_value = sctp_sk(sk)->user_frag;
  5680. if (put_user(len, optlen))
  5681. return -EFAULT;
  5682. if (len == sizeof(int)) {
  5683. if (copy_to_user(optval, &params.assoc_value, len))
  5684. return -EFAULT;
  5685. } else {
  5686. if (copy_to_user(optval, &params, len))
  5687. return -EFAULT;
  5688. }
  5689. return 0;
  5690. }
  5691. /*
  5692. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  5693. * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
  5694. */
  5695. static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
  5696. char __user *optval, int __user *optlen)
  5697. {
  5698. int val;
  5699. if (len < sizeof(int))
  5700. return -EINVAL;
  5701. len = sizeof(int);
  5702. val = sctp_sk(sk)->frag_interleave;
  5703. if (put_user(len, optlen))
  5704. return -EFAULT;
  5705. if (copy_to_user(optval, &val, len))
  5706. return -EFAULT;
  5707. return 0;
  5708. }
  5709. /*
  5710. * 7.1.25. Set or Get the sctp partial delivery point
  5711. * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
  5712. */
  5713. static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
  5714. char __user *optval,
  5715. int __user *optlen)
  5716. {
  5717. u32 val;
  5718. if (len < sizeof(u32))
  5719. return -EINVAL;
  5720. len = sizeof(u32);
  5721. val = sctp_sk(sk)->pd_point;
  5722. if (put_user(len, optlen))
  5723. return -EFAULT;
  5724. if (copy_to_user(optval, &val, len))
  5725. return -EFAULT;
  5726. return 0;
  5727. }
  5728. /*
  5729. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  5730. * (chapter and verse is quoted at sctp_setsockopt_maxburst())
  5731. */
  5732. static int sctp_getsockopt_maxburst(struct sock *sk, int len,
  5733. char __user *optval,
  5734. int __user *optlen)
  5735. {
  5736. struct sctp_assoc_value params;
  5737. struct sctp_association *asoc;
  5738. if (len == sizeof(int)) {
  5739. pr_warn_ratelimited(DEPRECATED
  5740. "%s (pid %d) "
  5741. "Use of int in max_burst socket option.\n"
  5742. "Use struct sctp_assoc_value instead\n",
  5743. current->comm, task_pid_nr(current));
  5744. params.assoc_id = SCTP_FUTURE_ASSOC;
  5745. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5746. len = sizeof(struct sctp_assoc_value);
  5747. if (copy_from_user(&params, optval, len))
  5748. return -EFAULT;
  5749. } else
  5750. return -EINVAL;
  5751. asoc = sctp_id2assoc(sk, params.assoc_id);
  5752. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  5753. sctp_style(sk, UDP))
  5754. return -EINVAL;
  5755. params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
  5756. if (len == sizeof(int)) {
  5757. if (copy_to_user(optval, &params.assoc_value, len))
  5758. return -EFAULT;
  5759. } else {
  5760. if (copy_to_user(optval, &params, len))
  5761. return -EFAULT;
  5762. }
  5763. return 0;
  5764. }
  5765. static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
  5766. char __user *optval, int __user *optlen)
  5767. {
  5768. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5769. struct sctp_hmacalgo __user *p = (void __user *)optval;
  5770. struct sctp_hmac_algo_param *hmacs;
  5771. __u16 data_len = 0;
  5772. u32 num_idents;
  5773. int i;
  5774. if (!ep->auth_enable)
  5775. return -EACCES;
  5776. hmacs = ep->auth_hmacs_list;
  5777. data_len = ntohs(hmacs->param_hdr.length) -
  5778. sizeof(struct sctp_paramhdr);
  5779. if (len < sizeof(struct sctp_hmacalgo) + data_len)
  5780. return -EINVAL;
  5781. len = sizeof(struct sctp_hmacalgo) + data_len;
  5782. num_idents = data_len / sizeof(u16);
  5783. if (put_user(len, optlen))
  5784. return -EFAULT;
  5785. if (put_user(num_idents, &p->shmac_num_idents))
  5786. return -EFAULT;
  5787. for (i = 0; i < num_idents; i++) {
  5788. __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
  5789. if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
  5790. return -EFAULT;
  5791. }
  5792. return 0;
  5793. }
  5794. static int sctp_getsockopt_active_key(struct sock *sk, int len,
  5795. char __user *optval, int __user *optlen)
  5796. {
  5797. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5798. struct sctp_authkeyid val;
  5799. struct sctp_association *asoc;
  5800. if (len < sizeof(struct sctp_authkeyid))
  5801. return -EINVAL;
  5802. len = sizeof(struct sctp_authkeyid);
  5803. if (copy_from_user(&val, optval, len))
  5804. return -EFAULT;
  5805. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  5806. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  5807. return -EINVAL;
  5808. if (asoc) {
  5809. if (!asoc->peer.auth_capable)
  5810. return -EACCES;
  5811. val.scact_keynumber = asoc->active_key_id;
  5812. } else {
  5813. if (!ep->auth_enable)
  5814. return -EACCES;
  5815. val.scact_keynumber = ep->active_key_id;
  5816. }
  5817. if (put_user(len, optlen))
  5818. return -EFAULT;
  5819. if (copy_to_user(optval, &val, len))
  5820. return -EFAULT;
  5821. return 0;
  5822. }
  5823. static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
  5824. char __user *optval, int __user *optlen)
  5825. {
  5826. struct sctp_authchunks __user *p = (void __user *)optval;
  5827. struct sctp_authchunks val;
  5828. struct sctp_association *asoc;
  5829. struct sctp_chunks_param *ch;
  5830. u32 num_chunks = 0;
  5831. char __user *to;
  5832. if (len < sizeof(struct sctp_authchunks))
  5833. return -EINVAL;
  5834. if (copy_from_user(&val, optval, sizeof(val)))
  5835. return -EFAULT;
  5836. to = p->gauth_chunks;
  5837. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  5838. if (!asoc)
  5839. return -EINVAL;
  5840. if (!asoc->peer.auth_capable)
  5841. return -EACCES;
  5842. ch = asoc->peer.peer_chunks;
  5843. if (!ch)
  5844. goto num;
  5845. /* See if the user provided enough room for all the data */
  5846. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  5847. if (len < num_chunks)
  5848. return -EINVAL;
  5849. if (copy_to_user(to, ch->chunks, num_chunks))
  5850. return -EFAULT;
  5851. num:
  5852. len = sizeof(struct sctp_authchunks) + num_chunks;
  5853. if (put_user(len, optlen))
  5854. return -EFAULT;
  5855. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  5856. return -EFAULT;
  5857. return 0;
  5858. }
  5859. static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
  5860. char __user *optval, int __user *optlen)
  5861. {
  5862. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5863. struct sctp_authchunks __user *p = (void __user *)optval;
  5864. struct sctp_authchunks val;
  5865. struct sctp_association *asoc;
  5866. struct sctp_chunks_param *ch;
  5867. u32 num_chunks = 0;
  5868. char __user *to;
  5869. if (len < sizeof(struct sctp_authchunks))
  5870. return -EINVAL;
  5871. if (copy_from_user(&val, optval, sizeof(val)))
  5872. return -EFAULT;
  5873. to = p->gauth_chunks;
  5874. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  5875. if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
  5876. sctp_style(sk, UDP))
  5877. return -EINVAL;
  5878. if (asoc) {
  5879. if (!asoc->peer.auth_capable)
  5880. return -EACCES;
  5881. ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
  5882. } else {
  5883. if (!ep->auth_enable)
  5884. return -EACCES;
  5885. ch = ep->auth_chunk_list;
  5886. }
  5887. if (!ch)
  5888. goto num;
  5889. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  5890. if (len < sizeof(struct sctp_authchunks) + num_chunks)
  5891. return -EINVAL;
  5892. if (copy_to_user(to, ch->chunks, num_chunks))
  5893. return -EFAULT;
  5894. num:
  5895. len = sizeof(struct sctp_authchunks) + num_chunks;
  5896. if (put_user(len, optlen))
  5897. return -EFAULT;
  5898. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  5899. return -EFAULT;
  5900. return 0;
  5901. }
  5902. /*
  5903. * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
  5904. * This option gets the current number of associations that are attached
  5905. * to a one-to-many style socket. The option value is an uint32_t.
  5906. */
  5907. static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
  5908. char __user *optval, int __user *optlen)
  5909. {
  5910. struct sctp_sock *sp = sctp_sk(sk);
  5911. struct sctp_association *asoc;
  5912. u32 val = 0;
  5913. if (sctp_style(sk, TCP))
  5914. return -EOPNOTSUPP;
  5915. if (len < sizeof(u32))
  5916. return -EINVAL;
  5917. len = sizeof(u32);
  5918. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  5919. val++;
  5920. }
  5921. if (put_user(len, optlen))
  5922. return -EFAULT;
  5923. if (copy_to_user(optval, &val, len))
  5924. return -EFAULT;
  5925. return 0;
  5926. }
  5927. /*
  5928. * 8.1.23 SCTP_AUTO_ASCONF
  5929. * See the corresponding setsockopt entry as description
  5930. */
  5931. static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
  5932. char __user *optval, int __user *optlen)
  5933. {
  5934. int val = 0;
  5935. if (len < sizeof(int))
  5936. return -EINVAL;
  5937. len = sizeof(int);
  5938. if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
  5939. val = 1;
  5940. if (put_user(len, optlen))
  5941. return -EFAULT;
  5942. if (copy_to_user(optval, &val, len))
  5943. return -EFAULT;
  5944. return 0;
  5945. }
  5946. /*
  5947. * 8.2.6. Get the Current Identifiers of Associations
  5948. * (SCTP_GET_ASSOC_ID_LIST)
  5949. *
  5950. * This option gets the current list of SCTP association identifiers of
  5951. * the SCTP associations handled by a one-to-many style socket.
  5952. */
  5953. static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
  5954. char __user *optval, int __user *optlen)
  5955. {
  5956. struct sctp_sock *sp = sctp_sk(sk);
  5957. struct sctp_association *asoc;
  5958. struct sctp_assoc_ids *ids;
  5959. u32 num = 0;
  5960. if (sctp_style(sk, TCP))
  5961. return -EOPNOTSUPP;
  5962. if (len < sizeof(struct sctp_assoc_ids))
  5963. return -EINVAL;
  5964. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  5965. num++;
  5966. }
  5967. if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
  5968. return -EINVAL;
  5969. len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
  5970. ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
  5971. if (unlikely(!ids))
  5972. return -ENOMEM;
  5973. ids->gaids_number_of_ids = num;
  5974. num = 0;
  5975. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  5976. ids->gaids_assoc_id[num++] = asoc->assoc_id;
  5977. }
  5978. if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
  5979. kfree(ids);
  5980. return -EFAULT;
  5981. }
  5982. kfree(ids);
  5983. return 0;
  5984. }
  5985. /*
  5986. * SCTP_PEER_ADDR_THLDS
  5987. *
  5988. * This option allows us to fetch the partially failed threshold for one or all
  5989. * transports in an association. See Section 6.1 of:
  5990. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  5991. */
  5992. static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
  5993. char __user *optval, int len,
  5994. int __user *optlen, bool v2)
  5995. {
  5996. struct sctp_paddrthlds_v2 val;
  5997. struct sctp_transport *trans;
  5998. struct sctp_association *asoc;
  5999. int min;
  6000. min = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds);
  6001. if (len < min)
  6002. return -EINVAL;
  6003. len = min;
  6004. if (copy_from_user(&val, optval, len))
  6005. return -EFAULT;
  6006. if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
  6007. trans = sctp_addr_id2transport(sk, &val.spt_address,
  6008. val.spt_assoc_id);
  6009. if (!trans)
  6010. return -ENOENT;
  6011. val.spt_pathmaxrxt = trans->pathmaxrxt;
  6012. val.spt_pathpfthld = trans->pf_retrans;
  6013. val.spt_pathcpthld = trans->ps_retrans;
  6014. goto out;
  6015. }
  6016. asoc = sctp_id2assoc(sk, val.spt_assoc_id);
  6017. if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
  6018. sctp_style(sk, UDP))
  6019. return -EINVAL;
  6020. if (asoc) {
  6021. val.spt_pathpfthld = asoc->pf_retrans;
  6022. val.spt_pathmaxrxt = asoc->pathmaxrxt;
  6023. val.spt_pathcpthld = asoc->ps_retrans;
  6024. } else {
  6025. struct sctp_sock *sp = sctp_sk(sk);
  6026. val.spt_pathpfthld = sp->pf_retrans;
  6027. val.spt_pathmaxrxt = sp->pathmaxrxt;
  6028. val.spt_pathcpthld = sp->ps_retrans;
  6029. }
  6030. out:
  6031. if (put_user(len, optlen) || copy_to_user(optval, &val, len))
  6032. return -EFAULT;
  6033. return 0;
  6034. }
  6035. /*
  6036. * SCTP_GET_ASSOC_STATS
  6037. *
  6038. * This option retrieves local per endpoint statistics. It is modeled
  6039. * after OpenSolaris' implementation
  6040. */
  6041. static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
  6042. char __user *optval,
  6043. int __user *optlen)
  6044. {
  6045. struct sctp_assoc_stats sas;
  6046. struct sctp_association *asoc = NULL;
  6047. /* User must provide at least the assoc id */
  6048. if (len < sizeof(sctp_assoc_t))
  6049. return -EINVAL;
  6050. /* Allow the struct to grow and fill in as much as possible */
  6051. len = min_t(size_t, len, sizeof(sas));
  6052. if (copy_from_user(&sas, optval, len))
  6053. return -EFAULT;
  6054. asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
  6055. if (!asoc)
  6056. return -EINVAL;
  6057. sas.sas_rtxchunks = asoc->stats.rtxchunks;
  6058. sas.sas_gapcnt = asoc->stats.gapcnt;
  6059. sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
  6060. sas.sas_osacks = asoc->stats.osacks;
  6061. sas.sas_isacks = asoc->stats.isacks;
  6062. sas.sas_octrlchunks = asoc->stats.octrlchunks;
  6063. sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
  6064. sas.sas_oodchunks = asoc->stats.oodchunks;
  6065. sas.sas_iodchunks = asoc->stats.iodchunks;
  6066. sas.sas_ouodchunks = asoc->stats.ouodchunks;
  6067. sas.sas_iuodchunks = asoc->stats.iuodchunks;
  6068. sas.sas_idupchunks = asoc->stats.idupchunks;
  6069. sas.sas_opackets = asoc->stats.opackets;
  6070. sas.sas_ipackets = asoc->stats.ipackets;
  6071. /* New high max rto observed, will return 0 if not a single
  6072. * RTO update took place. obs_rto_ipaddr will be bogus
  6073. * in such a case
  6074. */
  6075. sas.sas_maxrto = asoc->stats.max_obs_rto;
  6076. memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
  6077. sizeof(struct sockaddr_storage));
  6078. /* Mark beginning of a new observation period */
  6079. asoc->stats.max_obs_rto = asoc->rto_min;
  6080. if (put_user(len, optlen))
  6081. return -EFAULT;
  6082. pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
  6083. if (copy_to_user(optval, &sas, len))
  6084. return -EFAULT;
  6085. return 0;
  6086. }
  6087. static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
  6088. char __user *optval,
  6089. int __user *optlen)
  6090. {
  6091. int val = 0;
  6092. if (len < sizeof(int))
  6093. return -EINVAL;
  6094. len = sizeof(int);
  6095. if (sctp_sk(sk)->recvrcvinfo)
  6096. val = 1;
  6097. if (put_user(len, optlen))
  6098. return -EFAULT;
  6099. if (copy_to_user(optval, &val, len))
  6100. return -EFAULT;
  6101. return 0;
  6102. }
  6103. static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
  6104. char __user *optval,
  6105. int __user *optlen)
  6106. {
  6107. int val = 0;
  6108. if (len < sizeof(int))
  6109. return -EINVAL;
  6110. len = sizeof(int);
  6111. if (sctp_sk(sk)->recvnxtinfo)
  6112. val = 1;
  6113. if (put_user(len, optlen))
  6114. return -EFAULT;
  6115. if (copy_to_user(optval, &val, len))
  6116. return -EFAULT;
  6117. return 0;
  6118. }
  6119. static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
  6120. char __user *optval,
  6121. int __user *optlen)
  6122. {
  6123. struct sctp_assoc_value params;
  6124. struct sctp_association *asoc;
  6125. int retval = -EFAULT;
  6126. if (len < sizeof(params)) {
  6127. retval = -EINVAL;
  6128. goto out;
  6129. }
  6130. len = sizeof(params);
  6131. if (copy_from_user(&params, optval, len))
  6132. goto out;
  6133. asoc = sctp_id2assoc(sk, params.assoc_id);
  6134. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6135. sctp_style(sk, UDP)) {
  6136. retval = -EINVAL;
  6137. goto out;
  6138. }
  6139. params.assoc_value = asoc ? asoc->peer.prsctp_capable
  6140. : sctp_sk(sk)->ep->prsctp_enable;
  6141. if (put_user(len, optlen))
  6142. goto out;
  6143. if (copy_to_user(optval, &params, len))
  6144. goto out;
  6145. retval = 0;
  6146. out:
  6147. return retval;
  6148. }
  6149. static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
  6150. char __user *optval,
  6151. int __user *optlen)
  6152. {
  6153. struct sctp_default_prinfo info;
  6154. struct sctp_association *asoc;
  6155. int retval = -EFAULT;
  6156. if (len < sizeof(info)) {
  6157. retval = -EINVAL;
  6158. goto out;
  6159. }
  6160. len = sizeof(info);
  6161. if (copy_from_user(&info, optval, len))
  6162. goto out;
  6163. asoc = sctp_id2assoc(sk, info.pr_assoc_id);
  6164. if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
  6165. sctp_style(sk, UDP)) {
  6166. retval = -EINVAL;
  6167. goto out;
  6168. }
  6169. if (asoc) {
  6170. info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
  6171. info.pr_value = asoc->default_timetolive;
  6172. } else {
  6173. struct sctp_sock *sp = sctp_sk(sk);
  6174. info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
  6175. info.pr_value = sp->default_timetolive;
  6176. }
  6177. if (put_user(len, optlen))
  6178. goto out;
  6179. if (copy_to_user(optval, &info, len))
  6180. goto out;
  6181. retval = 0;
  6182. out:
  6183. return retval;
  6184. }
  6185. static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
  6186. char __user *optval,
  6187. int __user *optlen)
  6188. {
  6189. struct sctp_prstatus params;
  6190. struct sctp_association *asoc;
  6191. int policy;
  6192. int retval = -EINVAL;
  6193. if (len < sizeof(params))
  6194. goto out;
  6195. len = sizeof(params);
  6196. if (copy_from_user(&params, optval, len)) {
  6197. retval = -EFAULT;
  6198. goto out;
  6199. }
  6200. policy = params.sprstat_policy;
  6201. if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
  6202. ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
  6203. goto out;
  6204. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  6205. if (!asoc)
  6206. goto out;
  6207. if (policy == SCTP_PR_SCTP_ALL) {
  6208. params.sprstat_abandoned_unsent = 0;
  6209. params.sprstat_abandoned_sent = 0;
  6210. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  6211. params.sprstat_abandoned_unsent +=
  6212. asoc->abandoned_unsent[policy];
  6213. params.sprstat_abandoned_sent +=
  6214. asoc->abandoned_sent[policy];
  6215. }
  6216. } else {
  6217. params.sprstat_abandoned_unsent =
  6218. asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  6219. params.sprstat_abandoned_sent =
  6220. asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
  6221. }
  6222. if (put_user(len, optlen)) {
  6223. retval = -EFAULT;
  6224. goto out;
  6225. }
  6226. if (copy_to_user(optval, &params, len)) {
  6227. retval = -EFAULT;
  6228. goto out;
  6229. }
  6230. retval = 0;
  6231. out:
  6232. return retval;
  6233. }
  6234. static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
  6235. char __user *optval,
  6236. int __user *optlen)
  6237. {
  6238. struct sctp_stream_out_ext *streamoute;
  6239. struct sctp_association *asoc;
  6240. struct sctp_prstatus params;
  6241. int retval = -EINVAL;
  6242. int policy;
  6243. if (len < sizeof(params))
  6244. goto out;
  6245. len = sizeof(params);
  6246. if (copy_from_user(&params, optval, len)) {
  6247. retval = -EFAULT;
  6248. goto out;
  6249. }
  6250. policy = params.sprstat_policy;
  6251. if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
  6252. ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
  6253. goto out;
  6254. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  6255. if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
  6256. goto out;
  6257. streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
  6258. if (!streamoute) {
  6259. /* Not allocated yet, means all stats are 0 */
  6260. params.sprstat_abandoned_unsent = 0;
  6261. params.sprstat_abandoned_sent = 0;
  6262. retval = 0;
  6263. goto out;
  6264. }
  6265. if (policy == SCTP_PR_SCTP_ALL) {
  6266. params.sprstat_abandoned_unsent = 0;
  6267. params.sprstat_abandoned_sent = 0;
  6268. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  6269. params.sprstat_abandoned_unsent +=
  6270. streamoute->abandoned_unsent[policy];
  6271. params.sprstat_abandoned_sent +=
  6272. streamoute->abandoned_sent[policy];
  6273. }
  6274. } else {
  6275. params.sprstat_abandoned_unsent =
  6276. streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  6277. params.sprstat_abandoned_sent =
  6278. streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
  6279. }
  6280. if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
  6281. retval = -EFAULT;
  6282. goto out;
  6283. }
  6284. retval = 0;
  6285. out:
  6286. return retval;
  6287. }
  6288. static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
  6289. char __user *optval,
  6290. int __user *optlen)
  6291. {
  6292. struct sctp_assoc_value params;
  6293. struct sctp_association *asoc;
  6294. int retval = -EFAULT;
  6295. if (len < sizeof(params)) {
  6296. retval = -EINVAL;
  6297. goto out;
  6298. }
  6299. len = sizeof(params);
  6300. if (copy_from_user(&params, optval, len))
  6301. goto out;
  6302. asoc = sctp_id2assoc(sk, params.assoc_id);
  6303. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6304. sctp_style(sk, UDP)) {
  6305. retval = -EINVAL;
  6306. goto out;
  6307. }
  6308. params.assoc_value = asoc ? asoc->peer.reconf_capable
  6309. : sctp_sk(sk)->ep->reconf_enable;
  6310. if (put_user(len, optlen))
  6311. goto out;
  6312. if (copy_to_user(optval, &params, len))
  6313. goto out;
  6314. retval = 0;
  6315. out:
  6316. return retval;
  6317. }
  6318. static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
  6319. char __user *optval,
  6320. int __user *optlen)
  6321. {
  6322. struct sctp_assoc_value params;
  6323. struct sctp_association *asoc;
  6324. int retval = -EFAULT;
  6325. if (len < sizeof(params)) {
  6326. retval = -EINVAL;
  6327. goto out;
  6328. }
  6329. len = sizeof(params);
  6330. if (copy_from_user(&params, optval, len))
  6331. goto out;
  6332. asoc = sctp_id2assoc(sk, params.assoc_id);
  6333. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6334. sctp_style(sk, UDP)) {
  6335. retval = -EINVAL;
  6336. goto out;
  6337. }
  6338. params.assoc_value = asoc ? asoc->strreset_enable
  6339. : sctp_sk(sk)->ep->strreset_enable;
  6340. if (put_user(len, optlen))
  6341. goto out;
  6342. if (copy_to_user(optval, &params, len))
  6343. goto out;
  6344. retval = 0;
  6345. out:
  6346. return retval;
  6347. }
  6348. static int sctp_getsockopt_scheduler(struct sock *sk, int len,
  6349. char __user *optval,
  6350. int __user *optlen)
  6351. {
  6352. struct sctp_assoc_value params;
  6353. struct sctp_association *asoc;
  6354. int retval = -EFAULT;
  6355. if (len < sizeof(params)) {
  6356. retval = -EINVAL;
  6357. goto out;
  6358. }
  6359. len = sizeof(params);
  6360. if (copy_from_user(&params, optval, len))
  6361. goto out;
  6362. asoc = sctp_id2assoc(sk, params.assoc_id);
  6363. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6364. sctp_style(sk, UDP)) {
  6365. retval = -EINVAL;
  6366. goto out;
  6367. }
  6368. params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
  6369. : sctp_sk(sk)->default_ss;
  6370. if (put_user(len, optlen))
  6371. goto out;
  6372. if (copy_to_user(optval, &params, len))
  6373. goto out;
  6374. retval = 0;
  6375. out:
  6376. return retval;
  6377. }
  6378. static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
  6379. char __user *optval,
  6380. int __user *optlen)
  6381. {
  6382. struct sctp_stream_value params;
  6383. struct sctp_association *asoc;
  6384. int retval = -EFAULT;
  6385. if (len < sizeof(params)) {
  6386. retval = -EINVAL;
  6387. goto out;
  6388. }
  6389. len = sizeof(params);
  6390. if (copy_from_user(&params, optval, len))
  6391. goto out;
  6392. asoc = sctp_id2assoc(sk, params.assoc_id);
  6393. if (!asoc) {
  6394. retval = -EINVAL;
  6395. goto out;
  6396. }
  6397. retval = sctp_sched_get_value(asoc, params.stream_id,
  6398. &params.stream_value);
  6399. if (retval)
  6400. goto out;
  6401. if (put_user(len, optlen)) {
  6402. retval = -EFAULT;
  6403. goto out;
  6404. }
  6405. if (copy_to_user(optval, &params, len)) {
  6406. retval = -EFAULT;
  6407. goto out;
  6408. }
  6409. out:
  6410. return retval;
  6411. }
  6412. static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
  6413. char __user *optval,
  6414. int __user *optlen)
  6415. {
  6416. struct sctp_assoc_value params;
  6417. struct sctp_association *asoc;
  6418. int retval = -EFAULT;
  6419. if (len < sizeof(params)) {
  6420. retval = -EINVAL;
  6421. goto out;
  6422. }
  6423. len = sizeof(params);
  6424. if (copy_from_user(&params, optval, len))
  6425. goto out;
  6426. asoc = sctp_id2assoc(sk, params.assoc_id);
  6427. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6428. sctp_style(sk, UDP)) {
  6429. retval = -EINVAL;
  6430. goto out;
  6431. }
  6432. params.assoc_value = asoc ? asoc->peer.intl_capable
  6433. : sctp_sk(sk)->ep->intl_enable;
  6434. if (put_user(len, optlen))
  6435. goto out;
  6436. if (copy_to_user(optval, &params, len))
  6437. goto out;
  6438. retval = 0;
  6439. out:
  6440. return retval;
  6441. }
  6442. static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
  6443. char __user *optval,
  6444. int __user *optlen)
  6445. {
  6446. int val;
  6447. if (len < sizeof(int))
  6448. return -EINVAL;
  6449. len = sizeof(int);
  6450. val = sctp_sk(sk)->reuse;
  6451. if (put_user(len, optlen))
  6452. return -EFAULT;
  6453. if (copy_to_user(optval, &val, len))
  6454. return -EFAULT;
  6455. return 0;
  6456. }
  6457. static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
  6458. int __user *optlen)
  6459. {
  6460. struct sctp_association *asoc;
  6461. struct sctp_event param;
  6462. __u16 subscribe;
  6463. if (len < sizeof(param))
  6464. return -EINVAL;
  6465. len = sizeof(param);
  6466. if (copy_from_user(&param, optval, len))
  6467. return -EFAULT;
  6468. if (param.se_type < SCTP_SN_TYPE_BASE ||
  6469. param.se_type > SCTP_SN_TYPE_MAX)
  6470. return -EINVAL;
  6471. asoc = sctp_id2assoc(sk, param.se_assoc_id);
  6472. if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
  6473. sctp_style(sk, UDP))
  6474. return -EINVAL;
  6475. subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
  6476. param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
  6477. if (put_user(len, optlen))
  6478. return -EFAULT;
  6479. if (copy_to_user(optval, &param, len))
  6480. return -EFAULT;
  6481. return 0;
  6482. }
  6483. static int sctp_getsockopt_asconf_supported(struct sock *sk, int len,
  6484. char __user *optval,
  6485. int __user *optlen)
  6486. {
  6487. struct sctp_assoc_value params;
  6488. struct sctp_association *asoc;
  6489. int retval = -EFAULT;
  6490. if (len < sizeof(params)) {
  6491. retval = -EINVAL;
  6492. goto out;
  6493. }
  6494. len = sizeof(params);
  6495. if (copy_from_user(&params, optval, len))
  6496. goto out;
  6497. asoc = sctp_id2assoc(sk, params.assoc_id);
  6498. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6499. sctp_style(sk, UDP)) {
  6500. retval = -EINVAL;
  6501. goto out;
  6502. }
  6503. params.assoc_value = asoc ? asoc->peer.asconf_capable
  6504. : sctp_sk(sk)->ep->asconf_enable;
  6505. if (put_user(len, optlen))
  6506. goto out;
  6507. if (copy_to_user(optval, &params, len))
  6508. goto out;
  6509. retval = 0;
  6510. out:
  6511. return retval;
  6512. }
  6513. static int sctp_getsockopt_auth_supported(struct sock *sk, int len,
  6514. char __user *optval,
  6515. int __user *optlen)
  6516. {
  6517. struct sctp_assoc_value params;
  6518. struct sctp_association *asoc;
  6519. int retval = -EFAULT;
  6520. if (len < sizeof(params)) {
  6521. retval = -EINVAL;
  6522. goto out;
  6523. }
  6524. len = sizeof(params);
  6525. if (copy_from_user(&params, optval, len))
  6526. goto out;
  6527. asoc = sctp_id2assoc(sk, params.assoc_id);
  6528. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6529. sctp_style(sk, UDP)) {
  6530. retval = -EINVAL;
  6531. goto out;
  6532. }
  6533. params.assoc_value = asoc ? asoc->peer.auth_capable
  6534. : sctp_sk(sk)->ep->auth_enable;
  6535. if (put_user(len, optlen))
  6536. goto out;
  6537. if (copy_to_user(optval, &params, len))
  6538. goto out;
  6539. retval = 0;
  6540. out:
  6541. return retval;
  6542. }
  6543. static int sctp_getsockopt_ecn_supported(struct sock *sk, int len,
  6544. char __user *optval,
  6545. int __user *optlen)
  6546. {
  6547. struct sctp_assoc_value params;
  6548. struct sctp_association *asoc;
  6549. int retval = -EFAULT;
  6550. if (len < sizeof(params)) {
  6551. retval = -EINVAL;
  6552. goto out;
  6553. }
  6554. len = sizeof(params);
  6555. if (copy_from_user(&params, optval, len))
  6556. goto out;
  6557. asoc = sctp_id2assoc(sk, params.assoc_id);
  6558. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6559. sctp_style(sk, UDP)) {
  6560. retval = -EINVAL;
  6561. goto out;
  6562. }
  6563. params.assoc_value = asoc ? asoc->peer.ecn_capable
  6564. : sctp_sk(sk)->ep->ecn_enable;
  6565. if (put_user(len, optlen))
  6566. goto out;
  6567. if (copy_to_user(optval, &params, len))
  6568. goto out;
  6569. retval = 0;
  6570. out:
  6571. return retval;
  6572. }
  6573. static int sctp_getsockopt_pf_expose(struct sock *sk, int len,
  6574. char __user *optval,
  6575. int __user *optlen)
  6576. {
  6577. struct sctp_assoc_value params;
  6578. struct sctp_association *asoc;
  6579. int retval = -EFAULT;
  6580. if (len < sizeof(params)) {
  6581. retval = -EINVAL;
  6582. goto out;
  6583. }
  6584. len = sizeof(params);
  6585. if (copy_from_user(&params, optval, len))
  6586. goto out;
  6587. asoc = sctp_id2assoc(sk, params.assoc_id);
  6588. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6589. sctp_style(sk, UDP)) {
  6590. retval = -EINVAL;
  6591. goto out;
  6592. }
  6593. params.assoc_value = asoc ? asoc->pf_expose
  6594. : sctp_sk(sk)->pf_expose;
  6595. if (put_user(len, optlen))
  6596. goto out;
  6597. if (copy_to_user(optval, &params, len))
  6598. goto out;
  6599. retval = 0;
  6600. out:
  6601. return retval;
  6602. }
  6603. static int sctp_getsockopt(struct sock *sk, int level, int optname,
  6604. char __user *optval, int __user *optlen)
  6605. {
  6606. int retval = 0;
  6607. int len;
  6608. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  6609. /* I can hardly begin to describe how wrong this is. This is
  6610. * so broken as to be worse than useless. The API draft
  6611. * REALLY is NOT helpful here... I am not convinced that the
  6612. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  6613. * are at all well-founded.
  6614. */
  6615. if (level != SOL_SCTP) {
  6616. struct sctp_af *af = sctp_sk(sk)->pf->af;
  6617. retval = af->getsockopt(sk, level, optname, optval, optlen);
  6618. return retval;
  6619. }
  6620. if (get_user(len, optlen))
  6621. return -EFAULT;
  6622. if (len < 0)
  6623. return -EINVAL;
  6624. lock_sock(sk);
  6625. switch (optname) {
  6626. case SCTP_STATUS:
  6627. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  6628. break;
  6629. case SCTP_DISABLE_FRAGMENTS:
  6630. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  6631. optlen);
  6632. break;
  6633. case SCTP_EVENTS:
  6634. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  6635. break;
  6636. case SCTP_AUTOCLOSE:
  6637. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  6638. break;
  6639. case SCTP_SOCKOPT_PEELOFF:
  6640. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  6641. break;
  6642. case SCTP_SOCKOPT_PEELOFF_FLAGS:
  6643. retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
  6644. break;
  6645. case SCTP_PEER_ADDR_PARAMS:
  6646. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  6647. optlen);
  6648. break;
  6649. case SCTP_DELAYED_SACK:
  6650. retval = sctp_getsockopt_delayed_ack(sk, len, optval,
  6651. optlen);
  6652. break;
  6653. case SCTP_INITMSG:
  6654. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  6655. break;
  6656. case SCTP_GET_PEER_ADDRS:
  6657. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  6658. optlen);
  6659. break;
  6660. case SCTP_GET_LOCAL_ADDRS:
  6661. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  6662. optlen);
  6663. break;
  6664. case SCTP_SOCKOPT_CONNECTX3:
  6665. retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
  6666. break;
  6667. case SCTP_DEFAULT_SEND_PARAM:
  6668. retval = sctp_getsockopt_default_send_param(sk, len,
  6669. optval, optlen);
  6670. break;
  6671. case SCTP_DEFAULT_SNDINFO:
  6672. retval = sctp_getsockopt_default_sndinfo(sk, len,
  6673. optval, optlen);
  6674. break;
  6675. case SCTP_PRIMARY_ADDR:
  6676. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  6677. break;
  6678. case SCTP_NODELAY:
  6679. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  6680. break;
  6681. case SCTP_RTOINFO:
  6682. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  6683. break;
  6684. case SCTP_ASSOCINFO:
  6685. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  6686. break;
  6687. case SCTP_I_WANT_MAPPED_V4_ADDR:
  6688. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  6689. break;
  6690. case SCTP_MAXSEG:
  6691. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  6692. break;
  6693. case SCTP_GET_PEER_ADDR_INFO:
  6694. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  6695. optlen);
  6696. break;
  6697. case SCTP_ADAPTATION_LAYER:
  6698. retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
  6699. optlen);
  6700. break;
  6701. case SCTP_CONTEXT:
  6702. retval = sctp_getsockopt_context(sk, len, optval, optlen);
  6703. break;
  6704. case SCTP_FRAGMENT_INTERLEAVE:
  6705. retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
  6706. optlen);
  6707. break;
  6708. case SCTP_PARTIAL_DELIVERY_POINT:
  6709. retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
  6710. optlen);
  6711. break;
  6712. case SCTP_MAX_BURST:
  6713. retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
  6714. break;
  6715. case SCTP_AUTH_KEY:
  6716. case SCTP_AUTH_CHUNK:
  6717. case SCTP_AUTH_DELETE_KEY:
  6718. case SCTP_AUTH_DEACTIVATE_KEY:
  6719. retval = -EOPNOTSUPP;
  6720. break;
  6721. case SCTP_HMAC_IDENT:
  6722. retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
  6723. break;
  6724. case SCTP_AUTH_ACTIVE_KEY:
  6725. retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
  6726. break;
  6727. case SCTP_PEER_AUTH_CHUNKS:
  6728. retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
  6729. optlen);
  6730. break;
  6731. case SCTP_LOCAL_AUTH_CHUNKS:
  6732. retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
  6733. optlen);
  6734. break;
  6735. case SCTP_GET_ASSOC_NUMBER:
  6736. retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
  6737. break;
  6738. case SCTP_GET_ASSOC_ID_LIST:
  6739. retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
  6740. break;
  6741. case SCTP_AUTO_ASCONF:
  6742. retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
  6743. break;
  6744. case SCTP_PEER_ADDR_THLDS:
  6745. retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
  6746. optlen, false);
  6747. break;
  6748. case SCTP_PEER_ADDR_THLDS_V2:
  6749. retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
  6750. optlen, true);
  6751. break;
  6752. case SCTP_GET_ASSOC_STATS:
  6753. retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
  6754. break;
  6755. case SCTP_RECVRCVINFO:
  6756. retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
  6757. break;
  6758. case SCTP_RECVNXTINFO:
  6759. retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
  6760. break;
  6761. case SCTP_PR_SUPPORTED:
  6762. retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
  6763. break;
  6764. case SCTP_DEFAULT_PRINFO:
  6765. retval = sctp_getsockopt_default_prinfo(sk, len, optval,
  6766. optlen);
  6767. break;
  6768. case SCTP_PR_ASSOC_STATUS:
  6769. retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
  6770. optlen);
  6771. break;
  6772. case SCTP_PR_STREAM_STATUS:
  6773. retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
  6774. optlen);
  6775. break;
  6776. case SCTP_RECONFIG_SUPPORTED:
  6777. retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
  6778. optlen);
  6779. break;
  6780. case SCTP_ENABLE_STREAM_RESET:
  6781. retval = sctp_getsockopt_enable_strreset(sk, len, optval,
  6782. optlen);
  6783. break;
  6784. case SCTP_STREAM_SCHEDULER:
  6785. retval = sctp_getsockopt_scheduler(sk, len, optval,
  6786. optlen);
  6787. break;
  6788. case SCTP_STREAM_SCHEDULER_VALUE:
  6789. retval = sctp_getsockopt_scheduler_value(sk, len, optval,
  6790. optlen);
  6791. break;
  6792. case SCTP_INTERLEAVING_SUPPORTED:
  6793. retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
  6794. optlen);
  6795. break;
  6796. case SCTP_REUSE_PORT:
  6797. retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
  6798. break;
  6799. case SCTP_EVENT:
  6800. retval = sctp_getsockopt_event(sk, len, optval, optlen);
  6801. break;
  6802. case SCTP_ASCONF_SUPPORTED:
  6803. retval = sctp_getsockopt_asconf_supported(sk, len, optval,
  6804. optlen);
  6805. break;
  6806. case SCTP_AUTH_SUPPORTED:
  6807. retval = sctp_getsockopt_auth_supported(sk, len, optval,
  6808. optlen);
  6809. break;
  6810. case SCTP_ECN_SUPPORTED:
  6811. retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen);
  6812. break;
  6813. case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
  6814. retval = sctp_getsockopt_pf_expose(sk, len, optval, optlen);
  6815. break;
  6816. default:
  6817. retval = -ENOPROTOOPT;
  6818. break;
  6819. }
  6820. release_sock(sk);
  6821. return retval;
  6822. }
  6823. static int sctp_hash(struct sock *sk)
  6824. {
  6825. /* STUB */
  6826. return 0;
  6827. }
  6828. static void sctp_unhash(struct sock *sk)
  6829. {
  6830. /* STUB */
  6831. }
  6832. /* Check if port is acceptable. Possibly find first available port.
  6833. *
  6834. * The port hash table (contained in the 'global' SCTP protocol storage
  6835. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  6836. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  6837. * list (the list number is the port number hashed out, so as you
  6838. * would expect from a hash function, all the ports in a given list have
  6839. * such a number that hashes out to the same list number; you were
  6840. * expecting that, right?); so each list has a set of ports, with a
  6841. * link to the socket (struct sock) that uses it, the port number and
  6842. * a fastreuse flag (FIXME: NPI ipg).
  6843. */
  6844. static struct sctp_bind_bucket *sctp_bucket_create(
  6845. struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
  6846. static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  6847. {
  6848. struct sctp_sock *sp = sctp_sk(sk);
  6849. bool reuse = (sk->sk_reuse || sp->reuse);
  6850. struct sctp_bind_hashbucket *head; /* hash list */
  6851. struct net *net = sock_net(sk);
  6852. kuid_t uid = sock_i_uid(sk);
  6853. struct sctp_bind_bucket *pp;
  6854. unsigned short snum;
  6855. int ret;
  6856. snum = ntohs(addr->v4.sin_port);
  6857. pr_debug("%s: begins, snum:%d\n", __func__, snum);
  6858. if (snum == 0) {
  6859. /* Search for an available port. */
  6860. int low, high, remaining, index;
  6861. unsigned int rover;
  6862. inet_get_local_port_range(net, &low, &high);
  6863. remaining = (high - low) + 1;
  6864. rover = prandom_u32() % remaining + low;
  6865. do {
  6866. rover++;
  6867. if ((rover < low) || (rover > high))
  6868. rover = low;
  6869. if (inet_is_local_reserved_port(net, rover))
  6870. continue;
  6871. index = sctp_phashfn(net, rover);
  6872. head = &sctp_port_hashtable[index];
  6873. spin_lock_bh(&head->lock);
  6874. sctp_for_each_hentry(pp, &head->chain)
  6875. if ((pp->port == rover) &&
  6876. net_eq(net, pp->net))
  6877. goto next;
  6878. break;
  6879. next:
  6880. spin_unlock_bh(&head->lock);
  6881. cond_resched();
  6882. } while (--remaining > 0);
  6883. /* Exhausted local port range during search? */
  6884. ret = 1;
  6885. if (remaining <= 0)
  6886. return ret;
  6887. /* OK, here is the one we will use. HEAD (the port
  6888. * hash table list entry) is non-NULL and we hold it's
  6889. * mutex.
  6890. */
  6891. snum = rover;
  6892. } else {
  6893. /* We are given an specific port number; we verify
  6894. * that it is not being used. If it is used, we will
  6895. * exahust the search in the hash list corresponding
  6896. * to the port number (snum) - we detect that with the
  6897. * port iterator, pp being NULL.
  6898. */
  6899. head = &sctp_port_hashtable[sctp_phashfn(net, snum)];
  6900. spin_lock_bh(&head->lock);
  6901. sctp_for_each_hentry(pp, &head->chain) {
  6902. if ((pp->port == snum) && net_eq(pp->net, net))
  6903. goto pp_found;
  6904. }
  6905. }
  6906. pp = NULL;
  6907. goto pp_not_found;
  6908. pp_found:
  6909. if (!hlist_empty(&pp->owner)) {
  6910. /* We had a port hash table hit - there is an
  6911. * available port (pp != NULL) and it is being
  6912. * used by other socket (pp->owner not empty); that other
  6913. * socket is going to be sk2.
  6914. */
  6915. struct sock *sk2;
  6916. pr_debug("%s: found a possible match\n", __func__);
  6917. if ((pp->fastreuse && reuse &&
  6918. sk->sk_state != SCTP_SS_LISTENING) ||
  6919. (pp->fastreuseport && sk->sk_reuseport &&
  6920. uid_eq(pp->fastuid, uid)))
  6921. goto success;
  6922. /* Run through the list of sockets bound to the port
  6923. * (pp->port) [via the pointers bind_next and
  6924. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  6925. * we get the endpoint they describe and run through
  6926. * the endpoint's list of IP (v4 or v6) addresses,
  6927. * comparing each of the addresses with the address of
  6928. * the socket sk. If we find a match, then that means
  6929. * that this port/socket (sk) combination are already
  6930. * in an endpoint.
  6931. */
  6932. sk_for_each_bound(sk2, &pp->owner) {
  6933. struct sctp_sock *sp2 = sctp_sk(sk2);
  6934. struct sctp_endpoint *ep2 = sp2->ep;
  6935. if (sk == sk2 ||
  6936. (reuse && (sk2->sk_reuse || sp2->reuse) &&
  6937. sk2->sk_state != SCTP_SS_LISTENING) ||
  6938. (sk->sk_reuseport && sk2->sk_reuseport &&
  6939. uid_eq(uid, sock_i_uid(sk2))))
  6940. continue;
  6941. if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
  6942. addr, sp2, sp)) {
  6943. ret = 1;
  6944. goto fail_unlock;
  6945. }
  6946. }
  6947. pr_debug("%s: found a match\n", __func__);
  6948. }
  6949. pp_not_found:
  6950. /* If there was a hash table miss, create a new port. */
  6951. ret = 1;
  6952. if (!pp && !(pp = sctp_bucket_create(head, net, snum)))
  6953. goto fail_unlock;
  6954. /* In either case (hit or miss), make sure fastreuse is 1 only
  6955. * if sk->sk_reuse is too (that is, if the caller requested
  6956. * SO_REUSEADDR on this socket -sk-).
  6957. */
  6958. if (hlist_empty(&pp->owner)) {
  6959. if (reuse && sk->sk_state != SCTP_SS_LISTENING)
  6960. pp->fastreuse = 1;
  6961. else
  6962. pp->fastreuse = 0;
  6963. if (sk->sk_reuseport) {
  6964. pp->fastreuseport = 1;
  6965. pp->fastuid = uid;
  6966. } else {
  6967. pp->fastreuseport = 0;
  6968. }
  6969. } else {
  6970. if (pp->fastreuse &&
  6971. (!reuse || sk->sk_state == SCTP_SS_LISTENING))
  6972. pp->fastreuse = 0;
  6973. if (pp->fastreuseport &&
  6974. (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
  6975. pp->fastreuseport = 0;
  6976. }
  6977. /* We are set, so fill up all the data in the hash table
  6978. * entry, tie the socket list information with the rest of the
  6979. * sockets FIXME: Blurry, NPI (ipg).
  6980. */
  6981. success:
  6982. if (!sp->bind_hash) {
  6983. inet_sk(sk)->inet_num = snum;
  6984. sk_add_bind_node(sk, &pp->owner);
  6985. sp->bind_hash = pp;
  6986. }
  6987. ret = 0;
  6988. fail_unlock:
  6989. spin_unlock_bh(&head->lock);
  6990. return ret;
  6991. }
  6992. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  6993. * port is requested.
  6994. */
  6995. static int sctp_get_port(struct sock *sk, unsigned short snum)
  6996. {
  6997. union sctp_addr addr;
  6998. struct sctp_af *af = sctp_sk(sk)->pf->af;
  6999. /* Set up a dummy address struct from the sk. */
  7000. af->from_sk(&addr, sk);
  7001. addr.v4.sin_port = htons(snum);
  7002. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  7003. return sctp_get_port_local(sk, &addr);
  7004. }
  7005. /*
  7006. * Move a socket to LISTENING state.
  7007. */
  7008. static int sctp_listen_start(struct sock *sk, int backlog)
  7009. {
  7010. struct sctp_sock *sp = sctp_sk(sk);
  7011. struct sctp_endpoint *ep = sp->ep;
  7012. struct crypto_shash *tfm = NULL;
  7013. char alg[32];
  7014. /* Allocate HMAC for generating cookie. */
  7015. if (!sp->hmac && sp->sctp_hmac_alg) {
  7016. sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
  7017. tfm = crypto_alloc_shash(alg, 0, 0);
  7018. if (IS_ERR(tfm)) {
  7019. net_info_ratelimited("failed to load transform for %s: %ld\n",
  7020. sp->sctp_hmac_alg, PTR_ERR(tfm));
  7021. return -ENOSYS;
  7022. }
  7023. sctp_sk(sk)->hmac = tfm;
  7024. }
  7025. /*
  7026. * If a bind() or sctp_bindx() is not called prior to a listen()
  7027. * call that allows new associations to be accepted, the system
  7028. * picks an ephemeral port and will choose an address set equivalent
  7029. * to binding with a wildcard address.
  7030. *
  7031. * This is not currently spelled out in the SCTP sockets
  7032. * extensions draft, but follows the practice as seen in TCP
  7033. * sockets.
  7034. *
  7035. */
  7036. inet_sk_set_state(sk, SCTP_SS_LISTENING);
  7037. if (!ep->base.bind_addr.port) {
  7038. if (sctp_autobind(sk))
  7039. return -EAGAIN;
  7040. } else {
  7041. if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
  7042. inet_sk_set_state(sk, SCTP_SS_CLOSED);
  7043. return -EADDRINUSE;
  7044. }
  7045. }
  7046. WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
  7047. return sctp_hash_endpoint(ep);
  7048. }
  7049. /*
  7050. * 4.1.3 / 5.1.3 listen()
  7051. *
  7052. * By default, new associations are not accepted for UDP style sockets.
  7053. * An application uses listen() to mark a socket as being able to
  7054. * accept new associations.
  7055. *
  7056. * On TCP style sockets, applications use listen() to ready the SCTP
  7057. * endpoint for accepting inbound associations.
  7058. *
  7059. * On both types of endpoints a backlog of '0' disables listening.
  7060. *
  7061. * Move a socket to LISTENING state.
  7062. */
  7063. int sctp_inet_listen(struct socket *sock, int backlog)
  7064. {
  7065. struct sock *sk = sock->sk;
  7066. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  7067. int err = -EINVAL;
  7068. if (unlikely(backlog < 0))
  7069. return err;
  7070. lock_sock(sk);
  7071. /* Peeled-off sockets are not allowed to listen(). */
  7072. if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
  7073. goto out;
  7074. if (sock->state != SS_UNCONNECTED)
  7075. goto out;
  7076. if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
  7077. goto out;
  7078. /* If backlog is zero, disable listening. */
  7079. if (!backlog) {
  7080. if (sctp_sstate(sk, CLOSED))
  7081. goto out;
  7082. err = 0;
  7083. sctp_unhash_endpoint(ep);
  7084. sk->sk_state = SCTP_SS_CLOSED;
  7085. if (sk->sk_reuse || sctp_sk(sk)->reuse)
  7086. sctp_sk(sk)->bind_hash->fastreuse = 1;
  7087. goto out;
  7088. }
  7089. /* If we are already listening, just update the backlog */
  7090. if (sctp_sstate(sk, LISTENING))
  7091. WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
  7092. else {
  7093. err = sctp_listen_start(sk, backlog);
  7094. if (err)
  7095. goto out;
  7096. }
  7097. err = 0;
  7098. out:
  7099. release_sock(sk);
  7100. return err;
  7101. }
  7102. /*
  7103. * This function is done by modeling the current datagram_poll() and the
  7104. * tcp_poll(). Note that, based on these implementations, we don't
  7105. * lock the socket in this function, even though it seems that,
  7106. * ideally, locking or some other mechanisms can be used to ensure
  7107. * the integrity of the counters (sndbuf and wmem_alloc) used
  7108. * in this place. We assume that we don't need locks either until proven
  7109. * otherwise.
  7110. *
  7111. * Another thing to note is that we include the Async I/O support
  7112. * here, again, by modeling the current TCP/UDP code. We don't have
  7113. * a good way to test with it yet.
  7114. */
  7115. __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  7116. {
  7117. struct sock *sk = sock->sk;
  7118. struct sctp_sock *sp = sctp_sk(sk);
  7119. __poll_t mask;
  7120. poll_wait(file, sk_sleep(sk), wait);
  7121. sock_rps_record_flow(sk);
  7122. /* A TCP-style listening socket becomes readable when the accept queue
  7123. * is not empty.
  7124. */
  7125. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  7126. return (!list_empty(&sp->ep->asocs)) ?
  7127. (EPOLLIN | EPOLLRDNORM) : 0;
  7128. mask = 0;
  7129. /* Is there any exceptional events? */
  7130. if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
  7131. mask |= EPOLLERR |
  7132. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
  7133. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7134. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  7135. if (sk->sk_shutdown == SHUTDOWN_MASK)
  7136. mask |= EPOLLHUP;
  7137. /* Is it readable? Reconsider this code with TCP-style support. */
  7138. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  7139. mask |= EPOLLIN | EPOLLRDNORM;
  7140. /* The association is either gone or not ready. */
  7141. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  7142. return mask;
  7143. /* Is it writable? */
  7144. if (sctp_writeable(sk)) {
  7145. mask |= EPOLLOUT | EPOLLWRNORM;
  7146. } else {
  7147. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  7148. /*
  7149. * Since the socket is not locked, the buffer
  7150. * might be made available after the writeable check and
  7151. * before the bit is set. This could cause a lost I/O
  7152. * signal. tcp_poll() has a race breaker for this race
  7153. * condition. Based on their implementation, we put
  7154. * in the following code to cover it as well.
  7155. */
  7156. if (sctp_writeable(sk))
  7157. mask |= EPOLLOUT | EPOLLWRNORM;
  7158. }
  7159. return mask;
  7160. }
  7161. /********************************************************************
  7162. * 2nd Level Abstractions
  7163. ********************************************************************/
  7164. static struct sctp_bind_bucket *sctp_bucket_create(
  7165. struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
  7166. {
  7167. struct sctp_bind_bucket *pp;
  7168. pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
  7169. if (pp) {
  7170. SCTP_DBG_OBJCNT_INC(bind_bucket);
  7171. pp->port = snum;
  7172. pp->fastreuse = 0;
  7173. INIT_HLIST_HEAD(&pp->owner);
  7174. pp->net = net;
  7175. hlist_add_head(&pp->node, &head->chain);
  7176. }
  7177. return pp;
  7178. }
  7179. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  7180. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  7181. {
  7182. if (pp && hlist_empty(&pp->owner)) {
  7183. __hlist_del(&pp->node);
  7184. kmem_cache_free(sctp_bucket_cachep, pp);
  7185. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  7186. }
  7187. }
  7188. /* Release this socket's reference to a local port. */
  7189. static inline void __sctp_put_port(struct sock *sk)
  7190. {
  7191. struct sctp_bind_hashbucket *head =
  7192. &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
  7193. inet_sk(sk)->inet_num)];
  7194. struct sctp_bind_bucket *pp;
  7195. spin_lock(&head->lock);
  7196. pp = sctp_sk(sk)->bind_hash;
  7197. __sk_del_bind_node(sk);
  7198. sctp_sk(sk)->bind_hash = NULL;
  7199. inet_sk(sk)->inet_num = 0;
  7200. sctp_bucket_destroy(pp);
  7201. spin_unlock(&head->lock);
  7202. }
  7203. void sctp_put_port(struct sock *sk)
  7204. {
  7205. local_bh_disable();
  7206. __sctp_put_port(sk);
  7207. local_bh_enable();
  7208. }
  7209. /*
  7210. * The system picks an ephemeral port and choose an address set equivalent
  7211. * to binding with a wildcard address.
  7212. * One of those addresses will be the primary address for the association.
  7213. * This automatically enables the multihoming capability of SCTP.
  7214. */
  7215. static int sctp_autobind(struct sock *sk)
  7216. {
  7217. union sctp_addr autoaddr;
  7218. struct sctp_af *af;
  7219. __be16 port;
  7220. /* Initialize a local sockaddr structure to INADDR_ANY. */
  7221. af = sctp_sk(sk)->pf->af;
  7222. port = htons(inet_sk(sk)->inet_num);
  7223. af->inaddr_any(&autoaddr, port);
  7224. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  7225. }
  7226. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  7227. *
  7228. * From RFC 2292
  7229. * 4.2 The cmsghdr Structure *
  7230. *
  7231. * When ancillary data is sent or received, any number of ancillary data
  7232. * objects can be specified by the msg_control and msg_controllen members of
  7233. * the msghdr structure, because each object is preceded by
  7234. * a cmsghdr structure defining the object's length (the cmsg_len member).
  7235. * Historically Berkeley-derived implementations have passed only one object
  7236. * at a time, but this API allows multiple objects to be
  7237. * passed in a single call to sendmsg() or recvmsg(). The following example
  7238. * shows two ancillary data objects in a control buffer.
  7239. *
  7240. * |<--------------------------- msg_controllen -------------------------->|
  7241. * | |
  7242. *
  7243. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  7244. *
  7245. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  7246. * | | |
  7247. *
  7248. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  7249. *
  7250. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  7251. * | | | | |
  7252. *
  7253. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  7254. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  7255. *
  7256. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  7257. *
  7258. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  7259. * ^
  7260. * |
  7261. *
  7262. * msg_control
  7263. * points here
  7264. */
  7265. static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
  7266. {
  7267. struct msghdr *my_msg = (struct msghdr *)msg;
  7268. struct cmsghdr *cmsg;
  7269. for_each_cmsghdr(cmsg, my_msg) {
  7270. if (!CMSG_OK(my_msg, cmsg))
  7271. return -EINVAL;
  7272. /* Should we parse this header or ignore? */
  7273. if (cmsg->cmsg_level != IPPROTO_SCTP)
  7274. continue;
  7275. /* Strictly check lengths following example in SCM code. */
  7276. switch (cmsg->cmsg_type) {
  7277. case SCTP_INIT:
  7278. /* SCTP Socket API Extension
  7279. * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
  7280. *
  7281. * This cmsghdr structure provides information for
  7282. * initializing new SCTP associations with sendmsg().
  7283. * The SCTP_INITMSG socket option uses this same data
  7284. * structure. This structure is not used for
  7285. * recvmsg().
  7286. *
  7287. * cmsg_level cmsg_type cmsg_data[]
  7288. * ------------ ------------ ----------------------
  7289. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  7290. */
  7291. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
  7292. return -EINVAL;
  7293. cmsgs->init = CMSG_DATA(cmsg);
  7294. break;
  7295. case SCTP_SNDRCV:
  7296. /* SCTP Socket API Extension
  7297. * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
  7298. *
  7299. * This cmsghdr structure specifies SCTP options for
  7300. * sendmsg() and describes SCTP header information
  7301. * about a received message through recvmsg().
  7302. *
  7303. * cmsg_level cmsg_type cmsg_data[]
  7304. * ------------ ------------ ----------------------
  7305. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  7306. */
  7307. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  7308. return -EINVAL;
  7309. cmsgs->srinfo = CMSG_DATA(cmsg);
  7310. if (cmsgs->srinfo->sinfo_flags &
  7311. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  7312. SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
  7313. SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
  7314. return -EINVAL;
  7315. break;
  7316. case SCTP_SNDINFO:
  7317. /* SCTP Socket API Extension
  7318. * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
  7319. *
  7320. * This cmsghdr structure specifies SCTP options for
  7321. * sendmsg(). This structure and SCTP_RCVINFO replaces
  7322. * SCTP_SNDRCV which has been deprecated.
  7323. *
  7324. * cmsg_level cmsg_type cmsg_data[]
  7325. * ------------ ------------ ---------------------
  7326. * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
  7327. */
  7328. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
  7329. return -EINVAL;
  7330. cmsgs->sinfo = CMSG_DATA(cmsg);
  7331. if (cmsgs->sinfo->snd_flags &
  7332. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  7333. SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
  7334. SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
  7335. return -EINVAL;
  7336. break;
  7337. case SCTP_PRINFO:
  7338. /* SCTP Socket API Extension
  7339. * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
  7340. *
  7341. * This cmsghdr structure specifies SCTP options for sendmsg().
  7342. *
  7343. * cmsg_level cmsg_type cmsg_data[]
  7344. * ------------ ------------ ---------------------
  7345. * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
  7346. */
  7347. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
  7348. return -EINVAL;
  7349. cmsgs->prinfo = CMSG_DATA(cmsg);
  7350. if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
  7351. return -EINVAL;
  7352. if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
  7353. cmsgs->prinfo->pr_value = 0;
  7354. break;
  7355. case SCTP_AUTHINFO:
  7356. /* SCTP Socket API Extension
  7357. * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
  7358. *
  7359. * This cmsghdr structure specifies SCTP options for sendmsg().
  7360. *
  7361. * cmsg_level cmsg_type cmsg_data[]
  7362. * ------------ ------------ ---------------------
  7363. * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
  7364. */
  7365. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
  7366. return -EINVAL;
  7367. cmsgs->authinfo = CMSG_DATA(cmsg);
  7368. break;
  7369. case SCTP_DSTADDRV4:
  7370. case SCTP_DSTADDRV6:
  7371. /* SCTP Socket API Extension
  7372. * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
  7373. *
  7374. * This cmsghdr structure specifies SCTP options for sendmsg().
  7375. *
  7376. * cmsg_level cmsg_type cmsg_data[]
  7377. * ------------ ------------ ---------------------
  7378. * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
  7379. * ------------ ------------ ---------------------
  7380. * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
  7381. */
  7382. cmsgs->addrs_msg = my_msg;
  7383. break;
  7384. default:
  7385. return -EINVAL;
  7386. }
  7387. }
  7388. return 0;
  7389. }
  7390. /*
  7391. * Wait for a packet..
  7392. * Note: This function is the same function as in core/datagram.c
  7393. * with a few modifications to make lksctp work.
  7394. */
  7395. static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
  7396. {
  7397. int error;
  7398. DEFINE_WAIT(wait);
  7399. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  7400. /* Socket errors? */
  7401. error = sock_error(sk);
  7402. if (error)
  7403. goto out;
  7404. if (!skb_queue_empty(&sk->sk_receive_queue))
  7405. goto ready;
  7406. /* Socket shut down? */
  7407. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7408. goto out;
  7409. /* Sequenced packets can come disconnected. If so we report the
  7410. * problem.
  7411. */
  7412. error = -ENOTCONN;
  7413. /* Is there a good reason to think that we may receive some data? */
  7414. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  7415. goto out;
  7416. /* Handle signals. */
  7417. if (signal_pending(current))
  7418. goto interrupted;
  7419. /* Let another process have a go. Since we are going to sleep
  7420. * anyway. Note: This may cause odd behaviors if the message
  7421. * does not fit in the user's buffer, but this seems to be the
  7422. * only way to honor MSG_DONTWAIT realistically.
  7423. */
  7424. release_sock(sk);
  7425. *timeo_p = schedule_timeout(*timeo_p);
  7426. lock_sock(sk);
  7427. ready:
  7428. finish_wait(sk_sleep(sk), &wait);
  7429. return 0;
  7430. interrupted:
  7431. error = sock_intr_errno(*timeo_p);
  7432. out:
  7433. finish_wait(sk_sleep(sk), &wait);
  7434. *err = error;
  7435. return error;
  7436. }
  7437. /* Receive a datagram.
  7438. * Note: This is pretty much the same routine as in core/datagram.c
  7439. * with a few changes to make lksctp work.
  7440. */
  7441. struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  7442. int noblock, int *err)
  7443. {
  7444. int error;
  7445. struct sk_buff *skb;
  7446. long timeo;
  7447. timeo = sock_rcvtimeo(sk, noblock);
  7448. pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
  7449. MAX_SCHEDULE_TIMEOUT);
  7450. do {
  7451. /* Again only user level code calls this function,
  7452. * so nothing interrupt level
  7453. * will suddenly eat the receive_queue.
  7454. *
  7455. * Look at current nfs client by the way...
  7456. * However, this function was correct in any case. 8)
  7457. */
  7458. if (flags & MSG_PEEK) {
  7459. skb = skb_peek(&sk->sk_receive_queue);
  7460. if (skb)
  7461. refcount_inc(&skb->users);
  7462. } else {
  7463. skb = __skb_dequeue(&sk->sk_receive_queue);
  7464. }
  7465. if (skb)
  7466. return skb;
  7467. /* Caller is allowed not to check sk->sk_err before calling. */
  7468. error = sock_error(sk);
  7469. if (error)
  7470. goto no_packet;
  7471. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7472. break;
  7473. if (sk_can_busy_loop(sk)) {
  7474. sk_busy_loop(sk, noblock);
  7475. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  7476. continue;
  7477. }
  7478. /* User doesn't want to wait. */
  7479. error = -EAGAIN;
  7480. if (!timeo)
  7481. goto no_packet;
  7482. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  7483. return NULL;
  7484. no_packet:
  7485. *err = error;
  7486. return NULL;
  7487. }
  7488. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  7489. static void __sctp_write_space(struct sctp_association *asoc)
  7490. {
  7491. struct sock *sk = asoc->base.sk;
  7492. if (sctp_wspace(asoc) <= 0)
  7493. return;
  7494. if (waitqueue_active(&asoc->wait))
  7495. wake_up_interruptible(&asoc->wait);
  7496. if (sctp_writeable(sk)) {
  7497. struct socket_wq *wq;
  7498. rcu_read_lock();
  7499. wq = rcu_dereference(sk->sk_wq);
  7500. if (wq) {
  7501. if (waitqueue_active(&wq->wait))
  7502. wake_up_interruptible(&wq->wait);
  7503. /* Note that we try to include the Async I/O support
  7504. * here by modeling from the current TCP/UDP code.
  7505. * We have not tested with it yet.
  7506. */
  7507. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  7508. sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
  7509. }
  7510. rcu_read_unlock();
  7511. }
  7512. }
  7513. static void sctp_wake_up_waiters(struct sock *sk,
  7514. struct sctp_association *asoc)
  7515. {
  7516. struct sctp_association *tmp = asoc;
  7517. /* We do accounting for the sndbuf space per association,
  7518. * so we only need to wake our own association.
  7519. */
  7520. if (asoc->ep->sndbuf_policy)
  7521. return __sctp_write_space(asoc);
  7522. /* If association goes down and is just flushing its
  7523. * outq, then just normally notify others.
  7524. */
  7525. if (asoc->base.dead)
  7526. return sctp_write_space(sk);
  7527. /* Accounting for the sndbuf space is per socket, so we
  7528. * need to wake up others, try to be fair and in case of
  7529. * other associations, let them have a go first instead
  7530. * of just doing a sctp_write_space() call.
  7531. *
  7532. * Note that we reach sctp_wake_up_waiters() only when
  7533. * associations free up queued chunks, thus we are under
  7534. * lock and the list of associations on a socket is
  7535. * guaranteed not to change.
  7536. */
  7537. for (tmp = list_next_entry(tmp, asocs); 1;
  7538. tmp = list_next_entry(tmp, asocs)) {
  7539. /* Manually skip the head element. */
  7540. if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
  7541. continue;
  7542. /* Wake up association. */
  7543. __sctp_write_space(tmp);
  7544. /* We've reached the end. */
  7545. if (tmp == asoc)
  7546. break;
  7547. }
  7548. }
  7549. /* Do accounting for the sndbuf space.
  7550. * Decrement the used sndbuf space of the corresponding association by the
  7551. * data size which was just transmitted(freed).
  7552. */
  7553. static void sctp_wfree(struct sk_buff *skb)
  7554. {
  7555. struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
  7556. struct sctp_association *asoc = chunk->asoc;
  7557. struct sock *sk = asoc->base.sk;
  7558. sk_mem_uncharge(sk, skb->truesize);
  7559. sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
  7560. asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
  7561. WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
  7562. &sk->sk_wmem_alloc));
  7563. if (chunk->shkey) {
  7564. struct sctp_shared_key *shkey = chunk->shkey;
  7565. /* refcnt == 2 and !list_empty mean after this release, it's
  7566. * not being used anywhere, and it's time to notify userland
  7567. * that this shkey can be freed if it's been deactivated.
  7568. */
  7569. if (shkey->deactivated && !list_empty(&shkey->key_list) &&
  7570. refcount_read(&shkey->refcnt) == 2) {
  7571. struct sctp_ulpevent *ev;
  7572. ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
  7573. SCTP_AUTH_FREE_KEY,
  7574. GFP_KERNEL);
  7575. if (ev)
  7576. asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
  7577. }
  7578. sctp_auth_shkey_release(chunk->shkey);
  7579. }
  7580. sock_wfree(skb);
  7581. sctp_wake_up_waiters(sk, asoc);
  7582. sctp_association_put(asoc);
  7583. }
  7584. /* Do accounting for the receive space on the socket.
  7585. * Accounting for the association is done in ulpevent.c
  7586. * We set this as a destructor for the cloned data skbs so that
  7587. * accounting is done at the correct time.
  7588. */
  7589. void sctp_sock_rfree(struct sk_buff *skb)
  7590. {
  7591. struct sock *sk = skb->sk;
  7592. struct sctp_ulpevent *event = sctp_skb2event(skb);
  7593. atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
  7594. /*
  7595. * Mimic the behavior of sock_rfree
  7596. */
  7597. sk_mem_uncharge(sk, event->rmem_len);
  7598. }
  7599. /* Helper function to wait for space in the sndbuf. */
  7600. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  7601. size_t msg_len)
  7602. {
  7603. struct sock *sk = asoc->base.sk;
  7604. long current_timeo = *timeo_p;
  7605. DEFINE_WAIT(wait);
  7606. int err = 0;
  7607. pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
  7608. *timeo_p, msg_len);
  7609. /* Increment the association's refcnt. */
  7610. sctp_association_hold(asoc);
  7611. /* Wait on the association specific sndbuf space. */
  7612. for (;;) {
  7613. prepare_to_wait_exclusive(&asoc->wait, &wait,
  7614. TASK_INTERRUPTIBLE);
  7615. if (asoc->base.dead)
  7616. goto do_dead;
  7617. if (!*timeo_p)
  7618. goto do_nonblock;
  7619. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
  7620. goto do_error;
  7621. if (signal_pending(current))
  7622. goto do_interrupted;
  7623. if (sk_under_memory_pressure(sk))
  7624. sk_mem_reclaim(sk);
  7625. if ((int)msg_len <= sctp_wspace(asoc) &&
  7626. sk_wmem_schedule(sk, msg_len))
  7627. break;
  7628. /* Let another process have a go. Since we are going
  7629. * to sleep anyway.
  7630. */
  7631. release_sock(sk);
  7632. current_timeo = schedule_timeout(current_timeo);
  7633. lock_sock(sk);
  7634. if (sk != asoc->base.sk)
  7635. goto do_error;
  7636. *timeo_p = current_timeo;
  7637. }
  7638. out:
  7639. finish_wait(&asoc->wait, &wait);
  7640. /* Release the association's refcnt. */
  7641. sctp_association_put(asoc);
  7642. return err;
  7643. do_dead:
  7644. err = -ESRCH;
  7645. goto out;
  7646. do_error:
  7647. err = -EPIPE;
  7648. goto out;
  7649. do_interrupted:
  7650. err = sock_intr_errno(*timeo_p);
  7651. goto out;
  7652. do_nonblock:
  7653. err = -EAGAIN;
  7654. goto out;
  7655. }
  7656. void sctp_data_ready(struct sock *sk)
  7657. {
  7658. struct socket_wq *wq;
  7659. rcu_read_lock();
  7660. wq = rcu_dereference(sk->sk_wq);
  7661. if (skwq_has_sleeper(wq))
  7662. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  7663. EPOLLRDNORM | EPOLLRDBAND);
  7664. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  7665. rcu_read_unlock();
  7666. }
  7667. /* If socket sndbuf has changed, wake up all per association waiters. */
  7668. void sctp_write_space(struct sock *sk)
  7669. {
  7670. struct sctp_association *asoc;
  7671. /* Wake up the tasks in each wait queue. */
  7672. list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
  7673. __sctp_write_space(asoc);
  7674. }
  7675. }
  7676. /* Is there any sndbuf space available on the socket?
  7677. *
  7678. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  7679. * associations on the same socket. For a UDP-style socket with
  7680. * multiple associations, it is possible for it to be "unwriteable"
  7681. * prematurely. I assume that this is acceptable because
  7682. * a premature "unwriteable" is better than an accidental "writeable" which
  7683. * would cause an unwanted block under certain circumstances. For the 1-1
  7684. * UDP-style sockets or TCP-style sockets, this code should work.
  7685. * - Daisy
  7686. */
  7687. static bool sctp_writeable(struct sock *sk)
  7688. {
  7689. return sk->sk_sndbuf > sk->sk_wmem_queued;
  7690. }
  7691. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  7692. * returns immediately with EINPROGRESS.
  7693. */
  7694. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  7695. {
  7696. struct sock *sk = asoc->base.sk;
  7697. int err = 0;
  7698. long current_timeo = *timeo_p;
  7699. DEFINE_WAIT(wait);
  7700. pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
  7701. /* Increment the association's refcnt. */
  7702. sctp_association_hold(asoc);
  7703. for (;;) {
  7704. prepare_to_wait_exclusive(&asoc->wait, &wait,
  7705. TASK_INTERRUPTIBLE);
  7706. if (!*timeo_p)
  7707. goto do_nonblock;
  7708. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7709. break;
  7710. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  7711. asoc->base.dead)
  7712. goto do_error;
  7713. if (signal_pending(current))
  7714. goto do_interrupted;
  7715. if (sctp_state(asoc, ESTABLISHED))
  7716. break;
  7717. /* Let another process have a go. Since we are going
  7718. * to sleep anyway.
  7719. */
  7720. release_sock(sk);
  7721. current_timeo = schedule_timeout(current_timeo);
  7722. lock_sock(sk);
  7723. *timeo_p = current_timeo;
  7724. }
  7725. out:
  7726. finish_wait(&asoc->wait, &wait);
  7727. /* Release the association's refcnt. */
  7728. sctp_association_put(asoc);
  7729. return err;
  7730. do_error:
  7731. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  7732. err = -ETIMEDOUT;
  7733. else
  7734. err = -ECONNREFUSED;
  7735. goto out;
  7736. do_interrupted:
  7737. err = sock_intr_errno(*timeo_p);
  7738. goto out;
  7739. do_nonblock:
  7740. err = -EINPROGRESS;
  7741. goto out;
  7742. }
  7743. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  7744. {
  7745. struct sctp_endpoint *ep;
  7746. int err = 0;
  7747. DEFINE_WAIT(wait);
  7748. ep = sctp_sk(sk)->ep;
  7749. for (;;) {
  7750. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  7751. TASK_INTERRUPTIBLE);
  7752. if (list_empty(&ep->asocs)) {
  7753. release_sock(sk);
  7754. timeo = schedule_timeout(timeo);
  7755. lock_sock(sk);
  7756. }
  7757. err = -EINVAL;
  7758. if (!sctp_sstate(sk, LISTENING))
  7759. break;
  7760. err = 0;
  7761. if (!list_empty(&ep->asocs))
  7762. break;
  7763. err = sock_intr_errno(timeo);
  7764. if (signal_pending(current))
  7765. break;
  7766. err = -EAGAIN;
  7767. if (!timeo)
  7768. break;
  7769. }
  7770. finish_wait(sk_sleep(sk), &wait);
  7771. return err;
  7772. }
  7773. static void sctp_wait_for_close(struct sock *sk, long timeout)
  7774. {
  7775. DEFINE_WAIT(wait);
  7776. do {
  7777. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  7778. if (list_empty(&sctp_sk(sk)->ep->asocs))
  7779. break;
  7780. release_sock(sk);
  7781. timeout = schedule_timeout(timeout);
  7782. lock_sock(sk);
  7783. } while (!signal_pending(current) && timeout);
  7784. finish_wait(sk_sleep(sk), &wait);
  7785. }
  7786. static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
  7787. {
  7788. struct sk_buff *frag;
  7789. if (!skb->data_len)
  7790. goto done;
  7791. /* Don't forget the fragments. */
  7792. skb_walk_frags(skb, frag)
  7793. sctp_skb_set_owner_r_frag(frag, sk);
  7794. done:
  7795. sctp_skb_set_owner_r(skb, sk);
  7796. }
  7797. void sctp_copy_sock(struct sock *newsk, struct sock *sk,
  7798. struct sctp_association *asoc)
  7799. {
  7800. struct inet_sock *inet = inet_sk(sk);
  7801. struct inet_sock *newinet;
  7802. struct sctp_sock *sp = sctp_sk(sk);
  7803. struct sctp_endpoint *ep = sp->ep;
  7804. newsk->sk_type = sk->sk_type;
  7805. newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
  7806. newsk->sk_flags = sk->sk_flags;
  7807. newsk->sk_tsflags = sk->sk_tsflags;
  7808. newsk->sk_no_check_tx = sk->sk_no_check_tx;
  7809. newsk->sk_no_check_rx = sk->sk_no_check_rx;
  7810. newsk->sk_reuse = sk->sk_reuse;
  7811. sctp_sk(newsk)->reuse = sp->reuse;
  7812. newsk->sk_shutdown = sk->sk_shutdown;
  7813. newsk->sk_destruct = sctp_destruct_sock;
  7814. newsk->sk_family = sk->sk_family;
  7815. newsk->sk_protocol = IPPROTO_SCTP;
  7816. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  7817. newsk->sk_sndbuf = sk->sk_sndbuf;
  7818. newsk->sk_rcvbuf = sk->sk_rcvbuf;
  7819. newsk->sk_lingertime = sk->sk_lingertime;
  7820. newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
  7821. newsk->sk_sndtimeo = sk->sk_sndtimeo;
  7822. newsk->sk_rxhash = sk->sk_rxhash;
  7823. newinet = inet_sk(newsk);
  7824. /* Initialize sk's sport, dport, rcv_saddr and daddr for
  7825. * getsockname() and getpeername()
  7826. */
  7827. newinet->inet_sport = inet->inet_sport;
  7828. newinet->inet_saddr = inet->inet_saddr;
  7829. newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
  7830. newinet->inet_dport = htons(asoc->peer.port);
  7831. newinet->pmtudisc = inet->pmtudisc;
  7832. newinet->inet_id = prandom_u32();
  7833. newinet->uc_ttl = inet->uc_ttl;
  7834. newinet->mc_loop = 1;
  7835. newinet->mc_ttl = 1;
  7836. newinet->mc_index = 0;
  7837. newinet->mc_list = NULL;
  7838. if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
  7839. net_enable_timestamp();
  7840. /* Set newsk security attributes from orginal sk and connection
  7841. * security attribute from ep.
  7842. */
  7843. security_sctp_sk_clone(ep, sk, newsk);
  7844. }
  7845. static inline void sctp_copy_descendant(struct sock *sk_to,
  7846. const struct sock *sk_from)
  7847. {
  7848. size_t ancestor_size = sizeof(struct inet_sock);
  7849. ancestor_size += sk_from->sk_prot->obj_size;
  7850. ancestor_size -= offsetof(struct sctp_sock, pd_lobby);
  7851. __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
  7852. }
  7853. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  7854. * and its messages to the newsk.
  7855. */
  7856. static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  7857. struct sctp_association *assoc,
  7858. enum sctp_socket_type type)
  7859. {
  7860. struct sctp_sock *oldsp = sctp_sk(oldsk);
  7861. struct sctp_sock *newsp = sctp_sk(newsk);
  7862. struct sctp_bind_bucket *pp; /* hash list port iterator */
  7863. struct sctp_endpoint *newep = newsp->ep;
  7864. struct sk_buff *skb, *tmp;
  7865. struct sctp_ulpevent *event;
  7866. struct sctp_bind_hashbucket *head;
  7867. int err;
  7868. /* Migrate socket buffer sizes and all the socket level options to the
  7869. * new socket.
  7870. */
  7871. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  7872. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  7873. /* Brute force copy old sctp opt. */
  7874. sctp_copy_descendant(newsk, oldsk);
  7875. /* Restore the ep value that was overwritten with the above structure
  7876. * copy.
  7877. */
  7878. newsp->ep = newep;
  7879. newsp->hmac = NULL;
  7880. /* Hook this new socket in to the bind_hash list. */
  7881. head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
  7882. inet_sk(oldsk)->inet_num)];
  7883. spin_lock_bh(&head->lock);
  7884. pp = sctp_sk(oldsk)->bind_hash;
  7885. sk_add_bind_node(newsk, &pp->owner);
  7886. sctp_sk(newsk)->bind_hash = pp;
  7887. inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
  7888. spin_unlock_bh(&head->lock);
  7889. /* Copy the bind_addr list from the original endpoint to the new
  7890. * endpoint so that we can handle restarts properly
  7891. */
  7892. err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
  7893. &oldsp->ep->base.bind_addr, GFP_KERNEL);
  7894. if (err)
  7895. return err;
  7896. /* New ep's auth_hmacs should be set if old ep's is set, in case
  7897. * that net->sctp.auth_enable has been changed to 0 by users and
  7898. * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
  7899. */
  7900. if (oldsp->ep->auth_hmacs) {
  7901. err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
  7902. if (err)
  7903. return err;
  7904. }
  7905. sctp_auto_asconf_init(newsp);
  7906. /* Move any messages in the old socket's receive queue that are for the
  7907. * peeled off association to the new socket's receive queue.
  7908. */
  7909. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  7910. event = sctp_skb2event(skb);
  7911. if (event->asoc == assoc) {
  7912. __skb_unlink(skb, &oldsk->sk_receive_queue);
  7913. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  7914. sctp_skb_set_owner_r_frag(skb, newsk);
  7915. }
  7916. }
  7917. /* Clean up any messages pending delivery due to partial
  7918. * delivery. Three cases:
  7919. * 1) No partial deliver; no work.
  7920. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  7921. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  7922. */
  7923. atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
  7924. if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
  7925. struct sk_buff_head *queue;
  7926. /* Decide which queue to move pd_lobby skbs to. */
  7927. if (assoc->ulpq.pd_mode) {
  7928. queue = &newsp->pd_lobby;
  7929. } else
  7930. queue = &newsk->sk_receive_queue;
  7931. /* Walk through the pd_lobby, looking for skbs that
  7932. * need moved to the new socket.
  7933. */
  7934. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  7935. event = sctp_skb2event(skb);
  7936. if (event->asoc == assoc) {
  7937. __skb_unlink(skb, &oldsp->pd_lobby);
  7938. __skb_queue_tail(queue, skb);
  7939. sctp_skb_set_owner_r_frag(skb, newsk);
  7940. }
  7941. }
  7942. /* Clear up any skbs waiting for the partial
  7943. * delivery to finish.
  7944. */
  7945. if (assoc->ulpq.pd_mode)
  7946. sctp_clear_pd(oldsk, NULL);
  7947. }
  7948. sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
  7949. /* Set the type of socket to indicate that it is peeled off from the
  7950. * original UDP-style socket or created with the accept() call on a
  7951. * TCP-style socket..
  7952. */
  7953. newsp->type = type;
  7954. /* Mark the new socket "in-use" by the user so that any packets
  7955. * that may arrive on the association after we've moved it are
  7956. * queued to the backlog. This prevents a potential race between
  7957. * backlog processing on the old socket and new-packet processing
  7958. * on the new socket.
  7959. *
  7960. * The caller has just allocated newsk so we can guarantee that other
  7961. * paths won't try to lock it and then oldsk.
  7962. */
  7963. lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
  7964. sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
  7965. sctp_assoc_migrate(assoc, newsk);
  7966. sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
  7967. /* If the association on the newsk is already closed before accept()
  7968. * is called, set RCV_SHUTDOWN flag.
  7969. */
  7970. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
  7971. inet_sk_set_state(newsk, SCTP_SS_CLOSED);
  7972. newsk->sk_shutdown |= RCV_SHUTDOWN;
  7973. } else {
  7974. inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
  7975. }
  7976. release_sock(newsk);
  7977. return 0;
  7978. }
  7979. /* This proto struct describes the ULP interface for SCTP. */
  7980. struct proto sctp_prot = {
  7981. .name = "SCTP",
  7982. .owner = THIS_MODULE,
  7983. .close = sctp_close,
  7984. .disconnect = sctp_disconnect,
  7985. .accept = sctp_accept,
  7986. .ioctl = sctp_ioctl,
  7987. .init = sctp_init_sock,
  7988. .destroy = sctp_destroy_sock,
  7989. .shutdown = sctp_shutdown,
  7990. .setsockopt = sctp_setsockopt,
  7991. .getsockopt = sctp_getsockopt,
  7992. .sendmsg = sctp_sendmsg,
  7993. .recvmsg = sctp_recvmsg,
  7994. .bind = sctp_bind,
  7995. .bind_add = sctp_bind_add,
  7996. .backlog_rcv = sctp_backlog_rcv,
  7997. .hash = sctp_hash,
  7998. .unhash = sctp_unhash,
  7999. .no_autobind = true,
  8000. .obj_size = sizeof(struct sctp_sock),
  8001. .useroffset = offsetof(struct sctp_sock, subscribe),
  8002. .usersize = offsetof(struct sctp_sock, initmsg) -
  8003. offsetof(struct sctp_sock, subscribe) +
  8004. sizeof_field(struct sctp_sock, initmsg),
  8005. .sysctl_mem = sysctl_sctp_mem,
  8006. .sysctl_rmem = sysctl_sctp_rmem,
  8007. .sysctl_wmem = sysctl_sctp_wmem,
  8008. .memory_pressure = &sctp_memory_pressure,
  8009. .enter_memory_pressure = sctp_enter_memory_pressure,
  8010. .memory_allocated = &sctp_memory_allocated,
  8011. .sockets_allocated = &sctp_sockets_allocated,
  8012. };
  8013. #if IS_ENABLED(CONFIG_IPV6)
  8014. #include <net/transp_v6.h>
  8015. static void sctp_v6_destroy_sock(struct sock *sk)
  8016. {
  8017. sctp_destroy_sock(sk);
  8018. inet6_destroy_sock(sk);
  8019. }
  8020. struct proto sctpv6_prot = {
  8021. .name = "SCTPv6",
  8022. .owner = THIS_MODULE,
  8023. .close = sctp_close,
  8024. .disconnect = sctp_disconnect,
  8025. .accept = sctp_accept,
  8026. .ioctl = sctp_ioctl,
  8027. .init = sctp_init_sock,
  8028. .destroy = sctp_v6_destroy_sock,
  8029. .shutdown = sctp_shutdown,
  8030. .setsockopt = sctp_setsockopt,
  8031. .getsockopt = sctp_getsockopt,
  8032. .sendmsg = sctp_sendmsg,
  8033. .recvmsg = sctp_recvmsg,
  8034. .bind = sctp_bind,
  8035. .bind_add = sctp_bind_add,
  8036. .backlog_rcv = sctp_backlog_rcv,
  8037. .hash = sctp_hash,
  8038. .unhash = sctp_unhash,
  8039. .no_autobind = true,
  8040. .obj_size = sizeof(struct sctp6_sock),
  8041. .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
  8042. .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
  8043. offsetof(struct sctp6_sock, sctp.subscribe) +
  8044. sizeof_field(struct sctp6_sock, sctp.initmsg),
  8045. .sysctl_mem = sysctl_sctp_mem,
  8046. .sysctl_rmem = sysctl_sctp_rmem,
  8047. .sysctl_wmem = sysctl_sctp_wmem,
  8048. .memory_pressure = &sctp_memory_pressure,
  8049. .enter_memory_pressure = sctp_enter_memory_pressure,
  8050. .memory_allocated = &sctp_memory_allocated,
  8051. .sockets_allocated = &sctp_sockets_allocated,
  8052. };
  8053. #endif /* IS_ENABLED(CONFIG_IPV6) */