socket.c 189 KB

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