socket.c 232 KB

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