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. /* make sure it won't exceed MAX_SCHEDULE_TIMEOUT */
  1850. sp->autoclose = min_t(long, sp->autoclose, MAX_SCHEDULE_TIMEOUT / HZ);
  1851. return 0;
  1852. }
  1853. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1854. *
  1855. * Applications can enable or disable heartbeats for any peer address of
  1856. * an association, modify an address's heartbeat interval, force a
  1857. * heartbeat to be sent immediately, and adjust the address's maximum
  1858. * number of retransmissions sent before an address is considered
  1859. * unreachable. The following structure is used to access and modify an
  1860. * address's parameters:
  1861. *
  1862. * struct sctp_paddrparams {
  1863. * sctp_assoc_t spp_assoc_id;
  1864. * struct sockaddr_storage spp_address;
  1865. * uint32_t spp_hbinterval;
  1866. * uint16_t spp_pathmaxrxt;
  1867. * uint32_t spp_pathmtu;
  1868. * uint32_t spp_sackdelay;
  1869. * uint32_t spp_flags;
  1870. * };
  1871. *
  1872. * spp_assoc_id - (one-to-many style socket) This is filled in the
  1873. * application, and identifies the association for
  1874. * this query.
  1875. * spp_address - This specifies which address is of interest.
  1876. * spp_hbinterval - This contains the value of the heartbeat interval,
  1877. * in milliseconds. If a value of zero
  1878. * is present in this field then no changes are to
  1879. * be made to this parameter.
  1880. * spp_pathmaxrxt - This contains the maximum number of
  1881. * retransmissions before this address shall be
  1882. * considered unreachable. If a value of zero
  1883. * is present in this field then no changes are to
  1884. * be made to this parameter.
  1885. * spp_pathmtu - When Path MTU discovery is disabled the value
  1886. * specified here will be the "fixed" path mtu.
  1887. * Note that if the spp_address field is empty
  1888. * then all associations on this address will
  1889. * have this fixed path mtu set upon them.
  1890. *
  1891. * spp_sackdelay - When delayed sack is enabled, this value specifies
  1892. * the number of milliseconds that sacks will be delayed
  1893. * for. This value will apply to all addresses of an
  1894. * association if the spp_address field is empty. Note
  1895. * also, that if delayed sack is enabled and this
  1896. * value is set to 0, no change is made to the last
  1897. * recorded delayed sack timer value.
  1898. *
  1899. * spp_flags - These flags are used to control various features
  1900. * on an association. The flag field may contain
  1901. * zero or more of the following options.
  1902. *
  1903. * SPP_HB_ENABLE - Enable heartbeats on the
  1904. * specified address. Note that if the address
  1905. * field is empty all addresses for the association
  1906. * have heartbeats enabled upon them.
  1907. *
  1908. * SPP_HB_DISABLE - Disable heartbeats on the
  1909. * speicifed address. Note that if the address
  1910. * field is empty all addresses for the association
  1911. * will have their heartbeats disabled. Note also
  1912. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  1913. * mutually exclusive, only one of these two should
  1914. * be specified. Enabling both fields will have
  1915. * undetermined results.
  1916. *
  1917. * SPP_HB_DEMAND - Request a user initiated heartbeat
  1918. * to be made immediately.
  1919. *
  1920. * SPP_HB_TIME_IS_ZERO - Specify's that the time for
  1921. * heartbeat delayis to be set to the value of 0
  1922. * milliseconds.
  1923. *
  1924. * SPP_PMTUD_ENABLE - This field will enable PMTU
  1925. * discovery upon the specified address. Note that
  1926. * if the address feild is empty then all addresses
  1927. * on the association are effected.
  1928. *
  1929. * SPP_PMTUD_DISABLE - This field will disable PMTU
  1930. * discovery upon the specified address. Note that
  1931. * if the address feild is empty then all addresses
  1932. * on the association are effected. Not also that
  1933. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  1934. * exclusive. Enabling both will have undetermined
  1935. * results.
  1936. *
  1937. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  1938. * on delayed sack. The time specified in spp_sackdelay
  1939. * is used to specify the sack delay for this address. Note
  1940. * that if spp_address is empty then all addresses will
  1941. * enable delayed sack and take on the sack delay
  1942. * value specified in spp_sackdelay.
  1943. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  1944. * off delayed sack. If the spp_address field is blank then
  1945. * delayed sack is disabled for the entire association. Note
  1946. * also that this field is mutually exclusive to
  1947. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  1948. * results.
  1949. */
  1950. static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
  1951. struct sctp_transport *trans,
  1952. struct sctp_association *asoc,
  1953. struct sctp_sock *sp,
  1954. int hb_change,
  1955. int pmtud_change,
  1956. int sackdelay_change)
  1957. {
  1958. int error;
  1959. if (params->spp_flags & SPP_HB_DEMAND && trans) {
  1960. error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
  1961. if (error)
  1962. return error;
  1963. }
  1964. /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
  1965. * this field is ignored. Note also that a value of zero indicates
  1966. * the current setting should be left unchanged.
  1967. */
  1968. if (params->spp_flags & SPP_HB_ENABLE) {
  1969. /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
  1970. * set. This lets us use 0 value when this flag
  1971. * is set.
  1972. */
  1973. if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
  1974. params->spp_hbinterval = 0;
  1975. if (params->spp_hbinterval ||
  1976. (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
  1977. if (trans) {
  1978. trans->hbinterval =
  1979. msecs_to_jiffies(params->spp_hbinterval);
  1980. } else if (asoc) {
  1981. asoc->hbinterval =
  1982. msecs_to_jiffies(params->spp_hbinterval);
  1983. } else {
  1984. sp->hbinterval = params->spp_hbinterval;
  1985. }
  1986. }
  1987. }
  1988. if (hb_change) {
  1989. if (trans) {
  1990. trans->param_flags =
  1991. (trans->param_flags & ~SPP_HB) | hb_change;
  1992. } else if (asoc) {
  1993. asoc->param_flags =
  1994. (asoc->param_flags & ~SPP_HB) | hb_change;
  1995. } else {
  1996. sp->param_flags =
  1997. (sp->param_flags & ~SPP_HB) | hb_change;
  1998. }
  1999. }
  2000. /* When Path MTU discovery is disabled the value specified here will
  2001. * be the "fixed" path mtu (i.e. the value of the spp_flags field must
  2002. * include the flag SPP_PMTUD_DISABLE for this field to have any
  2003. * effect).
  2004. */
  2005. if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
  2006. if (trans) {
  2007. trans->pathmtu = params->spp_pathmtu;
  2008. sctp_assoc_sync_pmtu(asoc);
  2009. } else if (asoc) {
  2010. asoc->pathmtu = params->spp_pathmtu;
  2011. sctp_frag_point(asoc, params->spp_pathmtu);
  2012. } else {
  2013. sp->pathmtu = params->spp_pathmtu;
  2014. }
  2015. }
  2016. if (pmtud_change) {
  2017. if (trans) {
  2018. int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
  2019. (params->spp_flags & SPP_PMTUD_ENABLE);
  2020. trans->param_flags =
  2021. (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
  2022. if (update) {
  2023. sctp_transport_pmtu(trans, sctp_opt2sk(sp));
  2024. sctp_assoc_sync_pmtu(asoc);
  2025. }
  2026. } else if (asoc) {
  2027. asoc->param_flags =
  2028. (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
  2029. } else {
  2030. sp->param_flags =
  2031. (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
  2032. }
  2033. }
  2034. /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
  2035. * value of this field is ignored. Note also that a value of zero
  2036. * indicates the current setting should be left unchanged.
  2037. */
  2038. if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
  2039. if (trans) {
  2040. trans->sackdelay =
  2041. msecs_to_jiffies(params->spp_sackdelay);
  2042. } else if (asoc) {
  2043. asoc->sackdelay =
  2044. msecs_to_jiffies(params->spp_sackdelay);
  2045. } else {
  2046. sp->sackdelay = params->spp_sackdelay;
  2047. }
  2048. }
  2049. if (sackdelay_change) {
  2050. if (trans) {
  2051. trans->param_flags =
  2052. (trans->param_flags & ~SPP_SACKDELAY) |
  2053. sackdelay_change;
  2054. } else if (asoc) {
  2055. asoc->param_flags =
  2056. (asoc->param_flags & ~SPP_SACKDELAY) |
  2057. sackdelay_change;
  2058. } else {
  2059. sp->param_flags =
  2060. (sp->param_flags & ~SPP_SACKDELAY) |
  2061. sackdelay_change;
  2062. }
  2063. }
  2064. /* Note that a value of zero indicates the current setting should be
  2065. left unchanged.
  2066. */
  2067. if (params->spp_pathmaxrxt) {
  2068. if (trans) {
  2069. trans->pathmaxrxt = params->spp_pathmaxrxt;
  2070. } else if (asoc) {
  2071. asoc->pathmaxrxt = params->spp_pathmaxrxt;
  2072. } else {
  2073. sp->pathmaxrxt = params->spp_pathmaxrxt;
  2074. }
  2075. }
  2076. return 0;
  2077. }
  2078. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  2079. char __user *optval,
  2080. unsigned int optlen)
  2081. {
  2082. struct sctp_paddrparams params;
  2083. struct sctp_transport *trans = NULL;
  2084. struct sctp_association *asoc = NULL;
  2085. struct sctp_sock *sp = sctp_sk(sk);
  2086. int error;
  2087. int hb_change, pmtud_change, sackdelay_change;
  2088. if (optlen != sizeof(struct sctp_paddrparams))
  2089. return - EINVAL;
  2090. if (copy_from_user(&params, optval, optlen))
  2091. return -EFAULT;
  2092. /* Validate flags and value parameters. */
  2093. hb_change = params.spp_flags & SPP_HB;
  2094. pmtud_change = params.spp_flags & SPP_PMTUD;
  2095. sackdelay_change = params.spp_flags & SPP_SACKDELAY;
  2096. if (hb_change == SPP_HB ||
  2097. pmtud_change == SPP_PMTUD ||
  2098. sackdelay_change == SPP_SACKDELAY ||
  2099. params.spp_sackdelay > 500 ||
  2100. (params.spp_pathmtu &&
  2101. params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
  2102. return -EINVAL;
  2103. /* If an address other than INADDR_ANY is specified, and
  2104. * no transport is found, then the request is invalid.
  2105. */
  2106. if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
  2107. trans = sctp_addr_id2transport(sk, &params.spp_address,
  2108. params.spp_assoc_id);
  2109. if (!trans)
  2110. return -EINVAL;
  2111. }
  2112. /* Get association, if assoc_id != 0 and the socket is a one
  2113. * to many style socket, and an association was not found, then
  2114. * the id was invalid.
  2115. */
  2116. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  2117. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
  2118. return -EINVAL;
  2119. /* Heartbeat demand can only be sent on a transport or
  2120. * association, but not a socket.
  2121. */
  2122. if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
  2123. return -EINVAL;
  2124. /* Process parameters. */
  2125. error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2126. hb_change, pmtud_change,
  2127. sackdelay_change);
  2128. if (error)
  2129. return error;
  2130. /* If changes are for association, also apply parameters to each
  2131. * transport.
  2132. */
  2133. if (!trans && asoc) {
  2134. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2135. transports) {
  2136. sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2137. hb_change, pmtud_change,
  2138. sackdelay_change);
  2139. }
  2140. }
  2141. return 0;
  2142. }
  2143. /*
  2144. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  2145. *
  2146. * This option will effect the way delayed acks are performed. This
  2147. * option allows you to get or set the delayed ack time, in
  2148. * milliseconds. It also allows changing the delayed ack frequency.
  2149. * Changing the frequency to 1 disables the delayed sack algorithm. If
  2150. * the assoc_id is 0, then this sets or gets the endpoints default
  2151. * values. If the assoc_id field is non-zero, then the set or get
  2152. * effects the specified association for the one to many model (the
  2153. * assoc_id field is ignored by the one to one model). Note that if
  2154. * sack_delay or sack_freq are 0 when setting this option, then the
  2155. * current values will remain unchanged.
  2156. *
  2157. * struct sctp_sack_info {
  2158. * sctp_assoc_t sack_assoc_id;
  2159. * uint32_t sack_delay;
  2160. * uint32_t sack_freq;
  2161. * };
  2162. *
  2163. * sack_assoc_id - This parameter, indicates which association the user
  2164. * is performing an action upon. Note that if this field's value is
  2165. * zero then the endpoints default value is changed (effecting future
  2166. * associations only).
  2167. *
  2168. * sack_delay - This parameter contains the number of milliseconds that
  2169. * the user is requesting the delayed ACK timer be set to. Note that
  2170. * this value is defined in the standard to be between 200 and 500
  2171. * milliseconds.
  2172. *
  2173. * sack_freq - This parameter contains the number of packets that must
  2174. * be received before a sack is sent without waiting for the delay
  2175. * timer to expire. The default value for this is 2, setting this
  2176. * value to 1 will disable the delayed sack algorithm.
  2177. */
  2178. static int sctp_setsockopt_delayed_ack(struct sock *sk,
  2179. char __user *optval, unsigned int optlen)
  2180. {
  2181. struct sctp_sack_info params;
  2182. struct sctp_transport *trans = NULL;
  2183. struct sctp_association *asoc = NULL;
  2184. struct sctp_sock *sp = sctp_sk(sk);
  2185. if (optlen == sizeof(struct sctp_sack_info)) {
  2186. if (copy_from_user(&params, optval, optlen))
  2187. return -EFAULT;
  2188. if (params.sack_delay == 0 && params.sack_freq == 0)
  2189. return 0;
  2190. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2191. pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
  2192. pr_warn("Use struct sctp_sack_info instead\n");
  2193. if (copy_from_user(&params, optval, optlen))
  2194. return -EFAULT;
  2195. if (params.sack_delay == 0)
  2196. params.sack_freq = 1;
  2197. else
  2198. params.sack_freq = 0;
  2199. } else
  2200. return - EINVAL;
  2201. /* Validate value parameter. */
  2202. if (params.sack_delay > 500)
  2203. return -EINVAL;
  2204. /* Get association, if sack_assoc_id != 0 and the socket is a one
  2205. * to many style socket, and an association was not found, then
  2206. * the id was invalid.
  2207. */
  2208. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  2209. if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
  2210. return -EINVAL;
  2211. if (params.sack_delay) {
  2212. if (asoc) {
  2213. asoc->sackdelay =
  2214. msecs_to_jiffies(params.sack_delay);
  2215. asoc->param_flags =
  2216. (asoc->param_flags & ~SPP_SACKDELAY) |
  2217. SPP_SACKDELAY_ENABLE;
  2218. } else {
  2219. sp->sackdelay = params.sack_delay;
  2220. sp->param_flags =
  2221. (sp->param_flags & ~SPP_SACKDELAY) |
  2222. SPP_SACKDELAY_ENABLE;
  2223. }
  2224. }
  2225. if (params.sack_freq == 1) {
  2226. if (asoc) {
  2227. asoc->param_flags =
  2228. (asoc->param_flags & ~SPP_SACKDELAY) |
  2229. SPP_SACKDELAY_DISABLE;
  2230. } else {
  2231. sp->param_flags =
  2232. (sp->param_flags & ~SPP_SACKDELAY) |
  2233. SPP_SACKDELAY_DISABLE;
  2234. }
  2235. } else if (params.sack_freq > 1) {
  2236. if (asoc) {
  2237. asoc->sackfreq = params.sack_freq;
  2238. asoc->param_flags =
  2239. (asoc->param_flags & ~SPP_SACKDELAY) |
  2240. SPP_SACKDELAY_ENABLE;
  2241. } else {
  2242. sp->sackfreq = params.sack_freq;
  2243. sp->param_flags =
  2244. (sp->param_flags & ~SPP_SACKDELAY) |
  2245. SPP_SACKDELAY_ENABLE;
  2246. }
  2247. }
  2248. /* If change is for association, also apply to each transport. */
  2249. if (asoc) {
  2250. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2251. transports) {
  2252. if (params.sack_delay) {
  2253. trans->sackdelay =
  2254. msecs_to_jiffies(params.sack_delay);
  2255. trans->param_flags =
  2256. (trans->param_flags & ~SPP_SACKDELAY) |
  2257. SPP_SACKDELAY_ENABLE;
  2258. }
  2259. if (params.sack_freq == 1) {
  2260. trans->param_flags =
  2261. (trans->param_flags & ~SPP_SACKDELAY) |
  2262. SPP_SACKDELAY_DISABLE;
  2263. } else if (params.sack_freq > 1) {
  2264. trans->sackfreq = params.sack_freq;
  2265. trans->param_flags =
  2266. (trans->param_flags & ~SPP_SACKDELAY) |
  2267. SPP_SACKDELAY_ENABLE;
  2268. }
  2269. }
  2270. }
  2271. return 0;
  2272. }
  2273. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2274. *
  2275. * Applications can specify protocol parameters for the default association
  2276. * initialization. The option name argument to setsockopt() and getsockopt()
  2277. * is SCTP_INITMSG.
  2278. *
  2279. * Setting initialization parameters is effective only on an unconnected
  2280. * socket (for UDP-style sockets only future associations are effected
  2281. * by the change). With TCP-style sockets, this option is inherited by
  2282. * sockets derived from a listener socket.
  2283. */
  2284. static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
  2285. {
  2286. struct sctp_initmsg sinit;
  2287. struct sctp_sock *sp = sctp_sk(sk);
  2288. if (optlen != sizeof(struct sctp_initmsg))
  2289. return -EINVAL;
  2290. if (copy_from_user(&sinit, optval, optlen))
  2291. return -EFAULT;
  2292. if (sinit.sinit_num_ostreams)
  2293. sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
  2294. if (sinit.sinit_max_instreams)
  2295. sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
  2296. if (sinit.sinit_max_attempts)
  2297. sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
  2298. if (sinit.sinit_max_init_timeo)
  2299. sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
  2300. return 0;
  2301. }
  2302. /*
  2303. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2304. *
  2305. * Applications that wish to use the sendto() system call may wish to
  2306. * specify a default set of parameters that would normally be supplied
  2307. * through the inclusion of ancillary data. This socket option allows
  2308. * such an application to set the default sctp_sndrcvinfo structure.
  2309. * The application that wishes to use this socket option simply passes
  2310. * in to this call the sctp_sndrcvinfo structure defined in Section
  2311. * 5.2.2) The input parameters accepted by this call include
  2312. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2313. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2314. * to this call if the caller is using the UDP model.
  2315. */
  2316. static int sctp_setsockopt_default_send_param(struct sock *sk,
  2317. char __user *optval,
  2318. unsigned int optlen)
  2319. {
  2320. struct sctp_sndrcvinfo info;
  2321. struct sctp_association *asoc;
  2322. struct sctp_sock *sp = sctp_sk(sk);
  2323. if (optlen != sizeof(struct sctp_sndrcvinfo))
  2324. return -EINVAL;
  2325. if (copy_from_user(&info, optval, optlen))
  2326. return -EFAULT;
  2327. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  2328. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  2329. return -EINVAL;
  2330. if (asoc) {
  2331. asoc->default_stream = info.sinfo_stream;
  2332. asoc->default_flags = info.sinfo_flags;
  2333. asoc->default_ppid = info.sinfo_ppid;
  2334. asoc->default_context = info.sinfo_context;
  2335. asoc->default_timetolive = info.sinfo_timetolive;
  2336. } else {
  2337. sp->default_stream = info.sinfo_stream;
  2338. sp->default_flags = info.sinfo_flags;
  2339. sp->default_ppid = info.sinfo_ppid;
  2340. sp->default_context = info.sinfo_context;
  2341. sp->default_timetolive = info.sinfo_timetolive;
  2342. }
  2343. return 0;
  2344. }
  2345. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2346. *
  2347. * Requests that the local SCTP stack use the enclosed peer address as
  2348. * the association primary. The enclosed address must be one of the
  2349. * association peer's addresses.
  2350. */
  2351. static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
  2352. unsigned int optlen)
  2353. {
  2354. struct sctp_prim prim;
  2355. struct sctp_transport *trans;
  2356. if (optlen != sizeof(struct sctp_prim))
  2357. return -EINVAL;
  2358. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  2359. return -EFAULT;
  2360. trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
  2361. if (!trans)
  2362. return -EINVAL;
  2363. sctp_assoc_set_primary(trans->asoc, trans);
  2364. return 0;
  2365. }
  2366. /*
  2367. * 7.1.5 SCTP_NODELAY
  2368. *
  2369. * Turn on/off any Nagle-like algorithm. This means that packets are
  2370. * generally sent as soon as possible and no unnecessary delays are
  2371. * introduced, at the cost of more packets in the network. Expects an
  2372. * integer boolean flag.
  2373. */
  2374. static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
  2375. unsigned int optlen)
  2376. {
  2377. int val;
  2378. if (optlen < sizeof(int))
  2379. return -EINVAL;
  2380. if (get_user(val, (int __user *)optval))
  2381. return -EFAULT;
  2382. sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
  2383. return 0;
  2384. }
  2385. /*
  2386. *
  2387. * 7.1.1 SCTP_RTOINFO
  2388. *
  2389. * The protocol parameters used to initialize and bound retransmission
  2390. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2391. * and modify these parameters.
  2392. * All parameters are time values, in milliseconds. A value of 0, when
  2393. * modifying the parameters, indicates that the current value should not
  2394. * be changed.
  2395. *
  2396. */
  2397. static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
  2398. {
  2399. struct sctp_rtoinfo rtoinfo;
  2400. struct sctp_association *asoc;
  2401. if (optlen != sizeof (struct sctp_rtoinfo))
  2402. return -EINVAL;
  2403. if (copy_from_user(&rtoinfo, optval, optlen))
  2404. return -EFAULT;
  2405. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  2406. /* Set the values to the specific association */
  2407. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  2408. return -EINVAL;
  2409. if (asoc) {
  2410. if (rtoinfo.srto_initial != 0)
  2411. asoc->rto_initial =
  2412. msecs_to_jiffies(rtoinfo.srto_initial);
  2413. if (rtoinfo.srto_max != 0)
  2414. asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
  2415. if (rtoinfo.srto_min != 0)
  2416. asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
  2417. } else {
  2418. /* If there is no association or the association-id = 0
  2419. * set the values to the endpoint.
  2420. */
  2421. struct sctp_sock *sp = sctp_sk(sk);
  2422. if (rtoinfo.srto_initial != 0)
  2423. sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
  2424. if (rtoinfo.srto_max != 0)
  2425. sp->rtoinfo.srto_max = rtoinfo.srto_max;
  2426. if (rtoinfo.srto_min != 0)
  2427. sp->rtoinfo.srto_min = rtoinfo.srto_min;
  2428. }
  2429. return 0;
  2430. }
  2431. /*
  2432. *
  2433. * 7.1.2 SCTP_ASSOCINFO
  2434. *
  2435. * This option is used to tune the maximum retransmission attempts
  2436. * of the association.
  2437. * Returns an error if the new association retransmission value is
  2438. * greater than the sum of the retransmission value of the peer.
  2439. * See [SCTP] for more information.
  2440. *
  2441. */
  2442. static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
  2443. {
  2444. struct sctp_assocparams assocparams;
  2445. struct sctp_association *asoc;
  2446. if (optlen != sizeof(struct sctp_assocparams))
  2447. return -EINVAL;
  2448. if (copy_from_user(&assocparams, optval, optlen))
  2449. return -EFAULT;
  2450. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  2451. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  2452. return -EINVAL;
  2453. /* Set the values to the specific association */
  2454. if (asoc) {
  2455. if (assocparams.sasoc_asocmaxrxt != 0) {
  2456. __u32 path_sum = 0;
  2457. int paths = 0;
  2458. struct sctp_transport *peer_addr;
  2459. list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
  2460. transports) {
  2461. path_sum += peer_addr->pathmaxrxt;
  2462. paths++;
  2463. }
  2464. /* Only validate asocmaxrxt if we have more than
  2465. * one path/transport. We do this because path
  2466. * retransmissions are only counted when we have more
  2467. * then one path.
  2468. */
  2469. if (paths > 1 &&
  2470. assocparams.sasoc_asocmaxrxt > path_sum)
  2471. return -EINVAL;
  2472. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  2473. }
  2474. if (assocparams.sasoc_cookie_life != 0) {
  2475. asoc->cookie_life.tv_sec =
  2476. assocparams.sasoc_cookie_life / 1000;
  2477. asoc->cookie_life.tv_usec =
  2478. (assocparams.sasoc_cookie_life % 1000)
  2479. * 1000;
  2480. }
  2481. } else {
  2482. /* Set the values to the endpoint */
  2483. struct sctp_sock *sp = sctp_sk(sk);
  2484. if (assocparams.sasoc_asocmaxrxt != 0)
  2485. sp->assocparams.sasoc_asocmaxrxt =
  2486. assocparams.sasoc_asocmaxrxt;
  2487. if (assocparams.sasoc_cookie_life != 0)
  2488. sp->assocparams.sasoc_cookie_life =
  2489. assocparams.sasoc_cookie_life;
  2490. }
  2491. return 0;
  2492. }
  2493. /*
  2494. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  2495. *
  2496. * This socket option is a boolean flag which turns on or off mapped V4
  2497. * addresses. If this option is turned on and the socket is type
  2498. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  2499. * If this option is turned off, then no mapping will be done of V4
  2500. * addresses and a user will receive both PF_INET6 and PF_INET type
  2501. * addresses on the socket.
  2502. */
  2503. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
  2504. {
  2505. int val;
  2506. struct sctp_sock *sp = sctp_sk(sk);
  2507. if (optlen < sizeof(int))
  2508. return -EINVAL;
  2509. if (get_user(val, (int __user *)optval))
  2510. return -EFAULT;
  2511. if (val)
  2512. sp->v4mapped = 1;
  2513. else
  2514. sp->v4mapped = 0;
  2515. return 0;
  2516. }
  2517. /*
  2518. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  2519. * This option will get or set the maximum size to put in any outgoing
  2520. * SCTP DATA chunk. If a message is larger than this size it will be
  2521. * fragmented by SCTP into the specified size. Note that the underlying
  2522. * SCTP implementation may fragment into smaller sized chunks when the
  2523. * PMTU of the underlying association is smaller than the value set by
  2524. * the user. The default value for this option is '0' which indicates
  2525. * the user is NOT limiting fragmentation and only the PMTU will effect
  2526. * SCTP's choice of DATA chunk size. Note also that values set larger
  2527. * than the maximum size of an IP datagram will effectively let SCTP
  2528. * control fragmentation (i.e. the same as setting this option to 0).
  2529. *
  2530. * The following structure is used to access and modify this parameter:
  2531. *
  2532. * struct sctp_assoc_value {
  2533. * sctp_assoc_t assoc_id;
  2534. * uint32_t assoc_value;
  2535. * };
  2536. *
  2537. * assoc_id: This parameter is ignored for one-to-one style sockets.
  2538. * For one-to-many style sockets this parameter indicates which
  2539. * association the user is performing an action upon. Note that if
  2540. * this field's value is zero then the endpoints default value is
  2541. * changed (effecting future associations only).
  2542. * assoc_value: This parameter specifies the maximum size in bytes.
  2543. */
  2544. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
  2545. {
  2546. struct sctp_assoc_value params;
  2547. struct sctp_association *asoc;
  2548. struct sctp_sock *sp = sctp_sk(sk);
  2549. int val;
  2550. if (optlen == sizeof(int)) {
  2551. pr_warn("Use of int in maxseg socket option deprecated\n");
  2552. pr_warn("Use struct sctp_assoc_value instead\n");
  2553. if (copy_from_user(&val, optval, optlen))
  2554. return -EFAULT;
  2555. params.assoc_id = 0;
  2556. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2557. if (copy_from_user(&params, optval, optlen))
  2558. return -EFAULT;
  2559. val = params.assoc_value;
  2560. } else
  2561. return -EINVAL;
  2562. if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
  2563. return -EINVAL;
  2564. asoc = sctp_id2assoc(sk, params.assoc_id);
  2565. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  2566. return -EINVAL;
  2567. if (asoc) {
  2568. if (val == 0) {
  2569. val = asoc->pathmtu;
  2570. val -= sp->pf->af->net_header_len;
  2571. val -= sizeof(struct sctphdr) +
  2572. sizeof(struct sctp_data_chunk);
  2573. }
  2574. asoc->user_frag = val;
  2575. asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
  2576. } else {
  2577. sp->user_frag = val;
  2578. }
  2579. return 0;
  2580. }
  2581. /*
  2582. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2583. *
  2584. * Requests that the peer mark the enclosed address as the association
  2585. * primary. The enclosed address must be one of the association's
  2586. * locally bound addresses. The following structure is used to make a
  2587. * set primary request:
  2588. */
  2589. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  2590. unsigned int optlen)
  2591. {
  2592. struct sctp_sock *sp;
  2593. struct sctp_association *asoc = NULL;
  2594. struct sctp_setpeerprim prim;
  2595. struct sctp_chunk *chunk;
  2596. struct sctp_af *af;
  2597. int err;
  2598. sp = sctp_sk(sk);
  2599. if (!sctp_addip_enable)
  2600. return -EPERM;
  2601. if (optlen != sizeof(struct sctp_setpeerprim))
  2602. return -EINVAL;
  2603. if (copy_from_user(&prim, optval, optlen))
  2604. return -EFAULT;
  2605. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  2606. if (!asoc)
  2607. return -EINVAL;
  2608. if (!asoc->peer.asconf_capable)
  2609. return -EPERM;
  2610. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2611. return -EPERM;
  2612. if (!sctp_state(asoc, ESTABLISHED))
  2613. return -ENOTCONN;
  2614. af = sctp_get_af_specific(prim.sspp_addr.ss_family);
  2615. if (!af)
  2616. return -EINVAL;
  2617. if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
  2618. return -EADDRNOTAVAIL;
  2619. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  2620. return -EADDRNOTAVAIL;
  2621. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2622. chunk = sctp_make_asconf_set_prim(asoc,
  2623. (union sctp_addr *)&prim.sspp_addr);
  2624. if (!chunk)
  2625. return -ENOMEM;
  2626. err = sctp_send_asconf(asoc, chunk);
  2627. SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
  2628. return err;
  2629. }
  2630. static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
  2631. unsigned int optlen)
  2632. {
  2633. struct sctp_setadaptation adaptation;
  2634. if (optlen != sizeof(struct sctp_setadaptation))
  2635. return -EINVAL;
  2636. if (copy_from_user(&adaptation, optval, optlen))
  2637. return -EFAULT;
  2638. sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
  2639. return 0;
  2640. }
  2641. /*
  2642. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  2643. *
  2644. * The context field in the sctp_sndrcvinfo structure is normally only
  2645. * used when a failed message is retrieved holding the value that was
  2646. * sent down on the actual send call. This option allows the setting of
  2647. * a default context on an association basis that will be received on
  2648. * reading messages from the peer. This is especially helpful in the
  2649. * one-2-many model for an application to keep some reference to an
  2650. * internal state machine that is processing messages on the
  2651. * association. Note that the setting of this value only effects
  2652. * received messages from the peer and does not effect the value that is
  2653. * saved with outbound messages.
  2654. */
  2655. static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
  2656. unsigned int optlen)
  2657. {
  2658. struct sctp_assoc_value params;
  2659. struct sctp_sock *sp;
  2660. struct sctp_association *asoc;
  2661. if (optlen != sizeof(struct sctp_assoc_value))
  2662. return -EINVAL;
  2663. if (copy_from_user(&params, optval, optlen))
  2664. return -EFAULT;
  2665. sp = sctp_sk(sk);
  2666. if (params.assoc_id != 0) {
  2667. asoc = sctp_id2assoc(sk, params.assoc_id);
  2668. if (!asoc)
  2669. return -EINVAL;
  2670. asoc->default_rcv_context = params.assoc_value;
  2671. } else {
  2672. sp->default_rcv_context = params.assoc_value;
  2673. }
  2674. return 0;
  2675. }
  2676. /*
  2677. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  2678. *
  2679. * This options will at a minimum specify if the implementation is doing
  2680. * fragmented interleave. Fragmented interleave, for a one to many
  2681. * socket, is when subsequent calls to receive a message may return
  2682. * parts of messages from different associations. Some implementations
  2683. * may allow you to turn this value on or off. If so, when turned off,
  2684. * no fragment interleave will occur (which will cause a head of line
  2685. * blocking amongst multiple associations sharing the same one to many
  2686. * socket). When this option is turned on, then each receive call may
  2687. * come from a different association (thus the user must receive data
  2688. * with the extended calls (e.g. sctp_recvmsg) to keep track of which
  2689. * association each receive belongs to.
  2690. *
  2691. * This option takes a boolean value. A non-zero value indicates that
  2692. * fragmented interleave is on. A value of zero indicates that
  2693. * fragmented interleave is off.
  2694. *
  2695. * Note that it is important that an implementation that allows this
  2696. * option to be turned on, have it off by default. Otherwise an unaware
  2697. * application using the one to many model may become confused and act
  2698. * incorrectly.
  2699. */
  2700. static int sctp_setsockopt_fragment_interleave(struct sock *sk,
  2701. char __user *optval,
  2702. unsigned int optlen)
  2703. {
  2704. int val;
  2705. if (optlen != sizeof(int))
  2706. return -EINVAL;
  2707. if (get_user(val, (int __user *)optval))
  2708. return -EFAULT;
  2709. sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
  2710. return 0;
  2711. }
  2712. /*
  2713. * 8.1.21. Set or Get the SCTP Partial Delivery Point
  2714. * (SCTP_PARTIAL_DELIVERY_POINT)
  2715. *
  2716. * This option will set or get the SCTP partial delivery point. This
  2717. * point is the size of a message where the partial delivery API will be
  2718. * invoked to help free up rwnd space for the peer. Setting this to a
  2719. * lower value will cause partial deliveries to happen more often. The
  2720. * calls argument is an integer that sets or gets the partial delivery
  2721. * point. Note also that the call will fail if the user attempts to set
  2722. * this value larger than the socket receive buffer size.
  2723. *
  2724. * Note that any single message having a length smaller than or equal to
  2725. * the SCTP partial delivery point will be delivered in one single read
  2726. * call as long as the user provided buffer is large enough to hold the
  2727. * message.
  2728. */
  2729. static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
  2730. char __user *optval,
  2731. unsigned int optlen)
  2732. {
  2733. u32 val;
  2734. if (optlen != sizeof(u32))
  2735. return -EINVAL;
  2736. if (get_user(val, (int __user *)optval))
  2737. return -EFAULT;
  2738. /* Note: We double the receive buffer from what the user sets
  2739. * it to be, also initial rwnd is based on rcvbuf/2.
  2740. */
  2741. if (val > (sk->sk_rcvbuf >> 1))
  2742. return -EINVAL;
  2743. sctp_sk(sk)->pd_point = val;
  2744. return 0; /* is this the right error code? */
  2745. }
  2746. /*
  2747. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  2748. *
  2749. * This option will allow a user to change the maximum burst of packets
  2750. * that can be emitted by this association. Note that the default value
  2751. * is 4, and some implementations may restrict this setting so that it
  2752. * can only be lowered.
  2753. *
  2754. * NOTE: This text doesn't seem right. Do this on a socket basis with
  2755. * future associations inheriting the socket value.
  2756. */
  2757. static int sctp_setsockopt_maxburst(struct sock *sk,
  2758. char __user *optval,
  2759. unsigned int optlen)
  2760. {
  2761. struct sctp_assoc_value params;
  2762. struct sctp_sock *sp;
  2763. struct sctp_association *asoc;
  2764. int val;
  2765. int assoc_id = 0;
  2766. if (optlen == sizeof(int)) {
  2767. pr_warn("Use of int in max_burst socket option deprecated\n");
  2768. pr_warn("Use struct sctp_assoc_value instead\n");
  2769. if (copy_from_user(&val, optval, optlen))
  2770. return -EFAULT;
  2771. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2772. if (copy_from_user(&params, optval, optlen))
  2773. return -EFAULT;
  2774. val = params.assoc_value;
  2775. assoc_id = params.assoc_id;
  2776. } else
  2777. return -EINVAL;
  2778. sp = sctp_sk(sk);
  2779. if (assoc_id != 0) {
  2780. asoc = sctp_id2assoc(sk, assoc_id);
  2781. if (!asoc)
  2782. return -EINVAL;
  2783. asoc->max_burst = val;
  2784. } else
  2785. sp->max_burst = val;
  2786. return 0;
  2787. }
  2788. /*
  2789. * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
  2790. *
  2791. * This set option adds a chunk type that the user is requesting to be
  2792. * received only in an authenticated way. Changes to the list of chunks
  2793. * will only effect future associations on the socket.
  2794. */
  2795. static int sctp_setsockopt_auth_chunk(struct sock *sk,
  2796. char __user *optval,
  2797. unsigned int optlen)
  2798. {
  2799. struct sctp_authchunk val;
  2800. if (!sctp_auth_enable)
  2801. return -EACCES;
  2802. if (optlen != sizeof(struct sctp_authchunk))
  2803. return -EINVAL;
  2804. if (copy_from_user(&val, optval, optlen))
  2805. return -EFAULT;
  2806. switch (val.sauth_chunk) {
  2807. case SCTP_CID_INIT:
  2808. case SCTP_CID_INIT_ACK:
  2809. case SCTP_CID_SHUTDOWN_COMPLETE:
  2810. case SCTP_CID_AUTH:
  2811. return -EINVAL;
  2812. }
  2813. /* add this chunk id to the endpoint */
  2814. return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
  2815. }
  2816. /*
  2817. * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
  2818. *
  2819. * This option gets or sets the list of HMAC algorithms that the local
  2820. * endpoint requires the peer to use.
  2821. */
  2822. static int sctp_setsockopt_hmac_ident(struct sock *sk,
  2823. char __user *optval,
  2824. unsigned int optlen)
  2825. {
  2826. struct sctp_hmacalgo *hmacs;
  2827. u32 idents;
  2828. int err;
  2829. if (!sctp_auth_enable)
  2830. return -EACCES;
  2831. if (optlen < sizeof(struct sctp_hmacalgo))
  2832. return -EINVAL;
  2833. hmacs= memdup_user(optval, optlen);
  2834. if (IS_ERR(hmacs))
  2835. return PTR_ERR(hmacs);
  2836. idents = hmacs->shmac_num_idents;
  2837. if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
  2838. (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
  2839. err = -EINVAL;
  2840. goto out;
  2841. }
  2842. err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
  2843. out:
  2844. kfree(hmacs);
  2845. return err;
  2846. }
  2847. /*
  2848. * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
  2849. *
  2850. * This option will set a shared secret key which is used to build an
  2851. * association shared key.
  2852. */
  2853. static int sctp_setsockopt_auth_key(struct sock *sk,
  2854. char __user *optval,
  2855. unsigned int optlen)
  2856. {
  2857. struct sctp_authkey *authkey;
  2858. struct sctp_association *asoc;
  2859. int ret;
  2860. if (!sctp_auth_enable)
  2861. return -EACCES;
  2862. if (optlen <= sizeof(struct sctp_authkey))
  2863. return -EINVAL;
  2864. authkey= memdup_user(optval, optlen);
  2865. if (IS_ERR(authkey))
  2866. return PTR_ERR(authkey);
  2867. if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
  2868. ret = -EINVAL;
  2869. goto out;
  2870. }
  2871. asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
  2872. if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
  2873. ret = -EINVAL;
  2874. goto out;
  2875. }
  2876. ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
  2877. out:
  2878. kfree(authkey);
  2879. return ret;
  2880. }
  2881. /*
  2882. * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
  2883. *
  2884. * This option will get or set the active shared key to be used to build
  2885. * the association shared key.
  2886. */
  2887. static int sctp_setsockopt_active_key(struct sock *sk,
  2888. char __user *optval,
  2889. unsigned int optlen)
  2890. {
  2891. struct sctp_authkeyid val;
  2892. struct sctp_association *asoc;
  2893. if (!sctp_auth_enable)
  2894. return -EACCES;
  2895. if (optlen != sizeof(struct sctp_authkeyid))
  2896. return -EINVAL;
  2897. if (copy_from_user(&val, optval, optlen))
  2898. return -EFAULT;
  2899. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  2900. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  2901. return -EINVAL;
  2902. return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
  2903. val.scact_keynumber);
  2904. }
  2905. /*
  2906. * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
  2907. *
  2908. * This set option will delete a shared secret key from use.
  2909. */
  2910. static int sctp_setsockopt_del_key(struct sock *sk,
  2911. char __user *optval,
  2912. unsigned int optlen)
  2913. {
  2914. struct sctp_authkeyid val;
  2915. struct sctp_association *asoc;
  2916. if (!sctp_auth_enable)
  2917. return -EACCES;
  2918. if (optlen != sizeof(struct sctp_authkeyid))
  2919. return -EINVAL;
  2920. if (copy_from_user(&val, optval, optlen))
  2921. return -EFAULT;
  2922. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  2923. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  2924. return -EINVAL;
  2925. return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
  2926. val.scact_keynumber);
  2927. }
  2928. /* API 6.2 setsockopt(), getsockopt()
  2929. *
  2930. * Applications use setsockopt() and getsockopt() to set or retrieve
  2931. * socket options. Socket options are used to change the default
  2932. * behavior of sockets calls. They are described in Section 7.
  2933. *
  2934. * The syntax is:
  2935. *
  2936. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  2937. * int __user *optlen);
  2938. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  2939. * int optlen);
  2940. *
  2941. * sd - the socket descript.
  2942. * level - set to IPPROTO_SCTP for all SCTP options.
  2943. * optname - the option name.
  2944. * optval - the buffer to store the value of the option.
  2945. * optlen - the size of the buffer.
  2946. */
  2947. SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
  2948. char __user *optval, unsigned int optlen)
  2949. {
  2950. int retval = 0;
  2951. SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
  2952. sk, optname);
  2953. /* I can hardly begin to describe how wrong this is. This is
  2954. * so broken as to be worse than useless. The API draft
  2955. * REALLY is NOT helpful here... I am not convinced that the
  2956. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  2957. * are at all well-founded.
  2958. */
  2959. if (level != SOL_SCTP) {
  2960. struct sctp_af *af = sctp_sk(sk)->pf->af;
  2961. retval = af->setsockopt(sk, level, optname, optval, optlen);
  2962. goto out_nounlock;
  2963. }
  2964. sctp_lock_sock(sk);
  2965. switch (optname) {
  2966. case SCTP_SOCKOPT_BINDX_ADD:
  2967. /* 'optlen' is the size of the addresses buffer. */
  2968. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2969. optlen, SCTP_BINDX_ADD_ADDR);
  2970. break;
  2971. case SCTP_SOCKOPT_BINDX_REM:
  2972. /* 'optlen' is the size of the addresses buffer. */
  2973. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2974. optlen, SCTP_BINDX_REM_ADDR);
  2975. break;
  2976. case SCTP_SOCKOPT_CONNECTX_OLD:
  2977. /* 'optlen' is the size of the addresses buffer. */
  2978. retval = sctp_setsockopt_connectx_old(sk,
  2979. (struct sockaddr __user *)optval,
  2980. optlen);
  2981. break;
  2982. case SCTP_SOCKOPT_CONNECTX:
  2983. /* 'optlen' is the size of the addresses buffer. */
  2984. retval = sctp_setsockopt_connectx(sk,
  2985. (struct sockaddr __user *)optval,
  2986. optlen);
  2987. break;
  2988. case SCTP_DISABLE_FRAGMENTS:
  2989. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  2990. break;
  2991. case SCTP_EVENTS:
  2992. retval = sctp_setsockopt_events(sk, optval, optlen);
  2993. break;
  2994. case SCTP_AUTOCLOSE:
  2995. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  2996. break;
  2997. case SCTP_PEER_ADDR_PARAMS:
  2998. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  2999. break;
  3000. case SCTP_DELAYED_SACK:
  3001. retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
  3002. break;
  3003. case SCTP_PARTIAL_DELIVERY_POINT:
  3004. retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
  3005. break;
  3006. case SCTP_INITMSG:
  3007. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  3008. break;
  3009. case SCTP_DEFAULT_SEND_PARAM:
  3010. retval = sctp_setsockopt_default_send_param(sk, optval,
  3011. optlen);
  3012. break;
  3013. case SCTP_PRIMARY_ADDR:
  3014. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  3015. break;
  3016. case SCTP_SET_PEER_PRIMARY_ADDR:
  3017. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  3018. break;
  3019. case SCTP_NODELAY:
  3020. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  3021. break;
  3022. case SCTP_RTOINFO:
  3023. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  3024. break;
  3025. case SCTP_ASSOCINFO:
  3026. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  3027. break;
  3028. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3029. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  3030. break;
  3031. case SCTP_MAXSEG:
  3032. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  3033. break;
  3034. case SCTP_ADAPTATION_LAYER:
  3035. retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
  3036. break;
  3037. case SCTP_CONTEXT:
  3038. retval = sctp_setsockopt_context(sk, optval, optlen);
  3039. break;
  3040. case SCTP_FRAGMENT_INTERLEAVE:
  3041. retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
  3042. break;
  3043. case SCTP_MAX_BURST:
  3044. retval = sctp_setsockopt_maxburst(sk, optval, optlen);
  3045. break;
  3046. case SCTP_AUTH_CHUNK:
  3047. retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
  3048. break;
  3049. case SCTP_HMAC_IDENT:
  3050. retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
  3051. break;
  3052. case SCTP_AUTH_KEY:
  3053. retval = sctp_setsockopt_auth_key(sk, optval, optlen);
  3054. break;
  3055. case SCTP_AUTH_ACTIVE_KEY:
  3056. retval = sctp_setsockopt_active_key(sk, optval, optlen);
  3057. break;
  3058. case SCTP_AUTH_DELETE_KEY:
  3059. retval = sctp_setsockopt_del_key(sk, optval, optlen);
  3060. break;
  3061. default:
  3062. retval = -ENOPROTOOPT;
  3063. break;
  3064. }
  3065. sctp_release_sock(sk);
  3066. out_nounlock:
  3067. return retval;
  3068. }
  3069. /* API 3.1.6 connect() - UDP Style Syntax
  3070. *
  3071. * An application may use the connect() call in the UDP model to initiate an
  3072. * association without sending data.
  3073. *
  3074. * The syntax is:
  3075. *
  3076. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  3077. *
  3078. * sd: the socket descriptor to have a new association added to.
  3079. *
  3080. * nam: the address structure (either struct sockaddr_in or struct
  3081. * sockaddr_in6 defined in RFC2553 [7]).
  3082. *
  3083. * len: the size of the address.
  3084. */
  3085. SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
  3086. int addr_len)
  3087. {
  3088. int err = 0;
  3089. struct sctp_af *af;
  3090. sctp_lock_sock(sk);
  3091. SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
  3092. __func__, sk, addr, addr_len);
  3093. /* Validate addr_len before calling common connect/connectx routine. */
  3094. af = sctp_get_af_specific(addr->sa_family);
  3095. if (!af || addr_len < af->sockaddr_len) {
  3096. err = -EINVAL;
  3097. } else {
  3098. /* Pass correct addr len to common routine (so it knows there
  3099. * is only one address being passed.
  3100. */
  3101. err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
  3102. }
  3103. sctp_release_sock(sk);
  3104. return err;
  3105. }
  3106. /* FIXME: Write comments. */
  3107. SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
  3108. {
  3109. return -EOPNOTSUPP; /* STUB */
  3110. }
  3111. /* 4.1.4 accept() - TCP Style Syntax
  3112. *
  3113. * Applications use accept() call to remove an established SCTP
  3114. * association from the accept queue of the endpoint. A new socket
  3115. * descriptor will be returned from accept() to represent the newly
  3116. * formed association.
  3117. */
  3118. SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
  3119. {
  3120. struct sctp_sock *sp;
  3121. struct sctp_endpoint *ep;
  3122. struct sock *newsk = NULL;
  3123. struct sctp_association *asoc;
  3124. long timeo;
  3125. int error = 0;
  3126. sctp_lock_sock(sk);
  3127. sp = sctp_sk(sk);
  3128. ep = sp->ep;
  3129. if (!sctp_style(sk, TCP)) {
  3130. error = -EOPNOTSUPP;
  3131. goto out;
  3132. }
  3133. if (!sctp_sstate(sk, LISTENING)) {
  3134. error = -EINVAL;
  3135. goto out;
  3136. }
  3137. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  3138. error = sctp_wait_for_accept(sk, timeo);
  3139. if (error)
  3140. goto out;
  3141. /* We treat the list of associations on the endpoint as the accept
  3142. * queue and pick the first association on the list.
  3143. */
  3144. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  3145. newsk = sp->pf->create_accept_sk(sk, asoc);
  3146. if (!newsk) {
  3147. error = -ENOMEM;
  3148. goto out;
  3149. }
  3150. /* Populate the fields of the newsk from the oldsk and migrate the
  3151. * asoc to the newsk.
  3152. */
  3153. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  3154. out:
  3155. sctp_release_sock(sk);
  3156. *err = error;
  3157. return newsk;
  3158. }
  3159. /* The SCTP ioctl handler. */
  3160. SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  3161. {
  3162. int rc = -ENOTCONN;
  3163. sctp_lock_sock(sk);
  3164. /*
  3165. * SEQPACKET-style sockets in LISTENING state are valid, for
  3166. * SCTP, so only discard TCP-style sockets in LISTENING state.
  3167. */
  3168. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  3169. goto out;
  3170. switch (cmd) {
  3171. case SIOCINQ: {
  3172. struct sk_buff *skb;
  3173. unsigned int amount = 0;
  3174. skb = skb_peek(&sk->sk_receive_queue);
  3175. if (skb != NULL) {
  3176. /*
  3177. * We will only return the amount of this packet since
  3178. * that is all that will be read.
  3179. */
  3180. amount = skb->len;
  3181. }
  3182. rc = put_user(amount, (int __user *)arg);
  3183. break;
  3184. }
  3185. default:
  3186. rc = -ENOIOCTLCMD;
  3187. break;
  3188. }
  3189. out:
  3190. sctp_release_sock(sk);
  3191. return rc;
  3192. }
  3193. /* This is the function which gets called during socket creation to
  3194. * initialized the SCTP-specific portion of the sock.
  3195. * The sock structure should already be zero-filled memory.
  3196. */
  3197. SCTP_STATIC int sctp_init_sock(struct sock *sk)
  3198. {
  3199. struct sctp_endpoint *ep;
  3200. struct sctp_sock *sp;
  3201. SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
  3202. sp = sctp_sk(sk);
  3203. /* Initialize the SCTP per socket area. */
  3204. switch (sk->sk_type) {
  3205. case SOCK_SEQPACKET:
  3206. sp->type = SCTP_SOCKET_UDP;
  3207. break;
  3208. case SOCK_STREAM:
  3209. sp->type = SCTP_SOCKET_TCP;
  3210. break;
  3211. default:
  3212. return -ESOCKTNOSUPPORT;
  3213. }
  3214. /* Initialize default send parameters. These parameters can be
  3215. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  3216. */
  3217. sp->default_stream = 0;
  3218. sp->default_ppid = 0;
  3219. sp->default_flags = 0;
  3220. sp->default_context = 0;
  3221. sp->default_timetolive = 0;
  3222. sp->default_rcv_context = 0;
  3223. sp->max_burst = sctp_max_burst;
  3224. /* Initialize default setup parameters. These parameters
  3225. * can be modified with the SCTP_INITMSG socket option or
  3226. * overridden by the SCTP_INIT CMSG.
  3227. */
  3228. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  3229. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  3230. sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
  3231. sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
  3232. /* Initialize default RTO related parameters. These parameters can
  3233. * be modified for with the SCTP_RTOINFO socket option.
  3234. */
  3235. sp->rtoinfo.srto_initial = sctp_rto_initial;
  3236. sp->rtoinfo.srto_max = sctp_rto_max;
  3237. sp->rtoinfo.srto_min = sctp_rto_min;
  3238. /* Initialize default association related parameters. These parameters
  3239. * can be modified with the SCTP_ASSOCINFO socket option.
  3240. */
  3241. sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
  3242. sp->assocparams.sasoc_number_peer_destinations = 0;
  3243. sp->assocparams.sasoc_peer_rwnd = 0;
  3244. sp->assocparams.sasoc_local_rwnd = 0;
  3245. sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
  3246. /* Initialize default event subscriptions. By default, all the
  3247. * options are off.
  3248. */
  3249. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  3250. /* Default Peer Address Parameters. These defaults can
  3251. * be modified via SCTP_PEER_ADDR_PARAMS
  3252. */
  3253. sp->hbinterval = sctp_hb_interval;
  3254. sp->pathmaxrxt = sctp_max_retrans_path;
  3255. sp->pathmtu = 0; // allow default discovery
  3256. sp->sackdelay = sctp_sack_timeout;
  3257. sp->sackfreq = 2;
  3258. sp->param_flags = SPP_HB_ENABLE |
  3259. SPP_PMTUD_ENABLE |
  3260. SPP_SACKDELAY_ENABLE;
  3261. /* If enabled no SCTP message fragmentation will be performed.
  3262. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  3263. */
  3264. sp->disable_fragments = 0;
  3265. /* Enable Nagle algorithm by default. */
  3266. sp->nodelay = 0;
  3267. /* Enable by default. */
  3268. sp->v4mapped = 1;
  3269. /* Auto-close idle associations after the configured
  3270. * number of seconds. A value of 0 disables this
  3271. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  3272. * for UDP-style sockets only.
  3273. */
  3274. sp->autoclose = 0;
  3275. /* User specified fragmentation limit. */
  3276. sp->user_frag = 0;
  3277. sp->adaptation_ind = 0;
  3278. sp->pf = sctp_get_pf_specific(sk->sk_family);
  3279. /* Control variables for partial data delivery. */
  3280. atomic_set(&sp->pd_mode, 0);
  3281. skb_queue_head_init(&sp->pd_lobby);
  3282. sp->frag_interleave = 0;
  3283. /* Create a per socket endpoint structure. Even if we
  3284. * change the data structure relationships, this may still
  3285. * be useful for storing pre-connect address information.
  3286. */
  3287. ep = sctp_endpoint_new(sk, GFP_KERNEL);
  3288. if (!ep)
  3289. return -ENOMEM;
  3290. sp->ep = ep;
  3291. sp->hmac = NULL;
  3292. SCTP_DBG_OBJCNT_INC(sock);
  3293. local_bh_disable();
  3294. percpu_counter_inc(&sctp_sockets_allocated);
  3295. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  3296. local_bh_enable();
  3297. return 0;
  3298. }
  3299. /* Cleanup any SCTP per socket resources. */
  3300. SCTP_STATIC void sctp_destroy_sock(struct sock *sk)
  3301. {
  3302. struct sctp_endpoint *ep;
  3303. SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
  3304. /* Release our hold on the endpoint. */
  3305. ep = sctp_sk(sk)->ep;
  3306. sctp_endpoint_free(ep);
  3307. local_bh_disable();
  3308. percpu_counter_dec(&sctp_sockets_allocated);
  3309. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  3310. local_bh_enable();
  3311. }
  3312. /* API 4.1.7 shutdown() - TCP Style Syntax
  3313. * int shutdown(int socket, int how);
  3314. *
  3315. * sd - the socket descriptor of the association to be closed.
  3316. * how - Specifies the type of shutdown. The values are
  3317. * as follows:
  3318. * SHUT_RD
  3319. * Disables further receive operations. No SCTP
  3320. * protocol action is taken.
  3321. * SHUT_WR
  3322. * Disables further send operations, and initiates
  3323. * the SCTP shutdown sequence.
  3324. * SHUT_RDWR
  3325. * Disables further send and receive operations
  3326. * and initiates the SCTP shutdown sequence.
  3327. */
  3328. SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
  3329. {
  3330. struct sctp_endpoint *ep;
  3331. struct sctp_association *asoc;
  3332. if (!sctp_style(sk, TCP))
  3333. return;
  3334. if (how & SEND_SHUTDOWN) {
  3335. ep = sctp_sk(sk)->ep;
  3336. if (!list_empty(&ep->asocs)) {
  3337. asoc = list_entry(ep->asocs.next,
  3338. struct sctp_association, asocs);
  3339. sctp_primitive_SHUTDOWN(asoc, NULL);
  3340. }
  3341. }
  3342. }
  3343. /* 7.2.1 Association Status (SCTP_STATUS)
  3344. * Applications can retrieve current status information about an
  3345. * association, including association state, peer receiver window size,
  3346. * number of unacked data chunks, and number of data chunks pending
  3347. * receipt. This information is read-only.
  3348. */
  3349. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  3350. char __user *optval,
  3351. int __user *optlen)
  3352. {
  3353. struct sctp_status status;
  3354. struct sctp_association *asoc = NULL;
  3355. struct sctp_transport *transport;
  3356. sctp_assoc_t associd;
  3357. int retval = 0;
  3358. if (len < sizeof(status)) {
  3359. retval = -EINVAL;
  3360. goto out;
  3361. }
  3362. len = sizeof(status);
  3363. if (copy_from_user(&status, optval, len)) {
  3364. retval = -EFAULT;
  3365. goto out;
  3366. }
  3367. associd = status.sstat_assoc_id;
  3368. asoc = sctp_id2assoc(sk, associd);
  3369. if (!asoc) {
  3370. retval = -EINVAL;
  3371. goto out;
  3372. }
  3373. transport = asoc->peer.primary_path;
  3374. status.sstat_assoc_id = sctp_assoc2id(asoc);
  3375. status.sstat_state = asoc->state;
  3376. status.sstat_rwnd = asoc->peer.rwnd;
  3377. status.sstat_unackdata = asoc->unack_data;
  3378. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  3379. status.sstat_instrms = asoc->c.sinit_max_instreams;
  3380. status.sstat_outstrms = asoc->c.sinit_num_ostreams;
  3381. status.sstat_fragmentation_point = asoc->frag_point;
  3382. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  3383. memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
  3384. transport->af_specific->sockaddr_len);
  3385. /* Map ipv4 address into v4-mapped-on-v6 address. */
  3386. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  3387. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  3388. status.sstat_primary.spinfo_state = transport->state;
  3389. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  3390. status.sstat_primary.spinfo_srtt = transport->srtt;
  3391. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  3392. status.sstat_primary.spinfo_mtu = transport->pathmtu;
  3393. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  3394. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  3395. if (put_user(len, optlen)) {
  3396. retval = -EFAULT;
  3397. goto out;
  3398. }
  3399. SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
  3400. len, status.sstat_state, status.sstat_rwnd,
  3401. status.sstat_assoc_id);
  3402. if (copy_to_user(optval, &status, len)) {
  3403. retval = -EFAULT;
  3404. goto out;
  3405. }
  3406. out:
  3407. return retval;
  3408. }
  3409. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  3410. *
  3411. * Applications can retrieve information about a specific peer address
  3412. * of an association, including its reachability state, congestion
  3413. * window, and retransmission timer values. This information is
  3414. * read-only.
  3415. */
  3416. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  3417. char __user *optval,
  3418. int __user *optlen)
  3419. {
  3420. struct sctp_paddrinfo pinfo;
  3421. struct sctp_transport *transport;
  3422. int retval = 0;
  3423. if (len < sizeof(pinfo)) {
  3424. retval = -EINVAL;
  3425. goto out;
  3426. }
  3427. len = sizeof(pinfo);
  3428. if (copy_from_user(&pinfo, optval, len)) {
  3429. retval = -EFAULT;
  3430. goto out;
  3431. }
  3432. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  3433. pinfo.spinfo_assoc_id);
  3434. if (!transport)
  3435. return -EINVAL;
  3436. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  3437. pinfo.spinfo_state = transport->state;
  3438. pinfo.spinfo_cwnd = transport->cwnd;
  3439. pinfo.spinfo_srtt = transport->srtt;
  3440. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  3441. pinfo.spinfo_mtu = transport->pathmtu;
  3442. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  3443. pinfo.spinfo_state = SCTP_ACTIVE;
  3444. if (put_user(len, optlen)) {
  3445. retval = -EFAULT;
  3446. goto out;
  3447. }
  3448. if (copy_to_user(optval, &pinfo, len)) {
  3449. retval = -EFAULT;
  3450. goto out;
  3451. }
  3452. out:
  3453. return retval;
  3454. }
  3455. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  3456. *
  3457. * This option is a on/off flag. If enabled no SCTP message
  3458. * fragmentation will be performed. Instead if a message being sent
  3459. * exceeds the current PMTU size, the message will NOT be sent and
  3460. * instead a error will be indicated to the user.
  3461. */
  3462. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  3463. char __user *optval, int __user *optlen)
  3464. {
  3465. int val;
  3466. if (len < sizeof(int))
  3467. return -EINVAL;
  3468. len = sizeof(int);
  3469. val = (sctp_sk(sk)->disable_fragments == 1);
  3470. if (put_user(len, optlen))
  3471. return -EFAULT;
  3472. if (copy_to_user(optval, &val, len))
  3473. return -EFAULT;
  3474. return 0;
  3475. }
  3476. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  3477. *
  3478. * This socket option is used to specify various notifications and
  3479. * ancillary data the user wishes to receive.
  3480. */
  3481. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  3482. int __user *optlen)
  3483. {
  3484. if (len < sizeof(struct sctp_event_subscribe))
  3485. return -EINVAL;
  3486. len = sizeof(struct sctp_event_subscribe);
  3487. if (put_user(len, optlen))
  3488. return -EFAULT;
  3489. if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
  3490. return -EFAULT;
  3491. return 0;
  3492. }
  3493. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  3494. *
  3495. * This socket option is applicable to the UDP-style socket only. When
  3496. * set it will cause associations that are idle for more than the
  3497. * specified number of seconds to automatically close. An association
  3498. * being idle is defined an association that has NOT sent or received
  3499. * user data. The special value of '0' indicates that no automatic
  3500. * close of any associations should be performed. The option expects an
  3501. * integer defining the number of seconds of idle time before an
  3502. * association is closed.
  3503. */
  3504. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  3505. {
  3506. /* Applicable to UDP-style socket only */
  3507. if (sctp_style(sk, TCP))
  3508. return -EOPNOTSUPP;
  3509. if (len < sizeof(int))
  3510. return -EINVAL;
  3511. len = sizeof(int);
  3512. if (put_user(len, optlen))
  3513. return -EFAULT;
  3514. if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
  3515. return -EFAULT;
  3516. return 0;
  3517. }
  3518. /* Helper routine to branch off an association to a new socket. */
  3519. SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
  3520. struct socket **sockp)
  3521. {
  3522. struct sock *sk = asoc->base.sk;
  3523. struct socket *sock;
  3524. struct sctp_af *af;
  3525. int err = 0;
  3526. /* An association cannot be branched off from an already peeled-off
  3527. * socket, nor is this supported for tcp style sockets.
  3528. */
  3529. if (!sctp_style(sk, UDP))
  3530. return -EINVAL;
  3531. /* Create a new socket. */
  3532. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  3533. if (err < 0)
  3534. return err;
  3535. sctp_copy_sock(sock->sk, sk, asoc);
  3536. /* Make peeled-off sockets more like 1-1 accepted sockets.
  3537. * Set the daddr and initialize id to something more random
  3538. */
  3539. af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
  3540. af->to_sk_daddr(&asoc->peer.primary_addr, sk);
  3541. /* Populate the fields of the newsk from the oldsk and migrate the
  3542. * asoc to the newsk.
  3543. */
  3544. sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  3545. *sockp = sock;
  3546. return err;
  3547. }
  3548. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  3549. {
  3550. sctp_peeloff_arg_t peeloff;
  3551. struct socket *newsock;
  3552. int retval = 0;
  3553. struct sctp_association *asoc;
  3554. if (len < sizeof(sctp_peeloff_arg_t))
  3555. return -EINVAL;
  3556. len = sizeof(sctp_peeloff_arg_t);
  3557. if (copy_from_user(&peeloff, optval, len))
  3558. return -EFAULT;
  3559. asoc = sctp_id2assoc(sk, peeloff.associd);
  3560. if (!asoc) {
  3561. retval = -EINVAL;
  3562. goto out;
  3563. }
  3564. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __func__, sk, asoc);
  3565. retval = sctp_do_peeloff(asoc, &newsock);
  3566. if (retval < 0)
  3567. goto out;
  3568. /* Map the socket to an unused fd that can be returned to the user. */
  3569. retval = sock_map_fd(newsock, 0);
  3570. if (retval < 0) {
  3571. sock_release(newsock);
  3572. goto out;
  3573. }
  3574. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
  3575. __func__, sk, asoc, newsock->sk, retval);
  3576. /* Return the fd mapped to the new socket. */
  3577. peeloff.sd = retval;
  3578. if (put_user(len, optlen))
  3579. return -EFAULT;
  3580. if (copy_to_user(optval, &peeloff, len))
  3581. retval = -EFAULT;
  3582. out:
  3583. return retval;
  3584. }
  3585. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  3586. *
  3587. * Applications can enable or disable heartbeats for any peer address of
  3588. * an association, modify an address's heartbeat interval, force a
  3589. * heartbeat to be sent immediately, and adjust the address's maximum
  3590. * number of retransmissions sent before an address is considered
  3591. * unreachable. The following structure is used to access and modify an
  3592. * address's parameters:
  3593. *
  3594. * struct sctp_paddrparams {
  3595. * sctp_assoc_t spp_assoc_id;
  3596. * struct sockaddr_storage spp_address;
  3597. * uint32_t spp_hbinterval;
  3598. * uint16_t spp_pathmaxrxt;
  3599. * uint32_t spp_pathmtu;
  3600. * uint32_t spp_sackdelay;
  3601. * uint32_t spp_flags;
  3602. * };
  3603. *
  3604. * spp_assoc_id - (one-to-many style socket) This is filled in the
  3605. * application, and identifies the association for
  3606. * this query.
  3607. * spp_address - This specifies which address is of interest.
  3608. * spp_hbinterval - This contains the value of the heartbeat interval,
  3609. * in milliseconds. If a value of zero
  3610. * is present in this field then no changes are to
  3611. * be made to this parameter.
  3612. * spp_pathmaxrxt - This contains the maximum number of
  3613. * retransmissions before this address shall be
  3614. * considered unreachable. If a value of zero
  3615. * is present in this field then no changes are to
  3616. * be made to this parameter.
  3617. * spp_pathmtu - When Path MTU discovery is disabled the value
  3618. * specified here will be the "fixed" path mtu.
  3619. * Note that if the spp_address field is empty
  3620. * then all associations on this address will
  3621. * have this fixed path mtu set upon them.
  3622. *
  3623. * spp_sackdelay - When delayed sack is enabled, this value specifies
  3624. * the number of milliseconds that sacks will be delayed
  3625. * for. This value will apply to all addresses of an
  3626. * association if the spp_address field is empty. Note
  3627. * also, that if delayed sack is enabled and this
  3628. * value is set to 0, no change is made to the last
  3629. * recorded delayed sack timer value.
  3630. *
  3631. * spp_flags - These flags are used to control various features
  3632. * on an association. The flag field may contain
  3633. * zero or more of the following options.
  3634. *
  3635. * SPP_HB_ENABLE - Enable heartbeats on the
  3636. * specified address. Note that if the address
  3637. * field is empty all addresses for the association
  3638. * have heartbeats enabled upon them.
  3639. *
  3640. * SPP_HB_DISABLE - Disable heartbeats on the
  3641. * speicifed address. Note that if the address
  3642. * field is empty all addresses for the association
  3643. * will have their heartbeats disabled. Note also
  3644. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  3645. * mutually exclusive, only one of these two should
  3646. * be specified. Enabling both fields will have
  3647. * undetermined results.
  3648. *
  3649. * SPP_HB_DEMAND - Request a user initiated heartbeat
  3650. * to be made immediately.
  3651. *
  3652. * SPP_PMTUD_ENABLE - This field will enable PMTU
  3653. * discovery upon the specified address. Note that
  3654. * if the address feild is empty then all addresses
  3655. * on the association are effected.
  3656. *
  3657. * SPP_PMTUD_DISABLE - This field will disable PMTU
  3658. * discovery upon the specified address. Note that
  3659. * if the address feild is empty then all addresses
  3660. * on the association are effected. Not also that
  3661. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  3662. * exclusive. Enabling both will have undetermined
  3663. * results.
  3664. *
  3665. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  3666. * on delayed sack. The time specified in spp_sackdelay
  3667. * is used to specify the sack delay for this address. Note
  3668. * that if spp_address is empty then all addresses will
  3669. * enable delayed sack and take on the sack delay
  3670. * value specified in spp_sackdelay.
  3671. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  3672. * off delayed sack. If the spp_address field is blank then
  3673. * delayed sack is disabled for the entire association. Note
  3674. * also that this field is mutually exclusive to
  3675. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  3676. * results.
  3677. */
  3678. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  3679. char __user *optval, int __user *optlen)
  3680. {
  3681. struct sctp_paddrparams params;
  3682. struct sctp_transport *trans = NULL;
  3683. struct sctp_association *asoc = NULL;
  3684. struct sctp_sock *sp = sctp_sk(sk);
  3685. if (len < sizeof(struct sctp_paddrparams))
  3686. return -EINVAL;
  3687. len = sizeof(struct sctp_paddrparams);
  3688. if (copy_from_user(&params, optval, len))
  3689. return -EFAULT;
  3690. /* If an address other than INADDR_ANY is specified, and
  3691. * no transport is found, then the request is invalid.
  3692. */
  3693. if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
  3694. trans = sctp_addr_id2transport(sk, &params.spp_address,
  3695. params.spp_assoc_id);
  3696. if (!trans) {
  3697. SCTP_DEBUG_PRINTK("Failed no transport\n");
  3698. return -EINVAL;
  3699. }
  3700. }
  3701. /* Get association, if assoc_id != 0 and the socket is a one
  3702. * to many style socket, and an association was not found, then
  3703. * the id was invalid.
  3704. */
  3705. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  3706. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
  3707. SCTP_DEBUG_PRINTK("Failed no association\n");
  3708. return -EINVAL;
  3709. }
  3710. if (trans) {
  3711. /* Fetch transport values. */
  3712. params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
  3713. params.spp_pathmtu = trans->pathmtu;
  3714. params.spp_pathmaxrxt = trans->pathmaxrxt;
  3715. params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
  3716. /*draft-11 doesn't say what to return in spp_flags*/
  3717. params.spp_flags = trans->param_flags;
  3718. } else if (asoc) {
  3719. /* Fetch association values. */
  3720. params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
  3721. params.spp_pathmtu = asoc->pathmtu;
  3722. params.spp_pathmaxrxt = asoc->pathmaxrxt;
  3723. params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
  3724. /*draft-11 doesn't say what to return in spp_flags*/
  3725. params.spp_flags = asoc->param_flags;
  3726. } else {
  3727. /* Fetch socket values. */
  3728. params.spp_hbinterval = sp->hbinterval;
  3729. params.spp_pathmtu = sp->pathmtu;
  3730. params.spp_sackdelay = sp->sackdelay;
  3731. params.spp_pathmaxrxt = sp->pathmaxrxt;
  3732. /*draft-11 doesn't say what to return in spp_flags*/
  3733. params.spp_flags = sp->param_flags;
  3734. }
  3735. if (copy_to_user(optval, &params, len))
  3736. return -EFAULT;
  3737. if (put_user(len, optlen))
  3738. return -EFAULT;
  3739. return 0;
  3740. }
  3741. /*
  3742. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  3743. *
  3744. * This option will effect the way delayed acks are performed. This
  3745. * option allows you to get or set the delayed ack time, in
  3746. * milliseconds. It also allows changing the delayed ack frequency.
  3747. * Changing the frequency to 1 disables the delayed sack algorithm. If
  3748. * the assoc_id is 0, then this sets or gets the endpoints default
  3749. * values. If the assoc_id field is non-zero, then the set or get
  3750. * effects the specified association for the one to many model (the
  3751. * assoc_id field is ignored by the one to one model). Note that if
  3752. * sack_delay or sack_freq are 0 when setting this option, then the
  3753. * current values will remain unchanged.
  3754. *
  3755. * struct sctp_sack_info {
  3756. * sctp_assoc_t sack_assoc_id;
  3757. * uint32_t sack_delay;
  3758. * uint32_t sack_freq;
  3759. * };
  3760. *
  3761. * sack_assoc_id - This parameter, indicates which association the user
  3762. * is performing an action upon. Note that if this field's value is
  3763. * zero then the endpoints default value is changed (effecting future
  3764. * associations only).
  3765. *
  3766. * sack_delay - This parameter contains the number of milliseconds that
  3767. * the user is requesting the delayed ACK timer be set to. Note that
  3768. * this value is defined in the standard to be between 200 and 500
  3769. * milliseconds.
  3770. *
  3771. * sack_freq - This parameter contains the number of packets that must
  3772. * be received before a sack is sent without waiting for the delay
  3773. * timer to expire. The default value for this is 2, setting this
  3774. * value to 1 will disable the delayed sack algorithm.
  3775. */
  3776. static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
  3777. char __user *optval,
  3778. int __user *optlen)
  3779. {
  3780. struct sctp_sack_info params;
  3781. struct sctp_association *asoc = NULL;
  3782. struct sctp_sock *sp = sctp_sk(sk);
  3783. if (len >= sizeof(struct sctp_sack_info)) {
  3784. len = sizeof(struct sctp_sack_info);
  3785. if (copy_from_user(&params, optval, len))
  3786. return -EFAULT;
  3787. } else if (len == sizeof(struct sctp_assoc_value)) {
  3788. pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
  3789. pr_warn("Use struct sctp_sack_info instead\n");
  3790. if (copy_from_user(&params, optval, len))
  3791. return -EFAULT;
  3792. } else
  3793. return - EINVAL;
  3794. /* Get association, if sack_assoc_id != 0 and the socket is a one
  3795. * to many style socket, and an association was not found, then
  3796. * the id was invalid.
  3797. */
  3798. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  3799. if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
  3800. return -EINVAL;
  3801. if (asoc) {
  3802. /* Fetch association values. */
  3803. if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
  3804. params.sack_delay = jiffies_to_msecs(
  3805. asoc->sackdelay);
  3806. params.sack_freq = asoc->sackfreq;
  3807. } else {
  3808. params.sack_delay = 0;
  3809. params.sack_freq = 1;
  3810. }
  3811. } else {
  3812. /* Fetch socket values. */
  3813. if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
  3814. params.sack_delay = sp->sackdelay;
  3815. params.sack_freq = sp->sackfreq;
  3816. } else {
  3817. params.sack_delay = 0;
  3818. params.sack_freq = 1;
  3819. }
  3820. }
  3821. if (copy_to_user(optval, &params, len))
  3822. return -EFAULT;
  3823. if (put_user(len, optlen))
  3824. return -EFAULT;
  3825. return 0;
  3826. }
  3827. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  3828. *
  3829. * Applications can specify protocol parameters for the default association
  3830. * initialization. The option name argument to setsockopt() and getsockopt()
  3831. * is SCTP_INITMSG.
  3832. *
  3833. * Setting initialization parameters is effective only on an unconnected
  3834. * socket (for UDP-style sockets only future associations are effected
  3835. * by the change). With TCP-style sockets, this option is inherited by
  3836. * sockets derived from a listener socket.
  3837. */
  3838. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  3839. {
  3840. if (len < sizeof(struct sctp_initmsg))
  3841. return -EINVAL;
  3842. len = sizeof(struct sctp_initmsg);
  3843. if (put_user(len, optlen))
  3844. return -EFAULT;
  3845. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  3846. return -EFAULT;
  3847. return 0;
  3848. }
  3849. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  3850. char __user *optval, int __user *optlen)
  3851. {
  3852. struct sctp_association *asoc;
  3853. int cnt = 0;
  3854. struct sctp_getaddrs getaddrs;
  3855. struct sctp_transport *from;
  3856. void __user *to;
  3857. union sctp_addr temp;
  3858. struct sctp_sock *sp = sctp_sk(sk);
  3859. int addrlen;
  3860. size_t space_left;
  3861. int bytes_copied;
  3862. if (len < sizeof(struct sctp_getaddrs))
  3863. return -EINVAL;
  3864. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  3865. return -EFAULT;
  3866. /* For UDP-style sockets, id specifies the association to query. */
  3867. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3868. if (!asoc)
  3869. return -EINVAL;
  3870. to = optval + offsetof(struct sctp_getaddrs,addrs);
  3871. space_left = len - offsetof(struct sctp_getaddrs,addrs);
  3872. list_for_each_entry(from, &asoc->peer.transport_addr_list,
  3873. transports) {
  3874. memcpy(&temp, &from->ipaddr, sizeof(temp));
  3875. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3876. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3877. if (space_left < addrlen)
  3878. return -ENOMEM;
  3879. if (copy_to_user(to, &temp, addrlen))
  3880. return -EFAULT;
  3881. to += addrlen;
  3882. cnt++;
  3883. space_left -= addrlen;
  3884. }
  3885. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  3886. return -EFAULT;
  3887. bytes_copied = ((char __user *)to) - optval;
  3888. if (put_user(bytes_copied, optlen))
  3889. return -EFAULT;
  3890. return 0;
  3891. }
  3892. static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
  3893. size_t space_left, int *bytes_copied)
  3894. {
  3895. struct sctp_sockaddr_entry *addr;
  3896. union sctp_addr temp;
  3897. int cnt = 0;
  3898. int addrlen;
  3899. rcu_read_lock();
  3900. list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
  3901. if (!addr->valid)
  3902. continue;
  3903. if ((PF_INET == sk->sk_family) &&
  3904. (AF_INET6 == addr->a.sa.sa_family))
  3905. continue;
  3906. if ((PF_INET6 == sk->sk_family) &&
  3907. inet_v6_ipv6only(sk) &&
  3908. (AF_INET == addr->a.sa.sa_family))
  3909. continue;
  3910. memcpy(&temp, &addr->a, sizeof(temp));
  3911. if (!temp.v4.sin_port)
  3912. temp.v4.sin_port = htons(port);
  3913. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  3914. &temp);
  3915. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3916. if (space_left < addrlen) {
  3917. cnt = -ENOMEM;
  3918. break;
  3919. }
  3920. memcpy(to, &temp, addrlen);
  3921. to += addrlen;
  3922. cnt ++;
  3923. space_left -= addrlen;
  3924. *bytes_copied += addrlen;
  3925. }
  3926. rcu_read_unlock();
  3927. return cnt;
  3928. }
  3929. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  3930. char __user *optval, int __user *optlen)
  3931. {
  3932. struct sctp_bind_addr *bp;
  3933. struct sctp_association *asoc;
  3934. int cnt = 0;
  3935. struct sctp_getaddrs getaddrs;
  3936. struct sctp_sockaddr_entry *addr;
  3937. void __user *to;
  3938. union sctp_addr temp;
  3939. struct sctp_sock *sp = sctp_sk(sk);
  3940. int addrlen;
  3941. int err = 0;
  3942. size_t space_left;
  3943. int bytes_copied = 0;
  3944. void *addrs;
  3945. void *buf;
  3946. if (len < sizeof(struct sctp_getaddrs))
  3947. return -EINVAL;
  3948. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  3949. return -EFAULT;
  3950. /*
  3951. * For UDP-style sockets, id specifies the association to query.
  3952. * If the id field is set to the value '0' then the locally bound
  3953. * addresses are returned without regard to any particular
  3954. * association.
  3955. */
  3956. if (0 == getaddrs.assoc_id) {
  3957. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3958. } else {
  3959. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3960. if (!asoc)
  3961. return -EINVAL;
  3962. bp = &asoc->base.bind_addr;
  3963. }
  3964. to = optval + offsetof(struct sctp_getaddrs,addrs);
  3965. space_left = len - offsetof(struct sctp_getaddrs,addrs);
  3966. addrs = kmalloc(space_left, GFP_KERNEL);
  3967. if (!addrs)
  3968. return -ENOMEM;
  3969. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  3970. * addresses from the global local address list.
  3971. */
  3972. if (sctp_list_single_entry(&bp->address_list)) {
  3973. addr = list_entry(bp->address_list.next,
  3974. struct sctp_sockaddr_entry, list);
  3975. if (sctp_is_any(sk, &addr->a)) {
  3976. cnt = sctp_copy_laddrs(sk, bp->port, addrs,
  3977. space_left, &bytes_copied);
  3978. if (cnt < 0) {
  3979. err = cnt;
  3980. goto out;
  3981. }
  3982. goto copy_getaddrs;
  3983. }
  3984. }
  3985. buf = addrs;
  3986. /* Protection on the bound address list is not needed since
  3987. * in the socket option context we hold a socket lock and
  3988. * thus the bound address list can't change.
  3989. */
  3990. list_for_each_entry(addr, &bp->address_list, list) {
  3991. memcpy(&temp, &addr->a, sizeof(temp));
  3992. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3993. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3994. if (space_left < addrlen) {
  3995. err = -ENOMEM; /*fixme: right error?*/
  3996. goto out;
  3997. }
  3998. memcpy(buf, &temp, addrlen);
  3999. buf += addrlen;
  4000. bytes_copied += addrlen;
  4001. cnt ++;
  4002. space_left -= addrlen;
  4003. }
  4004. copy_getaddrs:
  4005. if (copy_to_user(to, addrs, bytes_copied)) {
  4006. err = -EFAULT;
  4007. goto out;
  4008. }
  4009. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
  4010. err = -EFAULT;
  4011. goto out;
  4012. }
  4013. if (put_user(bytes_copied, optlen))
  4014. err = -EFAULT;
  4015. out:
  4016. kfree(addrs);
  4017. return err;
  4018. }
  4019. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  4020. *
  4021. * Requests that the local SCTP stack use the enclosed peer address as
  4022. * the association primary. The enclosed address must be one of the
  4023. * association peer's addresses.
  4024. */
  4025. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  4026. char __user *optval, int __user *optlen)
  4027. {
  4028. struct sctp_prim prim;
  4029. struct sctp_association *asoc;
  4030. struct sctp_sock *sp = sctp_sk(sk);
  4031. if (len < sizeof(struct sctp_prim))
  4032. return -EINVAL;
  4033. len = sizeof(struct sctp_prim);
  4034. if (copy_from_user(&prim, optval, len))
  4035. return -EFAULT;
  4036. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  4037. if (!asoc)
  4038. return -EINVAL;
  4039. if (!asoc->peer.primary_path)
  4040. return -ENOTCONN;
  4041. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  4042. asoc->peer.primary_path->af_specific->sockaddr_len);
  4043. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
  4044. (union sctp_addr *)&prim.ssp_addr);
  4045. if (put_user(len, optlen))
  4046. return -EFAULT;
  4047. if (copy_to_user(optval, &prim, len))
  4048. return -EFAULT;
  4049. return 0;
  4050. }
  4051. /*
  4052. * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
  4053. *
  4054. * Requests that the local endpoint set the specified Adaptation Layer
  4055. * Indication parameter for all future INIT and INIT-ACK exchanges.
  4056. */
  4057. static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
  4058. char __user *optval, int __user *optlen)
  4059. {
  4060. struct sctp_setadaptation adaptation;
  4061. if (len < sizeof(struct sctp_setadaptation))
  4062. return -EINVAL;
  4063. len = sizeof(struct sctp_setadaptation);
  4064. adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
  4065. if (put_user(len, optlen))
  4066. return -EFAULT;
  4067. if (copy_to_user(optval, &adaptation, len))
  4068. return -EFAULT;
  4069. return 0;
  4070. }
  4071. /*
  4072. *
  4073. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  4074. *
  4075. * Applications that wish to use the sendto() system call may wish to
  4076. * specify a default set of parameters that would normally be supplied
  4077. * through the inclusion of ancillary data. This socket option allows
  4078. * such an application to set the default sctp_sndrcvinfo structure.
  4079. * The application that wishes to use this socket option simply passes
  4080. * in to this call the sctp_sndrcvinfo structure defined in Section
  4081. * 5.2.2) The input parameters accepted by this call include
  4082. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  4083. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  4084. * to this call if the caller is using the UDP model.
  4085. *
  4086. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  4087. */
  4088. static int sctp_getsockopt_default_send_param(struct sock *sk,
  4089. int len, char __user *optval,
  4090. int __user *optlen)
  4091. {
  4092. struct sctp_sndrcvinfo info;
  4093. struct sctp_association *asoc;
  4094. struct sctp_sock *sp = sctp_sk(sk);
  4095. if (len < sizeof(struct sctp_sndrcvinfo))
  4096. return -EINVAL;
  4097. len = sizeof(struct sctp_sndrcvinfo);
  4098. if (copy_from_user(&info, optval, len))
  4099. return -EFAULT;
  4100. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  4101. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  4102. return -EINVAL;
  4103. if (asoc) {
  4104. info.sinfo_stream = asoc->default_stream;
  4105. info.sinfo_flags = asoc->default_flags;
  4106. info.sinfo_ppid = asoc->default_ppid;
  4107. info.sinfo_context = asoc->default_context;
  4108. info.sinfo_timetolive = asoc->default_timetolive;
  4109. } else {
  4110. info.sinfo_stream = sp->default_stream;
  4111. info.sinfo_flags = sp->default_flags;
  4112. info.sinfo_ppid = sp->default_ppid;
  4113. info.sinfo_context = sp->default_context;
  4114. info.sinfo_timetolive = sp->default_timetolive;
  4115. }
  4116. if (put_user(len, optlen))
  4117. return -EFAULT;
  4118. if (copy_to_user(optval, &info, len))
  4119. return -EFAULT;
  4120. return 0;
  4121. }
  4122. /*
  4123. *
  4124. * 7.1.5 SCTP_NODELAY
  4125. *
  4126. * Turn on/off any Nagle-like algorithm. This means that packets are
  4127. * generally sent as soon as possible and no unnecessary delays are
  4128. * introduced, at the cost of more packets in the network. Expects an
  4129. * integer boolean flag.
  4130. */
  4131. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  4132. char __user *optval, int __user *optlen)
  4133. {
  4134. int val;
  4135. if (len < sizeof(int))
  4136. return -EINVAL;
  4137. len = sizeof(int);
  4138. val = (sctp_sk(sk)->nodelay == 1);
  4139. if (put_user(len, optlen))
  4140. return -EFAULT;
  4141. if (copy_to_user(optval, &val, len))
  4142. return -EFAULT;
  4143. return 0;
  4144. }
  4145. /*
  4146. *
  4147. * 7.1.1 SCTP_RTOINFO
  4148. *
  4149. * The protocol parameters used to initialize and bound retransmission
  4150. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  4151. * and modify these parameters.
  4152. * All parameters are time values, in milliseconds. A value of 0, when
  4153. * modifying the parameters, indicates that the current value should not
  4154. * be changed.
  4155. *
  4156. */
  4157. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  4158. char __user *optval,
  4159. int __user *optlen) {
  4160. struct sctp_rtoinfo rtoinfo;
  4161. struct sctp_association *asoc;
  4162. if (len < sizeof (struct sctp_rtoinfo))
  4163. return -EINVAL;
  4164. len = sizeof(struct sctp_rtoinfo);
  4165. if (copy_from_user(&rtoinfo, optval, len))
  4166. return -EFAULT;
  4167. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  4168. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  4169. return -EINVAL;
  4170. /* Values corresponding to the specific association. */
  4171. if (asoc) {
  4172. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  4173. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  4174. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  4175. } else {
  4176. /* Values corresponding to the endpoint. */
  4177. struct sctp_sock *sp = sctp_sk(sk);
  4178. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  4179. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  4180. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  4181. }
  4182. if (put_user(len, optlen))
  4183. return -EFAULT;
  4184. if (copy_to_user(optval, &rtoinfo, len))
  4185. return -EFAULT;
  4186. return 0;
  4187. }
  4188. /*
  4189. *
  4190. * 7.1.2 SCTP_ASSOCINFO
  4191. *
  4192. * This option is used to tune the maximum retransmission attempts
  4193. * of the association.
  4194. * Returns an error if the new association retransmission value is
  4195. * greater than the sum of the retransmission value of the peer.
  4196. * See [SCTP] for more information.
  4197. *
  4198. */
  4199. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  4200. char __user *optval,
  4201. int __user *optlen)
  4202. {
  4203. struct sctp_assocparams assocparams;
  4204. struct sctp_association *asoc;
  4205. struct list_head *pos;
  4206. int cnt = 0;
  4207. if (len < sizeof (struct sctp_assocparams))
  4208. return -EINVAL;
  4209. len = sizeof(struct sctp_assocparams);
  4210. if (copy_from_user(&assocparams, optval, len))
  4211. return -EFAULT;
  4212. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  4213. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  4214. return -EINVAL;
  4215. /* Values correspoinding to the specific association */
  4216. if (asoc) {
  4217. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  4218. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  4219. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  4220. assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
  4221. * 1000) +
  4222. (asoc->cookie_life.tv_usec
  4223. / 1000);
  4224. list_for_each(pos, &asoc->peer.transport_addr_list) {
  4225. cnt ++;
  4226. }
  4227. assocparams.sasoc_number_peer_destinations = cnt;
  4228. } else {
  4229. /* Values corresponding to the endpoint */
  4230. struct sctp_sock *sp = sctp_sk(sk);
  4231. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  4232. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  4233. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  4234. assocparams.sasoc_cookie_life =
  4235. sp->assocparams.sasoc_cookie_life;
  4236. assocparams.sasoc_number_peer_destinations =
  4237. sp->assocparams.
  4238. sasoc_number_peer_destinations;
  4239. }
  4240. if (put_user(len, optlen))
  4241. return -EFAULT;
  4242. if (copy_to_user(optval, &assocparams, len))
  4243. return -EFAULT;
  4244. return 0;
  4245. }
  4246. /*
  4247. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  4248. *
  4249. * This socket option is a boolean flag which turns on or off mapped V4
  4250. * addresses. If this option is turned on and the socket is type
  4251. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  4252. * If this option is turned off, then no mapping will be done of V4
  4253. * addresses and a user will receive both PF_INET6 and PF_INET type
  4254. * addresses on the socket.
  4255. */
  4256. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  4257. char __user *optval, int __user *optlen)
  4258. {
  4259. int val;
  4260. struct sctp_sock *sp = sctp_sk(sk);
  4261. if (len < sizeof(int))
  4262. return -EINVAL;
  4263. len = sizeof(int);
  4264. val = sp->v4mapped;
  4265. if (put_user(len, optlen))
  4266. return -EFAULT;
  4267. if (copy_to_user(optval, &val, len))
  4268. return -EFAULT;
  4269. return 0;
  4270. }
  4271. /*
  4272. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  4273. * (chapter and verse is quoted at sctp_setsockopt_context())
  4274. */
  4275. static int sctp_getsockopt_context(struct sock *sk, int len,
  4276. char __user *optval, int __user *optlen)
  4277. {
  4278. struct sctp_assoc_value params;
  4279. struct sctp_sock *sp;
  4280. struct sctp_association *asoc;
  4281. if (len < sizeof(struct sctp_assoc_value))
  4282. return -EINVAL;
  4283. len = sizeof(struct sctp_assoc_value);
  4284. if (copy_from_user(&params, optval, len))
  4285. return -EFAULT;
  4286. sp = sctp_sk(sk);
  4287. if (params.assoc_id != 0) {
  4288. asoc = sctp_id2assoc(sk, params.assoc_id);
  4289. if (!asoc)
  4290. return -EINVAL;
  4291. params.assoc_value = asoc->default_rcv_context;
  4292. } else {
  4293. params.assoc_value = sp->default_rcv_context;
  4294. }
  4295. if (put_user(len, optlen))
  4296. return -EFAULT;
  4297. if (copy_to_user(optval, &params, len))
  4298. return -EFAULT;
  4299. return 0;
  4300. }
  4301. /*
  4302. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  4303. * This option will get or set the maximum size to put in any outgoing
  4304. * SCTP DATA chunk. If a message is larger than this size it will be
  4305. * fragmented by SCTP into the specified size. Note that the underlying
  4306. * SCTP implementation may fragment into smaller sized chunks when the
  4307. * PMTU of the underlying association is smaller than the value set by
  4308. * the user. The default value for this option is '0' which indicates
  4309. * the user is NOT limiting fragmentation and only the PMTU will effect
  4310. * SCTP's choice of DATA chunk size. Note also that values set larger
  4311. * than the maximum size of an IP datagram will effectively let SCTP
  4312. * control fragmentation (i.e. the same as setting this option to 0).
  4313. *
  4314. * The following structure is used to access and modify this parameter:
  4315. *
  4316. * struct sctp_assoc_value {
  4317. * sctp_assoc_t assoc_id;
  4318. * uint32_t assoc_value;
  4319. * };
  4320. *
  4321. * assoc_id: This parameter is ignored for one-to-one style sockets.
  4322. * For one-to-many style sockets this parameter indicates which
  4323. * association the user is performing an action upon. Note that if
  4324. * this field's value is zero then the endpoints default value is
  4325. * changed (effecting future associations only).
  4326. * assoc_value: This parameter specifies the maximum size in bytes.
  4327. */
  4328. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  4329. char __user *optval, int __user *optlen)
  4330. {
  4331. struct sctp_assoc_value params;
  4332. struct sctp_association *asoc;
  4333. if (len == sizeof(int)) {
  4334. pr_warn("Use of int in maxseg socket option deprecated\n");
  4335. pr_warn("Use struct sctp_assoc_value instead\n");
  4336. params.assoc_id = 0;
  4337. } else if (len >= sizeof(struct sctp_assoc_value)) {
  4338. len = sizeof(struct sctp_assoc_value);
  4339. if (copy_from_user(&params, optval, sizeof(params)))
  4340. return -EFAULT;
  4341. } else
  4342. return -EINVAL;
  4343. asoc = sctp_id2assoc(sk, params.assoc_id);
  4344. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  4345. return -EINVAL;
  4346. if (asoc)
  4347. params.assoc_value = asoc->frag_point;
  4348. else
  4349. params.assoc_value = sctp_sk(sk)->user_frag;
  4350. if (put_user(len, optlen))
  4351. return -EFAULT;
  4352. if (len == sizeof(int)) {
  4353. if (copy_to_user(optval, &params.assoc_value, len))
  4354. return -EFAULT;
  4355. } else {
  4356. if (copy_to_user(optval, &params, len))
  4357. return -EFAULT;
  4358. }
  4359. return 0;
  4360. }
  4361. /*
  4362. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  4363. * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
  4364. */
  4365. static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
  4366. char __user *optval, int __user *optlen)
  4367. {
  4368. int val;
  4369. if (len < sizeof(int))
  4370. return -EINVAL;
  4371. len = sizeof(int);
  4372. val = sctp_sk(sk)->frag_interleave;
  4373. if (put_user(len, optlen))
  4374. return -EFAULT;
  4375. if (copy_to_user(optval, &val, len))
  4376. return -EFAULT;
  4377. return 0;
  4378. }
  4379. /*
  4380. * 7.1.25. Set or Get the sctp partial delivery point
  4381. * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
  4382. */
  4383. static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
  4384. char __user *optval,
  4385. int __user *optlen)
  4386. {
  4387. u32 val;
  4388. if (len < sizeof(u32))
  4389. return -EINVAL;
  4390. len = sizeof(u32);
  4391. val = sctp_sk(sk)->pd_point;
  4392. if (put_user(len, optlen))
  4393. return -EFAULT;
  4394. if (copy_to_user(optval, &val, len))
  4395. return -EFAULT;
  4396. return 0;
  4397. }
  4398. /*
  4399. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  4400. * (chapter and verse is quoted at sctp_setsockopt_maxburst())
  4401. */
  4402. static int sctp_getsockopt_maxburst(struct sock *sk, int len,
  4403. char __user *optval,
  4404. int __user *optlen)
  4405. {
  4406. struct sctp_assoc_value params;
  4407. struct sctp_sock *sp;
  4408. struct sctp_association *asoc;
  4409. if (len == sizeof(int)) {
  4410. pr_warn("Use of int in max_burst socket option deprecated\n");
  4411. pr_warn("Use struct sctp_assoc_value instead\n");
  4412. params.assoc_id = 0;
  4413. } else if (len >= sizeof(struct sctp_assoc_value)) {
  4414. len = sizeof(struct sctp_assoc_value);
  4415. if (copy_from_user(&params, optval, len))
  4416. return -EFAULT;
  4417. } else
  4418. return -EINVAL;
  4419. sp = sctp_sk(sk);
  4420. if (params.assoc_id != 0) {
  4421. asoc = sctp_id2assoc(sk, params.assoc_id);
  4422. if (!asoc)
  4423. return -EINVAL;
  4424. params.assoc_value = asoc->max_burst;
  4425. } else
  4426. params.assoc_value = sp->max_burst;
  4427. if (len == sizeof(int)) {
  4428. if (copy_to_user(optval, &params.assoc_value, len))
  4429. return -EFAULT;
  4430. } else {
  4431. if (copy_to_user(optval, &params, len))
  4432. return -EFAULT;
  4433. }
  4434. return 0;
  4435. }
  4436. static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
  4437. char __user *optval, int __user *optlen)
  4438. {
  4439. struct sctp_hmacalgo __user *p = (void __user *)optval;
  4440. struct sctp_hmac_algo_param *hmacs;
  4441. __u16 data_len = 0;
  4442. u32 num_idents;
  4443. if (!sctp_auth_enable)
  4444. return -EACCES;
  4445. hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
  4446. data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t);
  4447. if (len < sizeof(struct sctp_hmacalgo) + data_len)
  4448. return -EINVAL;
  4449. len = sizeof(struct sctp_hmacalgo) + data_len;
  4450. num_idents = data_len / sizeof(u16);
  4451. if (put_user(len, optlen))
  4452. return -EFAULT;
  4453. if (put_user(num_idents, &p->shmac_num_idents))
  4454. return -EFAULT;
  4455. if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len))
  4456. return -EFAULT;
  4457. return 0;
  4458. }
  4459. static int sctp_getsockopt_active_key(struct sock *sk, int len,
  4460. char __user *optval, int __user *optlen)
  4461. {
  4462. struct sctp_authkeyid val;
  4463. struct sctp_association *asoc;
  4464. if (!sctp_auth_enable)
  4465. return -EACCES;
  4466. if (len < sizeof(struct sctp_authkeyid))
  4467. return -EINVAL;
  4468. if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
  4469. return -EFAULT;
  4470. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  4471. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  4472. return -EINVAL;
  4473. if (asoc)
  4474. val.scact_keynumber = asoc->active_key_id;
  4475. else
  4476. val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
  4477. len = sizeof(struct sctp_authkeyid);
  4478. if (put_user(len, optlen))
  4479. return -EFAULT;
  4480. if (copy_to_user(optval, &val, len))
  4481. return -EFAULT;
  4482. return 0;
  4483. }
  4484. static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
  4485. char __user *optval, int __user *optlen)
  4486. {
  4487. struct sctp_authchunks __user *p = (void __user *)optval;
  4488. struct sctp_authchunks val;
  4489. struct sctp_association *asoc;
  4490. struct sctp_chunks_param *ch;
  4491. u32 num_chunks = 0;
  4492. char __user *to;
  4493. if (!sctp_auth_enable)
  4494. return -EACCES;
  4495. if (len < sizeof(struct sctp_authchunks))
  4496. return -EINVAL;
  4497. if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
  4498. return -EFAULT;
  4499. to = p->gauth_chunks;
  4500. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  4501. if (!asoc)
  4502. return -EINVAL;
  4503. ch = asoc->peer.peer_chunks;
  4504. if (!ch)
  4505. goto num;
  4506. /* See if the user provided enough room for all the data */
  4507. num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
  4508. if (len < num_chunks)
  4509. return -EINVAL;
  4510. if (copy_to_user(to, ch->chunks, num_chunks))
  4511. return -EFAULT;
  4512. num:
  4513. len = sizeof(struct sctp_authchunks) + num_chunks;
  4514. if (put_user(len, optlen)) return -EFAULT;
  4515. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  4516. return -EFAULT;
  4517. return 0;
  4518. }
  4519. static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
  4520. char __user *optval, int __user *optlen)
  4521. {
  4522. struct sctp_authchunks __user *p = (void __user *)optval;
  4523. struct sctp_authchunks val;
  4524. struct sctp_association *asoc;
  4525. struct sctp_chunks_param *ch;
  4526. u32 num_chunks = 0;
  4527. char __user *to;
  4528. if (!sctp_auth_enable)
  4529. return -EACCES;
  4530. if (len < sizeof(struct sctp_authchunks))
  4531. return -EINVAL;
  4532. if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
  4533. return -EFAULT;
  4534. to = p->gauth_chunks;
  4535. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  4536. if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
  4537. return -EINVAL;
  4538. if (asoc)
  4539. ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
  4540. else
  4541. ch = sctp_sk(sk)->ep->auth_chunk_list;
  4542. if (!ch)
  4543. goto num;
  4544. num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
  4545. if (len < sizeof(struct sctp_authchunks) + num_chunks)
  4546. return -EINVAL;
  4547. if (copy_to_user(to, ch->chunks, num_chunks))
  4548. return -EFAULT;
  4549. num:
  4550. len = sizeof(struct sctp_authchunks) + num_chunks;
  4551. if (put_user(len, optlen))
  4552. return -EFAULT;
  4553. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  4554. return -EFAULT;
  4555. return 0;
  4556. }
  4557. /*
  4558. * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
  4559. * This option gets the current number of associations that are attached
  4560. * to a one-to-many style socket. The option value is an uint32_t.
  4561. */
  4562. static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
  4563. char __user *optval, int __user *optlen)
  4564. {
  4565. struct sctp_sock *sp = sctp_sk(sk);
  4566. struct sctp_association *asoc;
  4567. u32 val = 0;
  4568. if (sctp_style(sk, TCP))
  4569. return -EOPNOTSUPP;
  4570. if (len < sizeof(u32))
  4571. return -EINVAL;
  4572. len = sizeof(u32);
  4573. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  4574. val++;
  4575. }
  4576. if (put_user(len, optlen))
  4577. return -EFAULT;
  4578. if (copy_to_user(optval, &val, len))
  4579. return -EFAULT;
  4580. return 0;
  4581. }
  4582. /*
  4583. * 8.2.6. Get the Current Identifiers of Associations
  4584. * (SCTP_GET_ASSOC_ID_LIST)
  4585. *
  4586. * This option gets the current list of SCTP association identifiers of
  4587. * the SCTP associations handled by a one-to-many style socket.
  4588. */
  4589. static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
  4590. char __user *optval, int __user *optlen)
  4591. {
  4592. struct sctp_sock *sp = sctp_sk(sk);
  4593. struct sctp_association *asoc;
  4594. struct sctp_assoc_ids *ids;
  4595. u32 num = 0;
  4596. if (sctp_style(sk, TCP))
  4597. return -EOPNOTSUPP;
  4598. if (len < sizeof(struct sctp_assoc_ids))
  4599. return -EINVAL;
  4600. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  4601. num++;
  4602. }
  4603. if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
  4604. return -EINVAL;
  4605. len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
  4606. ids = kmalloc(len, GFP_KERNEL);
  4607. if (unlikely(!ids))
  4608. return -ENOMEM;
  4609. ids->gaids_number_of_ids = num;
  4610. num = 0;
  4611. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  4612. ids->gaids_assoc_id[num++] = asoc->assoc_id;
  4613. }
  4614. if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
  4615. kfree(ids);
  4616. return -EFAULT;
  4617. }
  4618. kfree(ids);
  4619. return 0;
  4620. }
  4621. SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
  4622. char __user *optval, int __user *optlen)
  4623. {
  4624. int retval = 0;
  4625. int len;
  4626. SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
  4627. sk, optname);
  4628. /* I can hardly begin to describe how wrong this is. This is
  4629. * so broken as to be worse than useless. The API draft
  4630. * REALLY is NOT helpful here... I am not convinced that the
  4631. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  4632. * are at all well-founded.
  4633. */
  4634. if (level != SOL_SCTP) {
  4635. struct sctp_af *af = sctp_sk(sk)->pf->af;
  4636. retval = af->getsockopt(sk, level, optname, optval, optlen);
  4637. return retval;
  4638. }
  4639. if (get_user(len, optlen))
  4640. return -EFAULT;
  4641. sctp_lock_sock(sk);
  4642. switch (optname) {
  4643. case SCTP_STATUS:
  4644. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  4645. break;
  4646. case SCTP_DISABLE_FRAGMENTS:
  4647. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  4648. optlen);
  4649. break;
  4650. case SCTP_EVENTS:
  4651. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  4652. break;
  4653. case SCTP_AUTOCLOSE:
  4654. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  4655. break;
  4656. case SCTP_SOCKOPT_PEELOFF:
  4657. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  4658. break;
  4659. case SCTP_PEER_ADDR_PARAMS:
  4660. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  4661. optlen);
  4662. break;
  4663. case SCTP_DELAYED_SACK:
  4664. retval = sctp_getsockopt_delayed_ack(sk, len, optval,
  4665. optlen);
  4666. break;
  4667. case SCTP_INITMSG:
  4668. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  4669. break;
  4670. case SCTP_GET_PEER_ADDRS:
  4671. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  4672. optlen);
  4673. break;
  4674. case SCTP_GET_LOCAL_ADDRS:
  4675. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  4676. optlen);
  4677. break;
  4678. case SCTP_SOCKOPT_CONNECTX3:
  4679. retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
  4680. break;
  4681. case SCTP_DEFAULT_SEND_PARAM:
  4682. retval = sctp_getsockopt_default_send_param(sk, len,
  4683. optval, optlen);
  4684. break;
  4685. case SCTP_PRIMARY_ADDR:
  4686. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  4687. break;
  4688. case SCTP_NODELAY:
  4689. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  4690. break;
  4691. case SCTP_RTOINFO:
  4692. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  4693. break;
  4694. case SCTP_ASSOCINFO:
  4695. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  4696. break;
  4697. case SCTP_I_WANT_MAPPED_V4_ADDR:
  4698. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  4699. break;
  4700. case SCTP_MAXSEG:
  4701. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  4702. break;
  4703. case SCTP_GET_PEER_ADDR_INFO:
  4704. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  4705. optlen);
  4706. break;
  4707. case SCTP_ADAPTATION_LAYER:
  4708. retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
  4709. optlen);
  4710. break;
  4711. case SCTP_CONTEXT:
  4712. retval = sctp_getsockopt_context(sk, len, optval, optlen);
  4713. break;
  4714. case SCTP_FRAGMENT_INTERLEAVE:
  4715. retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
  4716. optlen);
  4717. break;
  4718. case SCTP_PARTIAL_DELIVERY_POINT:
  4719. retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
  4720. optlen);
  4721. break;
  4722. case SCTP_MAX_BURST:
  4723. retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
  4724. break;
  4725. case SCTP_AUTH_KEY:
  4726. case SCTP_AUTH_CHUNK:
  4727. case SCTP_AUTH_DELETE_KEY:
  4728. retval = -EOPNOTSUPP;
  4729. break;
  4730. case SCTP_HMAC_IDENT:
  4731. retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
  4732. break;
  4733. case SCTP_AUTH_ACTIVE_KEY:
  4734. retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
  4735. break;
  4736. case SCTP_PEER_AUTH_CHUNKS:
  4737. retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
  4738. optlen);
  4739. break;
  4740. case SCTP_LOCAL_AUTH_CHUNKS:
  4741. retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
  4742. optlen);
  4743. break;
  4744. case SCTP_GET_ASSOC_NUMBER:
  4745. retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
  4746. break;
  4747. case SCTP_GET_ASSOC_ID_LIST:
  4748. retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
  4749. break;
  4750. default:
  4751. retval = -ENOPROTOOPT;
  4752. break;
  4753. }
  4754. sctp_release_sock(sk);
  4755. return retval;
  4756. }
  4757. static void sctp_hash(struct sock *sk)
  4758. {
  4759. /* STUB */
  4760. }
  4761. static void sctp_unhash(struct sock *sk)
  4762. {
  4763. /* STUB */
  4764. }
  4765. /* Check if port is acceptable. Possibly find first available port.
  4766. *
  4767. * The port hash table (contained in the 'global' SCTP protocol storage
  4768. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  4769. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  4770. * list (the list number is the port number hashed out, so as you
  4771. * would expect from a hash function, all the ports in a given list have
  4772. * such a number that hashes out to the same list number; you were
  4773. * expecting that, right?); so each list has a set of ports, with a
  4774. * link to the socket (struct sock) that uses it, the port number and
  4775. * a fastreuse flag (FIXME: NPI ipg).
  4776. */
  4777. static struct sctp_bind_bucket *sctp_bucket_create(
  4778. struct sctp_bind_hashbucket *head, unsigned short snum);
  4779. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  4780. {
  4781. struct sctp_bind_hashbucket *head; /* hash list */
  4782. struct sctp_bind_bucket *pp; /* hash list port iterator */
  4783. struct hlist_node *node;
  4784. unsigned short snum;
  4785. int ret;
  4786. snum = ntohs(addr->v4.sin_port);
  4787. SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
  4788. sctp_local_bh_disable();
  4789. if (snum == 0) {
  4790. /* Search for an available port. */
  4791. int low, high, remaining, index;
  4792. unsigned int rover;
  4793. inet_get_local_port_range(&low, &high);
  4794. remaining = (high - low) + 1;
  4795. rover = net_random() % remaining + low;
  4796. do {
  4797. rover++;
  4798. if ((rover < low) || (rover > high))
  4799. rover = low;
  4800. if (inet_is_reserved_local_port(rover))
  4801. continue;
  4802. index = sctp_phashfn(rover);
  4803. head = &sctp_port_hashtable[index];
  4804. sctp_spin_lock(&head->lock);
  4805. sctp_for_each_hentry(pp, node, &head->chain)
  4806. if (pp->port == rover)
  4807. goto next;
  4808. break;
  4809. next:
  4810. sctp_spin_unlock(&head->lock);
  4811. } while (--remaining > 0);
  4812. /* Exhausted local port range during search? */
  4813. ret = 1;
  4814. if (remaining <= 0)
  4815. goto fail;
  4816. /* OK, here is the one we will use. HEAD (the port
  4817. * hash table list entry) is non-NULL and we hold it's
  4818. * mutex.
  4819. */
  4820. snum = rover;
  4821. } else {
  4822. /* We are given an specific port number; we verify
  4823. * that it is not being used. If it is used, we will
  4824. * exahust the search in the hash list corresponding
  4825. * to the port number (snum) - we detect that with the
  4826. * port iterator, pp being NULL.
  4827. */
  4828. head = &sctp_port_hashtable[sctp_phashfn(snum)];
  4829. sctp_spin_lock(&head->lock);
  4830. sctp_for_each_hentry(pp, node, &head->chain) {
  4831. if (pp->port == snum)
  4832. goto pp_found;
  4833. }
  4834. }
  4835. pp = NULL;
  4836. goto pp_not_found;
  4837. pp_found:
  4838. if (!hlist_empty(&pp->owner)) {
  4839. /* We had a port hash table hit - there is an
  4840. * available port (pp != NULL) and it is being
  4841. * used by other socket (pp->owner not empty); that other
  4842. * socket is going to be sk2.
  4843. */
  4844. int reuse = sk->sk_reuse;
  4845. struct sock *sk2;
  4846. SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
  4847. if (pp->fastreuse && sk->sk_reuse &&
  4848. sk->sk_state != SCTP_SS_LISTENING)
  4849. goto success;
  4850. /* Run through the list of sockets bound to the port
  4851. * (pp->port) [via the pointers bind_next and
  4852. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  4853. * we get the endpoint they describe and run through
  4854. * the endpoint's list of IP (v4 or v6) addresses,
  4855. * comparing each of the addresses with the address of
  4856. * the socket sk. If we find a match, then that means
  4857. * that this port/socket (sk) combination are already
  4858. * in an endpoint.
  4859. */
  4860. sk_for_each_bound(sk2, node, &pp->owner) {
  4861. struct sctp_endpoint *ep2;
  4862. ep2 = sctp_sk(sk2)->ep;
  4863. if (sk == sk2 ||
  4864. (reuse && sk2->sk_reuse &&
  4865. sk2->sk_state != SCTP_SS_LISTENING))
  4866. continue;
  4867. if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
  4868. sctp_sk(sk2), sctp_sk(sk))) {
  4869. ret = (long)sk2;
  4870. goto fail_unlock;
  4871. }
  4872. }
  4873. SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
  4874. }
  4875. pp_not_found:
  4876. /* If there was a hash table miss, create a new port. */
  4877. ret = 1;
  4878. if (!pp && !(pp = sctp_bucket_create(head, snum)))
  4879. goto fail_unlock;
  4880. /* In either case (hit or miss), make sure fastreuse is 1 only
  4881. * if sk->sk_reuse is too (that is, if the caller requested
  4882. * SO_REUSEADDR on this socket -sk-).
  4883. */
  4884. if (hlist_empty(&pp->owner)) {
  4885. if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
  4886. pp->fastreuse = 1;
  4887. else
  4888. pp->fastreuse = 0;
  4889. } else if (pp->fastreuse &&
  4890. (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
  4891. pp->fastreuse = 0;
  4892. /* We are set, so fill up all the data in the hash table
  4893. * entry, tie the socket list information with the rest of the
  4894. * sockets FIXME: Blurry, NPI (ipg).
  4895. */
  4896. success:
  4897. if (!sctp_sk(sk)->bind_hash) {
  4898. inet_sk(sk)->inet_num = snum;
  4899. sk_add_bind_node(sk, &pp->owner);
  4900. sctp_sk(sk)->bind_hash = pp;
  4901. }
  4902. ret = 0;
  4903. fail_unlock:
  4904. sctp_spin_unlock(&head->lock);
  4905. fail:
  4906. sctp_local_bh_enable();
  4907. return ret;
  4908. }
  4909. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  4910. * port is requested.
  4911. */
  4912. static int sctp_get_port(struct sock *sk, unsigned short snum)
  4913. {
  4914. long ret;
  4915. union sctp_addr addr;
  4916. struct sctp_af *af = sctp_sk(sk)->pf->af;
  4917. /* Set up a dummy address struct from the sk. */
  4918. af->from_sk(&addr, sk);
  4919. addr.v4.sin_port = htons(snum);
  4920. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  4921. ret = sctp_get_port_local(sk, &addr);
  4922. return ret ? 1 : 0;
  4923. }
  4924. /*
  4925. * Move a socket to LISTENING state.
  4926. */
  4927. SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog)
  4928. {
  4929. struct sctp_sock *sp = sctp_sk(sk);
  4930. struct sctp_endpoint *ep = sp->ep;
  4931. struct crypto_hash *tfm = NULL;
  4932. /* Allocate HMAC for generating cookie. */
  4933. if (!sctp_sk(sk)->hmac && sctp_hmac_alg) {
  4934. tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
  4935. if (IS_ERR(tfm)) {
  4936. if (net_ratelimit()) {
  4937. pr_info("failed to load transform for %s: %ld\n",
  4938. sctp_hmac_alg, PTR_ERR(tfm));
  4939. }
  4940. return -ENOSYS;
  4941. }
  4942. sctp_sk(sk)->hmac = tfm;
  4943. }
  4944. /*
  4945. * If a bind() or sctp_bindx() is not called prior to a listen()
  4946. * call that allows new associations to be accepted, the system
  4947. * picks an ephemeral port and will choose an address set equivalent
  4948. * to binding with a wildcard address.
  4949. *
  4950. * This is not currently spelled out in the SCTP sockets
  4951. * extensions draft, but follows the practice as seen in TCP
  4952. * sockets.
  4953. *
  4954. */
  4955. sk->sk_state = SCTP_SS_LISTENING;
  4956. if (!ep->base.bind_addr.port) {
  4957. if (sctp_autobind(sk))
  4958. return -EAGAIN;
  4959. } else {
  4960. if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
  4961. sk->sk_state = SCTP_SS_CLOSED;
  4962. return -EADDRINUSE;
  4963. }
  4964. }
  4965. sk->sk_max_ack_backlog = backlog;
  4966. sctp_hash_endpoint(ep);
  4967. return 0;
  4968. }
  4969. /*
  4970. * 4.1.3 / 5.1.3 listen()
  4971. *
  4972. * By default, new associations are not accepted for UDP style sockets.
  4973. * An application uses listen() to mark a socket as being able to
  4974. * accept new associations.
  4975. *
  4976. * On TCP style sockets, applications use listen() to ready the SCTP
  4977. * endpoint for accepting inbound associations.
  4978. *
  4979. * On both types of endpoints a backlog of '0' disables listening.
  4980. *
  4981. * Move a socket to LISTENING state.
  4982. */
  4983. int sctp_inet_listen(struct socket *sock, int backlog)
  4984. {
  4985. struct sock *sk = sock->sk;
  4986. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  4987. int err = -EINVAL;
  4988. if (unlikely(backlog < 0))
  4989. return err;
  4990. sctp_lock_sock(sk);
  4991. /* Peeled-off sockets are not allowed to listen(). */
  4992. if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
  4993. goto out;
  4994. if (sock->state != SS_UNCONNECTED)
  4995. goto out;
  4996. /* If backlog is zero, disable listening. */
  4997. if (!backlog) {
  4998. if (sctp_sstate(sk, CLOSED))
  4999. goto out;
  5000. err = 0;
  5001. sctp_unhash_endpoint(ep);
  5002. sk->sk_state = SCTP_SS_CLOSED;
  5003. if (sk->sk_reuse)
  5004. sctp_sk(sk)->bind_hash->fastreuse = 1;
  5005. goto out;
  5006. }
  5007. /* If we are already listening, just update the backlog */
  5008. if (sctp_sstate(sk, LISTENING))
  5009. sk->sk_max_ack_backlog = backlog;
  5010. else {
  5011. err = sctp_listen_start(sk, backlog);
  5012. if (err)
  5013. goto out;
  5014. }
  5015. err = 0;
  5016. out:
  5017. sctp_release_sock(sk);
  5018. return err;
  5019. }
  5020. /*
  5021. * This function is done by modeling the current datagram_poll() and the
  5022. * tcp_poll(). Note that, based on these implementations, we don't
  5023. * lock the socket in this function, even though it seems that,
  5024. * ideally, locking or some other mechanisms can be used to ensure
  5025. * the integrity of the counters (sndbuf and wmem_alloc) used
  5026. * in this place. We assume that we don't need locks either until proven
  5027. * otherwise.
  5028. *
  5029. * Another thing to note is that we include the Async I/O support
  5030. * here, again, by modeling the current TCP/UDP code. We don't have
  5031. * a good way to test with it yet.
  5032. */
  5033. unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  5034. {
  5035. struct sock *sk = sock->sk;
  5036. struct sctp_sock *sp = sctp_sk(sk);
  5037. unsigned int mask;
  5038. poll_wait(file, sk_sleep(sk), wait);
  5039. /* A TCP-style listening socket becomes readable when the accept queue
  5040. * is not empty.
  5041. */
  5042. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  5043. return (!list_empty(&sp->ep->asocs)) ?
  5044. (POLLIN | POLLRDNORM) : 0;
  5045. mask = 0;
  5046. /* Is there any exceptional events? */
  5047. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  5048. mask |= POLLERR;
  5049. if (sk->sk_shutdown & RCV_SHUTDOWN)
  5050. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  5051. if (sk->sk_shutdown == SHUTDOWN_MASK)
  5052. mask |= POLLHUP;
  5053. /* Is it readable? Reconsider this code with TCP-style support. */
  5054. if (!skb_queue_empty(&sk->sk_receive_queue))
  5055. mask |= POLLIN | POLLRDNORM;
  5056. /* The association is either gone or not ready. */
  5057. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  5058. return mask;
  5059. /* Is it writable? */
  5060. if (sctp_writeable(sk)) {
  5061. mask |= POLLOUT | POLLWRNORM;
  5062. } else {
  5063. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  5064. /*
  5065. * Since the socket is not locked, the buffer
  5066. * might be made available after the writeable check and
  5067. * before the bit is set. This could cause a lost I/O
  5068. * signal. tcp_poll() has a race breaker for this race
  5069. * condition. Based on their implementation, we put
  5070. * in the following code to cover it as well.
  5071. */
  5072. if (sctp_writeable(sk))
  5073. mask |= POLLOUT | POLLWRNORM;
  5074. }
  5075. return mask;
  5076. }
  5077. /********************************************************************
  5078. * 2nd Level Abstractions
  5079. ********************************************************************/
  5080. static struct sctp_bind_bucket *sctp_bucket_create(
  5081. struct sctp_bind_hashbucket *head, unsigned short snum)
  5082. {
  5083. struct sctp_bind_bucket *pp;
  5084. pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
  5085. if (pp) {
  5086. SCTP_DBG_OBJCNT_INC(bind_bucket);
  5087. pp->port = snum;
  5088. pp->fastreuse = 0;
  5089. INIT_HLIST_HEAD(&pp->owner);
  5090. hlist_add_head(&pp->node, &head->chain);
  5091. }
  5092. return pp;
  5093. }
  5094. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  5095. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  5096. {
  5097. if (pp && hlist_empty(&pp->owner)) {
  5098. __hlist_del(&pp->node);
  5099. kmem_cache_free(sctp_bucket_cachep, pp);
  5100. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  5101. }
  5102. }
  5103. /* Release this socket's reference to a local port. */
  5104. static inline void __sctp_put_port(struct sock *sk)
  5105. {
  5106. struct sctp_bind_hashbucket *head =
  5107. &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->inet_num)];
  5108. struct sctp_bind_bucket *pp;
  5109. sctp_spin_lock(&head->lock);
  5110. pp = sctp_sk(sk)->bind_hash;
  5111. __sk_del_bind_node(sk);
  5112. sctp_sk(sk)->bind_hash = NULL;
  5113. inet_sk(sk)->inet_num = 0;
  5114. sctp_bucket_destroy(pp);
  5115. sctp_spin_unlock(&head->lock);
  5116. }
  5117. void sctp_put_port(struct sock *sk)
  5118. {
  5119. sctp_local_bh_disable();
  5120. __sctp_put_port(sk);
  5121. sctp_local_bh_enable();
  5122. }
  5123. /*
  5124. * The system picks an ephemeral port and choose an address set equivalent
  5125. * to binding with a wildcard address.
  5126. * One of those addresses will be the primary address for the association.
  5127. * This automatically enables the multihoming capability of SCTP.
  5128. */
  5129. static int sctp_autobind(struct sock *sk)
  5130. {
  5131. union sctp_addr autoaddr;
  5132. struct sctp_af *af;
  5133. __be16 port;
  5134. /* Initialize a local sockaddr structure to INADDR_ANY. */
  5135. af = sctp_sk(sk)->pf->af;
  5136. port = htons(inet_sk(sk)->inet_num);
  5137. af->inaddr_any(&autoaddr, port);
  5138. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  5139. }
  5140. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  5141. *
  5142. * From RFC 2292
  5143. * 4.2 The cmsghdr Structure *
  5144. *
  5145. * When ancillary data is sent or received, any number of ancillary data
  5146. * objects can be specified by the msg_control and msg_controllen members of
  5147. * the msghdr structure, because each object is preceded by
  5148. * a cmsghdr structure defining the object's length (the cmsg_len member).
  5149. * Historically Berkeley-derived implementations have passed only one object
  5150. * at a time, but this API allows multiple objects to be
  5151. * passed in a single call to sendmsg() or recvmsg(). The following example
  5152. * shows two ancillary data objects in a control buffer.
  5153. *
  5154. * |<--------------------------- msg_controllen -------------------------->|
  5155. * | |
  5156. *
  5157. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  5158. *
  5159. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  5160. * | | |
  5161. *
  5162. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  5163. *
  5164. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  5165. * | | | | |
  5166. *
  5167. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  5168. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  5169. *
  5170. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  5171. *
  5172. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  5173. * ^
  5174. * |
  5175. *
  5176. * msg_control
  5177. * points here
  5178. */
  5179. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
  5180. sctp_cmsgs_t *cmsgs)
  5181. {
  5182. struct cmsghdr *cmsg;
  5183. struct msghdr *my_msg = (struct msghdr *)msg;
  5184. for (cmsg = CMSG_FIRSTHDR(msg);
  5185. cmsg != NULL;
  5186. cmsg = CMSG_NXTHDR(my_msg, cmsg)) {
  5187. if (!CMSG_OK(my_msg, cmsg))
  5188. return -EINVAL;
  5189. /* Should we parse this header or ignore? */
  5190. if (cmsg->cmsg_level != IPPROTO_SCTP)
  5191. continue;
  5192. /* Strictly check lengths following example in SCM code. */
  5193. switch (cmsg->cmsg_type) {
  5194. case SCTP_INIT:
  5195. /* SCTP Socket API Extension
  5196. * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
  5197. *
  5198. * This cmsghdr structure provides information for
  5199. * initializing new SCTP associations with sendmsg().
  5200. * The SCTP_INITMSG socket option uses this same data
  5201. * structure. This structure is not used for
  5202. * recvmsg().
  5203. *
  5204. * cmsg_level cmsg_type cmsg_data[]
  5205. * ------------ ------------ ----------------------
  5206. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  5207. */
  5208. if (cmsg->cmsg_len !=
  5209. CMSG_LEN(sizeof(struct sctp_initmsg)))
  5210. return -EINVAL;
  5211. cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
  5212. break;
  5213. case SCTP_SNDRCV:
  5214. /* SCTP Socket API Extension
  5215. * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
  5216. *
  5217. * This cmsghdr structure specifies SCTP options for
  5218. * sendmsg() and describes SCTP header information
  5219. * about a received message through recvmsg().
  5220. *
  5221. * cmsg_level cmsg_type cmsg_data[]
  5222. * ------------ ------------ ----------------------
  5223. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  5224. */
  5225. if (cmsg->cmsg_len !=
  5226. CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  5227. return -EINVAL;
  5228. cmsgs->info =
  5229. (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  5230. /* Minimally, validate the sinfo_flags. */
  5231. if (cmsgs->info->sinfo_flags &
  5232. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  5233. SCTP_ABORT | SCTP_EOF))
  5234. return -EINVAL;
  5235. break;
  5236. default:
  5237. return -EINVAL;
  5238. }
  5239. }
  5240. return 0;
  5241. }
  5242. /*
  5243. * Wait for a packet..
  5244. * Note: This function is the same function as in core/datagram.c
  5245. * with a few modifications to make lksctp work.
  5246. */
  5247. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
  5248. {
  5249. int error;
  5250. DEFINE_WAIT(wait);
  5251. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  5252. /* Socket errors? */
  5253. error = sock_error(sk);
  5254. if (error)
  5255. goto out;
  5256. if (!skb_queue_empty(&sk->sk_receive_queue))
  5257. goto ready;
  5258. /* Socket shut down? */
  5259. if (sk->sk_shutdown & RCV_SHUTDOWN)
  5260. goto out;
  5261. /* Sequenced packets can come disconnected. If so we report the
  5262. * problem.
  5263. */
  5264. error = -ENOTCONN;
  5265. /* Is there a good reason to think that we may receive some data? */
  5266. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  5267. goto out;
  5268. /* Handle signals. */
  5269. if (signal_pending(current))
  5270. goto interrupted;
  5271. /* Let another process have a go. Since we are going to sleep
  5272. * anyway. Note: This may cause odd behaviors if the message
  5273. * does not fit in the user's buffer, but this seems to be the
  5274. * only way to honor MSG_DONTWAIT realistically.
  5275. */
  5276. sctp_release_sock(sk);
  5277. *timeo_p = schedule_timeout(*timeo_p);
  5278. sctp_lock_sock(sk);
  5279. ready:
  5280. finish_wait(sk_sleep(sk), &wait);
  5281. return 0;
  5282. interrupted:
  5283. error = sock_intr_errno(*timeo_p);
  5284. out:
  5285. finish_wait(sk_sleep(sk), &wait);
  5286. *err = error;
  5287. return error;
  5288. }
  5289. /* Receive a datagram.
  5290. * Note: This is pretty much the same routine as in core/datagram.c
  5291. * with a few changes to make lksctp work.
  5292. */
  5293. static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  5294. int noblock, int *err)
  5295. {
  5296. int error;
  5297. struct sk_buff *skb;
  5298. long timeo;
  5299. timeo = sock_rcvtimeo(sk, noblock);
  5300. SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
  5301. timeo, MAX_SCHEDULE_TIMEOUT);
  5302. do {
  5303. /* Again only user level code calls this function,
  5304. * so nothing interrupt level
  5305. * will suddenly eat the receive_queue.
  5306. *
  5307. * Look at current nfs client by the way...
  5308. * However, this function was correct in any case. 8)
  5309. */
  5310. if (flags & MSG_PEEK) {
  5311. spin_lock_bh(&sk->sk_receive_queue.lock);
  5312. skb = skb_peek(&sk->sk_receive_queue);
  5313. if (skb)
  5314. atomic_inc(&skb->users);
  5315. spin_unlock_bh(&sk->sk_receive_queue.lock);
  5316. } else {
  5317. skb = skb_dequeue(&sk->sk_receive_queue);
  5318. }
  5319. if (skb)
  5320. return skb;
  5321. /* Caller is allowed not to check sk->sk_err before calling. */
  5322. error = sock_error(sk);
  5323. if (error)
  5324. goto no_packet;
  5325. if (sk->sk_shutdown & RCV_SHUTDOWN)
  5326. break;
  5327. /* User doesn't want to wait. */
  5328. error = -EAGAIN;
  5329. if (!timeo)
  5330. goto no_packet;
  5331. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  5332. return NULL;
  5333. no_packet:
  5334. *err = error;
  5335. return NULL;
  5336. }
  5337. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  5338. static void __sctp_write_space(struct sctp_association *asoc)
  5339. {
  5340. struct sock *sk = asoc->base.sk;
  5341. struct socket *sock = sk->sk_socket;
  5342. if ((sctp_wspace(asoc) > 0) && sock) {
  5343. if (waitqueue_active(&asoc->wait))
  5344. wake_up_interruptible(&asoc->wait);
  5345. if (sctp_writeable(sk)) {
  5346. wait_queue_head_t *wq = sk_sleep(sk);
  5347. if (wq && waitqueue_active(wq))
  5348. wake_up_interruptible(wq);
  5349. /* Note that we try to include the Async I/O support
  5350. * here by modeling from the current TCP/UDP code.
  5351. * We have not tested with it yet.
  5352. */
  5353. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  5354. sock_wake_async(sock,
  5355. SOCK_WAKE_SPACE, POLL_OUT);
  5356. }
  5357. }
  5358. }
  5359. /* Do accounting for the sndbuf space.
  5360. * Decrement the used sndbuf space of the corresponding association by the
  5361. * data size which was just transmitted(freed).
  5362. */
  5363. static void sctp_wfree(struct sk_buff *skb)
  5364. {
  5365. struct sctp_association *asoc;
  5366. struct sctp_chunk *chunk;
  5367. struct sock *sk;
  5368. /* Get the saved chunk pointer. */
  5369. chunk = *((struct sctp_chunk **)(skb->cb));
  5370. asoc = chunk->asoc;
  5371. sk = asoc->base.sk;
  5372. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  5373. sizeof(struct sk_buff) +
  5374. sizeof(struct sctp_chunk);
  5375. atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  5376. /*
  5377. * This undoes what is done via sctp_set_owner_w and sk_mem_charge
  5378. */
  5379. sk->sk_wmem_queued -= skb->truesize;
  5380. sk_mem_uncharge(sk, skb->truesize);
  5381. sock_wfree(skb);
  5382. __sctp_write_space(asoc);
  5383. sctp_association_put(asoc);
  5384. }
  5385. /* Do accounting for the receive space on the socket.
  5386. * Accounting for the association is done in ulpevent.c
  5387. * We set this as a destructor for the cloned data skbs so that
  5388. * accounting is done at the correct time.
  5389. */
  5390. void sctp_sock_rfree(struct sk_buff *skb)
  5391. {
  5392. struct sock *sk = skb->sk;
  5393. struct sctp_ulpevent *event = sctp_skb2event(skb);
  5394. atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
  5395. /*
  5396. * Mimic the behavior of sock_rfree
  5397. */
  5398. sk_mem_uncharge(sk, event->rmem_len);
  5399. }
  5400. /* Helper function to wait for space in the sndbuf. */
  5401. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  5402. size_t msg_len)
  5403. {
  5404. struct sock *sk = asoc->base.sk;
  5405. int err = 0;
  5406. long current_timeo = *timeo_p;
  5407. DEFINE_WAIT(wait);
  5408. SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
  5409. asoc, (long)(*timeo_p), msg_len);
  5410. /* Increment the association's refcnt. */
  5411. sctp_association_hold(asoc);
  5412. /* Wait on the association specific sndbuf space. */
  5413. for (;;) {
  5414. prepare_to_wait_exclusive(&asoc->wait, &wait,
  5415. TASK_INTERRUPTIBLE);
  5416. if (!*timeo_p)
  5417. goto do_nonblock;
  5418. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  5419. asoc->base.dead)
  5420. goto do_error;
  5421. if (signal_pending(current))
  5422. goto do_interrupted;
  5423. if (msg_len <= sctp_wspace(asoc))
  5424. break;
  5425. /* Let another process have a go. Since we are going
  5426. * to sleep anyway.
  5427. */
  5428. sctp_release_sock(sk);
  5429. current_timeo = schedule_timeout(current_timeo);
  5430. BUG_ON(sk != asoc->base.sk);
  5431. sctp_lock_sock(sk);
  5432. *timeo_p = current_timeo;
  5433. }
  5434. out:
  5435. finish_wait(&asoc->wait, &wait);
  5436. /* Release the association's refcnt. */
  5437. sctp_association_put(asoc);
  5438. return err;
  5439. do_error:
  5440. err = -EPIPE;
  5441. goto out;
  5442. do_interrupted:
  5443. err = sock_intr_errno(*timeo_p);
  5444. goto out;
  5445. do_nonblock:
  5446. err = -EAGAIN;
  5447. goto out;
  5448. }
  5449. void sctp_data_ready(struct sock *sk, int len)
  5450. {
  5451. struct socket_wq *wq;
  5452. rcu_read_lock();
  5453. wq = rcu_dereference(sk->sk_wq);
  5454. if (wq_has_sleeper(wq))
  5455. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  5456. POLLRDNORM | POLLRDBAND);
  5457. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  5458. rcu_read_unlock();
  5459. }
  5460. /* If socket sndbuf has changed, wake up all per association waiters. */
  5461. void sctp_write_space(struct sock *sk)
  5462. {
  5463. struct sctp_association *asoc;
  5464. /* Wake up the tasks in each wait queue. */
  5465. list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
  5466. __sctp_write_space(asoc);
  5467. }
  5468. }
  5469. /* Is there any sndbuf space available on the socket?
  5470. *
  5471. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  5472. * associations on the same socket. For a UDP-style socket with
  5473. * multiple associations, it is possible for it to be "unwriteable"
  5474. * prematurely. I assume that this is acceptable because
  5475. * a premature "unwriteable" is better than an accidental "writeable" which
  5476. * would cause an unwanted block under certain circumstances. For the 1-1
  5477. * UDP-style sockets or TCP-style sockets, this code should work.
  5478. * - Daisy
  5479. */
  5480. static int sctp_writeable(struct sock *sk)
  5481. {
  5482. int amt = 0;
  5483. amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  5484. if (amt < 0)
  5485. amt = 0;
  5486. return amt;
  5487. }
  5488. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  5489. * returns immediately with EINPROGRESS.
  5490. */
  5491. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  5492. {
  5493. struct sock *sk = asoc->base.sk;
  5494. int err = 0;
  5495. long current_timeo = *timeo_p;
  5496. DEFINE_WAIT(wait);
  5497. SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc,
  5498. (long)(*timeo_p));
  5499. /* Increment the association's refcnt. */
  5500. sctp_association_hold(asoc);
  5501. for (;;) {
  5502. prepare_to_wait_exclusive(&asoc->wait, &wait,
  5503. TASK_INTERRUPTIBLE);
  5504. if (!*timeo_p)
  5505. goto do_nonblock;
  5506. if (sk->sk_shutdown & RCV_SHUTDOWN)
  5507. break;
  5508. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  5509. asoc->base.dead)
  5510. goto do_error;
  5511. if (signal_pending(current))
  5512. goto do_interrupted;
  5513. if (sctp_state(asoc, ESTABLISHED))
  5514. break;
  5515. /* Let another process have a go. Since we are going
  5516. * to sleep anyway.
  5517. */
  5518. sctp_release_sock(sk);
  5519. current_timeo = schedule_timeout(current_timeo);
  5520. sctp_lock_sock(sk);
  5521. *timeo_p = current_timeo;
  5522. }
  5523. out:
  5524. finish_wait(&asoc->wait, &wait);
  5525. /* Release the association's refcnt. */
  5526. sctp_association_put(asoc);
  5527. return err;
  5528. do_error:
  5529. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  5530. err = -ETIMEDOUT;
  5531. else
  5532. err = -ECONNREFUSED;
  5533. goto out;
  5534. do_interrupted:
  5535. err = sock_intr_errno(*timeo_p);
  5536. goto out;
  5537. do_nonblock:
  5538. err = -EINPROGRESS;
  5539. goto out;
  5540. }
  5541. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  5542. {
  5543. struct sctp_endpoint *ep;
  5544. int err = 0;
  5545. DEFINE_WAIT(wait);
  5546. ep = sctp_sk(sk)->ep;
  5547. for (;;) {
  5548. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  5549. TASK_INTERRUPTIBLE);
  5550. if (list_empty(&ep->asocs)) {
  5551. sctp_release_sock(sk);
  5552. timeo = schedule_timeout(timeo);
  5553. sctp_lock_sock(sk);
  5554. }
  5555. err = -EINVAL;
  5556. if (!sctp_sstate(sk, LISTENING))
  5557. break;
  5558. err = 0;
  5559. if (!list_empty(&ep->asocs))
  5560. break;
  5561. err = sock_intr_errno(timeo);
  5562. if (signal_pending(current))
  5563. break;
  5564. err = -EAGAIN;
  5565. if (!timeo)
  5566. break;
  5567. }
  5568. finish_wait(sk_sleep(sk), &wait);
  5569. return err;
  5570. }
  5571. static void sctp_wait_for_close(struct sock *sk, long timeout)
  5572. {
  5573. DEFINE_WAIT(wait);
  5574. do {
  5575. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  5576. if (list_empty(&sctp_sk(sk)->ep->asocs))
  5577. break;
  5578. sctp_release_sock(sk);
  5579. timeout = schedule_timeout(timeout);
  5580. sctp_lock_sock(sk);
  5581. } while (!signal_pending(current) && timeout);
  5582. finish_wait(sk_sleep(sk), &wait);
  5583. }
  5584. static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
  5585. {
  5586. struct sk_buff *frag;
  5587. if (!skb->data_len)
  5588. goto done;
  5589. /* Don't forget the fragments. */
  5590. skb_walk_frags(skb, frag)
  5591. sctp_skb_set_owner_r_frag(frag, sk);
  5592. done:
  5593. sctp_skb_set_owner_r(skb, sk);
  5594. }
  5595. void sctp_copy_sock(struct sock *newsk, struct sock *sk,
  5596. struct sctp_association *asoc)
  5597. {
  5598. struct inet_sock *inet = inet_sk(sk);
  5599. struct inet_sock *newinet;
  5600. newsk->sk_type = sk->sk_type;
  5601. newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
  5602. newsk->sk_flags = sk->sk_flags;
  5603. newsk->sk_no_check = sk->sk_no_check;
  5604. newsk->sk_reuse = sk->sk_reuse;
  5605. newsk->sk_shutdown = sk->sk_shutdown;
  5606. newsk->sk_destruct = inet_sock_destruct;
  5607. newsk->sk_family = sk->sk_family;
  5608. newsk->sk_protocol = IPPROTO_SCTP;
  5609. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  5610. newsk->sk_sndbuf = sk->sk_sndbuf;
  5611. newsk->sk_rcvbuf = sk->sk_rcvbuf;
  5612. newsk->sk_lingertime = sk->sk_lingertime;
  5613. newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
  5614. newsk->sk_sndtimeo = sk->sk_sndtimeo;
  5615. newinet = inet_sk(newsk);
  5616. /* Initialize sk's sport, dport, rcv_saddr and daddr for
  5617. * getsockname() and getpeername()
  5618. */
  5619. newinet->inet_sport = inet->inet_sport;
  5620. newinet->inet_saddr = inet->inet_saddr;
  5621. newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
  5622. newinet->inet_dport = htons(asoc->peer.port);
  5623. newinet->pmtudisc = inet->pmtudisc;
  5624. newinet->inet_id = asoc->next_tsn ^ jiffies;
  5625. newinet->uc_ttl = inet->uc_ttl;
  5626. newinet->mc_loop = 1;
  5627. newinet->mc_ttl = 1;
  5628. newinet->mc_index = 0;
  5629. newinet->mc_list = NULL;
  5630. }
  5631. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  5632. * and its messages to the newsk.
  5633. */
  5634. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  5635. struct sctp_association *assoc,
  5636. sctp_socket_type_t type)
  5637. {
  5638. struct sctp_sock *oldsp = sctp_sk(oldsk);
  5639. struct sctp_sock *newsp = sctp_sk(newsk);
  5640. struct sctp_bind_bucket *pp; /* hash list port iterator */
  5641. struct sctp_endpoint *newep = newsp->ep;
  5642. struct sk_buff *skb, *tmp;
  5643. struct sctp_ulpevent *event;
  5644. struct sctp_bind_hashbucket *head;
  5645. /* Migrate socket buffer sizes and all the socket level options to the
  5646. * new socket.
  5647. */
  5648. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  5649. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  5650. /* Brute force copy old sctp opt. */
  5651. inet_sk_copy_descendant(newsk, oldsk);
  5652. /* Restore the ep value that was overwritten with the above structure
  5653. * copy.
  5654. */
  5655. newsp->ep = newep;
  5656. newsp->hmac = NULL;
  5657. /* Hook this new socket in to the bind_hash list. */
  5658. head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->inet_num)];
  5659. sctp_local_bh_disable();
  5660. sctp_spin_lock(&head->lock);
  5661. pp = sctp_sk(oldsk)->bind_hash;
  5662. sk_add_bind_node(newsk, &pp->owner);
  5663. sctp_sk(newsk)->bind_hash = pp;
  5664. inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
  5665. sctp_spin_unlock(&head->lock);
  5666. sctp_local_bh_enable();
  5667. /* Copy the bind_addr list from the original endpoint to the new
  5668. * endpoint so that we can handle restarts properly
  5669. */
  5670. sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
  5671. &oldsp->ep->base.bind_addr, GFP_KERNEL);
  5672. /* Move any messages in the old socket's receive queue that are for the
  5673. * peeled off association to the new socket's receive queue.
  5674. */
  5675. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  5676. event = sctp_skb2event(skb);
  5677. if (event->asoc == assoc) {
  5678. __skb_unlink(skb, &oldsk->sk_receive_queue);
  5679. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  5680. sctp_skb_set_owner_r_frag(skb, newsk);
  5681. }
  5682. }
  5683. /* Clean up any messages pending delivery due to partial
  5684. * delivery. Three cases:
  5685. * 1) No partial deliver; no work.
  5686. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  5687. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  5688. */
  5689. skb_queue_head_init(&newsp->pd_lobby);
  5690. atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
  5691. if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
  5692. struct sk_buff_head *queue;
  5693. /* Decide which queue to move pd_lobby skbs to. */
  5694. if (assoc->ulpq.pd_mode) {
  5695. queue = &newsp->pd_lobby;
  5696. } else
  5697. queue = &newsk->sk_receive_queue;
  5698. /* Walk through the pd_lobby, looking for skbs that
  5699. * need moved to the new socket.
  5700. */
  5701. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  5702. event = sctp_skb2event(skb);
  5703. if (event->asoc == assoc) {
  5704. __skb_unlink(skb, &oldsp->pd_lobby);
  5705. __skb_queue_tail(queue, skb);
  5706. sctp_skb_set_owner_r_frag(skb, newsk);
  5707. }
  5708. }
  5709. /* Clear up any skbs waiting for the partial
  5710. * delivery to finish.
  5711. */
  5712. if (assoc->ulpq.pd_mode)
  5713. sctp_clear_pd(oldsk, NULL);
  5714. }
  5715. sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
  5716. sctp_skb_set_owner_r_frag(skb, newsk);
  5717. sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
  5718. sctp_skb_set_owner_r_frag(skb, newsk);
  5719. /* Set the type of socket to indicate that it is peeled off from the
  5720. * original UDP-style socket or created with the accept() call on a
  5721. * TCP-style socket..
  5722. */
  5723. newsp->type = type;
  5724. /* Mark the new socket "in-use" by the user so that any packets
  5725. * that may arrive on the association after we've moved it are
  5726. * queued to the backlog. This prevents a potential race between
  5727. * backlog processing on the old socket and new-packet processing
  5728. * on the new socket.
  5729. *
  5730. * The caller has just allocated newsk so we can guarantee that other
  5731. * paths won't try to lock it and then oldsk.
  5732. */
  5733. lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
  5734. sctp_assoc_migrate(assoc, newsk);
  5735. /* If the association on the newsk is already closed before accept()
  5736. * is called, set RCV_SHUTDOWN flag.
  5737. */
  5738. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
  5739. newsk->sk_shutdown |= RCV_SHUTDOWN;
  5740. newsk->sk_state = SCTP_SS_ESTABLISHED;
  5741. sctp_release_sock(newsk);
  5742. }
  5743. /* This proto struct describes the ULP interface for SCTP. */
  5744. struct proto sctp_prot = {
  5745. .name = "SCTP",
  5746. .owner = THIS_MODULE,
  5747. .close = sctp_close,
  5748. .connect = sctp_connect,
  5749. .disconnect = sctp_disconnect,
  5750. .accept = sctp_accept,
  5751. .ioctl = sctp_ioctl,
  5752. .init = sctp_init_sock,
  5753. .destroy = sctp_destroy_sock,
  5754. .shutdown = sctp_shutdown,
  5755. .setsockopt = sctp_setsockopt,
  5756. .getsockopt = sctp_getsockopt,
  5757. .sendmsg = sctp_sendmsg,
  5758. .recvmsg = sctp_recvmsg,
  5759. .bind = sctp_bind,
  5760. .backlog_rcv = sctp_backlog_rcv,
  5761. .hash = sctp_hash,
  5762. .unhash = sctp_unhash,
  5763. .get_port = sctp_get_port,
  5764. .obj_size = sizeof(struct sctp_sock),
  5765. .sysctl_mem = sysctl_sctp_mem,
  5766. .sysctl_rmem = sysctl_sctp_rmem,
  5767. .sysctl_wmem = sysctl_sctp_wmem,
  5768. .memory_pressure = &sctp_memory_pressure,
  5769. .enter_memory_pressure = sctp_enter_memory_pressure,
  5770. .memory_allocated = &sctp_memory_allocated,
  5771. .sockets_allocated = &sctp_sockets_allocated,
  5772. };
  5773. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  5774. struct proto sctpv6_prot = {
  5775. .name = "SCTPv6",
  5776. .owner = THIS_MODULE,
  5777. .close = sctp_close,
  5778. .connect = sctp_connect,
  5779. .disconnect = sctp_disconnect,
  5780. .accept = sctp_accept,
  5781. .ioctl = sctp_ioctl,
  5782. .init = sctp_init_sock,
  5783. .destroy = sctp_destroy_sock,
  5784. .shutdown = sctp_shutdown,
  5785. .setsockopt = sctp_setsockopt,
  5786. .getsockopt = sctp_getsockopt,
  5787. .sendmsg = sctp_sendmsg,
  5788. .recvmsg = sctp_recvmsg,
  5789. .bind = sctp_bind,
  5790. .backlog_rcv = sctp_backlog_rcv,
  5791. .hash = sctp_hash,
  5792. .unhash = sctp_unhash,
  5793. .get_port = sctp_get_port,
  5794. .obj_size = sizeof(struct sctp6_sock),
  5795. .sysctl_mem = sysctl_sctp_mem,
  5796. .sysctl_rmem = sysctl_sctp_rmem,
  5797. .sysctl_wmem = sysctl_sctp_wmem,
  5798. .memory_pressure = &sctp_memory_pressure,
  5799. .enter_memory_pressure = sctp_enter_memory_pressure,
  5800. .memory_allocated = &sctp_memory_allocated,
  5801. .sockets_allocated = &sctp_sockets_allocated,
  5802. };
  5803. #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */