socket.c 202 KB

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