socket.c 266 KB

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