socket.c 208 KB

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