socket.c 232 KB

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