socket.c 247 KB

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