socket.c 189 KB

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