socket.c 174 KB

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