socket.c 223 KB

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