socket.c 194 KB

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