socket.c 247 KB

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