socket.c 200 KB

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