socket.c 185 KB

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