socket.c 266 KB

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