socket.c 230 KB

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