socket.c 194 KB

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