socket.c 187 KB

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