socket.c 238 KB

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