socket.c 238 KB

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