socket.c 219 KB

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