socket.c 247 KB

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