socket.c 214 KB

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