socket.c 199 KB

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