socket.c 219 KB

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