socket.c 194 KB

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