socket.c 207 KB

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