socket.c 175 KB

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