sm_statefuns.c 202 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441
  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-2002 Intel Corp.
  6. * Copyright (c) 2002 Nokia Corp.
  7. *
  8. * This is part of the SCTP Linux Kernel Implementation.
  9. *
  10. * These are the state functions for the state machine.
  11. *
  12. * This SCTP implementation is free software;
  13. * you can redistribute it and/or modify it under the terms of
  14. * the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2, or (at your option)
  16. * any later version.
  17. *
  18. * This SCTP implementation is distributed in the hope that it
  19. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  20. * ************************
  21. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. * See the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with GNU CC; see the file COPYING. If not, see
  26. * <http://www.gnu.org/licenses/>.
  27. *
  28. * Please send any bug reports or fixes you make to the
  29. * email address(es):
  30. * lksctp developers <linux-sctp@vger.kernel.org>
  31. *
  32. * Written or modified by:
  33. * La Monte H.P. Yarroll <piggy@acm.org>
  34. * Karl Knutson <karl@athena.chicago.il.us>
  35. * Mathew Kotowsky <kotowsky@sctp.org>
  36. * Sridhar Samudrala <samudrala@us.ibm.com>
  37. * Jon Grimm <jgrimm@us.ibm.com>
  38. * Hui Huang <hui.huang@nokia.com>
  39. * Dajiang Zhang <dajiang.zhang@nokia.com>
  40. * Daisy Chang <daisyc@us.ibm.com>
  41. * Ardelle Fan <ardelle.fan@intel.com>
  42. * Ryan Layer <rmlayer@us.ibm.com>
  43. * Kevin Gao <kevin.gao@intel.com>
  44. */
  45. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  46. #include <linux/types.h>
  47. #include <linux/kernel.h>
  48. #include <linux/ip.h>
  49. #include <linux/ipv6.h>
  50. #include <linux/net.h>
  51. #include <linux/inet.h>
  52. #include <linux/slab.h>
  53. #include <net/sock.h>
  54. #include <net/inet_ecn.h>
  55. #include <linux/skbuff.h>
  56. #include <net/sctp/sctp.h>
  57. #include <net/sctp/sm.h>
  58. #include <net/sctp/structs.h>
  59. static struct sctp_packet *sctp_abort_pkt_new(
  60. struct net *net,
  61. const struct sctp_endpoint *ep,
  62. const struct sctp_association *asoc,
  63. struct sctp_chunk *chunk,
  64. const void *payload, size_t paylen);
  65. static int sctp_eat_data(const struct sctp_association *asoc,
  66. struct sctp_chunk *chunk,
  67. struct sctp_cmd_seq *commands);
  68. static struct sctp_packet *sctp_ootb_pkt_new(
  69. struct net *net,
  70. const struct sctp_association *asoc,
  71. const struct sctp_chunk *chunk);
  72. static void sctp_send_stale_cookie_err(struct net *net,
  73. const struct sctp_endpoint *ep,
  74. const struct sctp_association *asoc,
  75. const struct sctp_chunk *chunk,
  76. struct sctp_cmd_seq *commands,
  77. struct sctp_chunk *err_chunk);
  78. static enum sctp_disposition sctp_sf_do_5_2_6_stale(
  79. struct net *net,
  80. const struct sctp_endpoint *ep,
  81. const struct sctp_association *asoc,
  82. const union sctp_subtype type,
  83. void *arg,
  84. struct sctp_cmd_seq *commands);
  85. static enum sctp_disposition sctp_sf_shut_8_4_5(
  86. struct net *net,
  87. const struct sctp_endpoint *ep,
  88. const struct sctp_association *asoc,
  89. const union sctp_subtype type,
  90. void *arg,
  91. struct sctp_cmd_seq *commands);
  92. static enum sctp_disposition sctp_sf_tabort_8_4_8(
  93. struct net *net,
  94. const struct sctp_endpoint *ep,
  95. const struct sctp_association *asoc,
  96. const union sctp_subtype type,
  97. void *arg,
  98. struct sctp_cmd_seq *commands);
  99. static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
  100. static enum sctp_disposition sctp_stop_t1_and_abort(
  101. struct net *net,
  102. struct sctp_cmd_seq *commands,
  103. __be16 error, int sk_err,
  104. const struct sctp_association *asoc,
  105. struct sctp_transport *transport);
  106. static enum sctp_disposition sctp_sf_abort_violation(
  107. struct net *net,
  108. const struct sctp_endpoint *ep,
  109. const struct sctp_association *asoc,
  110. void *arg,
  111. struct sctp_cmd_seq *commands,
  112. const __u8 *payload,
  113. const size_t paylen);
  114. static enum sctp_disposition sctp_sf_violation_chunklen(
  115. struct net *net,
  116. const struct sctp_endpoint *ep,
  117. const struct sctp_association *asoc,
  118. const union sctp_subtype type,
  119. void *arg,
  120. struct sctp_cmd_seq *commands);
  121. static enum sctp_disposition sctp_sf_violation_paramlen(
  122. struct net *net,
  123. const struct sctp_endpoint *ep,
  124. const struct sctp_association *asoc,
  125. const union sctp_subtype type,
  126. void *arg, void *ext,
  127. struct sctp_cmd_seq *commands);
  128. static enum sctp_disposition sctp_sf_violation_ctsn(
  129. struct net *net,
  130. const struct sctp_endpoint *ep,
  131. const struct sctp_association *asoc,
  132. const union sctp_subtype type,
  133. void *arg,
  134. struct sctp_cmd_seq *commands);
  135. static enum sctp_disposition sctp_sf_violation_chunk(
  136. struct net *net,
  137. const struct sctp_endpoint *ep,
  138. const struct sctp_association *asoc,
  139. const union sctp_subtype type,
  140. void *arg,
  141. struct sctp_cmd_seq *commands);
  142. static enum sctp_ierror sctp_sf_authenticate(
  143. struct net *net,
  144. const struct sctp_endpoint *ep,
  145. const struct sctp_association *asoc,
  146. const union sctp_subtype type,
  147. struct sctp_chunk *chunk);
  148. static enum sctp_disposition __sctp_sf_do_9_1_abort(
  149. struct net *net,
  150. const struct sctp_endpoint *ep,
  151. const struct sctp_association *asoc,
  152. const union sctp_subtype type,
  153. void *arg,
  154. struct sctp_cmd_seq *commands);
  155. /* Small helper function that checks if the chunk length
  156. * is of the appropriate length. The 'required_length' argument
  157. * is set to be the size of a specific chunk we are testing.
  158. * Return Values: true = Valid length
  159. * false = Invalid length
  160. *
  161. */
  162. static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
  163. __u16 required_length)
  164. {
  165. __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
  166. /* Previously already marked? */
  167. if (unlikely(chunk->pdiscard))
  168. return false;
  169. if (unlikely(chunk_length < required_length))
  170. return false;
  171. return true;
  172. }
  173. /**********************************************************
  174. * These are the state functions for handling chunk events.
  175. **********************************************************/
  176. /*
  177. * Process the final SHUTDOWN COMPLETE.
  178. *
  179. * Section: 4 (C) (diagram), 9.2
  180. * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
  181. * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
  182. * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
  183. * should stop the T2-shutdown timer and remove all knowledge of the
  184. * association (and thus the association enters the CLOSED state).
  185. *
  186. * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
  187. * C) Rules for packet carrying SHUTDOWN COMPLETE:
  188. * ...
  189. * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
  190. * if the Verification Tag field of the packet matches its own tag and
  191. * the T bit is not set
  192. * OR
  193. * it is set to its peer's tag and the T bit is set in the Chunk
  194. * Flags.
  195. * Otherwise, the receiver MUST silently discard the packet
  196. * and take no further action. An endpoint MUST ignore the
  197. * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
  198. *
  199. * Inputs
  200. * (endpoint, asoc, chunk)
  201. *
  202. * Outputs
  203. * (asoc, reply_msg, msg_up, timers, counters)
  204. *
  205. * The return value is the disposition of the chunk.
  206. */
  207. enum sctp_disposition sctp_sf_do_4_C(struct net *net,
  208. const struct sctp_endpoint *ep,
  209. const struct sctp_association *asoc,
  210. const union sctp_subtype type,
  211. void *arg, struct sctp_cmd_seq *commands)
  212. {
  213. struct sctp_chunk *chunk = arg;
  214. struct sctp_ulpevent *ev;
  215. if (!sctp_vtag_verify_either(chunk, asoc))
  216. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  217. /* RFC 2960 6.10 Bundling
  218. *
  219. * An endpoint MUST NOT bundle INIT, INIT ACK or
  220. * SHUTDOWN COMPLETE with any other chunks.
  221. */
  222. if (!chunk->singleton)
  223. return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
  224. /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
  225. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  226. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  227. commands);
  228. /* RFC 2960 10.2 SCTP-to-ULP
  229. *
  230. * H) SHUTDOWN COMPLETE notification
  231. *
  232. * When SCTP completes the shutdown procedures (section 9.2) this
  233. * notification is passed to the upper layer.
  234. */
  235. ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
  236. 0, 0, 0, NULL, GFP_ATOMIC);
  237. if (ev)
  238. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  239. SCTP_ULPEVENT(ev));
  240. /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
  241. * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
  242. * not the chunk should be discarded. If the endpoint is in
  243. * the SHUTDOWN-ACK-SENT state the endpoint should stop the
  244. * T2-shutdown timer and remove all knowledge of the
  245. * association (and thus the association enters the CLOSED
  246. * state).
  247. */
  248. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  249. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  250. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  251. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  252. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  253. SCTP_STATE(SCTP_STATE_CLOSED));
  254. SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
  255. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  256. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  257. return SCTP_DISPOSITION_DELETE_TCB;
  258. }
  259. /*
  260. * Respond to a normal INIT chunk.
  261. * We are the side that is being asked for an association.
  262. *
  263. * Section: 5.1 Normal Establishment of an Association, B
  264. * B) "Z" shall respond immediately with an INIT ACK chunk. The
  265. * destination IP address of the INIT ACK MUST be set to the source
  266. * IP address of the INIT to which this INIT ACK is responding. In
  267. * the response, besides filling in other parameters, "Z" must set the
  268. * Verification Tag field to Tag_A, and also provide its own
  269. * Verification Tag (Tag_Z) in the Initiate Tag field.
  270. *
  271. * Verification Tag: Must be 0.
  272. *
  273. * Inputs
  274. * (endpoint, asoc, chunk)
  275. *
  276. * Outputs
  277. * (asoc, reply_msg, msg_up, timers, counters)
  278. *
  279. * The return value is the disposition of the chunk.
  280. */
  281. enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
  282. const struct sctp_endpoint *ep,
  283. const struct sctp_association *asoc,
  284. const union sctp_subtype type,
  285. void *arg,
  286. struct sctp_cmd_seq *commands)
  287. {
  288. struct sctp_chunk *chunk = arg, *repl, *err_chunk;
  289. struct sctp_unrecognized_param *unk_param;
  290. struct sctp_association *new_asoc;
  291. struct sctp_packet *packet;
  292. int len;
  293. /* 6.10 Bundling
  294. * An endpoint MUST NOT bundle INIT, INIT ACK or
  295. * SHUTDOWN COMPLETE with any other chunks.
  296. *
  297. * IG Section 2.11.2
  298. * Furthermore, we require that the receiver of an INIT chunk MUST
  299. * enforce these rules by silently discarding an arriving packet
  300. * with an INIT chunk that is bundled with other chunks.
  301. */
  302. if (!chunk->singleton)
  303. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  304. /* If the packet is an OOTB packet which is temporarily on the
  305. * control endpoint, respond with an ABORT.
  306. */
  307. if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
  308. SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
  309. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  310. }
  311. /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
  312. * Tag.
  313. */
  314. if (chunk->sctp_hdr->vtag != 0)
  315. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  316. /* Make sure that the INIT chunk has a valid length.
  317. * Normally, this would cause an ABORT with a Protocol Violation
  318. * error, but since we don't have an association, we'll
  319. * just discard the packet.
  320. */
  321. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
  322. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  323. /* If the INIT is coming toward a closing socket, we'll send back
  324. * and ABORT. Essentially, this catches the race of INIT being
  325. * backloged to the socket at the same time as the user isses close().
  326. * Since the socket and all its associations are going away, we
  327. * can treat this OOTB
  328. */
  329. if (sctp_sstate(ep->base.sk, CLOSING))
  330. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  331. /* Verify the INIT chunk before processing it. */
  332. err_chunk = NULL;
  333. if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
  334. (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
  335. &err_chunk)) {
  336. /* This chunk contains fatal error. It is to be discarded.
  337. * Send an ABORT, with causes if there is any.
  338. */
  339. if (err_chunk) {
  340. packet = sctp_abort_pkt_new(net, ep, asoc, arg,
  341. (__u8 *)(err_chunk->chunk_hdr) +
  342. sizeof(struct sctp_chunkhdr),
  343. ntohs(err_chunk->chunk_hdr->length) -
  344. sizeof(struct sctp_chunkhdr));
  345. sctp_chunk_free(err_chunk);
  346. if (packet) {
  347. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  348. SCTP_PACKET(packet));
  349. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  350. return SCTP_DISPOSITION_CONSUME;
  351. } else {
  352. return SCTP_DISPOSITION_NOMEM;
  353. }
  354. } else {
  355. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
  356. commands);
  357. }
  358. }
  359. /* Grab the INIT header. */
  360. chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
  361. /* Tag the variable length parameters. */
  362. chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
  363. new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
  364. if (!new_asoc)
  365. goto nomem;
  366. if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
  367. sctp_scope(sctp_source(chunk)),
  368. GFP_ATOMIC) < 0)
  369. goto nomem_init;
  370. /* The call, sctp_process_init(), can fail on memory allocation. */
  371. if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
  372. (struct sctp_init_chunk *)chunk->chunk_hdr,
  373. GFP_ATOMIC))
  374. goto nomem_init;
  375. /* B) "Z" shall respond immediately with an INIT ACK chunk. */
  376. /* If there are errors need to be reported for unknown parameters,
  377. * make sure to reserve enough room in the INIT ACK for them.
  378. */
  379. len = 0;
  380. if (err_chunk)
  381. len = ntohs(err_chunk->chunk_hdr->length) -
  382. sizeof(struct sctp_chunkhdr);
  383. repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
  384. if (!repl)
  385. goto nomem_init;
  386. /* If there are errors need to be reported for unknown parameters,
  387. * include them in the outgoing INIT ACK as "Unrecognized parameter"
  388. * parameter.
  389. */
  390. if (err_chunk) {
  391. /* Get the "Unrecognized parameter" parameter(s) out of the
  392. * ERROR chunk generated by sctp_verify_init(). Since the
  393. * error cause code for "unknown parameter" and the
  394. * "Unrecognized parameter" type is the same, we can
  395. * construct the parameters in INIT ACK by copying the
  396. * ERROR causes over.
  397. */
  398. unk_param = (struct sctp_unrecognized_param *)
  399. ((__u8 *)(err_chunk->chunk_hdr) +
  400. sizeof(struct sctp_chunkhdr));
  401. /* Replace the cause code with the "Unrecognized parameter"
  402. * parameter type.
  403. */
  404. sctp_addto_chunk(repl, len, unk_param);
  405. sctp_chunk_free(err_chunk);
  406. }
  407. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
  408. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  409. /*
  410. * Note: After sending out INIT ACK with the State Cookie parameter,
  411. * "Z" MUST NOT allocate any resources, nor keep any states for the
  412. * new association. Otherwise, "Z" will be vulnerable to resource
  413. * attacks.
  414. */
  415. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  416. return SCTP_DISPOSITION_DELETE_TCB;
  417. nomem_init:
  418. sctp_association_free(new_asoc);
  419. nomem:
  420. if (err_chunk)
  421. sctp_chunk_free(err_chunk);
  422. return SCTP_DISPOSITION_NOMEM;
  423. }
  424. /*
  425. * Respond to a normal INIT ACK chunk.
  426. * We are the side that is initiating the association.
  427. *
  428. * Section: 5.1 Normal Establishment of an Association, C
  429. * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
  430. * timer and leave COOKIE-WAIT state. "A" shall then send the State
  431. * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
  432. * the T1-cookie timer, and enter the COOKIE-ECHOED state.
  433. *
  434. * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
  435. * DATA chunks, but it MUST be the first chunk in the packet and
  436. * until the COOKIE ACK is returned the sender MUST NOT send any
  437. * other packets to the peer.
  438. *
  439. * Verification Tag: 3.3.3
  440. * If the value of the Initiate Tag in a received INIT ACK chunk is
  441. * found to be 0, the receiver MUST treat it as an error and close the
  442. * association by transmitting an ABORT.
  443. *
  444. * Inputs
  445. * (endpoint, asoc, chunk)
  446. *
  447. * Outputs
  448. * (asoc, reply_msg, msg_up, timers, counters)
  449. *
  450. * The return value is the disposition of the chunk.
  451. */
  452. enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
  453. const struct sctp_endpoint *ep,
  454. const struct sctp_association *asoc,
  455. const union sctp_subtype type,
  456. void *arg,
  457. struct sctp_cmd_seq *commands)
  458. {
  459. struct sctp_init_chunk *initchunk;
  460. struct sctp_chunk *chunk = arg;
  461. struct sctp_chunk *err_chunk;
  462. struct sctp_packet *packet;
  463. if (!sctp_vtag_verify(chunk, asoc))
  464. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  465. /* 6.10 Bundling
  466. * An endpoint MUST NOT bundle INIT, INIT ACK or
  467. * SHUTDOWN COMPLETE with any other chunks.
  468. */
  469. if (!chunk->singleton)
  470. return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
  471. /* Make sure that the INIT-ACK chunk has a valid length */
  472. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
  473. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  474. commands);
  475. /* Grab the INIT header. */
  476. chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
  477. /* Verify the INIT chunk before processing it. */
  478. err_chunk = NULL;
  479. if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
  480. (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
  481. &err_chunk)) {
  482. enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
  483. /* This chunk contains fatal error. It is to be discarded.
  484. * Send an ABORT, with causes. If there are no causes,
  485. * then there wasn't enough memory. Just terminate
  486. * the association.
  487. */
  488. if (err_chunk) {
  489. packet = sctp_abort_pkt_new(net, ep, asoc, arg,
  490. (__u8 *)(err_chunk->chunk_hdr) +
  491. sizeof(struct sctp_chunkhdr),
  492. ntohs(err_chunk->chunk_hdr->length) -
  493. sizeof(struct sctp_chunkhdr));
  494. sctp_chunk_free(err_chunk);
  495. if (packet) {
  496. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  497. SCTP_PACKET(packet));
  498. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  499. error = SCTP_ERROR_INV_PARAM;
  500. }
  501. }
  502. /* SCTP-AUTH, Section 6.3:
  503. * It should be noted that if the receiver wants to tear
  504. * down an association in an authenticated way only, the
  505. * handling of malformed packets should not result in
  506. * tearing down the association.
  507. *
  508. * This means that if we only want to abort associations
  509. * in an authenticated way (i.e AUTH+ABORT), then we
  510. * can't destroy this association just because the packet
  511. * was malformed.
  512. */
  513. if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
  514. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  515. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  516. return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
  517. asoc, chunk->transport);
  518. }
  519. /* Tag the variable length parameters. Note that we never
  520. * convert the parameters in an INIT chunk.
  521. */
  522. chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
  523. initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
  524. sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
  525. SCTP_PEER_INIT(initchunk));
  526. /* Reset init error count upon receipt of INIT-ACK. */
  527. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
  528. /* 5.1 C) "A" shall stop the T1-init timer and leave
  529. * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
  530. * timer, and enter the COOKIE-ECHOED state.
  531. */
  532. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  533. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  534. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  535. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  536. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  537. SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
  538. /* SCTP-AUTH: genereate the assocition shared keys so that
  539. * we can potentially signe the COOKIE-ECHO.
  540. */
  541. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
  542. /* 5.1 C) "A" shall then send the State Cookie received in the
  543. * INIT ACK chunk in a COOKIE ECHO chunk, ...
  544. */
  545. /* If there is any errors to report, send the ERROR chunk generated
  546. * for unknown parameters as well.
  547. */
  548. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
  549. SCTP_CHUNK(err_chunk));
  550. return SCTP_DISPOSITION_CONSUME;
  551. }
  552. /*
  553. * Respond to a normal COOKIE ECHO chunk.
  554. * We are the side that is being asked for an association.
  555. *
  556. * Section: 5.1 Normal Establishment of an Association, D
  557. * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
  558. * with a COOKIE ACK chunk after building a TCB and moving to
  559. * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
  560. * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
  561. * chunk MUST be the first chunk in the packet.
  562. *
  563. * IMPLEMENTATION NOTE: An implementation may choose to send the
  564. * Communication Up notification to the SCTP user upon reception
  565. * of a valid COOKIE ECHO chunk.
  566. *
  567. * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
  568. * D) Rules for packet carrying a COOKIE ECHO
  569. *
  570. * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
  571. * Initial Tag received in the INIT ACK.
  572. *
  573. * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
  574. *
  575. * Inputs
  576. * (endpoint, asoc, chunk)
  577. *
  578. * Outputs
  579. * (asoc, reply_msg, msg_up, timers, counters)
  580. *
  581. * The return value is the disposition of the chunk.
  582. */
  583. enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
  584. const struct sctp_endpoint *ep,
  585. const struct sctp_association *asoc,
  586. const union sctp_subtype type,
  587. void *arg,
  588. struct sctp_cmd_seq *commands)
  589. {
  590. struct sctp_ulpevent *ev, *ai_ev = NULL;
  591. struct sctp_association *new_asoc;
  592. struct sctp_init_chunk *peer_init;
  593. struct sctp_chunk *chunk = arg;
  594. struct sctp_chunk *err_chk_p;
  595. struct sctp_chunk *repl;
  596. struct sock *sk;
  597. int error = 0;
  598. /* If the packet is an OOTB packet which is temporarily on the
  599. * control endpoint, respond with an ABORT.
  600. */
  601. if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
  602. SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
  603. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  604. }
  605. /* Make sure that the COOKIE_ECHO chunk has a valid length.
  606. * In this case, we check that we have enough for at least a
  607. * chunk header. More detailed verification is done
  608. * in sctp_unpack_cookie().
  609. */
  610. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  611. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  612. /* If the endpoint is not listening or if the number of associations
  613. * on the TCP-style socket exceed the max backlog, respond with an
  614. * ABORT.
  615. */
  616. sk = ep->base.sk;
  617. if (!sctp_sstate(sk, LISTENING) ||
  618. (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
  619. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  620. /* "Decode" the chunk. We have no optional parameters so we
  621. * are in good shape.
  622. */
  623. chunk->subh.cookie_hdr =
  624. (struct sctp_signed_cookie *)chunk->skb->data;
  625. if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
  626. sizeof(struct sctp_chunkhdr)))
  627. goto nomem;
  628. /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
  629. * "Z" will reply with a COOKIE ACK chunk after building a TCB
  630. * and moving to the ESTABLISHED state.
  631. */
  632. new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
  633. &err_chk_p);
  634. /* FIXME:
  635. * If the re-build failed, what is the proper error path
  636. * from here?
  637. *
  638. * [We should abort the association. --piggy]
  639. */
  640. if (!new_asoc) {
  641. /* FIXME: Several errors are possible. A bad cookie should
  642. * be silently discarded, but think about logging it too.
  643. */
  644. switch (error) {
  645. case -SCTP_IERROR_NOMEM:
  646. goto nomem;
  647. case -SCTP_IERROR_STALE_COOKIE:
  648. sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
  649. err_chk_p);
  650. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  651. case -SCTP_IERROR_BAD_SIG:
  652. default:
  653. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  654. }
  655. }
  656. /* Delay state machine commands until later.
  657. *
  658. * Re-build the bind address for the association is done in
  659. * the sctp_unpack_cookie() already.
  660. */
  661. /* This is a brand-new association, so these are not yet side
  662. * effects--it is safe to run them here.
  663. */
  664. peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
  665. if (!sctp_process_init(new_asoc, chunk,
  666. &chunk->subh.cookie_hdr->c.peer_addr,
  667. peer_init, GFP_ATOMIC))
  668. goto nomem_init;
  669. /* SCTP-AUTH: Now that we've populate required fields in
  670. * sctp_process_init, set up the assocaition shared keys as
  671. * necessary so that we can potentially authenticate the ACK
  672. */
  673. error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
  674. if (error)
  675. goto nomem_init;
  676. /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
  677. * is supposed to be authenticated and we have to do delayed
  678. * authentication. We've just recreated the association using
  679. * the information in the cookie and now it's much easier to
  680. * do the authentication.
  681. */
  682. if (chunk->auth_chunk) {
  683. struct sctp_chunk auth;
  684. enum sctp_ierror ret;
  685. /* Make sure that we and the peer are AUTH capable */
  686. if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
  687. sctp_association_free(new_asoc);
  688. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  689. }
  690. /* set-up our fake chunk so that we can process it */
  691. auth.skb = chunk->auth_chunk;
  692. auth.asoc = chunk->asoc;
  693. auth.sctp_hdr = chunk->sctp_hdr;
  694. auth.chunk_hdr = (struct sctp_chunkhdr *)
  695. skb_push(chunk->auth_chunk,
  696. sizeof(struct sctp_chunkhdr));
  697. skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
  698. auth.transport = chunk->transport;
  699. ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
  700. if (ret != SCTP_IERROR_NO_ERROR) {
  701. sctp_association_free(new_asoc);
  702. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  703. }
  704. }
  705. repl = sctp_make_cookie_ack(new_asoc, chunk);
  706. if (!repl)
  707. goto nomem_init;
  708. /* RFC 2960 5.1 Normal Establishment of an Association
  709. *
  710. * D) IMPLEMENTATION NOTE: An implementation may choose to
  711. * send the Communication Up notification to the SCTP user
  712. * upon reception of a valid COOKIE ECHO chunk.
  713. */
  714. ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
  715. new_asoc->c.sinit_num_ostreams,
  716. new_asoc->c.sinit_max_instreams,
  717. NULL, GFP_ATOMIC);
  718. if (!ev)
  719. goto nomem_ev;
  720. /* Sockets API Draft Section 5.3.1.6
  721. * When a peer sends a Adaptation Layer Indication parameter , SCTP
  722. * delivers this notification to inform the application that of the
  723. * peers requested adaptation layer.
  724. */
  725. if (new_asoc->peer.adaptation_ind) {
  726. ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
  727. GFP_ATOMIC);
  728. if (!ai_ev)
  729. goto nomem_aiev;
  730. }
  731. /* Add all the state machine commands now since we've created
  732. * everything. This way we don't introduce memory corruptions
  733. * during side-effect processing and correclty count established
  734. * associations.
  735. */
  736. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
  737. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  738. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  739. SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
  740. SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
  741. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
  742. if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
  743. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  744. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  745. /* This will send the COOKIE ACK */
  746. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  747. /* Queue the ASSOC_CHANGE event */
  748. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  749. /* Send up the Adaptation Layer Indication event */
  750. if (ai_ev)
  751. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  752. SCTP_ULPEVENT(ai_ev));
  753. return SCTP_DISPOSITION_CONSUME;
  754. nomem_aiev:
  755. sctp_ulpevent_free(ev);
  756. nomem_ev:
  757. sctp_chunk_free(repl);
  758. nomem_init:
  759. sctp_association_free(new_asoc);
  760. nomem:
  761. return SCTP_DISPOSITION_NOMEM;
  762. }
  763. /*
  764. * Respond to a normal COOKIE ACK chunk.
  765. * We are the side that is asking for an association.
  766. *
  767. * RFC 2960 5.1 Normal Establishment of an Association
  768. *
  769. * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
  770. * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
  771. * timer. It may also notify its ULP about the successful
  772. * establishment of the association with a Communication Up
  773. * notification (see Section 10).
  774. *
  775. * Verification Tag:
  776. * Inputs
  777. * (endpoint, asoc, chunk)
  778. *
  779. * Outputs
  780. * (asoc, reply_msg, msg_up, timers, counters)
  781. *
  782. * The return value is the disposition of the chunk.
  783. */
  784. enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
  785. const struct sctp_endpoint *ep,
  786. const struct sctp_association *asoc,
  787. const union sctp_subtype type,
  788. void *arg,
  789. struct sctp_cmd_seq *commands)
  790. {
  791. struct sctp_chunk *chunk = arg;
  792. struct sctp_ulpevent *ev;
  793. if (!sctp_vtag_verify(chunk, asoc))
  794. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  795. /* Verify that the chunk length for the COOKIE-ACK is OK.
  796. * If we don't do this, any bundled chunks may be junked.
  797. */
  798. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  799. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  800. commands);
  801. /* Reset init error count upon receipt of COOKIE-ACK,
  802. * to avoid problems with the managemement of this
  803. * counter in stale cookie situations when a transition back
  804. * from the COOKIE-ECHOED state to the COOKIE-WAIT
  805. * state is performed.
  806. */
  807. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
  808. /* RFC 2960 5.1 Normal Establishment of an Association
  809. *
  810. * E) Upon reception of the COOKIE ACK, endpoint "A" will move
  811. * from the COOKIE-ECHOED state to the ESTABLISHED state,
  812. * stopping the T1-cookie timer.
  813. */
  814. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  815. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  816. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  817. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  818. SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
  819. SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
  820. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
  821. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
  822. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  823. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  824. /* It may also notify its ULP about the successful
  825. * establishment of the association with a Communication Up
  826. * notification (see Section 10).
  827. */
  828. ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
  829. 0, asoc->c.sinit_num_ostreams,
  830. asoc->c.sinit_max_instreams,
  831. NULL, GFP_ATOMIC);
  832. if (!ev)
  833. goto nomem;
  834. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  835. /* Sockets API Draft Section 5.3.1.6
  836. * When a peer sends a Adaptation Layer Indication parameter , SCTP
  837. * delivers this notification to inform the application that of the
  838. * peers requested adaptation layer.
  839. */
  840. if (asoc->peer.adaptation_ind) {
  841. ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
  842. if (!ev)
  843. goto nomem;
  844. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  845. SCTP_ULPEVENT(ev));
  846. }
  847. return SCTP_DISPOSITION_CONSUME;
  848. nomem:
  849. return SCTP_DISPOSITION_NOMEM;
  850. }
  851. /* Generate and sendout a heartbeat packet. */
  852. static enum sctp_disposition sctp_sf_heartbeat(
  853. const struct sctp_endpoint *ep,
  854. const struct sctp_association *asoc,
  855. const union sctp_subtype type,
  856. void *arg,
  857. struct sctp_cmd_seq *commands)
  858. {
  859. struct sctp_transport *transport = (struct sctp_transport *) arg;
  860. struct sctp_chunk *reply;
  861. /* Send a heartbeat to our peer. */
  862. reply = sctp_make_heartbeat(asoc, transport);
  863. if (!reply)
  864. return SCTP_DISPOSITION_NOMEM;
  865. /* Set rto_pending indicating that an RTT measurement
  866. * is started with this heartbeat chunk.
  867. */
  868. sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
  869. SCTP_TRANSPORT(transport));
  870. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  871. return SCTP_DISPOSITION_CONSUME;
  872. }
  873. /* Generate a HEARTBEAT packet on the given transport. */
  874. enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
  875. const struct sctp_endpoint *ep,
  876. const struct sctp_association *asoc,
  877. const union sctp_subtype type,
  878. void *arg,
  879. struct sctp_cmd_seq *commands)
  880. {
  881. struct sctp_transport *transport = (struct sctp_transport *) arg;
  882. if (asoc->overall_error_count >= asoc->max_retrans) {
  883. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  884. SCTP_ERROR(ETIMEDOUT));
  885. /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  886. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  887. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  888. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  889. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  890. return SCTP_DISPOSITION_DELETE_TCB;
  891. }
  892. /* Section 3.3.5.
  893. * The Sender-specific Heartbeat Info field should normally include
  894. * information about the sender's current time when this HEARTBEAT
  895. * chunk is sent and the destination transport address to which this
  896. * HEARTBEAT is sent (see Section 8.3).
  897. */
  898. if (transport->param_flags & SPP_HB_ENABLE) {
  899. if (SCTP_DISPOSITION_NOMEM ==
  900. sctp_sf_heartbeat(ep, asoc, type, arg,
  901. commands))
  902. return SCTP_DISPOSITION_NOMEM;
  903. /* Set transport error counter and association error counter
  904. * when sending heartbeat.
  905. */
  906. sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
  907. SCTP_TRANSPORT(transport));
  908. }
  909. sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
  910. SCTP_TRANSPORT(transport));
  911. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
  912. SCTP_TRANSPORT(transport));
  913. return SCTP_DISPOSITION_CONSUME;
  914. }
  915. /* resend asoc strreset_chunk. */
  916. enum sctp_disposition sctp_sf_send_reconf(struct net *net,
  917. const struct sctp_endpoint *ep,
  918. const struct sctp_association *asoc,
  919. const union sctp_subtype type,
  920. void *arg,
  921. struct sctp_cmd_seq *commands)
  922. {
  923. struct sctp_transport *transport = arg;
  924. if (asoc->overall_error_count >= asoc->max_retrans) {
  925. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  926. SCTP_ERROR(ETIMEDOUT));
  927. /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  928. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  929. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  930. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  931. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  932. return SCTP_DISPOSITION_DELETE_TCB;
  933. }
  934. sctp_chunk_hold(asoc->strreset_chunk);
  935. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  936. SCTP_CHUNK(asoc->strreset_chunk));
  937. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
  938. return SCTP_DISPOSITION_CONSUME;
  939. }
  940. /*
  941. * Process an heartbeat request.
  942. *
  943. * Section: 8.3 Path Heartbeat
  944. * The receiver of the HEARTBEAT should immediately respond with a
  945. * HEARTBEAT ACK that contains the Heartbeat Information field copied
  946. * from the received HEARTBEAT chunk.
  947. *
  948. * Verification Tag: 8.5 Verification Tag [Normal verification]
  949. * When receiving an SCTP packet, the endpoint MUST ensure that the
  950. * value in the Verification Tag field of the received SCTP packet
  951. * matches its own Tag. If the received Verification Tag value does not
  952. * match the receiver's own tag value, the receiver shall silently
  953. * discard the packet and shall not process it any further except for
  954. * those cases listed in Section 8.5.1 below.
  955. *
  956. * Inputs
  957. * (endpoint, asoc, chunk)
  958. *
  959. * Outputs
  960. * (asoc, reply_msg, msg_up, timers, counters)
  961. *
  962. * The return value is the disposition of the chunk.
  963. */
  964. enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
  965. const struct sctp_endpoint *ep,
  966. const struct sctp_association *asoc,
  967. const union sctp_subtype type,
  968. void *arg, struct sctp_cmd_seq *commands)
  969. {
  970. struct sctp_paramhdr *param_hdr;
  971. struct sctp_chunk *chunk = arg;
  972. struct sctp_chunk *reply;
  973. size_t paylen = 0;
  974. if (!sctp_vtag_verify(chunk, asoc))
  975. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  976. /* Make sure that the HEARTBEAT chunk has a valid length. */
  977. if (!sctp_chunk_length_valid(chunk,
  978. sizeof(struct sctp_heartbeat_chunk)))
  979. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  980. commands);
  981. /* 8.3 The receiver of the HEARTBEAT should immediately
  982. * respond with a HEARTBEAT ACK that contains the Heartbeat
  983. * Information field copied from the received HEARTBEAT chunk.
  984. */
  985. chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
  986. param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
  987. paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
  988. if (ntohs(param_hdr->length) > paylen)
  989. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  990. param_hdr, commands);
  991. if (!pskb_pull(chunk->skb, paylen))
  992. goto nomem;
  993. reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
  994. if (!reply)
  995. goto nomem;
  996. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  997. return SCTP_DISPOSITION_CONSUME;
  998. nomem:
  999. return SCTP_DISPOSITION_NOMEM;
  1000. }
  1001. /*
  1002. * Process the returning HEARTBEAT ACK.
  1003. *
  1004. * Section: 8.3 Path Heartbeat
  1005. * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
  1006. * should clear the error counter of the destination transport
  1007. * address to which the HEARTBEAT was sent, and mark the destination
  1008. * transport address as active if it is not so marked. The endpoint may
  1009. * optionally report to the upper layer when an inactive destination
  1010. * address is marked as active due to the reception of the latest
  1011. * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
  1012. * clear the association overall error count as well (as defined
  1013. * in section 8.1).
  1014. *
  1015. * The receiver of the HEARTBEAT ACK should also perform an RTT
  1016. * measurement for that destination transport address using the time
  1017. * value carried in the HEARTBEAT ACK chunk.
  1018. *
  1019. * Verification Tag: 8.5 Verification Tag [Normal verification]
  1020. *
  1021. * Inputs
  1022. * (endpoint, asoc, chunk)
  1023. *
  1024. * Outputs
  1025. * (asoc, reply_msg, msg_up, timers, counters)
  1026. *
  1027. * The return value is the disposition of the chunk.
  1028. */
  1029. enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
  1030. const struct sctp_endpoint *ep,
  1031. const struct sctp_association *asoc,
  1032. const union sctp_subtype type,
  1033. void *arg,
  1034. struct sctp_cmd_seq *commands)
  1035. {
  1036. struct sctp_sender_hb_info *hbinfo;
  1037. struct sctp_chunk *chunk = arg;
  1038. struct sctp_transport *link;
  1039. unsigned long max_interval;
  1040. union sctp_addr from_addr;
  1041. if (!sctp_vtag_verify(chunk, asoc))
  1042. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1043. /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
  1044. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
  1045. sizeof(*hbinfo)))
  1046. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  1047. commands);
  1048. hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
  1049. /* Make sure that the length of the parameter is what we expect */
  1050. if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
  1051. return SCTP_DISPOSITION_DISCARD;
  1052. from_addr = hbinfo->daddr;
  1053. link = sctp_assoc_lookup_paddr(asoc, &from_addr);
  1054. /* This should never happen, but lets log it if so. */
  1055. if (unlikely(!link)) {
  1056. if (from_addr.sa.sa_family == AF_INET6) {
  1057. net_warn_ratelimited("%s association %p could not find address %pI6\n",
  1058. __func__,
  1059. asoc,
  1060. &from_addr.v6.sin6_addr);
  1061. } else {
  1062. net_warn_ratelimited("%s association %p could not find address %pI4\n",
  1063. __func__,
  1064. asoc,
  1065. &from_addr.v4.sin_addr.s_addr);
  1066. }
  1067. return SCTP_DISPOSITION_DISCARD;
  1068. }
  1069. /* Validate the 64-bit random nonce. */
  1070. if (hbinfo->hb_nonce != link->hb_nonce)
  1071. return SCTP_DISPOSITION_DISCARD;
  1072. max_interval = link->hbinterval + link->rto;
  1073. /* Check if the timestamp looks valid. */
  1074. if (time_after(hbinfo->sent_at, jiffies) ||
  1075. time_after(jiffies, hbinfo->sent_at + max_interval)) {
  1076. pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
  1077. "for transport:%p\n", __func__, link);
  1078. return SCTP_DISPOSITION_DISCARD;
  1079. }
  1080. /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
  1081. * the HEARTBEAT should clear the error counter of the
  1082. * destination transport address to which the HEARTBEAT was
  1083. * sent and mark the destination transport address as active if
  1084. * it is not so marked.
  1085. */
  1086. sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
  1087. return SCTP_DISPOSITION_CONSUME;
  1088. }
  1089. /* Helper function to send out an abort for the restart
  1090. * condition.
  1091. */
  1092. static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
  1093. struct sctp_chunk *init,
  1094. struct sctp_cmd_seq *commands)
  1095. {
  1096. struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
  1097. union sctp_addr_param *addrparm;
  1098. struct sctp_errhdr *errhdr;
  1099. char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
  1100. struct sctp_endpoint *ep;
  1101. struct sctp_packet *pkt;
  1102. int len;
  1103. /* Build the error on the stack. We are way to malloc crazy
  1104. * throughout the code today.
  1105. */
  1106. errhdr = (struct sctp_errhdr *)buffer;
  1107. addrparm = (union sctp_addr_param *)errhdr->variable;
  1108. /* Copy into a parm format. */
  1109. len = af->to_addr_param(ssa, addrparm);
  1110. len += sizeof(*errhdr);
  1111. errhdr->cause = SCTP_ERROR_RESTART;
  1112. errhdr->length = htons(len);
  1113. /* Assign to the control socket. */
  1114. ep = sctp_sk(net->sctp.ctl_sock)->ep;
  1115. /* Association is NULL since this may be a restart attack and we
  1116. * want to send back the attacker's vtag.
  1117. */
  1118. pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
  1119. if (!pkt)
  1120. goto out;
  1121. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
  1122. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  1123. /* Discard the rest of the inbound packet. */
  1124. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  1125. out:
  1126. /* Even if there is no memory, treat as a failure so
  1127. * the packet will get dropped.
  1128. */
  1129. return 0;
  1130. }
  1131. static bool list_has_sctp_addr(const struct list_head *list,
  1132. union sctp_addr *ipaddr)
  1133. {
  1134. struct sctp_transport *addr;
  1135. list_for_each_entry(addr, list, transports) {
  1136. if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
  1137. return true;
  1138. }
  1139. return false;
  1140. }
  1141. /* A restart is occurring, check to make sure no new addresses
  1142. * are being added as we may be under a takeover attack.
  1143. */
  1144. static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
  1145. const struct sctp_association *asoc,
  1146. struct sctp_chunk *init,
  1147. struct sctp_cmd_seq *commands)
  1148. {
  1149. struct net *net = sock_net(new_asoc->base.sk);
  1150. struct sctp_transport *new_addr;
  1151. int ret = 1;
  1152. /* Implementor's Guide - Section 5.2.2
  1153. * ...
  1154. * Before responding the endpoint MUST check to see if the
  1155. * unexpected INIT adds new addresses to the association. If new
  1156. * addresses are added to the association, the endpoint MUST respond
  1157. * with an ABORT..
  1158. */
  1159. /* Search through all current addresses and make sure
  1160. * we aren't adding any new ones.
  1161. */
  1162. list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
  1163. transports) {
  1164. if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
  1165. &new_addr->ipaddr)) {
  1166. sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
  1167. commands);
  1168. ret = 0;
  1169. break;
  1170. }
  1171. }
  1172. /* Return success if all addresses were found. */
  1173. return ret;
  1174. }
  1175. /* Populate the verification/tie tags based on overlapping INIT
  1176. * scenario.
  1177. *
  1178. * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
  1179. */
  1180. static void sctp_tietags_populate(struct sctp_association *new_asoc,
  1181. const struct sctp_association *asoc)
  1182. {
  1183. switch (asoc->state) {
  1184. /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
  1185. case SCTP_STATE_COOKIE_WAIT:
  1186. new_asoc->c.my_vtag = asoc->c.my_vtag;
  1187. new_asoc->c.my_ttag = asoc->c.my_vtag;
  1188. new_asoc->c.peer_ttag = 0;
  1189. break;
  1190. case SCTP_STATE_COOKIE_ECHOED:
  1191. new_asoc->c.my_vtag = asoc->c.my_vtag;
  1192. new_asoc->c.my_ttag = asoc->c.my_vtag;
  1193. new_asoc->c.peer_ttag = asoc->c.peer_vtag;
  1194. break;
  1195. /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
  1196. * COOKIE-WAIT and SHUTDOWN-ACK-SENT
  1197. */
  1198. default:
  1199. new_asoc->c.my_ttag = asoc->c.my_vtag;
  1200. new_asoc->c.peer_ttag = asoc->c.peer_vtag;
  1201. break;
  1202. }
  1203. /* Other parameters for the endpoint SHOULD be copied from the
  1204. * existing parameters of the association (e.g. number of
  1205. * outbound streams) into the INIT ACK and cookie.
  1206. */
  1207. new_asoc->rwnd = asoc->rwnd;
  1208. new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
  1209. new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
  1210. new_asoc->c.initial_tsn = asoc->c.initial_tsn;
  1211. }
  1212. /*
  1213. * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
  1214. * handling action.
  1215. *
  1216. * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
  1217. *
  1218. * Returns value representing action to be taken. These action values
  1219. * correspond to Action/Description values in RFC 2960, Table 2.
  1220. */
  1221. static char sctp_tietags_compare(struct sctp_association *new_asoc,
  1222. const struct sctp_association *asoc)
  1223. {
  1224. /* In this case, the peer may have restarted. */
  1225. if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
  1226. (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
  1227. (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
  1228. (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
  1229. return 'A';
  1230. /* Collision case B. */
  1231. if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
  1232. ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
  1233. (0 == asoc->c.peer_vtag))) {
  1234. return 'B';
  1235. }
  1236. /* Collision case D. */
  1237. if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
  1238. (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
  1239. return 'D';
  1240. /* Collision case C. */
  1241. if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
  1242. (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
  1243. (0 == new_asoc->c.my_ttag) &&
  1244. (0 == new_asoc->c.peer_ttag))
  1245. return 'C';
  1246. /* No match to any of the special cases; discard this packet. */
  1247. return 'E';
  1248. }
  1249. /* Common helper routine for both duplicate and simulataneous INIT
  1250. * chunk handling.
  1251. */
  1252. static enum sctp_disposition sctp_sf_do_unexpected_init(
  1253. struct net *net,
  1254. const struct sctp_endpoint *ep,
  1255. const struct sctp_association *asoc,
  1256. const union sctp_subtype type,
  1257. void *arg,
  1258. struct sctp_cmd_seq *commands)
  1259. {
  1260. struct sctp_chunk *chunk = arg, *repl, *err_chunk;
  1261. struct sctp_unrecognized_param *unk_param;
  1262. struct sctp_association *new_asoc;
  1263. enum sctp_disposition retval;
  1264. struct sctp_packet *packet;
  1265. int len;
  1266. /* 6.10 Bundling
  1267. * An endpoint MUST NOT bundle INIT, INIT ACK or
  1268. * SHUTDOWN COMPLETE with any other chunks.
  1269. *
  1270. * IG Section 2.11.2
  1271. * Furthermore, we require that the receiver of an INIT chunk MUST
  1272. * enforce these rules by silently discarding an arriving packet
  1273. * with an INIT chunk that is bundled with other chunks.
  1274. */
  1275. if (!chunk->singleton)
  1276. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1277. /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
  1278. * Tag.
  1279. */
  1280. if (chunk->sctp_hdr->vtag != 0)
  1281. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  1282. /* Make sure that the INIT chunk has a valid length.
  1283. * In this case, we generate a protocol violation since we have
  1284. * an association established.
  1285. */
  1286. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
  1287. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  1288. commands);
  1289. /* Grab the INIT header. */
  1290. chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
  1291. /* Tag the variable length parameters. */
  1292. chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
  1293. /* Verify the INIT chunk before processing it. */
  1294. err_chunk = NULL;
  1295. if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
  1296. (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
  1297. &err_chunk)) {
  1298. /* This chunk contains fatal error. It is to be discarded.
  1299. * Send an ABORT, with causes if there is any.
  1300. */
  1301. if (err_chunk) {
  1302. packet = sctp_abort_pkt_new(net, ep, asoc, arg,
  1303. (__u8 *)(err_chunk->chunk_hdr) +
  1304. sizeof(struct sctp_chunkhdr),
  1305. ntohs(err_chunk->chunk_hdr->length) -
  1306. sizeof(struct sctp_chunkhdr));
  1307. if (packet) {
  1308. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  1309. SCTP_PACKET(packet));
  1310. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  1311. retval = SCTP_DISPOSITION_CONSUME;
  1312. } else {
  1313. retval = SCTP_DISPOSITION_NOMEM;
  1314. }
  1315. goto cleanup;
  1316. } else {
  1317. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
  1318. commands);
  1319. }
  1320. }
  1321. /*
  1322. * Other parameters for the endpoint SHOULD be copied from the
  1323. * existing parameters of the association (e.g. number of
  1324. * outbound streams) into the INIT ACK and cookie.
  1325. * FIXME: We are copying parameters from the endpoint not the
  1326. * association.
  1327. */
  1328. new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
  1329. if (!new_asoc)
  1330. goto nomem;
  1331. if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
  1332. sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
  1333. goto nomem;
  1334. /* In the outbound INIT ACK the endpoint MUST copy its current
  1335. * Verification Tag and Peers Verification tag into a reserved
  1336. * place (local tie-tag and per tie-tag) within the state cookie.
  1337. */
  1338. if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
  1339. (struct sctp_init_chunk *)chunk->chunk_hdr,
  1340. GFP_ATOMIC))
  1341. goto nomem;
  1342. /* Make sure no new addresses are being added during the
  1343. * restart. Do not do this check for COOKIE-WAIT state,
  1344. * since there are no peer addresses to check against.
  1345. * Upon return an ABORT will have been sent if needed.
  1346. */
  1347. if (!sctp_state(asoc, COOKIE_WAIT)) {
  1348. if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
  1349. commands)) {
  1350. retval = SCTP_DISPOSITION_CONSUME;
  1351. goto nomem_retval;
  1352. }
  1353. }
  1354. sctp_tietags_populate(new_asoc, asoc);
  1355. /* B) "Z" shall respond immediately with an INIT ACK chunk. */
  1356. /* If there are errors need to be reported for unknown parameters,
  1357. * make sure to reserve enough room in the INIT ACK for them.
  1358. */
  1359. len = 0;
  1360. if (err_chunk) {
  1361. len = ntohs(err_chunk->chunk_hdr->length) -
  1362. sizeof(struct sctp_chunkhdr);
  1363. }
  1364. repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
  1365. if (!repl)
  1366. goto nomem;
  1367. /* If there are errors need to be reported for unknown parameters,
  1368. * include them in the outgoing INIT ACK as "Unrecognized parameter"
  1369. * parameter.
  1370. */
  1371. if (err_chunk) {
  1372. /* Get the "Unrecognized parameter" parameter(s) out of the
  1373. * ERROR chunk generated by sctp_verify_init(). Since the
  1374. * error cause code for "unknown parameter" and the
  1375. * "Unrecognized parameter" type is the same, we can
  1376. * construct the parameters in INIT ACK by copying the
  1377. * ERROR causes over.
  1378. */
  1379. unk_param = (struct sctp_unrecognized_param *)
  1380. ((__u8 *)(err_chunk->chunk_hdr) +
  1381. sizeof(struct sctp_chunkhdr));
  1382. /* Replace the cause code with the "Unrecognized parameter"
  1383. * parameter type.
  1384. */
  1385. sctp_addto_chunk(repl, len, unk_param);
  1386. }
  1387. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
  1388. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1389. /*
  1390. * Note: After sending out INIT ACK with the State Cookie parameter,
  1391. * "Z" MUST NOT allocate any resources for this new association.
  1392. * Otherwise, "Z" will be vulnerable to resource attacks.
  1393. */
  1394. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  1395. retval = SCTP_DISPOSITION_CONSUME;
  1396. return retval;
  1397. nomem:
  1398. retval = SCTP_DISPOSITION_NOMEM;
  1399. nomem_retval:
  1400. if (new_asoc)
  1401. sctp_association_free(new_asoc);
  1402. cleanup:
  1403. if (err_chunk)
  1404. sctp_chunk_free(err_chunk);
  1405. return retval;
  1406. }
  1407. /*
  1408. * Handle simultaneous INIT.
  1409. * This means we started an INIT and then we got an INIT request from
  1410. * our peer.
  1411. *
  1412. * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
  1413. * This usually indicates an initialization collision, i.e., each
  1414. * endpoint is attempting, at about the same time, to establish an
  1415. * association with the other endpoint.
  1416. *
  1417. * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
  1418. * endpoint MUST respond with an INIT ACK using the same parameters it
  1419. * sent in its original INIT chunk (including its Verification Tag,
  1420. * unchanged). These original parameters are combined with those from the
  1421. * newly received INIT chunk. The endpoint shall also generate a State
  1422. * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
  1423. * INIT to calculate the State Cookie.
  1424. *
  1425. * After that, the endpoint MUST NOT change its state, the T1-init
  1426. * timer shall be left running and the corresponding TCB MUST NOT be
  1427. * destroyed. The normal procedures for handling State Cookies when
  1428. * a TCB exists will resolve the duplicate INITs to a single association.
  1429. *
  1430. * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
  1431. * its Tie-Tags with the Tag information of itself and its peer (see
  1432. * section 5.2.2 for a description of the Tie-Tags).
  1433. *
  1434. * Verification Tag: Not explicit, but an INIT can not have a valid
  1435. * verification tag, so we skip the check.
  1436. *
  1437. * Inputs
  1438. * (endpoint, asoc, chunk)
  1439. *
  1440. * Outputs
  1441. * (asoc, reply_msg, msg_up, timers, counters)
  1442. *
  1443. * The return value is the disposition of the chunk.
  1444. */
  1445. enum sctp_disposition sctp_sf_do_5_2_1_siminit(
  1446. struct net *net,
  1447. const struct sctp_endpoint *ep,
  1448. const struct sctp_association *asoc,
  1449. const union sctp_subtype type,
  1450. void *arg,
  1451. struct sctp_cmd_seq *commands)
  1452. {
  1453. /* Call helper to do the real work for both simulataneous and
  1454. * duplicate INIT chunk handling.
  1455. */
  1456. return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
  1457. }
  1458. /*
  1459. * Handle duplicated INIT messages. These are usually delayed
  1460. * restransmissions.
  1461. *
  1462. * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
  1463. * COOKIE-ECHOED and COOKIE-WAIT
  1464. *
  1465. * Unless otherwise stated, upon reception of an unexpected INIT for
  1466. * this association, the endpoint shall generate an INIT ACK with a
  1467. * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
  1468. * current Verification Tag and peer's Verification Tag into a reserved
  1469. * place within the state cookie. We shall refer to these locations as
  1470. * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
  1471. * containing this INIT ACK MUST carry a Verification Tag value equal to
  1472. * the Initiation Tag found in the unexpected INIT. And the INIT ACK
  1473. * MUST contain a new Initiation Tag (randomly generated see Section
  1474. * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
  1475. * existing parameters of the association (e.g. number of outbound
  1476. * streams) into the INIT ACK and cookie.
  1477. *
  1478. * After sending out the INIT ACK, the endpoint shall take no further
  1479. * actions, i.e., the existing association, including its current state,
  1480. * and the corresponding TCB MUST NOT be changed.
  1481. *
  1482. * Note: Only when a TCB exists and the association is not in a COOKIE-
  1483. * WAIT state are the Tie-Tags populated. For a normal association INIT
  1484. * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
  1485. * set to 0 (indicating that no previous TCB existed). The INIT ACK and
  1486. * State Cookie are populated as specified in section 5.2.1.
  1487. *
  1488. * Verification Tag: Not specified, but an INIT has no way of knowing
  1489. * what the verification tag could be, so we ignore it.
  1490. *
  1491. * Inputs
  1492. * (endpoint, asoc, chunk)
  1493. *
  1494. * Outputs
  1495. * (asoc, reply_msg, msg_up, timers, counters)
  1496. *
  1497. * The return value is the disposition of the chunk.
  1498. */
  1499. enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
  1500. struct net *net,
  1501. const struct sctp_endpoint *ep,
  1502. const struct sctp_association *asoc,
  1503. const union sctp_subtype type,
  1504. void *arg,
  1505. struct sctp_cmd_seq *commands)
  1506. {
  1507. /* Call helper to do the real work for both simulataneous and
  1508. * duplicate INIT chunk handling.
  1509. */
  1510. return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
  1511. }
  1512. /*
  1513. * Unexpected INIT-ACK handler.
  1514. *
  1515. * Section 5.2.3
  1516. * If an INIT ACK received by an endpoint in any state other than the
  1517. * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
  1518. * An unexpected INIT ACK usually indicates the processing of an old or
  1519. * duplicated INIT chunk.
  1520. */
  1521. enum sctp_disposition sctp_sf_do_5_2_3_initack(
  1522. struct net *net,
  1523. const struct sctp_endpoint *ep,
  1524. const struct sctp_association *asoc,
  1525. const union sctp_subtype type,
  1526. void *arg,
  1527. struct sctp_cmd_seq *commands)
  1528. {
  1529. /* Per the above section, we'll discard the chunk if we have an
  1530. * endpoint. If this is an OOTB INIT-ACK, treat it as such.
  1531. */
  1532. if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
  1533. return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
  1534. else
  1535. return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
  1536. }
  1537. /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
  1538. *
  1539. * Section 5.2.4
  1540. * A) In this case, the peer may have restarted.
  1541. */
  1542. static enum sctp_disposition sctp_sf_do_dupcook_a(
  1543. struct net *net,
  1544. const struct sctp_endpoint *ep,
  1545. const struct sctp_association *asoc,
  1546. struct sctp_chunk *chunk,
  1547. struct sctp_cmd_seq *commands,
  1548. struct sctp_association *new_asoc)
  1549. {
  1550. struct sctp_init_chunk *peer_init;
  1551. enum sctp_disposition disposition;
  1552. struct sctp_ulpevent *ev;
  1553. struct sctp_chunk *repl;
  1554. struct sctp_chunk *err;
  1555. /* new_asoc is a brand-new association, so these are not yet
  1556. * side effects--it is safe to run them here.
  1557. */
  1558. peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
  1559. if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
  1560. GFP_ATOMIC))
  1561. goto nomem;
  1562. /* Make sure no new addresses are being added during the
  1563. * restart. Though this is a pretty complicated attack
  1564. * since you'd have to get inside the cookie.
  1565. */
  1566. if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
  1567. return SCTP_DISPOSITION_CONSUME;
  1568. }
  1569. /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
  1570. * the peer has restarted (Action A), it MUST NOT setup a new
  1571. * association but instead resend the SHUTDOWN ACK and send an ERROR
  1572. * chunk with a "Cookie Received while Shutting Down" error cause to
  1573. * its peer.
  1574. */
  1575. if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
  1576. disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
  1577. SCTP_ST_CHUNK(chunk->chunk_hdr->type),
  1578. chunk, commands);
  1579. if (SCTP_DISPOSITION_NOMEM == disposition)
  1580. goto nomem;
  1581. err = sctp_make_op_error(asoc, chunk,
  1582. SCTP_ERROR_COOKIE_IN_SHUTDOWN,
  1583. NULL, 0, 0);
  1584. if (err)
  1585. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1586. SCTP_CHUNK(err));
  1587. return SCTP_DISPOSITION_CONSUME;
  1588. }
  1589. /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
  1590. * data. Consider the optional choice of resending of this data.
  1591. */
  1592. sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
  1593. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  1594. SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
  1595. sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
  1596. /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
  1597. * and ASCONF-ACK cache.
  1598. */
  1599. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  1600. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  1601. sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
  1602. repl = sctp_make_cookie_ack(new_asoc, chunk);
  1603. if (!repl)
  1604. goto nomem;
  1605. /* Report association restart to upper layer. */
  1606. ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
  1607. new_asoc->c.sinit_num_ostreams,
  1608. new_asoc->c.sinit_max_instreams,
  1609. NULL, GFP_ATOMIC);
  1610. if (!ev)
  1611. goto nomem_ev;
  1612. /* Update the content of current association. */
  1613. sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
  1614. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  1615. if (sctp_state(asoc, SHUTDOWN_PENDING) &&
  1616. (sctp_sstate(asoc->base.sk, CLOSING) ||
  1617. sock_flag(asoc->base.sk, SOCK_DEAD))) {
  1618. /* if were currently in SHUTDOWN_PENDING, but the socket
  1619. * has been closed by user, don't transition to ESTABLISHED.
  1620. * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
  1621. */
  1622. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1623. return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
  1624. SCTP_ST_CHUNK(0), NULL,
  1625. commands);
  1626. } else {
  1627. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  1628. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  1629. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1630. }
  1631. return SCTP_DISPOSITION_CONSUME;
  1632. nomem_ev:
  1633. sctp_chunk_free(repl);
  1634. nomem:
  1635. return SCTP_DISPOSITION_NOMEM;
  1636. }
  1637. /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
  1638. *
  1639. * Section 5.2.4
  1640. * B) In this case, both sides may be attempting to start an association
  1641. * at about the same time but the peer endpoint started its INIT
  1642. * after responding to the local endpoint's INIT
  1643. */
  1644. /* This case represents an initialization collision. */
  1645. static enum sctp_disposition sctp_sf_do_dupcook_b(
  1646. struct net *net,
  1647. const struct sctp_endpoint *ep,
  1648. const struct sctp_association *asoc,
  1649. struct sctp_chunk *chunk,
  1650. struct sctp_cmd_seq *commands,
  1651. struct sctp_association *new_asoc)
  1652. {
  1653. struct sctp_init_chunk *peer_init;
  1654. struct sctp_chunk *repl;
  1655. /* new_asoc is a brand-new association, so these are not yet
  1656. * side effects--it is safe to run them here.
  1657. */
  1658. peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
  1659. if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
  1660. GFP_ATOMIC))
  1661. goto nomem;
  1662. /* Update the content of current association. */
  1663. sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
  1664. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  1665. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  1666. SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
  1667. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
  1668. repl = sctp_make_cookie_ack(new_asoc, chunk);
  1669. if (!repl)
  1670. goto nomem;
  1671. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1672. /* RFC 2960 5.1 Normal Establishment of an Association
  1673. *
  1674. * D) IMPLEMENTATION NOTE: An implementation may choose to
  1675. * send the Communication Up notification to the SCTP user
  1676. * upon reception of a valid COOKIE ECHO chunk.
  1677. *
  1678. * Sadly, this needs to be implemented as a side-effect, because
  1679. * we are not guaranteed to have set the association id of the real
  1680. * association and so these notifications need to be delayed until
  1681. * the association id is allocated.
  1682. */
  1683. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
  1684. /* Sockets API Draft Section 5.3.1.6
  1685. * When a peer sends a Adaptation Layer Indication parameter , SCTP
  1686. * delivers this notification to inform the application that of the
  1687. * peers requested adaptation layer.
  1688. *
  1689. * This also needs to be done as a side effect for the same reason as
  1690. * above.
  1691. */
  1692. if (asoc->peer.adaptation_ind)
  1693. sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
  1694. return SCTP_DISPOSITION_CONSUME;
  1695. nomem:
  1696. return SCTP_DISPOSITION_NOMEM;
  1697. }
  1698. /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
  1699. *
  1700. * Section 5.2.4
  1701. * C) In this case, the local endpoint's cookie has arrived late.
  1702. * Before it arrived, the local endpoint sent an INIT and received an
  1703. * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
  1704. * but a new tag of its own.
  1705. */
  1706. /* This case represents an initialization collision. */
  1707. static enum sctp_disposition sctp_sf_do_dupcook_c(
  1708. struct net *net,
  1709. const struct sctp_endpoint *ep,
  1710. const struct sctp_association *asoc,
  1711. struct sctp_chunk *chunk,
  1712. struct sctp_cmd_seq *commands,
  1713. struct sctp_association *new_asoc)
  1714. {
  1715. /* The cookie should be silently discarded.
  1716. * The endpoint SHOULD NOT change states and should leave
  1717. * any timers running.
  1718. */
  1719. return SCTP_DISPOSITION_DISCARD;
  1720. }
  1721. /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
  1722. *
  1723. * Section 5.2.4
  1724. *
  1725. * D) When both local and remote tags match the endpoint should always
  1726. * enter the ESTABLISHED state, if it has not already done so.
  1727. */
  1728. /* This case represents an initialization collision. */
  1729. static enum sctp_disposition sctp_sf_do_dupcook_d(
  1730. struct net *net,
  1731. const struct sctp_endpoint *ep,
  1732. const struct sctp_association *asoc,
  1733. struct sctp_chunk *chunk,
  1734. struct sctp_cmd_seq *commands,
  1735. struct sctp_association *new_asoc)
  1736. {
  1737. struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
  1738. struct sctp_chunk *repl;
  1739. /* Clarification from Implementor's Guide:
  1740. * D) When both local and remote tags match the endpoint should
  1741. * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
  1742. * It should stop any cookie timer that may be running and send
  1743. * a COOKIE ACK.
  1744. */
  1745. /* Don't accidentally move back into established state. */
  1746. if (asoc->state < SCTP_STATE_ESTABLISHED) {
  1747. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  1748. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  1749. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  1750. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  1751. SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
  1752. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
  1753. SCTP_NULL());
  1754. /* RFC 2960 5.1 Normal Establishment of an Association
  1755. *
  1756. * D) IMPLEMENTATION NOTE: An implementation may choose
  1757. * to send the Communication Up notification to the
  1758. * SCTP user upon reception of a valid COOKIE
  1759. * ECHO chunk.
  1760. */
  1761. ev = sctp_ulpevent_make_assoc_change(asoc, 0,
  1762. SCTP_COMM_UP, 0,
  1763. asoc->c.sinit_num_ostreams,
  1764. asoc->c.sinit_max_instreams,
  1765. NULL, GFP_ATOMIC);
  1766. if (!ev)
  1767. goto nomem;
  1768. /* Sockets API Draft Section 5.3.1.6
  1769. * When a peer sends a Adaptation Layer Indication parameter,
  1770. * SCTP delivers this notification to inform the application
  1771. * that of the peers requested adaptation layer.
  1772. */
  1773. if (asoc->peer.adaptation_ind) {
  1774. ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
  1775. GFP_ATOMIC);
  1776. if (!ai_ev)
  1777. goto nomem;
  1778. }
  1779. }
  1780. repl = sctp_make_cookie_ack(new_asoc, chunk);
  1781. if (!repl)
  1782. goto nomem;
  1783. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1784. if (ev)
  1785. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  1786. SCTP_ULPEVENT(ev));
  1787. if (ai_ev)
  1788. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  1789. SCTP_ULPEVENT(ai_ev));
  1790. return SCTP_DISPOSITION_CONSUME;
  1791. nomem:
  1792. if (ai_ev)
  1793. sctp_ulpevent_free(ai_ev);
  1794. if (ev)
  1795. sctp_ulpevent_free(ev);
  1796. return SCTP_DISPOSITION_NOMEM;
  1797. }
  1798. /*
  1799. * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
  1800. * chunk was retransmitted and then delayed in the network.
  1801. *
  1802. * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
  1803. *
  1804. * Verification Tag: None. Do cookie validation.
  1805. *
  1806. * Inputs
  1807. * (endpoint, asoc, chunk)
  1808. *
  1809. * Outputs
  1810. * (asoc, reply_msg, msg_up, timers, counters)
  1811. *
  1812. * The return value is the disposition of the chunk.
  1813. */
  1814. enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
  1815. struct net *net,
  1816. const struct sctp_endpoint *ep,
  1817. const struct sctp_association *asoc,
  1818. const union sctp_subtype type,
  1819. void *arg,
  1820. struct sctp_cmd_seq *commands)
  1821. {
  1822. struct sctp_association *new_asoc;
  1823. struct sctp_chunk *chunk = arg;
  1824. enum sctp_disposition retval;
  1825. struct sctp_chunk *err_chk_p;
  1826. int error = 0;
  1827. char action;
  1828. /* Make sure that the chunk has a valid length from the protocol
  1829. * perspective. In this case check to make sure we have at least
  1830. * enough for the chunk header. Cookie length verification is
  1831. * done later.
  1832. */
  1833. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  1834. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  1835. commands);
  1836. /* "Decode" the chunk. We have no optional parameters so we
  1837. * are in good shape.
  1838. */
  1839. chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
  1840. if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
  1841. sizeof(struct sctp_chunkhdr)))
  1842. goto nomem;
  1843. /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
  1844. * of a duplicate COOKIE ECHO match the Verification Tags of the
  1845. * current association, consider the State Cookie valid even if
  1846. * the lifespan is exceeded.
  1847. */
  1848. new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
  1849. &err_chk_p);
  1850. /* FIXME:
  1851. * If the re-build failed, what is the proper error path
  1852. * from here?
  1853. *
  1854. * [We should abort the association. --piggy]
  1855. */
  1856. if (!new_asoc) {
  1857. /* FIXME: Several errors are possible. A bad cookie should
  1858. * be silently discarded, but think about logging it too.
  1859. */
  1860. switch (error) {
  1861. case -SCTP_IERROR_NOMEM:
  1862. goto nomem;
  1863. case -SCTP_IERROR_STALE_COOKIE:
  1864. sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
  1865. err_chk_p);
  1866. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1867. case -SCTP_IERROR_BAD_SIG:
  1868. default:
  1869. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1870. }
  1871. }
  1872. /* Set temp so that it won't be added into hashtable */
  1873. new_asoc->temp = 1;
  1874. /* Compare the tie_tag in cookie with the verification tag of
  1875. * current association.
  1876. */
  1877. action = sctp_tietags_compare(new_asoc, asoc);
  1878. switch (action) {
  1879. case 'A': /* Association restart. */
  1880. retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
  1881. new_asoc);
  1882. break;
  1883. case 'B': /* Collision case B. */
  1884. retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
  1885. new_asoc);
  1886. break;
  1887. case 'C': /* Collision case C. */
  1888. retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
  1889. new_asoc);
  1890. break;
  1891. case 'D': /* Collision case D. */
  1892. retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
  1893. new_asoc);
  1894. break;
  1895. default: /* Discard packet for all others. */
  1896. retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1897. break;
  1898. }
  1899. /* Delete the tempory new association. */
  1900. sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
  1901. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  1902. /* Restore association pointer to provide SCTP command interpeter
  1903. * with a valid context in case it needs to manipulate
  1904. * the queues */
  1905. sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
  1906. SCTP_ASOC((struct sctp_association *)asoc));
  1907. return retval;
  1908. nomem:
  1909. return SCTP_DISPOSITION_NOMEM;
  1910. }
  1911. /*
  1912. * Process an ABORT. (SHUTDOWN-PENDING state)
  1913. *
  1914. * See sctp_sf_do_9_1_abort().
  1915. */
  1916. enum sctp_disposition sctp_sf_shutdown_pending_abort(
  1917. struct net *net,
  1918. const struct sctp_endpoint *ep,
  1919. const struct sctp_association *asoc,
  1920. const union sctp_subtype type,
  1921. void *arg,
  1922. struct sctp_cmd_seq *commands)
  1923. {
  1924. struct sctp_chunk *chunk = arg;
  1925. if (!sctp_vtag_verify_either(chunk, asoc))
  1926. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1927. /* Make sure that the ABORT chunk has a valid length.
  1928. * Since this is an ABORT chunk, we have to discard it
  1929. * because of the following text:
  1930. * RFC 2960, Section 3.3.7
  1931. * If an endpoint receives an ABORT with a format error or for an
  1932. * association that doesn't exist, it MUST silently discard it.
  1933. * Because the length is "invalid", we can't really discard just
  1934. * as we do not know its true length. So, to be safe, discard the
  1935. * packet.
  1936. */
  1937. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
  1938. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1939. /* ADD-IP: Special case for ABORT chunks
  1940. * F4) One special consideration is that ABORT Chunks arriving
  1941. * destined to the IP address being deleted MUST be
  1942. * ignored (see Section 5.3.1 for further details).
  1943. */
  1944. if (SCTP_ADDR_DEL ==
  1945. sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
  1946. return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
  1947. return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
  1948. }
  1949. /*
  1950. * Process an ABORT. (SHUTDOWN-SENT state)
  1951. *
  1952. * See sctp_sf_do_9_1_abort().
  1953. */
  1954. enum sctp_disposition sctp_sf_shutdown_sent_abort(
  1955. struct net *net,
  1956. const struct sctp_endpoint *ep,
  1957. const struct sctp_association *asoc,
  1958. const union sctp_subtype type,
  1959. void *arg,
  1960. struct sctp_cmd_seq *commands)
  1961. {
  1962. struct sctp_chunk *chunk = arg;
  1963. if (!sctp_vtag_verify_either(chunk, asoc))
  1964. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1965. /* Make sure that the ABORT chunk has a valid length.
  1966. * Since this is an ABORT chunk, we have to discard it
  1967. * because of the following text:
  1968. * RFC 2960, Section 3.3.7
  1969. * If an endpoint receives an ABORT with a format error or for an
  1970. * association that doesn't exist, it MUST silently discard it.
  1971. * Because the length is "invalid", we can't really discard just
  1972. * as we do not know its true length. So, to be safe, discard the
  1973. * packet.
  1974. */
  1975. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
  1976. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1977. /* ADD-IP: Special case for ABORT chunks
  1978. * F4) One special consideration is that ABORT Chunks arriving
  1979. * destined to the IP address being deleted MUST be
  1980. * ignored (see Section 5.3.1 for further details).
  1981. */
  1982. if (SCTP_ADDR_DEL ==
  1983. sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
  1984. return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
  1985. /* Stop the T2-shutdown timer. */
  1986. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  1987. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  1988. /* Stop the T5-shutdown guard timer. */
  1989. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  1990. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  1991. return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
  1992. }
  1993. /*
  1994. * Process an ABORT. (SHUTDOWN-ACK-SENT state)
  1995. *
  1996. * See sctp_sf_do_9_1_abort().
  1997. */
  1998. enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
  1999. struct net *net,
  2000. const struct sctp_endpoint *ep,
  2001. const struct sctp_association *asoc,
  2002. const union sctp_subtype type,
  2003. void *arg,
  2004. struct sctp_cmd_seq *commands)
  2005. {
  2006. /* The same T2 timer, so we should be able to use
  2007. * common function with the SHUTDOWN-SENT state.
  2008. */
  2009. return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
  2010. }
  2011. /*
  2012. * Handle an Error received in COOKIE_ECHOED state.
  2013. *
  2014. * Only handle the error type of stale COOKIE Error, the other errors will
  2015. * be ignored.
  2016. *
  2017. * Inputs
  2018. * (endpoint, asoc, chunk)
  2019. *
  2020. * Outputs
  2021. * (asoc, reply_msg, msg_up, timers, counters)
  2022. *
  2023. * The return value is the disposition of the chunk.
  2024. */
  2025. enum sctp_disposition sctp_sf_cookie_echoed_err(
  2026. struct net *net,
  2027. const struct sctp_endpoint *ep,
  2028. const struct sctp_association *asoc,
  2029. const union sctp_subtype type,
  2030. void *arg,
  2031. struct sctp_cmd_seq *commands)
  2032. {
  2033. struct sctp_chunk *chunk = arg;
  2034. struct sctp_errhdr *err;
  2035. if (!sctp_vtag_verify(chunk, asoc))
  2036. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2037. /* Make sure that the ERROR chunk has a valid length.
  2038. * The parameter walking depends on this as well.
  2039. */
  2040. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
  2041. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2042. commands);
  2043. /* Process the error here */
  2044. /* FUTURE FIXME: When PR-SCTP related and other optional
  2045. * parms are emitted, this will have to change to handle multiple
  2046. * errors.
  2047. */
  2048. sctp_walk_errors(err, chunk->chunk_hdr) {
  2049. if (SCTP_ERROR_STALE_COOKIE == err->cause)
  2050. return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
  2051. arg, commands);
  2052. }
  2053. /* It is possible to have malformed error causes, and that
  2054. * will cause us to end the walk early. However, since
  2055. * we are discarding the packet, there should be no adverse
  2056. * affects.
  2057. */
  2058. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2059. }
  2060. /*
  2061. * Handle a Stale COOKIE Error
  2062. *
  2063. * Section: 5.2.6 Handle Stale COOKIE Error
  2064. * If the association is in the COOKIE-ECHOED state, the endpoint may elect
  2065. * one of the following three alternatives.
  2066. * ...
  2067. * 3) Send a new INIT chunk to the endpoint, adding a Cookie
  2068. * Preservative parameter requesting an extension to the lifetime of
  2069. * the State Cookie. When calculating the time extension, an
  2070. * implementation SHOULD use the RTT information measured based on the
  2071. * previous COOKIE ECHO / ERROR exchange, and should add no more
  2072. * than 1 second beyond the measured RTT, due to long State Cookie
  2073. * lifetimes making the endpoint more subject to a replay attack.
  2074. *
  2075. * Verification Tag: Not explicit, but safe to ignore.
  2076. *
  2077. * Inputs
  2078. * (endpoint, asoc, chunk)
  2079. *
  2080. * Outputs
  2081. * (asoc, reply_msg, msg_up, timers, counters)
  2082. *
  2083. * The return value is the disposition of the chunk.
  2084. */
  2085. static enum sctp_disposition sctp_sf_do_5_2_6_stale(
  2086. struct net *net,
  2087. const struct sctp_endpoint *ep,
  2088. const struct sctp_association *asoc,
  2089. const union sctp_subtype type,
  2090. void *arg,
  2091. struct sctp_cmd_seq *commands)
  2092. {
  2093. int attempts = asoc->init_err_counter + 1;
  2094. struct sctp_chunk *chunk = arg, *reply;
  2095. struct sctp_cookie_preserve_param bht;
  2096. struct sctp_bind_addr *bp;
  2097. struct sctp_errhdr *err;
  2098. u32 stale;
  2099. if (attempts > asoc->max_init_attempts) {
  2100. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  2101. SCTP_ERROR(ETIMEDOUT));
  2102. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  2103. SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
  2104. return SCTP_DISPOSITION_DELETE_TCB;
  2105. }
  2106. err = (struct sctp_errhdr *)(chunk->skb->data);
  2107. /* When calculating the time extension, an implementation
  2108. * SHOULD use the RTT information measured based on the
  2109. * previous COOKIE ECHO / ERROR exchange, and should add no
  2110. * more than 1 second beyond the measured RTT, due to long
  2111. * State Cookie lifetimes making the endpoint more subject to
  2112. * a replay attack.
  2113. * Measure of Staleness's unit is usec. (1/1000000 sec)
  2114. * Suggested Cookie Life-span Increment's unit is msec.
  2115. * (1/1000 sec)
  2116. * In general, if you use the suggested cookie life, the value
  2117. * found in the field of measure of staleness should be doubled
  2118. * to give ample time to retransmit the new cookie and thus
  2119. * yield a higher probability of success on the reattempt.
  2120. */
  2121. stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
  2122. stale = (stale * 2) / 1000;
  2123. bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
  2124. bht.param_hdr.length = htons(sizeof(bht));
  2125. bht.lifespan_increment = htonl(stale);
  2126. /* Build that new INIT chunk. */
  2127. bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
  2128. reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
  2129. if (!reply)
  2130. goto nomem;
  2131. sctp_addto_chunk(reply, sizeof(bht), &bht);
  2132. /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
  2133. sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
  2134. /* Stop pending T3-rtx and heartbeat timers */
  2135. sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
  2136. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
  2137. /* Delete non-primary peer ip addresses since we are transitioning
  2138. * back to the COOKIE-WAIT state
  2139. */
  2140. sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
  2141. /* If we've sent any data bundled with COOKIE-ECHO we will need to
  2142. * resend
  2143. */
  2144. sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
  2145. SCTP_TRANSPORT(asoc->peer.primary_path));
  2146. /* Cast away the const modifier, as we want to just
  2147. * rerun it through as a sideffect.
  2148. */
  2149. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
  2150. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2151. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  2152. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  2153. SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
  2154. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  2155. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  2156. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  2157. return SCTP_DISPOSITION_CONSUME;
  2158. nomem:
  2159. return SCTP_DISPOSITION_NOMEM;
  2160. }
  2161. /*
  2162. * Process an ABORT.
  2163. *
  2164. * Section: 9.1
  2165. * After checking the Verification Tag, the receiving endpoint shall
  2166. * remove the association from its record, and shall report the
  2167. * termination to its upper layer.
  2168. *
  2169. * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
  2170. * B) Rules for packet carrying ABORT:
  2171. *
  2172. * - The endpoint shall always fill in the Verification Tag field of the
  2173. * outbound packet with the destination endpoint's tag value if it
  2174. * is known.
  2175. *
  2176. * - If the ABORT is sent in response to an OOTB packet, the endpoint
  2177. * MUST follow the procedure described in Section 8.4.
  2178. *
  2179. * - The receiver MUST accept the packet if the Verification Tag
  2180. * matches either its own tag, OR the tag of its peer. Otherwise, the
  2181. * receiver MUST silently discard the packet and take no further
  2182. * action.
  2183. *
  2184. * Inputs
  2185. * (endpoint, asoc, chunk)
  2186. *
  2187. * Outputs
  2188. * (asoc, reply_msg, msg_up, timers, counters)
  2189. *
  2190. * The return value is the disposition of the chunk.
  2191. */
  2192. enum sctp_disposition sctp_sf_do_9_1_abort(
  2193. struct net *net,
  2194. const struct sctp_endpoint *ep,
  2195. const struct sctp_association *asoc,
  2196. const union sctp_subtype type,
  2197. void *arg,
  2198. struct sctp_cmd_seq *commands)
  2199. {
  2200. struct sctp_chunk *chunk = arg;
  2201. if (!sctp_vtag_verify_either(chunk, asoc))
  2202. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2203. /* Make sure that the ABORT chunk has a valid length.
  2204. * Since this is an ABORT chunk, we have to discard it
  2205. * because of the following text:
  2206. * RFC 2960, Section 3.3.7
  2207. * If an endpoint receives an ABORT with a format error or for an
  2208. * association that doesn't exist, it MUST silently discard it.
  2209. * Because the length is "invalid", we can't really discard just
  2210. * as we do not know its true length. So, to be safe, discard the
  2211. * packet.
  2212. */
  2213. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
  2214. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2215. /* ADD-IP: Special case for ABORT chunks
  2216. * F4) One special consideration is that ABORT Chunks arriving
  2217. * destined to the IP address being deleted MUST be
  2218. * ignored (see Section 5.3.1 for further details).
  2219. */
  2220. if (SCTP_ADDR_DEL ==
  2221. sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
  2222. return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
  2223. return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
  2224. }
  2225. static enum sctp_disposition __sctp_sf_do_9_1_abort(
  2226. struct net *net,
  2227. const struct sctp_endpoint *ep,
  2228. const struct sctp_association *asoc,
  2229. const union sctp_subtype type,
  2230. void *arg,
  2231. struct sctp_cmd_seq *commands)
  2232. {
  2233. __be16 error = SCTP_ERROR_NO_ERROR;
  2234. struct sctp_chunk *chunk = arg;
  2235. unsigned int len;
  2236. /* See if we have an error cause code in the chunk. */
  2237. len = ntohs(chunk->chunk_hdr->length);
  2238. if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
  2239. struct sctp_errhdr *err;
  2240. sctp_walk_errors(err, chunk->chunk_hdr);
  2241. if ((void *)err != (void *)chunk->chunk_end)
  2242. return sctp_sf_pdiscard(net, ep, asoc, type, arg,
  2243. commands);
  2244. error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
  2245. }
  2246. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
  2247. /* ASSOC_FAILED will DELETE_TCB. */
  2248. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
  2249. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  2250. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  2251. return SCTP_DISPOSITION_ABORT;
  2252. }
  2253. /*
  2254. * Process an ABORT. (COOKIE-WAIT state)
  2255. *
  2256. * See sctp_sf_do_9_1_abort() above.
  2257. */
  2258. enum sctp_disposition sctp_sf_cookie_wait_abort(
  2259. struct net *net,
  2260. const struct sctp_endpoint *ep,
  2261. const struct sctp_association *asoc,
  2262. const union sctp_subtype type,
  2263. void *arg,
  2264. struct sctp_cmd_seq *commands)
  2265. {
  2266. __be16 error = SCTP_ERROR_NO_ERROR;
  2267. struct sctp_chunk *chunk = arg;
  2268. unsigned int len;
  2269. if (!sctp_vtag_verify_either(chunk, asoc))
  2270. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2271. /* Make sure that the ABORT chunk has a valid length.
  2272. * Since this is an ABORT chunk, we have to discard it
  2273. * because of the following text:
  2274. * RFC 2960, Section 3.3.7
  2275. * If an endpoint receives an ABORT with a format error or for an
  2276. * association that doesn't exist, it MUST silently discard it.
  2277. * Because the length is "invalid", we can't really discard just
  2278. * as we do not know its true length. So, to be safe, discard the
  2279. * packet.
  2280. */
  2281. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
  2282. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2283. /* See if we have an error cause code in the chunk. */
  2284. len = ntohs(chunk->chunk_hdr->length);
  2285. if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
  2286. error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
  2287. return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
  2288. chunk->transport);
  2289. }
  2290. /*
  2291. * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
  2292. */
  2293. enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
  2294. struct net *net,
  2295. const struct sctp_endpoint *ep,
  2296. const struct sctp_association *asoc,
  2297. const union sctp_subtype type,
  2298. void *arg,
  2299. struct sctp_cmd_seq *commands)
  2300. {
  2301. return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
  2302. ENOPROTOOPT, asoc,
  2303. (struct sctp_transport *)arg);
  2304. }
  2305. /*
  2306. * Process an ABORT. (COOKIE-ECHOED state)
  2307. */
  2308. enum sctp_disposition sctp_sf_cookie_echoed_abort(
  2309. struct net *net,
  2310. const struct sctp_endpoint *ep,
  2311. const struct sctp_association *asoc,
  2312. const union sctp_subtype type,
  2313. void *arg,
  2314. struct sctp_cmd_seq *commands)
  2315. {
  2316. /* There is a single T1 timer, so we should be able to use
  2317. * common function with the COOKIE-WAIT state.
  2318. */
  2319. return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
  2320. }
  2321. /*
  2322. * Stop T1 timer and abort association with "INIT failed".
  2323. *
  2324. * This is common code called by several sctp_sf_*_abort() functions above.
  2325. */
  2326. static enum sctp_disposition sctp_stop_t1_and_abort(
  2327. struct net *net,
  2328. struct sctp_cmd_seq *commands,
  2329. __be16 error, int sk_err,
  2330. const struct sctp_association *asoc,
  2331. struct sctp_transport *transport)
  2332. {
  2333. pr_debug("%s: ABORT received (INIT)\n", __func__);
  2334. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  2335. SCTP_STATE(SCTP_STATE_CLOSED));
  2336. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  2337. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2338. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  2339. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
  2340. /* CMD_INIT_FAILED will DELETE_TCB. */
  2341. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  2342. SCTP_PERR(error));
  2343. return SCTP_DISPOSITION_ABORT;
  2344. }
  2345. /*
  2346. * sctp_sf_do_9_2_shut
  2347. *
  2348. * Section: 9.2
  2349. * Upon the reception of the SHUTDOWN, the peer endpoint shall
  2350. * - enter the SHUTDOWN-RECEIVED state,
  2351. *
  2352. * - stop accepting new data from its SCTP user
  2353. *
  2354. * - verify, by checking the Cumulative TSN Ack field of the chunk,
  2355. * that all its outstanding DATA chunks have been received by the
  2356. * SHUTDOWN sender.
  2357. *
  2358. * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
  2359. * send a SHUTDOWN in response to a ULP request. And should discard
  2360. * subsequent SHUTDOWN chunks.
  2361. *
  2362. * If there are still outstanding DATA chunks left, the SHUTDOWN
  2363. * receiver shall continue to follow normal data transmission
  2364. * procedures defined in Section 6 until all outstanding DATA chunks
  2365. * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
  2366. * new data from its SCTP user.
  2367. *
  2368. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2369. *
  2370. * Inputs
  2371. * (endpoint, asoc, chunk)
  2372. *
  2373. * Outputs
  2374. * (asoc, reply_msg, msg_up, timers, counters)
  2375. *
  2376. * The return value is the disposition of the chunk.
  2377. */
  2378. enum sctp_disposition sctp_sf_do_9_2_shutdown(
  2379. struct net *net,
  2380. const struct sctp_endpoint *ep,
  2381. const struct sctp_association *asoc,
  2382. const union sctp_subtype type,
  2383. void *arg,
  2384. struct sctp_cmd_seq *commands)
  2385. {
  2386. enum sctp_disposition disposition;
  2387. struct sctp_chunk *chunk = arg;
  2388. struct sctp_shutdownhdr *sdh;
  2389. struct sctp_ulpevent *ev;
  2390. __u32 ctsn;
  2391. if (!sctp_vtag_verify(chunk, asoc))
  2392. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2393. /* Make sure that the SHUTDOWN chunk has a valid length. */
  2394. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
  2395. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2396. commands);
  2397. /* Convert the elaborate header. */
  2398. sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
  2399. skb_pull(chunk->skb, sizeof(*sdh));
  2400. chunk->subh.shutdown_hdr = sdh;
  2401. ctsn = ntohl(sdh->cum_tsn_ack);
  2402. if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
  2403. pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
  2404. asoc->ctsn_ack_point);
  2405. return SCTP_DISPOSITION_DISCARD;
  2406. }
  2407. /* If Cumulative TSN Ack beyond the max tsn currently
  2408. * send, terminating the association and respond to the
  2409. * sender with an ABORT.
  2410. */
  2411. if (!TSN_lt(ctsn, asoc->next_tsn))
  2412. return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
  2413. /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
  2414. * When a peer sends a SHUTDOWN, SCTP delivers this notification to
  2415. * inform the application that it should cease sending data.
  2416. */
  2417. ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
  2418. if (!ev) {
  2419. disposition = SCTP_DISPOSITION_NOMEM;
  2420. goto out;
  2421. }
  2422. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  2423. /* Upon the reception of the SHUTDOWN, the peer endpoint shall
  2424. * - enter the SHUTDOWN-RECEIVED state,
  2425. * - stop accepting new data from its SCTP user
  2426. *
  2427. * [This is implicit in the new state.]
  2428. */
  2429. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  2430. SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
  2431. disposition = SCTP_DISPOSITION_CONSUME;
  2432. if (sctp_outq_is_empty(&asoc->outqueue)) {
  2433. disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
  2434. arg, commands);
  2435. }
  2436. if (SCTP_DISPOSITION_NOMEM == disposition)
  2437. goto out;
  2438. /* - verify, by checking the Cumulative TSN Ack field of the
  2439. * chunk, that all its outstanding DATA chunks have been
  2440. * received by the SHUTDOWN sender.
  2441. */
  2442. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
  2443. SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
  2444. out:
  2445. return disposition;
  2446. }
  2447. /*
  2448. * sctp_sf_do_9_2_shut_ctsn
  2449. *
  2450. * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
  2451. * it MUST NOT send a SHUTDOWN in response to a ULP request.
  2452. * The Cumulative TSN Ack of the received SHUTDOWN chunk
  2453. * MUST be processed.
  2454. */
  2455. enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
  2456. struct net *net,
  2457. const struct sctp_endpoint *ep,
  2458. const struct sctp_association *asoc,
  2459. const union sctp_subtype type,
  2460. void *arg,
  2461. struct sctp_cmd_seq *commands)
  2462. {
  2463. struct sctp_chunk *chunk = arg;
  2464. struct sctp_shutdownhdr *sdh;
  2465. __u32 ctsn;
  2466. if (!sctp_vtag_verify(chunk, asoc))
  2467. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2468. /* Make sure that the SHUTDOWN chunk has a valid length. */
  2469. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
  2470. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2471. commands);
  2472. sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
  2473. ctsn = ntohl(sdh->cum_tsn_ack);
  2474. if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
  2475. pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
  2476. asoc->ctsn_ack_point);
  2477. return SCTP_DISPOSITION_DISCARD;
  2478. }
  2479. /* If Cumulative TSN Ack beyond the max tsn currently
  2480. * send, terminating the association and respond to the
  2481. * sender with an ABORT.
  2482. */
  2483. if (!TSN_lt(ctsn, asoc->next_tsn))
  2484. return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
  2485. /* verify, by checking the Cumulative TSN Ack field of the
  2486. * chunk, that all its outstanding DATA chunks have been
  2487. * received by the SHUTDOWN sender.
  2488. */
  2489. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
  2490. SCTP_BE32(sdh->cum_tsn_ack));
  2491. return SCTP_DISPOSITION_CONSUME;
  2492. }
  2493. /* RFC 2960 9.2
  2494. * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
  2495. * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
  2496. * transport addresses (either in the IP addresses or in the INIT chunk)
  2497. * that belong to this association, it should discard the INIT chunk and
  2498. * retransmit the SHUTDOWN ACK chunk.
  2499. */
  2500. enum sctp_disposition sctp_sf_do_9_2_reshutack(
  2501. struct net *net,
  2502. const struct sctp_endpoint *ep,
  2503. const struct sctp_association *asoc,
  2504. const union sctp_subtype type,
  2505. void *arg,
  2506. struct sctp_cmd_seq *commands)
  2507. {
  2508. struct sctp_chunk *chunk = arg;
  2509. struct sctp_chunk *reply;
  2510. /* Make sure that the chunk has a valid length */
  2511. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  2512. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2513. commands);
  2514. /* Since we are not going to really process this INIT, there
  2515. * is no point in verifying chunk boundries. Just generate
  2516. * the SHUTDOWN ACK.
  2517. */
  2518. reply = sctp_make_shutdown_ack(asoc, chunk);
  2519. if (NULL == reply)
  2520. goto nomem;
  2521. /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
  2522. * the T2-SHUTDOWN timer.
  2523. */
  2524. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  2525. /* and restart the T2-shutdown timer. */
  2526. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  2527. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  2528. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  2529. return SCTP_DISPOSITION_CONSUME;
  2530. nomem:
  2531. return SCTP_DISPOSITION_NOMEM;
  2532. }
  2533. /*
  2534. * sctp_sf_do_ecn_cwr
  2535. *
  2536. * Section: Appendix A: Explicit Congestion Notification
  2537. *
  2538. * CWR:
  2539. *
  2540. * RFC 2481 details a specific bit for a sender to send in the header of
  2541. * its next outbound TCP segment to indicate to its peer that it has
  2542. * reduced its congestion window. This is termed the CWR bit. For
  2543. * SCTP the same indication is made by including the CWR chunk.
  2544. * This chunk contains one data element, i.e. the TSN number that
  2545. * was sent in the ECNE chunk. This element represents the lowest
  2546. * TSN number in the datagram that was originally marked with the
  2547. * CE bit.
  2548. *
  2549. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2550. * Inputs
  2551. * (endpoint, asoc, chunk)
  2552. *
  2553. * Outputs
  2554. * (asoc, reply_msg, msg_up, timers, counters)
  2555. *
  2556. * The return value is the disposition of the chunk.
  2557. */
  2558. enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
  2559. const struct sctp_endpoint *ep,
  2560. const struct sctp_association *asoc,
  2561. const union sctp_subtype type,
  2562. void *arg,
  2563. struct sctp_cmd_seq *commands)
  2564. {
  2565. struct sctp_chunk *chunk = arg;
  2566. struct sctp_cwrhdr *cwr;
  2567. u32 lowest_tsn;
  2568. if (!sctp_vtag_verify(chunk, asoc))
  2569. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2570. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
  2571. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2572. commands);
  2573. cwr = (struct sctp_cwrhdr *)chunk->skb->data;
  2574. skb_pull(chunk->skb, sizeof(*cwr));
  2575. lowest_tsn = ntohl(cwr->lowest_tsn);
  2576. /* Does this CWR ack the last sent congestion notification? */
  2577. if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
  2578. /* Stop sending ECNE. */
  2579. sctp_add_cmd_sf(commands,
  2580. SCTP_CMD_ECN_CWR,
  2581. SCTP_U32(lowest_tsn));
  2582. }
  2583. return SCTP_DISPOSITION_CONSUME;
  2584. }
  2585. /*
  2586. * sctp_sf_do_ecne
  2587. *
  2588. * Section: Appendix A: Explicit Congestion Notification
  2589. *
  2590. * ECN-Echo
  2591. *
  2592. * RFC 2481 details a specific bit for a receiver to send back in its
  2593. * TCP acknowledgements to notify the sender of the Congestion
  2594. * Experienced (CE) bit having arrived from the network. For SCTP this
  2595. * same indication is made by including the ECNE chunk. This chunk
  2596. * contains one data element, i.e. the lowest TSN associated with the IP
  2597. * datagram marked with the CE bit.....
  2598. *
  2599. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2600. * Inputs
  2601. * (endpoint, asoc, chunk)
  2602. *
  2603. * Outputs
  2604. * (asoc, reply_msg, msg_up, timers, counters)
  2605. *
  2606. * The return value is the disposition of the chunk.
  2607. */
  2608. enum sctp_disposition sctp_sf_do_ecne(struct net *net,
  2609. const struct sctp_endpoint *ep,
  2610. const struct sctp_association *asoc,
  2611. const union sctp_subtype type,
  2612. void *arg, struct sctp_cmd_seq *commands)
  2613. {
  2614. struct sctp_chunk *chunk = arg;
  2615. struct sctp_ecnehdr *ecne;
  2616. if (!sctp_vtag_verify(chunk, asoc))
  2617. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2618. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
  2619. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2620. commands);
  2621. ecne = (struct sctp_ecnehdr *)chunk->skb->data;
  2622. skb_pull(chunk->skb, sizeof(*ecne));
  2623. /* If this is a newer ECNE than the last CWR packet we sent out */
  2624. sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
  2625. SCTP_U32(ntohl(ecne->lowest_tsn)));
  2626. return SCTP_DISPOSITION_CONSUME;
  2627. }
  2628. /*
  2629. * Section: 6.2 Acknowledgement on Reception of DATA Chunks
  2630. *
  2631. * The SCTP endpoint MUST always acknowledge the reception of each valid
  2632. * DATA chunk.
  2633. *
  2634. * The guidelines on delayed acknowledgement algorithm specified in
  2635. * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
  2636. * acknowledgement SHOULD be generated for at least every second packet
  2637. * (not every second DATA chunk) received, and SHOULD be generated within
  2638. * 200 ms of the arrival of any unacknowledged DATA chunk. In some
  2639. * situations it may be beneficial for an SCTP transmitter to be more
  2640. * conservative than the algorithms detailed in this document allow.
  2641. * However, an SCTP transmitter MUST NOT be more aggressive than the
  2642. * following algorithms allow.
  2643. *
  2644. * A SCTP receiver MUST NOT generate more than one SACK for every
  2645. * incoming packet, other than to update the offered window as the
  2646. * receiving application consumes new data.
  2647. *
  2648. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2649. *
  2650. * Inputs
  2651. * (endpoint, asoc, chunk)
  2652. *
  2653. * Outputs
  2654. * (asoc, reply_msg, msg_up, timers, counters)
  2655. *
  2656. * The return value is the disposition of the chunk.
  2657. */
  2658. enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
  2659. const struct sctp_endpoint *ep,
  2660. const struct sctp_association *asoc,
  2661. const union sctp_subtype type,
  2662. void *arg,
  2663. struct sctp_cmd_seq *commands)
  2664. {
  2665. union sctp_arg force = SCTP_NOFORCE();
  2666. struct sctp_chunk *chunk = arg;
  2667. int error;
  2668. if (!sctp_vtag_verify(chunk, asoc)) {
  2669. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  2670. SCTP_NULL());
  2671. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2672. }
  2673. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
  2674. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2675. commands);
  2676. error = sctp_eat_data(asoc, chunk, commands);
  2677. switch (error) {
  2678. case SCTP_IERROR_NO_ERROR:
  2679. break;
  2680. case SCTP_IERROR_HIGH_TSN:
  2681. case SCTP_IERROR_BAD_STREAM:
  2682. SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
  2683. goto discard_noforce;
  2684. case SCTP_IERROR_DUP_TSN:
  2685. case SCTP_IERROR_IGNORE_TSN:
  2686. SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
  2687. goto discard_force;
  2688. case SCTP_IERROR_NO_DATA:
  2689. return SCTP_DISPOSITION_ABORT;
  2690. case SCTP_IERROR_PROTO_VIOLATION:
  2691. return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
  2692. (u8 *)chunk->subh.data_hdr,
  2693. sizeof(struct sctp_datahdr));
  2694. default:
  2695. BUG();
  2696. }
  2697. if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
  2698. force = SCTP_FORCE();
  2699. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
  2700. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  2701. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  2702. }
  2703. /* If this is the last chunk in a packet, we need to count it
  2704. * toward sack generation. Note that we need to SACK every
  2705. * OTHER packet containing data chunks, EVEN IF WE DISCARD
  2706. * THEM. We elect to NOT generate SACK's if the chunk fails
  2707. * the verification tag test.
  2708. *
  2709. * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
  2710. *
  2711. * The SCTP endpoint MUST always acknowledge the reception of
  2712. * each valid DATA chunk.
  2713. *
  2714. * The guidelines on delayed acknowledgement algorithm
  2715. * specified in Section 4.2 of [RFC2581] SHOULD be followed.
  2716. * Specifically, an acknowledgement SHOULD be generated for at
  2717. * least every second packet (not every second DATA chunk)
  2718. * received, and SHOULD be generated within 200 ms of the
  2719. * arrival of any unacknowledged DATA chunk. In some
  2720. * situations it may be beneficial for an SCTP transmitter to
  2721. * be more conservative than the algorithms detailed in this
  2722. * document allow. However, an SCTP transmitter MUST NOT be
  2723. * more aggressive than the following algorithms allow.
  2724. */
  2725. if (chunk->end_of_packet)
  2726. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
  2727. return SCTP_DISPOSITION_CONSUME;
  2728. discard_force:
  2729. /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
  2730. *
  2731. * When a packet arrives with duplicate DATA chunk(s) and with
  2732. * no new DATA chunk(s), the endpoint MUST immediately send a
  2733. * SACK with no delay. If a packet arrives with duplicate
  2734. * DATA chunk(s) bundled with new DATA chunks, the endpoint
  2735. * MAY immediately send a SACK. Normally receipt of duplicate
  2736. * DATA chunks will occur when the original SACK chunk was lost
  2737. * and the peer's RTO has expired. The duplicate TSN number(s)
  2738. * SHOULD be reported in the SACK as duplicate.
  2739. */
  2740. /* In our case, we split the MAY SACK advice up whether or not
  2741. * the last chunk is a duplicate.'
  2742. */
  2743. if (chunk->end_of_packet)
  2744. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  2745. return SCTP_DISPOSITION_DISCARD;
  2746. discard_noforce:
  2747. if (chunk->end_of_packet)
  2748. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
  2749. return SCTP_DISPOSITION_DISCARD;
  2750. }
  2751. /*
  2752. * sctp_sf_eat_data_fast_4_4
  2753. *
  2754. * Section: 4 (4)
  2755. * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
  2756. * DATA chunks without delay.
  2757. *
  2758. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2759. * Inputs
  2760. * (endpoint, asoc, chunk)
  2761. *
  2762. * Outputs
  2763. * (asoc, reply_msg, msg_up, timers, counters)
  2764. *
  2765. * The return value is the disposition of the chunk.
  2766. */
  2767. enum sctp_disposition sctp_sf_eat_data_fast_4_4(
  2768. struct net *net,
  2769. const struct sctp_endpoint *ep,
  2770. const struct sctp_association *asoc,
  2771. const union sctp_subtype type,
  2772. void *arg,
  2773. struct sctp_cmd_seq *commands)
  2774. {
  2775. struct sctp_chunk *chunk = arg;
  2776. int error;
  2777. if (!sctp_vtag_verify(chunk, asoc)) {
  2778. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  2779. SCTP_NULL());
  2780. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2781. }
  2782. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
  2783. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2784. commands);
  2785. error = sctp_eat_data(asoc, chunk, commands);
  2786. switch (error) {
  2787. case SCTP_IERROR_NO_ERROR:
  2788. case SCTP_IERROR_HIGH_TSN:
  2789. case SCTP_IERROR_DUP_TSN:
  2790. case SCTP_IERROR_IGNORE_TSN:
  2791. case SCTP_IERROR_BAD_STREAM:
  2792. break;
  2793. case SCTP_IERROR_NO_DATA:
  2794. return SCTP_DISPOSITION_ABORT;
  2795. case SCTP_IERROR_PROTO_VIOLATION:
  2796. return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
  2797. (u8 *)chunk->subh.data_hdr,
  2798. sizeof(struct sctp_datahdr));
  2799. default:
  2800. BUG();
  2801. }
  2802. /* Go a head and force a SACK, since we are shutting down. */
  2803. /* Implementor's Guide.
  2804. *
  2805. * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
  2806. * respond to each received packet containing one or more DATA chunk(s)
  2807. * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
  2808. */
  2809. if (chunk->end_of_packet) {
  2810. /* We must delay the chunk creation since the cumulative
  2811. * TSN has not been updated yet.
  2812. */
  2813. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
  2814. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  2815. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  2816. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  2817. }
  2818. return SCTP_DISPOSITION_CONSUME;
  2819. }
  2820. /*
  2821. * Section: 6.2 Processing a Received SACK
  2822. * D) Any time a SACK arrives, the endpoint performs the following:
  2823. *
  2824. * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
  2825. * then drop the SACK. Since Cumulative TSN Ack is monotonically
  2826. * increasing, a SACK whose Cumulative TSN Ack is less than the
  2827. * Cumulative TSN Ack Point indicates an out-of-order SACK.
  2828. *
  2829. * ii) Set rwnd equal to the newly received a_rwnd minus the number
  2830. * of bytes still outstanding after processing the Cumulative TSN Ack
  2831. * and the Gap Ack Blocks.
  2832. *
  2833. * iii) If the SACK is missing a TSN that was previously
  2834. * acknowledged via a Gap Ack Block (e.g., the data receiver
  2835. * reneged on the data), then mark the corresponding DATA chunk
  2836. * as available for retransmit: Mark it as missing for fast
  2837. * retransmit as described in Section 7.2.4 and if no retransmit
  2838. * timer is running for the destination address to which the DATA
  2839. * chunk was originally transmitted, then T3-rtx is started for
  2840. * that destination address.
  2841. *
  2842. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2843. *
  2844. * Inputs
  2845. * (endpoint, asoc, chunk)
  2846. *
  2847. * Outputs
  2848. * (asoc, reply_msg, msg_up, timers, counters)
  2849. *
  2850. * The return value is the disposition of the chunk.
  2851. */
  2852. enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
  2853. const struct sctp_endpoint *ep,
  2854. const struct sctp_association *asoc,
  2855. const union sctp_subtype type,
  2856. void *arg,
  2857. struct sctp_cmd_seq *commands)
  2858. {
  2859. struct sctp_chunk *chunk = arg;
  2860. struct sctp_sackhdr *sackh;
  2861. __u32 ctsn;
  2862. if (!sctp_vtag_verify(chunk, asoc))
  2863. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2864. /* Make sure that the SACK chunk has a valid length. */
  2865. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
  2866. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2867. commands);
  2868. /* Pull the SACK chunk from the data buffer */
  2869. sackh = sctp_sm_pull_sack(chunk);
  2870. /* Was this a bogus SACK? */
  2871. if (!sackh)
  2872. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2873. chunk->subh.sack_hdr = sackh;
  2874. ctsn = ntohl(sackh->cum_tsn_ack);
  2875. /* i) If Cumulative TSN Ack is less than the Cumulative TSN
  2876. * Ack Point, then drop the SACK. Since Cumulative TSN
  2877. * Ack is monotonically increasing, a SACK whose
  2878. * Cumulative TSN Ack is less than the Cumulative TSN Ack
  2879. * Point indicates an out-of-order SACK.
  2880. */
  2881. if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
  2882. pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
  2883. asoc->ctsn_ack_point);
  2884. return SCTP_DISPOSITION_DISCARD;
  2885. }
  2886. /* If Cumulative TSN Ack beyond the max tsn currently
  2887. * send, terminating the association and respond to the
  2888. * sender with an ABORT.
  2889. */
  2890. if (!TSN_lt(ctsn, asoc->next_tsn))
  2891. return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
  2892. /* Return this SACK for further processing. */
  2893. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
  2894. /* Note: We do the rest of the work on the PROCESS_SACK
  2895. * sideeffect.
  2896. */
  2897. return SCTP_DISPOSITION_CONSUME;
  2898. }
  2899. /*
  2900. * Generate an ABORT in response to a packet.
  2901. *
  2902. * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
  2903. *
  2904. * 8) The receiver should respond to the sender of the OOTB packet with
  2905. * an ABORT. When sending the ABORT, the receiver of the OOTB packet
  2906. * MUST fill in the Verification Tag field of the outbound packet
  2907. * with the value found in the Verification Tag field of the OOTB
  2908. * packet and set the T-bit in the Chunk Flags to indicate that the
  2909. * Verification Tag is reflected. After sending this ABORT, the
  2910. * receiver of the OOTB packet shall discard the OOTB packet and take
  2911. * no further action.
  2912. *
  2913. * Verification Tag:
  2914. *
  2915. * The return value is the disposition of the chunk.
  2916. */
  2917. static enum sctp_disposition sctp_sf_tabort_8_4_8(
  2918. struct net *net,
  2919. const struct sctp_endpoint *ep,
  2920. const struct sctp_association *asoc,
  2921. const union sctp_subtype type,
  2922. void *arg,
  2923. struct sctp_cmd_seq *commands)
  2924. {
  2925. struct sctp_packet *packet = NULL;
  2926. struct sctp_chunk *chunk = arg;
  2927. struct sctp_chunk *abort;
  2928. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  2929. if (!packet)
  2930. return SCTP_DISPOSITION_NOMEM;
  2931. /* Make an ABORT. The T bit will be set if the asoc
  2932. * is NULL.
  2933. */
  2934. abort = sctp_make_abort(asoc, chunk, 0);
  2935. if (!abort) {
  2936. sctp_ootb_pkt_free(packet);
  2937. return SCTP_DISPOSITION_NOMEM;
  2938. }
  2939. /* Reflect vtag if T-Bit is set */
  2940. if (sctp_test_T_bit(abort))
  2941. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  2942. /* Set the skb to the belonging sock for accounting. */
  2943. abort->skb->sk = ep->base.sk;
  2944. sctp_packet_append_chunk(packet, abort);
  2945. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  2946. SCTP_PACKET(packet));
  2947. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  2948. sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2949. return SCTP_DISPOSITION_CONSUME;
  2950. }
  2951. /*
  2952. * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
  2953. * event as ULP notification for each cause included in the chunk.
  2954. *
  2955. * API 5.3.1.3 - SCTP_REMOTE_ERROR
  2956. *
  2957. * The return value is the disposition of the chunk.
  2958. */
  2959. enum sctp_disposition sctp_sf_operr_notify(struct net *net,
  2960. const struct sctp_endpoint *ep,
  2961. const struct sctp_association *asoc,
  2962. const union sctp_subtype type,
  2963. void *arg,
  2964. struct sctp_cmd_seq *commands)
  2965. {
  2966. struct sctp_chunk *chunk = arg;
  2967. struct sctp_errhdr *err;
  2968. if (!sctp_vtag_verify(chunk, asoc))
  2969. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2970. /* Make sure that the ERROR chunk has a valid length. */
  2971. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
  2972. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2973. commands);
  2974. sctp_walk_errors(err, chunk->chunk_hdr);
  2975. if ((void *)err != (void *)chunk->chunk_end)
  2976. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  2977. (void *)err, commands);
  2978. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
  2979. SCTP_CHUNK(chunk));
  2980. return SCTP_DISPOSITION_CONSUME;
  2981. }
  2982. /*
  2983. * Process an inbound SHUTDOWN ACK.
  2984. *
  2985. * From Section 9.2:
  2986. * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
  2987. * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
  2988. * peer, and remove all record of the association.
  2989. *
  2990. * The return value is the disposition.
  2991. */
  2992. enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
  2993. const struct sctp_endpoint *ep,
  2994. const struct sctp_association *asoc,
  2995. const union sctp_subtype type,
  2996. void *arg,
  2997. struct sctp_cmd_seq *commands)
  2998. {
  2999. struct sctp_chunk *chunk = arg;
  3000. struct sctp_chunk *reply;
  3001. struct sctp_ulpevent *ev;
  3002. if (!sctp_vtag_verify(chunk, asoc))
  3003. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3004. /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
  3005. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  3006. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3007. commands);
  3008. /* 10.2 H) SHUTDOWN COMPLETE notification
  3009. *
  3010. * When SCTP completes the shutdown procedures (section 9.2) this
  3011. * notification is passed to the upper layer.
  3012. */
  3013. ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
  3014. 0, 0, 0, NULL, GFP_ATOMIC);
  3015. if (!ev)
  3016. goto nomem;
  3017. /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
  3018. reply = sctp_make_shutdown_complete(asoc, chunk);
  3019. if (!reply)
  3020. goto nomem_chunk;
  3021. /* Do all the commands now (after allocation), so that we
  3022. * have consistent state if memory allocation failes
  3023. */
  3024. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  3025. /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
  3026. * stop the T2-shutdown timer,
  3027. */
  3028. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3029. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  3030. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3031. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  3032. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  3033. SCTP_STATE(SCTP_STATE_CLOSED));
  3034. SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
  3035. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  3036. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  3037. /* ...and remove all record of the association. */
  3038. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  3039. return SCTP_DISPOSITION_DELETE_TCB;
  3040. nomem_chunk:
  3041. sctp_ulpevent_free(ev);
  3042. nomem:
  3043. return SCTP_DISPOSITION_NOMEM;
  3044. }
  3045. /*
  3046. * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
  3047. *
  3048. * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
  3049. * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
  3050. * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
  3051. * packet must fill in the Verification Tag field of the outbound
  3052. * packet with the Verification Tag received in the SHUTDOWN ACK and
  3053. * set the T-bit in the Chunk Flags to indicate that the Verification
  3054. * Tag is reflected.
  3055. *
  3056. * 8) The receiver should respond to the sender of the OOTB packet with
  3057. * an ABORT. When sending the ABORT, the receiver of the OOTB packet
  3058. * MUST fill in the Verification Tag field of the outbound packet
  3059. * with the value found in the Verification Tag field of the OOTB
  3060. * packet and set the T-bit in the Chunk Flags to indicate that the
  3061. * Verification Tag is reflected. After sending this ABORT, the
  3062. * receiver of the OOTB packet shall discard the OOTB packet and take
  3063. * no further action.
  3064. */
  3065. enum sctp_disposition sctp_sf_ootb(struct net *net,
  3066. const struct sctp_endpoint *ep,
  3067. const struct sctp_association *asoc,
  3068. const union sctp_subtype type,
  3069. void *arg, struct sctp_cmd_seq *commands)
  3070. {
  3071. struct sctp_chunk *chunk = arg;
  3072. struct sk_buff *skb = chunk->skb;
  3073. struct sctp_chunkhdr *ch;
  3074. struct sctp_errhdr *err;
  3075. int ootb_cookie_ack = 0;
  3076. int ootb_shut_ack = 0;
  3077. __u8 *ch_end;
  3078. SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
  3079. ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
  3080. do {
  3081. /* Report violation if the chunk is less then minimal */
  3082. if (ntohs(ch->length) < sizeof(*ch))
  3083. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3084. commands);
  3085. /* Report violation if chunk len overflows */
  3086. ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
  3087. if (ch_end > skb_tail_pointer(skb))
  3088. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3089. commands);
  3090. /* Now that we know we at least have a chunk header,
  3091. * do things that are type appropriate.
  3092. */
  3093. if (SCTP_CID_SHUTDOWN_ACK == ch->type)
  3094. ootb_shut_ack = 1;
  3095. /* RFC 2960, Section 3.3.7
  3096. * Moreover, under any circumstances, an endpoint that
  3097. * receives an ABORT MUST NOT respond to that ABORT by
  3098. * sending an ABORT of its own.
  3099. */
  3100. if (SCTP_CID_ABORT == ch->type)
  3101. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3102. /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
  3103. * or a COOKIE ACK the SCTP Packet should be silently
  3104. * discarded.
  3105. */
  3106. if (SCTP_CID_COOKIE_ACK == ch->type)
  3107. ootb_cookie_ack = 1;
  3108. if (SCTP_CID_ERROR == ch->type) {
  3109. sctp_walk_errors(err, ch) {
  3110. if (SCTP_ERROR_STALE_COOKIE == err->cause) {
  3111. ootb_cookie_ack = 1;
  3112. break;
  3113. }
  3114. }
  3115. }
  3116. ch = (struct sctp_chunkhdr *)ch_end;
  3117. } while (ch_end < skb_tail_pointer(skb));
  3118. if (ootb_shut_ack)
  3119. return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
  3120. else if (ootb_cookie_ack)
  3121. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3122. else
  3123. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  3124. }
  3125. /*
  3126. * Handle an "Out of the blue" SHUTDOWN ACK.
  3127. *
  3128. * Section: 8.4 5, sctpimpguide 2.41.
  3129. *
  3130. * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
  3131. * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
  3132. * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
  3133. * packet must fill in the Verification Tag field of the outbound
  3134. * packet with the Verification Tag received in the SHUTDOWN ACK and
  3135. * set the T-bit in the Chunk Flags to indicate that the Verification
  3136. * Tag is reflected.
  3137. *
  3138. * Inputs
  3139. * (endpoint, asoc, type, arg, commands)
  3140. *
  3141. * Outputs
  3142. * (enum sctp_disposition)
  3143. *
  3144. * The return value is the disposition of the chunk.
  3145. */
  3146. static enum sctp_disposition sctp_sf_shut_8_4_5(
  3147. struct net *net,
  3148. const struct sctp_endpoint *ep,
  3149. const struct sctp_association *asoc,
  3150. const union sctp_subtype type,
  3151. void *arg,
  3152. struct sctp_cmd_seq *commands)
  3153. {
  3154. struct sctp_packet *packet = NULL;
  3155. struct sctp_chunk *chunk = arg;
  3156. struct sctp_chunk *shut;
  3157. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  3158. if (!packet)
  3159. return SCTP_DISPOSITION_NOMEM;
  3160. /* Make an SHUTDOWN_COMPLETE.
  3161. * The T bit will be set if the asoc is NULL.
  3162. */
  3163. shut = sctp_make_shutdown_complete(asoc, chunk);
  3164. if (!shut) {
  3165. sctp_ootb_pkt_free(packet);
  3166. return SCTP_DISPOSITION_NOMEM;
  3167. }
  3168. /* Reflect vtag if T-Bit is set */
  3169. if (sctp_test_T_bit(shut))
  3170. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  3171. /* Set the skb to the belonging sock for accounting. */
  3172. shut->skb->sk = ep->base.sk;
  3173. sctp_packet_append_chunk(packet, shut);
  3174. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  3175. SCTP_PACKET(packet));
  3176. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  3177. /* If the chunk length is invalid, we don't want to process
  3178. * the reset of the packet.
  3179. */
  3180. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  3181. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3182. /* We need to discard the rest of the packet to prevent
  3183. * potential bomming attacks from additional bundled chunks.
  3184. * This is documented in SCTP Threats ID.
  3185. */
  3186. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3187. }
  3188. /*
  3189. * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
  3190. *
  3191. * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
  3192. * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
  3193. * procedures in section 8.4 SHOULD be followed, in other words it
  3194. * should be treated as an Out Of The Blue packet.
  3195. * [This means that we do NOT check the Verification Tag on these
  3196. * chunks. --piggy ]
  3197. *
  3198. */
  3199. enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
  3200. const struct sctp_endpoint *ep,
  3201. const struct sctp_association *asoc,
  3202. const union sctp_subtype type,
  3203. void *arg,
  3204. struct sctp_cmd_seq *commands)
  3205. {
  3206. struct sctp_chunk *chunk = arg;
  3207. /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
  3208. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  3209. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3210. commands);
  3211. /* Although we do have an association in this case, it corresponds
  3212. * to a restarted association. So the packet is treated as an OOTB
  3213. * packet and the state function that handles OOTB SHUTDOWN_ACK is
  3214. * called with a NULL association.
  3215. */
  3216. SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
  3217. return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
  3218. }
  3219. /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
  3220. enum sctp_disposition sctp_sf_do_asconf(struct net *net,
  3221. const struct sctp_endpoint *ep,
  3222. const struct sctp_association *asoc,
  3223. const union sctp_subtype type,
  3224. void *arg,
  3225. struct sctp_cmd_seq *commands)
  3226. {
  3227. struct sctp_paramhdr *err_param = NULL;
  3228. struct sctp_chunk *asconf_ack = NULL;
  3229. struct sctp_chunk *chunk = arg;
  3230. struct sctp_addiphdr *hdr;
  3231. __u32 serial;
  3232. if (!sctp_vtag_verify(chunk, asoc)) {
  3233. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3234. SCTP_NULL());
  3235. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3236. }
  3237. /* ADD-IP: Section 4.1.1
  3238. * This chunk MUST be sent in an authenticated way by using
  3239. * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
  3240. * is received unauthenticated it MUST be silently discarded as
  3241. * described in [I-D.ietf-tsvwg-sctp-auth].
  3242. */
  3243. if (!net->sctp.addip_noauth && !chunk->auth)
  3244. return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
  3245. commands);
  3246. /* Make sure that the ASCONF ADDIP chunk has a valid length. */
  3247. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
  3248. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3249. commands);
  3250. hdr = (struct sctp_addiphdr *)chunk->skb->data;
  3251. serial = ntohl(hdr->serial);
  3252. /* Verify the ASCONF chunk before processing it. */
  3253. if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
  3254. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  3255. (void *)err_param, commands);
  3256. /* ADDIP 5.2 E1) Compare the value of the serial number to the value
  3257. * the endpoint stored in a new association variable
  3258. * 'Peer-Serial-Number'.
  3259. */
  3260. if (serial == asoc->peer.addip_serial + 1) {
  3261. /* If this is the first instance of ASCONF in the packet,
  3262. * we can clean our old ASCONF-ACKs.
  3263. */
  3264. if (!chunk->has_asconf)
  3265. sctp_assoc_clean_asconf_ack_cache(asoc);
  3266. /* ADDIP 5.2 E4) When the Sequence Number matches the next one
  3267. * expected, process the ASCONF as described below and after
  3268. * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
  3269. * the response packet and cache a copy of it (in the event it
  3270. * later needs to be retransmitted).
  3271. *
  3272. * Essentially, do V1-V5.
  3273. */
  3274. asconf_ack = sctp_process_asconf((struct sctp_association *)
  3275. asoc, chunk);
  3276. if (!asconf_ack)
  3277. return SCTP_DISPOSITION_NOMEM;
  3278. } else if (serial < asoc->peer.addip_serial + 1) {
  3279. /* ADDIP 5.2 E2)
  3280. * If the value found in the Sequence Number is less than the
  3281. * ('Peer- Sequence-Number' + 1), simply skip to the next
  3282. * ASCONF, and include in the outbound response packet
  3283. * any previously cached ASCONF-ACK response that was
  3284. * sent and saved that matches the Sequence Number of the
  3285. * ASCONF. Note: It is possible that no cached ASCONF-ACK
  3286. * Chunk exists. This will occur when an older ASCONF
  3287. * arrives out of order. In such a case, the receiver
  3288. * should skip the ASCONF Chunk and not include ASCONF-ACK
  3289. * Chunk for that chunk.
  3290. */
  3291. asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
  3292. if (!asconf_ack)
  3293. return SCTP_DISPOSITION_DISCARD;
  3294. /* Reset the transport so that we select the correct one
  3295. * this time around. This is to make sure that we don't
  3296. * accidentally use a stale transport that's been removed.
  3297. */
  3298. asconf_ack->transport = NULL;
  3299. } else {
  3300. /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
  3301. * it must be either a stale packet or from an attacker.
  3302. */
  3303. return SCTP_DISPOSITION_DISCARD;
  3304. }
  3305. /* ADDIP 5.2 E6) The destination address of the SCTP packet
  3306. * containing the ASCONF-ACK Chunks MUST be the source address of
  3307. * the SCTP packet that held the ASCONF Chunks.
  3308. *
  3309. * To do this properly, we'll set the destination address of the chunk
  3310. * and at the transmit time, will try look up the transport to use.
  3311. * Since ASCONFs may be bundled, the correct transport may not be
  3312. * created until we process the entire packet, thus this workaround.
  3313. */
  3314. asconf_ack->dest = chunk->source;
  3315. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
  3316. if (asoc->new_transport) {
  3317. sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
  3318. ((struct sctp_association *)asoc)->new_transport = NULL;
  3319. }
  3320. return SCTP_DISPOSITION_CONSUME;
  3321. }
  3322. /*
  3323. * ADDIP Section 4.3 General rules for address manipulation
  3324. * When building TLV parameters for the ASCONF Chunk that will add or
  3325. * delete IP addresses the D0 to D13 rules should be applied:
  3326. */
  3327. enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
  3328. const struct sctp_endpoint *ep,
  3329. const struct sctp_association *asoc,
  3330. const union sctp_subtype type,
  3331. void *arg,
  3332. struct sctp_cmd_seq *commands)
  3333. {
  3334. struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
  3335. struct sctp_paramhdr *err_param = NULL;
  3336. struct sctp_chunk *asconf_ack = arg;
  3337. struct sctp_addiphdr *addip_hdr;
  3338. __u32 sent_serial, rcvd_serial;
  3339. struct sctp_chunk *abort;
  3340. if (!sctp_vtag_verify(asconf_ack, asoc)) {
  3341. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3342. SCTP_NULL());
  3343. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3344. }
  3345. /* ADD-IP, Section 4.1.2:
  3346. * This chunk MUST be sent in an authenticated way by using
  3347. * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
  3348. * is received unauthenticated it MUST be silently discarded as
  3349. * described in [I-D.ietf-tsvwg-sctp-auth].
  3350. */
  3351. if (!net->sctp.addip_noauth && !asconf_ack->auth)
  3352. return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
  3353. commands);
  3354. /* Make sure that the ADDIP chunk has a valid length. */
  3355. if (!sctp_chunk_length_valid(asconf_ack,
  3356. sizeof(struct sctp_addip_chunk)))
  3357. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3358. commands);
  3359. addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
  3360. rcvd_serial = ntohl(addip_hdr->serial);
  3361. /* Verify the ASCONF-ACK chunk before processing it. */
  3362. if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
  3363. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  3364. (void *)err_param, commands);
  3365. if (last_asconf) {
  3366. addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
  3367. sent_serial = ntohl(addip_hdr->serial);
  3368. } else {
  3369. sent_serial = asoc->addip_serial - 1;
  3370. }
  3371. /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
  3372. * equal to the next serial number to be used but no ASCONF chunk is
  3373. * outstanding the endpoint MUST ABORT the association. Note that a
  3374. * sequence number is greater than if it is no more than 2^^31-1
  3375. * larger than the current sequence number (using serial arithmetic).
  3376. */
  3377. if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
  3378. !(asoc->addip_last_asconf)) {
  3379. abort = sctp_make_abort(asoc, asconf_ack,
  3380. sizeof(struct sctp_errhdr));
  3381. if (abort) {
  3382. sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
  3383. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3384. SCTP_CHUNK(abort));
  3385. }
  3386. /* We are going to ABORT, so we might as well stop
  3387. * processing the rest of the chunks in the packet.
  3388. */
  3389. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3390. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  3391. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  3392. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  3393. SCTP_ERROR(ECONNABORTED));
  3394. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  3395. SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
  3396. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  3397. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  3398. return SCTP_DISPOSITION_ABORT;
  3399. }
  3400. if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
  3401. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3402. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  3403. if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
  3404. asconf_ack)) {
  3405. /* Successfully processed ASCONF_ACK. We can
  3406. * release the next asconf if we have one.
  3407. */
  3408. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
  3409. SCTP_NULL());
  3410. return SCTP_DISPOSITION_CONSUME;
  3411. }
  3412. abort = sctp_make_abort(asoc, asconf_ack,
  3413. sizeof(struct sctp_errhdr));
  3414. if (abort) {
  3415. sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
  3416. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3417. SCTP_CHUNK(abort));
  3418. }
  3419. /* We are going to ABORT, so we might as well stop
  3420. * processing the rest of the chunks in the packet.
  3421. */
  3422. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  3423. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  3424. SCTP_ERROR(ECONNABORTED));
  3425. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  3426. SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
  3427. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  3428. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  3429. return SCTP_DISPOSITION_ABORT;
  3430. }
  3431. return SCTP_DISPOSITION_DISCARD;
  3432. }
  3433. /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
  3434. enum sctp_disposition sctp_sf_do_reconf(struct net *net,
  3435. const struct sctp_endpoint *ep,
  3436. const struct sctp_association *asoc,
  3437. const union sctp_subtype type,
  3438. void *arg,
  3439. struct sctp_cmd_seq *commands)
  3440. {
  3441. struct sctp_paramhdr *err_param = NULL;
  3442. struct sctp_chunk *chunk = arg;
  3443. struct sctp_reconf_chunk *hdr;
  3444. union sctp_params param;
  3445. if (!sctp_vtag_verify(chunk, asoc)) {
  3446. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3447. SCTP_NULL());
  3448. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3449. }
  3450. /* Make sure that the RECONF chunk has a valid length. */
  3451. if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
  3452. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3453. commands);
  3454. if (!sctp_verify_reconf(asoc, chunk, &err_param))
  3455. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  3456. (void *)err_param, commands);
  3457. hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
  3458. sctp_walk_params(param, hdr, params) {
  3459. struct sctp_chunk *reply = NULL;
  3460. struct sctp_ulpevent *ev = NULL;
  3461. if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
  3462. reply = sctp_process_strreset_outreq(
  3463. (struct sctp_association *)asoc, param, &ev);
  3464. else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
  3465. reply = sctp_process_strreset_inreq(
  3466. (struct sctp_association *)asoc, param, &ev);
  3467. else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
  3468. reply = sctp_process_strreset_tsnreq(
  3469. (struct sctp_association *)asoc, param, &ev);
  3470. else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
  3471. reply = sctp_process_strreset_addstrm_out(
  3472. (struct sctp_association *)asoc, param, &ev);
  3473. else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
  3474. reply = sctp_process_strreset_addstrm_in(
  3475. (struct sctp_association *)asoc, param, &ev);
  3476. else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
  3477. reply = sctp_process_strreset_resp(
  3478. (struct sctp_association *)asoc, param, &ev);
  3479. if (ev)
  3480. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  3481. SCTP_ULPEVENT(ev));
  3482. if (reply)
  3483. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3484. SCTP_CHUNK(reply));
  3485. }
  3486. return SCTP_DISPOSITION_CONSUME;
  3487. }
  3488. /*
  3489. * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
  3490. *
  3491. * When a FORWARD TSN chunk arrives, the data receiver MUST first update
  3492. * its cumulative TSN point to the value carried in the FORWARD TSN
  3493. * chunk, and then MUST further advance its cumulative TSN point locally
  3494. * if possible.
  3495. * After the above processing, the data receiver MUST stop reporting any
  3496. * missing TSNs earlier than or equal to the new cumulative TSN point.
  3497. *
  3498. * Verification Tag: 8.5 Verification Tag [Normal verification]
  3499. *
  3500. * The return value is the disposition of the chunk.
  3501. */
  3502. enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
  3503. const struct sctp_endpoint *ep,
  3504. const struct sctp_association *asoc,
  3505. const union sctp_subtype type,
  3506. void *arg,
  3507. struct sctp_cmd_seq *commands)
  3508. {
  3509. struct sctp_fwdtsn_hdr *fwdtsn_hdr;
  3510. struct sctp_chunk *chunk = arg;
  3511. struct sctp_fwdtsn_skip *skip;
  3512. __u16 len;
  3513. __u32 tsn;
  3514. if (!sctp_vtag_verify(chunk, asoc)) {
  3515. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3516. SCTP_NULL());
  3517. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3518. }
  3519. if (!asoc->peer.prsctp_capable)
  3520. return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
  3521. /* Make sure that the FORWARD_TSN chunk has valid length. */
  3522. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
  3523. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3524. commands);
  3525. fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
  3526. chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
  3527. len = ntohs(chunk->chunk_hdr->length);
  3528. len -= sizeof(struct sctp_chunkhdr);
  3529. skb_pull(chunk->skb, len);
  3530. tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
  3531. pr_debug("%s: TSN 0x%x\n", __func__, tsn);
  3532. /* The TSN is too high--silently discard the chunk and count on it
  3533. * getting retransmitted later.
  3534. */
  3535. if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
  3536. goto discard_noforce;
  3537. /* Silently discard the chunk if stream-id is not valid */
  3538. sctp_walk_fwdtsn(skip, chunk) {
  3539. if (ntohs(skip->stream) >= asoc->stream.incnt)
  3540. goto discard_noforce;
  3541. }
  3542. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
  3543. if (len > sizeof(struct sctp_fwdtsn_hdr))
  3544. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
  3545. SCTP_CHUNK(chunk));
  3546. /* Count this as receiving DATA. */
  3547. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
  3548. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  3549. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  3550. }
  3551. /* FIXME: For now send a SACK, but DATA processing may
  3552. * send another.
  3553. */
  3554. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
  3555. return SCTP_DISPOSITION_CONSUME;
  3556. discard_noforce:
  3557. return SCTP_DISPOSITION_DISCARD;
  3558. }
  3559. enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
  3560. struct net *net,
  3561. const struct sctp_endpoint *ep,
  3562. const struct sctp_association *asoc,
  3563. const union sctp_subtype type,
  3564. void *arg,
  3565. struct sctp_cmd_seq *commands)
  3566. {
  3567. struct sctp_fwdtsn_hdr *fwdtsn_hdr;
  3568. struct sctp_chunk *chunk = arg;
  3569. struct sctp_fwdtsn_skip *skip;
  3570. __u16 len;
  3571. __u32 tsn;
  3572. if (!sctp_vtag_verify(chunk, asoc)) {
  3573. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3574. SCTP_NULL());
  3575. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3576. }
  3577. if (!asoc->peer.prsctp_capable)
  3578. return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
  3579. /* Make sure that the FORWARD_TSN chunk has a valid length. */
  3580. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
  3581. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3582. commands);
  3583. fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
  3584. chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
  3585. len = ntohs(chunk->chunk_hdr->length);
  3586. len -= sizeof(struct sctp_chunkhdr);
  3587. skb_pull(chunk->skb, len);
  3588. tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
  3589. pr_debug("%s: TSN 0x%x\n", __func__, tsn);
  3590. /* The TSN is too high--silently discard the chunk and count on it
  3591. * getting retransmitted later.
  3592. */
  3593. if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
  3594. goto gen_shutdown;
  3595. /* Silently discard the chunk if stream-id is not valid */
  3596. sctp_walk_fwdtsn(skip, chunk) {
  3597. if (ntohs(skip->stream) >= asoc->stream.incnt)
  3598. goto gen_shutdown;
  3599. }
  3600. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
  3601. if (len > sizeof(struct sctp_fwdtsn_hdr))
  3602. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
  3603. SCTP_CHUNK(chunk));
  3604. /* Go a head and force a SACK, since we are shutting down. */
  3605. gen_shutdown:
  3606. /* Implementor's Guide.
  3607. *
  3608. * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
  3609. * respond to each received packet containing one or more DATA chunk(s)
  3610. * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
  3611. */
  3612. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
  3613. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  3614. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  3615. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  3616. return SCTP_DISPOSITION_CONSUME;
  3617. }
  3618. /*
  3619. * SCTP-AUTH Section 6.3 Receiving authenticated chukns
  3620. *
  3621. * The receiver MUST use the HMAC algorithm indicated in the HMAC
  3622. * Identifier field. If this algorithm was not specified by the
  3623. * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
  3624. * during association setup, the AUTH chunk and all chunks after it MUST
  3625. * be discarded and an ERROR chunk SHOULD be sent with the error cause
  3626. * defined in Section 4.1.
  3627. *
  3628. * If an endpoint with no shared key receives a Shared Key Identifier
  3629. * other than 0, it MUST silently discard all authenticated chunks. If
  3630. * the endpoint has at least one endpoint pair shared key for the peer,
  3631. * it MUST use the key specified by the Shared Key Identifier if a
  3632. * key has been configured for that Shared Key Identifier. If no
  3633. * endpoint pair shared key has been configured for that Shared Key
  3634. * Identifier, all authenticated chunks MUST be silently discarded.
  3635. *
  3636. * Verification Tag: 8.5 Verification Tag [Normal verification]
  3637. *
  3638. * The return value is the disposition of the chunk.
  3639. */
  3640. static enum sctp_ierror sctp_sf_authenticate(
  3641. struct net *net,
  3642. const struct sctp_endpoint *ep,
  3643. const struct sctp_association *asoc,
  3644. const union sctp_subtype type,
  3645. struct sctp_chunk *chunk)
  3646. {
  3647. struct sctp_authhdr *auth_hdr;
  3648. __u8 *save_digest, *digest;
  3649. struct sctp_hmac *hmac;
  3650. unsigned int sig_len;
  3651. __u16 key_id;
  3652. /* Pull in the auth header, so we can do some more verification */
  3653. auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
  3654. chunk->subh.auth_hdr = auth_hdr;
  3655. skb_pull(chunk->skb, sizeof(*auth_hdr));
  3656. /* Make sure that we support the HMAC algorithm from the auth
  3657. * chunk.
  3658. */
  3659. if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
  3660. return SCTP_IERROR_AUTH_BAD_HMAC;
  3661. /* Make sure that the provided shared key identifier has been
  3662. * configured
  3663. */
  3664. key_id = ntohs(auth_hdr->shkey_id);
  3665. if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
  3666. return SCTP_IERROR_AUTH_BAD_KEYID;
  3667. /* Make sure that the length of the signature matches what
  3668. * we expect.
  3669. */
  3670. sig_len = ntohs(chunk->chunk_hdr->length) -
  3671. sizeof(struct sctp_auth_chunk);
  3672. hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
  3673. if (sig_len != hmac->hmac_len)
  3674. return SCTP_IERROR_PROTO_VIOLATION;
  3675. /* Now that we've done validation checks, we can compute and
  3676. * verify the hmac. The steps involved are:
  3677. * 1. Save the digest from the chunk.
  3678. * 2. Zero out the digest in the chunk.
  3679. * 3. Compute the new digest
  3680. * 4. Compare saved and new digests.
  3681. */
  3682. digest = auth_hdr->hmac;
  3683. skb_pull(chunk->skb, sig_len);
  3684. save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
  3685. if (!save_digest)
  3686. goto nomem;
  3687. memset(digest, 0, sig_len);
  3688. sctp_auth_calculate_hmac(asoc, chunk->skb,
  3689. (struct sctp_auth_chunk *)chunk->chunk_hdr,
  3690. GFP_ATOMIC);
  3691. /* Discard the packet if the digests do not match */
  3692. if (memcmp(save_digest, digest, sig_len)) {
  3693. kfree(save_digest);
  3694. return SCTP_IERROR_BAD_SIG;
  3695. }
  3696. kfree(save_digest);
  3697. chunk->auth = 1;
  3698. return SCTP_IERROR_NO_ERROR;
  3699. nomem:
  3700. return SCTP_IERROR_NOMEM;
  3701. }
  3702. enum sctp_disposition sctp_sf_eat_auth(struct net *net,
  3703. const struct sctp_endpoint *ep,
  3704. const struct sctp_association *asoc,
  3705. const union sctp_subtype type,
  3706. void *arg, struct sctp_cmd_seq *commands)
  3707. {
  3708. struct sctp_chunk *chunk = arg;
  3709. struct sctp_authhdr *auth_hdr;
  3710. struct sctp_chunk *err_chunk;
  3711. enum sctp_ierror error;
  3712. /* Make sure that the peer has AUTH capable */
  3713. if (!asoc->peer.auth_capable)
  3714. return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
  3715. if (!sctp_vtag_verify(chunk, asoc)) {
  3716. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3717. SCTP_NULL());
  3718. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3719. }
  3720. /* Make sure that the AUTH chunk has valid length. */
  3721. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
  3722. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3723. commands);
  3724. auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
  3725. error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
  3726. switch (error) {
  3727. case SCTP_IERROR_AUTH_BAD_HMAC:
  3728. /* Generate the ERROR chunk and discard the rest
  3729. * of the packet
  3730. */
  3731. err_chunk = sctp_make_op_error(asoc, chunk,
  3732. SCTP_ERROR_UNSUP_HMAC,
  3733. &auth_hdr->hmac_id,
  3734. sizeof(__u16), 0);
  3735. if (err_chunk) {
  3736. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3737. SCTP_CHUNK(err_chunk));
  3738. }
  3739. /* Fall Through */
  3740. case SCTP_IERROR_AUTH_BAD_KEYID:
  3741. case SCTP_IERROR_BAD_SIG:
  3742. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3743. case SCTP_IERROR_PROTO_VIOLATION:
  3744. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3745. commands);
  3746. case SCTP_IERROR_NOMEM:
  3747. return SCTP_DISPOSITION_NOMEM;
  3748. default: /* Prevent gcc warnings */
  3749. break;
  3750. }
  3751. if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
  3752. struct sctp_ulpevent *ev;
  3753. ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
  3754. SCTP_AUTH_NEWKEY, GFP_ATOMIC);
  3755. if (!ev)
  3756. return -ENOMEM;
  3757. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  3758. SCTP_ULPEVENT(ev));
  3759. }
  3760. return SCTP_DISPOSITION_CONSUME;
  3761. }
  3762. /*
  3763. * Process an unknown chunk.
  3764. *
  3765. * Section: 3.2. Also, 2.1 in the implementor's guide.
  3766. *
  3767. * Chunk Types are encoded such that the highest-order two bits specify
  3768. * the action that must be taken if the processing endpoint does not
  3769. * recognize the Chunk Type.
  3770. *
  3771. * 00 - Stop processing this SCTP packet and discard it, do not process
  3772. * any further chunks within it.
  3773. *
  3774. * 01 - Stop processing this SCTP packet and discard it, do not process
  3775. * any further chunks within it, and report the unrecognized
  3776. * chunk in an 'Unrecognized Chunk Type'.
  3777. *
  3778. * 10 - Skip this chunk and continue processing.
  3779. *
  3780. * 11 - Skip this chunk and continue processing, but report in an ERROR
  3781. * Chunk using the 'Unrecognized Chunk Type' cause of error.
  3782. *
  3783. * The return value is the disposition of the chunk.
  3784. */
  3785. enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
  3786. const struct sctp_endpoint *ep,
  3787. const struct sctp_association *asoc,
  3788. const union sctp_subtype type,
  3789. void *arg,
  3790. struct sctp_cmd_seq *commands)
  3791. {
  3792. struct sctp_chunk *unk_chunk = arg;
  3793. struct sctp_chunk *err_chunk;
  3794. struct sctp_chunkhdr *hdr;
  3795. pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
  3796. if (!sctp_vtag_verify(unk_chunk, asoc))
  3797. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3798. /* Make sure that the chunk has a valid length.
  3799. * Since we don't know the chunk type, we use a general
  3800. * chunkhdr structure to make a comparison.
  3801. */
  3802. if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
  3803. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3804. commands);
  3805. switch (type.chunk & SCTP_CID_ACTION_MASK) {
  3806. case SCTP_CID_ACTION_DISCARD:
  3807. /* Discard the packet. */
  3808. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3809. case SCTP_CID_ACTION_DISCARD_ERR:
  3810. /* Generate an ERROR chunk as response. */
  3811. hdr = unk_chunk->chunk_hdr;
  3812. err_chunk = sctp_make_op_error(asoc, unk_chunk,
  3813. SCTP_ERROR_UNKNOWN_CHUNK, hdr,
  3814. SCTP_PAD4(ntohs(hdr->length)),
  3815. 0);
  3816. if (err_chunk) {
  3817. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3818. SCTP_CHUNK(err_chunk));
  3819. }
  3820. /* Discard the packet. */
  3821. sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3822. return SCTP_DISPOSITION_CONSUME;
  3823. case SCTP_CID_ACTION_SKIP:
  3824. /* Skip the chunk. */
  3825. return SCTP_DISPOSITION_DISCARD;
  3826. case SCTP_CID_ACTION_SKIP_ERR:
  3827. /* Generate an ERROR chunk as response. */
  3828. hdr = unk_chunk->chunk_hdr;
  3829. err_chunk = sctp_make_op_error(asoc, unk_chunk,
  3830. SCTP_ERROR_UNKNOWN_CHUNK, hdr,
  3831. SCTP_PAD4(ntohs(hdr->length)),
  3832. 0);
  3833. if (err_chunk) {
  3834. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3835. SCTP_CHUNK(err_chunk));
  3836. }
  3837. /* Skip the chunk. */
  3838. return SCTP_DISPOSITION_CONSUME;
  3839. default:
  3840. break;
  3841. }
  3842. return SCTP_DISPOSITION_DISCARD;
  3843. }
  3844. /*
  3845. * Discard the chunk.
  3846. *
  3847. * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
  3848. * [Too numerous to mention...]
  3849. * Verification Tag: No verification needed.
  3850. * Inputs
  3851. * (endpoint, asoc, chunk)
  3852. *
  3853. * Outputs
  3854. * (asoc, reply_msg, msg_up, timers, counters)
  3855. *
  3856. * The return value is the disposition of the chunk.
  3857. */
  3858. enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
  3859. const struct sctp_endpoint *ep,
  3860. const struct sctp_association *asoc,
  3861. const union sctp_subtype type,
  3862. void *arg,
  3863. struct sctp_cmd_seq *commands)
  3864. {
  3865. struct sctp_chunk *chunk = arg;
  3866. /* Make sure that the chunk has a valid length.
  3867. * Since we don't know the chunk type, we use a general
  3868. * chunkhdr structure to make a comparison.
  3869. */
  3870. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  3871. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3872. commands);
  3873. pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
  3874. return SCTP_DISPOSITION_DISCARD;
  3875. }
  3876. /*
  3877. * Discard the whole packet.
  3878. *
  3879. * Section: 8.4 2)
  3880. *
  3881. * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
  3882. * silently discard the OOTB packet and take no further action.
  3883. *
  3884. * Verification Tag: No verification necessary
  3885. *
  3886. * Inputs
  3887. * (endpoint, asoc, chunk)
  3888. *
  3889. * Outputs
  3890. * (asoc, reply_msg, msg_up, timers, counters)
  3891. *
  3892. * The return value is the disposition of the chunk.
  3893. */
  3894. enum sctp_disposition sctp_sf_pdiscard(struct net *net,
  3895. const struct sctp_endpoint *ep,
  3896. const struct sctp_association *asoc,
  3897. const union sctp_subtype type,
  3898. void *arg, struct sctp_cmd_seq *commands)
  3899. {
  3900. SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
  3901. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  3902. return SCTP_DISPOSITION_CONSUME;
  3903. }
  3904. /*
  3905. * The other end is violating protocol.
  3906. *
  3907. * Section: Not specified
  3908. * Verification Tag: Not specified
  3909. * Inputs
  3910. * (endpoint, asoc, chunk)
  3911. *
  3912. * Outputs
  3913. * (asoc, reply_msg, msg_up, timers, counters)
  3914. *
  3915. * We simply tag the chunk as a violation. The state machine will log
  3916. * the violation and continue.
  3917. */
  3918. enum sctp_disposition sctp_sf_violation(struct net *net,
  3919. const struct sctp_endpoint *ep,
  3920. const struct sctp_association *asoc,
  3921. const union sctp_subtype type,
  3922. void *arg,
  3923. struct sctp_cmd_seq *commands)
  3924. {
  3925. struct sctp_chunk *chunk = arg;
  3926. /* Make sure that the chunk has a valid length. */
  3927. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  3928. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3929. commands);
  3930. return SCTP_DISPOSITION_VIOLATION;
  3931. }
  3932. /*
  3933. * Common function to handle a protocol violation.
  3934. */
  3935. static enum sctp_disposition sctp_sf_abort_violation(
  3936. struct net *net,
  3937. const struct sctp_endpoint *ep,
  3938. const struct sctp_association *asoc,
  3939. void *arg,
  3940. struct sctp_cmd_seq *commands,
  3941. const __u8 *payload,
  3942. const size_t paylen)
  3943. {
  3944. struct sctp_packet *packet = NULL;
  3945. struct sctp_chunk *chunk = arg;
  3946. struct sctp_chunk *abort = NULL;
  3947. /* SCTP-AUTH, Section 6.3:
  3948. * It should be noted that if the receiver wants to tear
  3949. * down an association in an authenticated way only, the
  3950. * handling of malformed packets should not result in
  3951. * tearing down the association.
  3952. *
  3953. * This means that if we only want to abort associations
  3954. * in an authenticated way (i.e AUTH+ABORT), then we
  3955. * can't destroy this association just because the packet
  3956. * was malformed.
  3957. */
  3958. if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
  3959. goto discard;
  3960. /* Make the abort chunk. */
  3961. abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
  3962. if (!abort)
  3963. goto nomem;
  3964. if (asoc) {
  3965. /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
  3966. if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
  3967. !asoc->peer.i.init_tag) {
  3968. struct sctp_initack_chunk *initack;
  3969. initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
  3970. if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
  3971. abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
  3972. else {
  3973. unsigned int inittag;
  3974. inittag = ntohl(initack->init_hdr.init_tag);
  3975. sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
  3976. SCTP_U32(inittag));
  3977. }
  3978. }
  3979. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  3980. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  3981. if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
  3982. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3983. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  3984. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  3985. SCTP_ERROR(ECONNREFUSED));
  3986. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  3987. SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
  3988. } else {
  3989. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  3990. SCTP_ERROR(ECONNABORTED));
  3991. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  3992. SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
  3993. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  3994. }
  3995. } else {
  3996. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  3997. if (!packet)
  3998. goto nomem_pkt;
  3999. if (sctp_test_T_bit(abort))
  4000. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  4001. abort->skb->sk = ep->base.sk;
  4002. sctp_packet_append_chunk(packet, abort);
  4003. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  4004. SCTP_PACKET(packet));
  4005. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  4006. }
  4007. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  4008. discard:
  4009. sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
  4010. return SCTP_DISPOSITION_ABORT;
  4011. nomem_pkt:
  4012. sctp_chunk_free(abort);
  4013. nomem:
  4014. return SCTP_DISPOSITION_NOMEM;
  4015. }
  4016. /*
  4017. * Handle a protocol violation when the chunk length is invalid.
  4018. * "Invalid" length is identified as smaller than the minimal length a
  4019. * given chunk can be. For example, a SACK chunk has invalid length
  4020. * if its length is set to be smaller than the size of struct sctp_sack_chunk.
  4021. *
  4022. * We inform the other end by sending an ABORT with a Protocol Violation
  4023. * error code.
  4024. *
  4025. * Section: Not specified
  4026. * Verification Tag: Nothing to do
  4027. * Inputs
  4028. * (endpoint, asoc, chunk)
  4029. *
  4030. * Outputs
  4031. * (reply_msg, msg_up, counters)
  4032. *
  4033. * Generate an ABORT chunk and terminate the association.
  4034. */
  4035. static enum sctp_disposition sctp_sf_violation_chunklen(
  4036. struct net *net,
  4037. const struct sctp_endpoint *ep,
  4038. const struct sctp_association *asoc,
  4039. const union sctp_subtype type,
  4040. void *arg,
  4041. struct sctp_cmd_seq *commands)
  4042. {
  4043. static const char err_str[] = "The following chunk had invalid length:";
  4044. return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
  4045. sizeof(err_str));
  4046. }
  4047. /*
  4048. * Handle a protocol violation when the parameter length is invalid.
  4049. * If the length is smaller than the minimum length of a given parameter,
  4050. * or accumulated length in multi parameters exceeds the end of the chunk,
  4051. * the length is considered as invalid.
  4052. */
  4053. static enum sctp_disposition sctp_sf_violation_paramlen(
  4054. struct net *net,
  4055. const struct sctp_endpoint *ep,
  4056. const struct sctp_association *asoc,
  4057. const union sctp_subtype type,
  4058. void *arg, void *ext,
  4059. struct sctp_cmd_seq *commands)
  4060. {
  4061. struct sctp_paramhdr *param = ext;
  4062. struct sctp_chunk *abort = NULL;
  4063. struct sctp_chunk *chunk = arg;
  4064. if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
  4065. goto discard;
  4066. /* Make the abort chunk. */
  4067. abort = sctp_make_violation_paramlen(asoc, chunk, param);
  4068. if (!abort)
  4069. goto nomem;
  4070. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  4071. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  4072. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  4073. SCTP_ERROR(ECONNABORTED));
  4074. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4075. SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
  4076. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  4077. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  4078. discard:
  4079. sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
  4080. return SCTP_DISPOSITION_ABORT;
  4081. nomem:
  4082. return SCTP_DISPOSITION_NOMEM;
  4083. }
  4084. /* Handle a protocol violation when the peer trying to advance the
  4085. * cumulative tsn ack to a point beyond the max tsn currently sent.
  4086. *
  4087. * We inform the other end by sending an ABORT with a Protocol Violation
  4088. * error code.
  4089. */
  4090. static enum sctp_disposition sctp_sf_violation_ctsn(
  4091. struct net *net,
  4092. const struct sctp_endpoint *ep,
  4093. const struct sctp_association *asoc,
  4094. const union sctp_subtype type,
  4095. void *arg,
  4096. struct sctp_cmd_seq *commands)
  4097. {
  4098. static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
  4099. return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
  4100. sizeof(err_str));
  4101. }
  4102. /* Handle protocol violation of an invalid chunk bundling. For example,
  4103. * when we have an association and we receive bundled INIT-ACK, or
  4104. * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
  4105. * statement from the specs. Additionally, there might be an attacker
  4106. * on the path and we may not want to continue this communication.
  4107. */
  4108. static enum sctp_disposition sctp_sf_violation_chunk(
  4109. struct net *net,
  4110. const struct sctp_endpoint *ep,
  4111. const struct sctp_association *asoc,
  4112. const union sctp_subtype type,
  4113. void *arg,
  4114. struct sctp_cmd_seq *commands)
  4115. {
  4116. static const char err_str[] = "The following chunk violates protocol:";
  4117. if (!asoc)
  4118. return sctp_sf_violation(net, ep, asoc, type, arg, commands);
  4119. return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
  4120. sizeof(err_str));
  4121. }
  4122. /***************************************************************************
  4123. * These are the state functions for handling primitive (Section 10) events.
  4124. ***************************************************************************/
  4125. /*
  4126. * sctp_sf_do_prm_asoc
  4127. *
  4128. * Section: 10.1 ULP-to-SCTP
  4129. * B) Associate
  4130. *
  4131. * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
  4132. * outbound stream count)
  4133. * -> association id [,destination transport addr list] [,outbound stream
  4134. * count]
  4135. *
  4136. * This primitive allows the upper layer to initiate an association to a
  4137. * specific peer endpoint.
  4138. *
  4139. * The peer endpoint shall be specified by one of the transport addresses
  4140. * which defines the endpoint (see Section 1.4). If the local SCTP
  4141. * instance has not been initialized, the ASSOCIATE is considered an
  4142. * error.
  4143. * [This is not relevant for the kernel implementation since we do all
  4144. * initialization at boot time. It we hadn't initialized we wouldn't
  4145. * get anywhere near this code.]
  4146. *
  4147. * An association id, which is a local handle to the SCTP association,
  4148. * will be returned on successful establishment of the association. If
  4149. * SCTP is not able to open an SCTP association with the peer endpoint,
  4150. * an error is returned.
  4151. * [In the kernel implementation, the struct sctp_association needs to
  4152. * be created BEFORE causing this primitive to run.]
  4153. *
  4154. * Other association parameters may be returned, including the
  4155. * complete destination transport addresses of the peer as well as the
  4156. * outbound stream count of the local endpoint. One of the transport
  4157. * address from the returned destination addresses will be selected by
  4158. * the local endpoint as default primary path for sending SCTP packets
  4159. * to this peer. The returned "destination transport addr list" can
  4160. * be used by the ULP to change the default primary path or to force
  4161. * sending a packet to a specific transport address. [All of this
  4162. * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
  4163. * function.]
  4164. *
  4165. * Mandatory attributes:
  4166. *
  4167. * o local SCTP instance name - obtained from the INITIALIZE operation.
  4168. * [This is the argument asoc.]
  4169. * o destination transport addr - specified as one of the transport
  4170. * addresses of the peer endpoint with which the association is to be
  4171. * established.
  4172. * [This is asoc->peer.active_path.]
  4173. * o outbound stream count - the number of outbound streams the ULP
  4174. * would like to open towards this peer endpoint.
  4175. * [BUG: This is not currently implemented.]
  4176. * Optional attributes:
  4177. *
  4178. * None.
  4179. *
  4180. * The return value is a disposition.
  4181. */
  4182. enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
  4183. const struct sctp_endpoint *ep,
  4184. const struct sctp_association *asoc,
  4185. const union sctp_subtype type,
  4186. void *arg,
  4187. struct sctp_cmd_seq *commands)
  4188. {
  4189. struct sctp_association *my_asoc;
  4190. struct sctp_chunk *repl;
  4191. /* The comment below says that we enter COOKIE-WAIT AFTER
  4192. * sending the INIT, but that doesn't actually work in our
  4193. * implementation...
  4194. */
  4195. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4196. SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
  4197. /* RFC 2960 5.1 Normal Establishment of an Association
  4198. *
  4199. * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
  4200. * must provide its Verification Tag (Tag_A) in the Initiate
  4201. * Tag field. Tag_A SHOULD be a random number in the range of
  4202. * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
  4203. */
  4204. repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
  4205. if (!repl)
  4206. goto nomem;
  4207. /* Choose transport for INIT. */
  4208. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
  4209. SCTP_CHUNK(repl));
  4210. /* Cast away the const modifier, as we want to just
  4211. * rerun it through as a sideffect.
  4212. */
  4213. my_asoc = (struct sctp_association *)asoc;
  4214. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
  4215. /* After sending the INIT, "A" starts the T1-init timer and
  4216. * enters the COOKIE-WAIT state.
  4217. */
  4218. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  4219. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  4220. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  4221. return SCTP_DISPOSITION_CONSUME;
  4222. nomem:
  4223. return SCTP_DISPOSITION_NOMEM;
  4224. }
  4225. /*
  4226. * Process the SEND primitive.
  4227. *
  4228. * Section: 10.1 ULP-to-SCTP
  4229. * E) Send
  4230. *
  4231. * Format: SEND(association id, buffer address, byte count [,context]
  4232. * [,stream id] [,life time] [,destination transport address]
  4233. * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
  4234. * -> result
  4235. *
  4236. * This is the main method to send user data via SCTP.
  4237. *
  4238. * Mandatory attributes:
  4239. *
  4240. * o association id - local handle to the SCTP association
  4241. *
  4242. * o buffer address - the location where the user message to be
  4243. * transmitted is stored;
  4244. *
  4245. * o byte count - The size of the user data in number of bytes;
  4246. *
  4247. * Optional attributes:
  4248. *
  4249. * o context - an optional 32 bit integer that will be carried in the
  4250. * sending failure notification to the ULP if the transportation of
  4251. * this User Message fails.
  4252. *
  4253. * o stream id - to indicate which stream to send the data on. If not
  4254. * specified, stream 0 will be used.
  4255. *
  4256. * o life time - specifies the life time of the user data. The user data
  4257. * will not be sent by SCTP after the life time expires. This
  4258. * parameter can be used to avoid efforts to transmit stale
  4259. * user messages. SCTP notifies the ULP if the data cannot be
  4260. * initiated to transport (i.e. sent to the destination via SCTP's
  4261. * send primitive) within the life time variable. However, the
  4262. * user data will be transmitted if SCTP has attempted to transmit a
  4263. * chunk before the life time expired.
  4264. *
  4265. * o destination transport address - specified as one of the destination
  4266. * transport addresses of the peer endpoint to which this packet
  4267. * should be sent. Whenever possible, SCTP should use this destination
  4268. * transport address for sending the packets, instead of the current
  4269. * primary path.
  4270. *
  4271. * o unorder flag - this flag, if present, indicates that the user
  4272. * would like the data delivered in an unordered fashion to the peer
  4273. * (i.e., the U flag is set to 1 on all DATA chunks carrying this
  4274. * message).
  4275. *
  4276. * o no-bundle flag - instructs SCTP not to bundle this user data with
  4277. * other outbound DATA chunks. SCTP MAY still bundle even when
  4278. * this flag is present, when faced with network congestion.
  4279. *
  4280. * o payload protocol-id - A 32 bit unsigned integer that is to be
  4281. * passed to the peer indicating the type of payload protocol data
  4282. * being transmitted. This value is passed as opaque data by SCTP.
  4283. *
  4284. * The return value is the disposition.
  4285. */
  4286. enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
  4287. const struct sctp_endpoint *ep,
  4288. const struct sctp_association *asoc,
  4289. const union sctp_subtype type,
  4290. void *arg,
  4291. struct sctp_cmd_seq *commands)
  4292. {
  4293. struct sctp_datamsg *msg = arg;
  4294. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
  4295. return SCTP_DISPOSITION_CONSUME;
  4296. }
  4297. /*
  4298. * Process the SHUTDOWN primitive.
  4299. *
  4300. * Section: 10.1:
  4301. * C) Shutdown
  4302. *
  4303. * Format: SHUTDOWN(association id)
  4304. * -> result
  4305. *
  4306. * Gracefully closes an association. Any locally queued user data
  4307. * will be delivered to the peer. The association will be terminated only
  4308. * after the peer acknowledges all the SCTP packets sent. A success code
  4309. * will be returned on successful termination of the association. If
  4310. * attempting to terminate the association results in a failure, an error
  4311. * code shall be returned.
  4312. *
  4313. * Mandatory attributes:
  4314. *
  4315. * o association id - local handle to the SCTP association
  4316. *
  4317. * Optional attributes:
  4318. *
  4319. * None.
  4320. *
  4321. * The return value is the disposition.
  4322. */
  4323. enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
  4324. struct net *net,
  4325. const struct sctp_endpoint *ep,
  4326. const struct sctp_association *asoc,
  4327. const union sctp_subtype type,
  4328. void *arg,
  4329. struct sctp_cmd_seq *commands)
  4330. {
  4331. enum sctp_disposition disposition;
  4332. /* From 9.2 Shutdown of an Association
  4333. * Upon receipt of the SHUTDOWN primitive from its upper
  4334. * layer, the endpoint enters SHUTDOWN-PENDING state and
  4335. * remains there until all outstanding data has been
  4336. * acknowledged by its peer. The endpoint accepts no new data
  4337. * from its upper layer, but retransmits data to the far end
  4338. * if necessary to fill gaps.
  4339. */
  4340. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4341. SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
  4342. disposition = SCTP_DISPOSITION_CONSUME;
  4343. if (sctp_outq_is_empty(&asoc->outqueue)) {
  4344. disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
  4345. arg, commands);
  4346. }
  4347. return disposition;
  4348. }
  4349. /*
  4350. * Process the ABORT primitive.
  4351. *
  4352. * Section: 10.1:
  4353. * C) Abort
  4354. *
  4355. * Format: Abort(association id [, cause code])
  4356. * -> result
  4357. *
  4358. * Ungracefully closes an association. Any locally queued user data
  4359. * will be discarded and an ABORT chunk is sent to the peer. A success code
  4360. * will be returned on successful abortion of the association. If
  4361. * attempting to abort the association results in a failure, an error
  4362. * code shall be returned.
  4363. *
  4364. * Mandatory attributes:
  4365. *
  4366. * o association id - local handle to the SCTP association
  4367. *
  4368. * Optional attributes:
  4369. *
  4370. * o cause code - reason of the abort to be passed to the peer
  4371. *
  4372. * None.
  4373. *
  4374. * The return value is the disposition.
  4375. */
  4376. enum sctp_disposition sctp_sf_do_9_1_prm_abort(
  4377. struct net *net,
  4378. const struct sctp_endpoint *ep,
  4379. const struct sctp_association *asoc,
  4380. const union sctp_subtype type,
  4381. void *arg,
  4382. struct sctp_cmd_seq *commands)
  4383. {
  4384. /* From 9.1 Abort of an Association
  4385. * Upon receipt of the ABORT primitive from its upper
  4386. * layer, the endpoint enters CLOSED state and
  4387. * discard all outstanding data has been
  4388. * acknowledged by its peer. The endpoint accepts no new data
  4389. * from its upper layer, but retransmits data to the far end
  4390. * if necessary to fill gaps.
  4391. */
  4392. struct sctp_chunk *abort = arg;
  4393. if (abort)
  4394. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  4395. /* Even if we can't send the ABORT due to low memory delete the
  4396. * TCB. This is a departure from our typical NOMEM handling.
  4397. */
  4398. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  4399. SCTP_ERROR(ECONNABORTED));
  4400. /* Delete the established association. */
  4401. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4402. SCTP_PERR(SCTP_ERROR_USER_ABORT));
  4403. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  4404. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  4405. return SCTP_DISPOSITION_ABORT;
  4406. }
  4407. /* We tried an illegal operation on an association which is closed. */
  4408. enum sctp_disposition sctp_sf_error_closed(struct net *net,
  4409. const struct sctp_endpoint *ep,
  4410. const struct sctp_association *asoc,
  4411. const union sctp_subtype type,
  4412. void *arg,
  4413. struct sctp_cmd_seq *commands)
  4414. {
  4415. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
  4416. return SCTP_DISPOSITION_CONSUME;
  4417. }
  4418. /* We tried an illegal operation on an association which is shutting
  4419. * down.
  4420. */
  4421. enum sctp_disposition sctp_sf_error_shutdown(
  4422. struct net *net,
  4423. const struct sctp_endpoint *ep,
  4424. const struct sctp_association *asoc,
  4425. const union sctp_subtype type,
  4426. void *arg,
  4427. struct sctp_cmd_seq *commands)
  4428. {
  4429. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
  4430. SCTP_ERROR(-ESHUTDOWN));
  4431. return SCTP_DISPOSITION_CONSUME;
  4432. }
  4433. /*
  4434. * sctp_cookie_wait_prm_shutdown
  4435. *
  4436. * Section: 4 Note: 2
  4437. * Verification Tag:
  4438. * Inputs
  4439. * (endpoint, asoc)
  4440. *
  4441. * The RFC does not explicitly address this issue, but is the route through the
  4442. * state table when someone issues a shutdown while in COOKIE_WAIT state.
  4443. *
  4444. * Outputs
  4445. * (timers)
  4446. */
  4447. enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
  4448. struct net *net,
  4449. const struct sctp_endpoint *ep,
  4450. const struct sctp_association *asoc,
  4451. const union sctp_subtype type,
  4452. void *arg,
  4453. struct sctp_cmd_seq *commands)
  4454. {
  4455. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4456. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  4457. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4458. SCTP_STATE(SCTP_STATE_CLOSED));
  4459. SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
  4460. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  4461. return SCTP_DISPOSITION_DELETE_TCB;
  4462. }
  4463. /*
  4464. * sctp_cookie_echoed_prm_shutdown
  4465. *
  4466. * Section: 4 Note: 2
  4467. * Verification Tag:
  4468. * Inputs
  4469. * (endpoint, asoc)
  4470. *
  4471. * The RFC does not explcitly address this issue, but is the route through the
  4472. * state table when someone issues a shutdown while in COOKIE_ECHOED state.
  4473. *
  4474. * Outputs
  4475. * (timers)
  4476. */
  4477. enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
  4478. struct net *net,
  4479. const struct sctp_endpoint *ep,
  4480. const struct sctp_association *asoc,
  4481. const union sctp_subtype type,
  4482. void *arg,
  4483. struct sctp_cmd_seq *commands)
  4484. {
  4485. /* There is a single T1 timer, so we should be able to use
  4486. * common function with the COOKIE-WAIT state.
  4487. */
  4488. return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
  4489. }
  4490. /*
  4491. * sctp_sf_cookie_wait_prm_abort
  4492. *
  4493. * Section: 4 Note: 2
  4494. * Verification Tag:
  4495. * Inputs
  4496. * (endpoint, asoc)
  4497. *
  4498. * The RFC does not explicitly address this issue, but is the route through the
  4499. * state table when someone issues an abort while in COOKIE_WAIT state.
  4500. *
  4501. * Outputs
  4502. * (timers)
  4503. */
  4504. enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
  4505. struct net *net,
  4506. const struct sctp_endpoint *ep,
  4507. const struct sctp_association *asoc,
  4508. const union sctp_subtype type,
  4509. void *arg,
  4510. struct sctp_cmd_seq *commands)
  4511. {
  4512. struct sctp_chunk *abort = arg;
  4513. /* Stop T1-init timer */
  4514. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4515. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  4516. if (abort)
  4517. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  4518. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4519. SCTP_STATE(SCTP_STATE_CLOSED));
  4520. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  4521. /* Even if we can't send the ABORT due to low memory delete the
  4522. * TCB. This is a departure from our typical NOMEM handling.
  4523. */
  4524. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  4525. SCTP_ERROR(ECONNREFUSED));
  4526. /* Delete the established association. */
  4527. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  4528. SCTP_PERR(SCTP_ERROR_USER_ABORT));
  4529. return SCTP_DISPOSITION_ABORT;
  4530. }
  4531. /*
  4532. * sctp_sf_cookie_echoed_prm_abort
  4533. *
  4534. * Section: 4 Note: 3
  4535. * Verification Tag:
  4536. * Inputs
  4537. * (endpoint, asoc)
  4538. *
  4539. * The RFC does not explcitly address this issue, but is the route through the
  4540. * state table when someone issues an abort while in COOKIE_ECHOED state.
  4541. *
  4542. * Outputs
  4543. * (timers)
  4544. */
  4545. enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
  4546. struct net *net,
  4547. const struct sctp_endpoint *ep,
  4548. const struct sctp_association *asoc,
  4549. const union sctp_subtype type,
  4550. void *arg,
  4551. struct sctp_cmd_seq *commands)
  4552. {
  4553. /* There is a single T1 timer, so we should be able to use
  4554. * common function with the COOKIE-WAIT state.
  4555. */
  4556. return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
  4557. }
  4558. /*
  4559. * sctp_sf_shutdown_pending_prm_abort
  4560. *
  4561. * Inputs
  4562. * (endpoint, asoc)
  4563. *
  4564. * The RFC does not explicitly address this issue, but is the route through the
  4565. * state table when someone issues an abort while in SHUTDOWN-PENDING state.
  4566. *
  4567. * Outputs
  4568. * (timers)
  4569. */
  4570. enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
  4571. struct net *net,
  4572. const struct sctp_endpoint *ep,
  4573. const struct sctp_association *asoc,
  4574. const union sctp_subtype type,
  4575. void *arg,
  4576. struct sctp_cmd_seq *commands)
  4577. {
  4578. /* Stop the T5-shutdown guard timer. */
  4579. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4580. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  4581. return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
  4582. }
  4583. /*
  4584. * sctp_sf_shutdown_sent_prm_abort
  4585. *
  4586. * Inputs
  4587. * (endpoint, asoc)
  4588. *
  4589. * The RFC does not explicitly address this issue, but is the route through the
  4590. * state table when someone issues an abort while in SHUTDOWN-SENT state.
  4591. *
  4592. * Outputs
  4593. * (timers)
  4594. */
  4595. enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
  4596. struct net *net,
  4597. const struct sctp_endpoint *ep,
  4598. const struct sctp_association *asoc,
  4599. const union sctp_subtype type,
  4600. void *arg,
  4601. struct sctp_cmd_seq *commands)
  4602. {
  4603. /* Stop the T2-shutdown timer. */
  4604. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4605. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  4606. /* Stop the T5-shutdown guard timer. */
  4607. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4608. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  4609. return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
  4610. }
  4611. /*
  4612. * sctp_sf_cookie_echoed_prm_abort
  4613. *
  4614. * Inputs
  4615. * (endpoint, asoc)
  4616. *
  4617. * The RFC does not explcitly address this issue, but is the route through the
  4618. * state table when someone issues an abort while in COOKIE_ECHOED state.
  4619. *
  4620. * Outputs
  4621. * (timers)
  4622. */
  4623. enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
  4624. struct net *net,
  4625. const struct sctp_endpoint *ep,
  4626. const struct sctp_association *asoc,
  4627. const union sctp_subtype type,
  4628. void *arg,
  4629. struct sctp_cmd_seq *commands)
  4630. {
  4631. /* The same T2 timer, so we should be able to use
  4632. * common function with the SHUTDOWN-SENT state.
  4633. */
  4634. return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
  4635. }
  4636. /*
  4637. * Process the REQUESTHEARTBEAT primitive
  4638. *
  4639. * 10.1 ULP-to-SCTP
  4640. * J) Request Heartbeat
  4641. *
  4642. * Format: REQUESTHEARTBEAT(association id, destination transport address)
  4643. *
  4644. * -> result
  4645. *
  4646. * Instructs the local endpoint to perform a HeartBeat on the specified
  4647. * destination transport address of the given association. The returned
  4648. * result should indicate whether the transmission of the HEARTBEAT
  4649. * chunk to the destination address is successful.
  4650. *
  4651. * Mandatory attributes:
  4652. *
  4653. * o association id - local handle to the SCTP association
  4654. *
  4655. * o destination transport address - the transport address of the
  4656. * association on which a heartbeat should be issued.
  4657. */
  4658. enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
  4659. struct net *net,
  4660. const struct sctp_endpoint *ep,
  4661. const struct sctp_association *asoc,
  4662. const union sctp_subtype type,
  4663. void *arg,
  4664. struct sctp_cmd_seq *commands)
  4665. {
  4666. if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
  4667. (struct sctp_transport *)arg, commands))
  4668. return SCTP_DISPOSITION_NOMEM;
  4669. /*
  4670. * RFC 2960 (bis), section 8.3
  4671. *
  4672. * D) Request an on-demand HEARTBEAT on a specific destination
  4673. * transport address of a given association.
  4674. *
  4675. * The endpoint should increment the respective error counter of
  4676. * the destination transport address each time a HEARTBEAT is sent
  4677. * to that address and not acknowledged within one RTO.
  4678. *
  4679. */
  4680. sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
  4681. SCTP_TRANSPORT(arg));
  4682. return SCTP_DISPOSITION_CONSUME;
  4683. }
  4684. /*
  4685. * ADDIP Section 4.1 ASCONF Chunk Procedures
  4686. * When an endpoint has an ASCONF signaled change to be sent to the
  4687. * remote endpoint it should do A1 to A9
  4688. */
  4689. enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
  4690. const struct sctp_endpoint *ep,
  4691. const struct sctp_association *asoc,
  4692. const union sctp_subtype type,
  4693. void *arg,
  4694. struct sctp_cmd_seq *commands)
  4695. {
  4696. struct sctp_chunk *chunk = arg;
  4697. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
  4698. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  4699. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  4700. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
  4701. return SCTP_DISPOSITION_CONSUME;
  4702. }
  4703. /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
  4704. enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
  4705. const struct sctp_endpoint *ep,
  4706. const struct sctp_association *asoc,
  4707. const union sctp_subtype type,
  4708. void *arg,
  4709. struct sctp_cmd_seq *commands)
  4710. {
  4711. struct sctp_chunk *chunk = arg;
  4712. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
  4713. return SCTP_DISPOSITION_CONSUME;
  4714. }
  4715. /*
  4716. * Ignore the primitive event
  4717. *
  4718. * The return value is the disposition of the primitive.
  4719. */
  4720. enum sctp_disposition sctp_sf_ignore_primitive(
  4721. struct net *net,
  4722. const struct sctp_endpoint *ep,
  4723. const struct sctp_association *asoc,
  4724. const union sctp_subtype type,
  4725. void *arg,
  4726. struct sctp_cmd_seq *commands)
  4727. {
  4728. pr_debug("%s: primitive type:%d is ignored\n", __func__,
  4729. type.primitive);
  4730. return SCTP_DISPOSITION_DISCARD;
  4731. }
  4732. /***************************************************************************
  4733. * These are the state functions for the OTHER events.
  4734. ***************************************************************************/
  4735. /*
  4736. * When the SCTP stack has no more user data to send or retransmit, this
  4737. * notification is given to the user. Also, at the time when a user app
  4738. * subscribes to this event, if there is no data to be sent or
  4739. * retransmit, the stack will immediately send up this notification.
  4740. */
  4741. enum sctp_disposition sctp_sf_do_no_pending_tsn(
  4742. struct net *net,
  4743. const struct sctp_endpoint *ep,
  4744. const struct sctp_association *asoc,
  4745. const union sctp_subtype type,
  4746. void *arg,
  4747. struct sctp_cmd_seq *commands)
  4748. {
  4749. struct sctp_ulpevent *event;
  4750. event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
  4751. if (!event)
  4752. return SCTP_DISPOSITION_NOMEM;
  4753. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
  4754. return SCTP_DISPOSITION_CONSUME;
  4755. }
  4756. /*
  4757. * Start the shutdown negotiation.
  4758. *
  4759. * From Section 9.2:
  4760. * Once all its outstanding data has been acknowledged, the endpoint
  4761. * shall send a SHUTDOWN chunk to its peer including in the Cumulative
  4762. * TSN Ack field the last sequential TSN it has received from the peer.
  4763. * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
  4764. * state. If the timer expires, the endpoint must re-send the SHUTDOWN
  4765. * with the updated last sequential TSN received from its peer.
  4766. *
  4767. * The return value is the disposition.
  4768. */
  4769. enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
  4770. struct net *net,
  4771. const struct sctp_endpoint *ep,
  4772. const struct sctp_association *asoc,
  4773. const union sctp_subtype type,
  4774. void *arg,
  4775. struct sctp_cmd_seq *commands)
  4776. {
  4777. struct sctp_chunk *reply;
  4778. /* Once all its outstanding data has been acknowledged, the
  4779. * endpoint shall send a SHUTDOWN chunk to its peer including
  4780. * in the Cumulative TSN Ack field the last sequential TSN it
  4781. * has received from the peer.
  4782. */
  4783. reply = sctp_make_shutdown(asoc, NULL);
  4784. if (!reply)
  4785. goto nomem;
  4786. /* Set the transport for the SHUTDOWN chunk and the timeout for the
  4787. * T2-shutdown timer.
  4788. */
  4789. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  4790. /* It shall then start the T2-shutdown timer */
  4791. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  4792. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  4793. /* RFC 4960 Section 9.2
  4794. * The sender of the SHUTDOWN MAY also start an overall guard timer
  4795. * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
  4796. */
  4797. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  4798. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  4799. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
  4800. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4801. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  4802. /* and enter the SHUTDOWN-SENT state. */
  4803. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4804. SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
  4805. /* sctp-implguide 2.10 Issues with Heartbeating and failover
  4806. *
  4807. * HEARTBEAT ... is discontinued after sending either SHUTDOWN
  4808. * or SHUTDOWN-ACK.
  4809. */
  4810. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
  4811. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  4812. return SCTP_DISPOSITION_CONSUME;
  4813. nomem:
  4814. return SCTP_DISPOSITION_NOMEM;
  4815. }
  4816. /*
  4817. * Generate a SHUTDOWN ACK now that everything is SACK'd.
  4818. *
  4819. * From Section 9.2:
  4820. *
  4821. * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
  4822. * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
  4823. * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
  4824. * endpoint must re-send the SHUTDOWN ACK.
  4825. *
  4826. * The return value is the disposition.
  4827. */
  4828. enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
  4829. struct net *net,
  4830. const struct sctp_endpoint *ep,
  4831. const struct sctp_association *asoc,
  4832. const union sctp_subtype type,
  4833. void *arg,
  4834. struct sctp_cmd_seq *commands)
  4835. {
  4836. struct sctp_chunk *chunk = arg;
  4837. struct sctp_chunk *reply;
  4838. /* There are 2 ways of getting here:
  4839. * 1) called in response to a SHUTDOWN chunk
  4840. * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
  4841. *
  4842. * For the case (2), the arg parameter is set to NULL. We need
  4843. * to check that we have a chunk before accessing it's fields.
  4844. */
  4845. if (chunk) {
  4846. if (!sctp_vtag_verify(chunk, asoc))
  4847. return sctp_sf_pdiscard(net, ep, asoc, type, arg,
  4848. commands);
  4849. /* Make sure that the SHUTDOWN chunk has a valid length. */
  4850. if (!sctp_chunk_length_valid(
  4851. chunk, sizeof(struct sctp_shutdown_chunk)))
  4852. return sctp_sf_violation_chunklen(net, ep, asoc, type,
  4853. arg, commands);
  4854. }
  4855. /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
  4856. * shall send a SHUTDOWN ACK ...
  4857. */
  4858. reply = sctp_make_shutdown_ack(asoc, chunk);
  4859. if (!reply)
  4860. goto nomem;
  4861. /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
  4862. * the T2-shutdown timer.
  4863. */
  4864. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  4865. /* and start/restart a T2-shutdown timer of its own, */
  4866. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  4867. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  4868. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
  4869. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4870. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  4871. /* Enter the SHUTDOWN-ACK-SENT state. */
  4872. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4873. SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
  4874. /* sctp-implguide 2.10 Issues with Heartbeating and failover
  4875. *
  4876. * HEARTBEAT ... is discontinued after sending either SHUTDOWN
  4877. * or SHUTDOWN-ACK.
  4878. */
  4879. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
  4880. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  4881. return SCTP_DISPOSITION_CONSUME;
  4882. nomem:
  4883. return SCTP_DISPOSITION_NOMEM;
  4884. }
  4885. /*
  4886. * Ignore the event defined as other
  4887. *
  4888. * The return value is the disposition of the event.
  4889. */
  4890. enum sctp_disposition sctp_sf_ignore_other(struct net *net,
  4891. const struct sctp_endpoint *ep,
  4892. const struct sctp_association *asoc,
  4893. const union sctp_subtype type,
  4894. void *arg,
  4895. struct sctp_cmd_seq *commands)
  4896. {
  4897. pr_debug("%s: the event other type:%d is ignored\n",
  4898. __func__, type.other);
  4899. return SCTP_DISPOSITION_DISCARD;
  4900. }
  4901. /************************************************************
  4902. * These are the state functions for handling timeout events.
  4903. ************************************************************/
  4904. /*
  4905. * RTX Timeout
  4906. *
  4907. * Section: 6.3.3 Handle T3-rtx Expiration
  4908. *
  4909. * Whenever the retransmission timer T3-rtx expires for a destination
  4910. * address, do the following:
  4911. * [See below]
  4912. *
  4913. * The return value is the disposition of the chunk.
  4914. */
  4915. enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
  4916. const struct sctp_endpoint *ep,
  4917. const struct sctp_association *asoc,
  4918. const union sctp_subtype type,
  4919. void *arg,
  4920. struct sctp_cmd_seq *commands)
  4921. {
  4922. struct sctp_transport *transport = arg;
  4923. SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
  4924. if (asoc->overall_error_count >= asoc->max_retrans) {
  4925. if (asoc->peer.zero_window_announced &&
  4926. asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
  4927. /*
  4928. * We are here likely because the receiver had its rwnd
  4929. * closed for a while and we have not been able to
  4930. * transmit the locally queued data within the maximum
  4931. * retransmission attempts limit. Start the T5
  4932. * shutdown guard timer to give the receiver one last
  4933. * chance and some additional time to recover before
  4934. * aborting.
  4935. */
  4936. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
  4937. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  4938. } else {
  4939. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  4940. SCTP_ERROR(ETIMEDOUT));
  4941. /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  4942. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4943. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  4944. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  4945. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  4946. return SCTP_DISPOSITION_DELETE_TCB;
  4947. }
  4948. }
  4949. /* E1) For the destination address for which the timer
  4950. * expires, adjust its ssthresh with rules defined in Section
  4951. * 7.2.3 and set the cwnd <- MTU.
  4952. */
  4953. /* E2) For the destination address for which the timer
  4954. * expires, set RTO <- RTO * 2 ("back off the timer"). The
  4955. * maximum value discussed in rule C7 above (RTO.max) may be
  4956. * used to provide an upper bound to this doubling operation.
  4957. */
  4958. /* E3) Determine how many of the earliest (i.e., lowest TSN)
  4959. * outstanding DATA chunks for the address for which the
  4960. * T3-rtx has expired will fit into a single packet, subject
  4961. * to the MTU constraint for the path corresponding to the
  4962. * destination transport address to which the retransmission
  4963. * is being sent (this may be different from the address for
  4964. * which the timer expires [see Section 6.4]). Call this
  4965. * value K. Bundle and retransmit those K DATA chunks in a
  4966. * single packet to the destination endpoint.
  4967. *
  4968. * Note: Any DATA chunks that were sent to the address for
  4969. * which the T3-rtx timer expired but did not fit in one MTU
  4970. * (rule E3 above), should be marked for retransmission and
  4971. * sent as soon as cwnd allows (normally when a SACK arrives).
  4972. */
  4973. /* Do some failure management (Section 8.2). */
  4974. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
  4975. /* NB: Rules E4 and F1 are implicit in R1. */
  4976. sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
  4977. return SCTP_DISPOSITION_CONSUME;
  4978. }
  4979. /*
  4980. * Generate delayed SACK on timeout
  4981. *
  4982. * Section: 6.2 Acknowledgement on Reception of DATA Chunks
  4983. *
  4984. * The guidelines on delayed acknowledgement algorithm specified in
  4985. * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
  4986. * acknowledgement SHOULD be generated for at least every second packet
  4987. * (not every second DATA chunk) received, and SHOULD be generated
  4988. * within 200 ms of the arrival of any unacknowledged DATA chunk. In
  4989. * some situations it may be beneficial for an SCTP transmitter to be
  4990. * more conservative than the algorithms detailed in this document
  4991. * allow. However, an SCTP transmitter MUST NOT be more aggressive than
  4992. * the following algorithms allow.
  4993. */
  4994. enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
  4995. const struct sctp_endpoint *ep,
  4996. const struct sctp_association *asoc,
  4997. const union sctp_subtype type,
  4998. void *arg,
  4999. struct sctp_cmd_seq *commands)
  5000. {
  5001. SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
  5002. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  5003. return SCTP_DISPOSITION_CONSUME;
  5004. }
  5005. /*
  5006. * sctp_sf_t1_init_timer_expire
  5007. *
  5008. * Section: 4 Note: 2
  5009. * Verification Tag:
  5010. * Inputs
  5011. * (endpoint, asoc)
  5012. *
  5013. * RFC 2960 Section 4 Notes
  5014. * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
  5015. * and re-start the T1-init timer without changing state. This MUST
  5016. * be repeated up to 'Max.Init.Retransmits' times. After that, the
  5017. * endpoint MUST abort the initialization process and report the
  5018. * error to SCTP user.
  5019. *
  5020. * Outputs
  5021. * (timers, events)
  5022. *
  5023. */
  5024. enum sctp_disposition sctp_sf_t1_init_timer_expire(
  5025. struct net *net,
  5026. const struct sctp_endpoint *ep,
  5027. const struct sctp_association *asoc,
  5028. const union sctp_subtype type,
  5029. void *arg,
  5030. struct sctp_cmd_seq *commands)
  5031. {
  5032. int attempts = asoc->init_err_counter + 1;
  5033. struct sctp_chunk *repl = NULL;
  5034. struct sctp_bind_addr *bp;
  5035. pr_debug("%s: timer T1 expired (INIT)\n", __func__);
  5036. SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
  5037. if (attempts <= asoc->max_init_attempts) {
  5038. bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
  5039. repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
  5040. if (!repl)
  5041. return SCTP_DISPOSITION_NOMEM;
  5042. /* Choose transport for INIT. */
  5043. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
  5044. SCTP_CHUNK(repl));
  5045. /* Issue a sideeffect to do the needed accounting. */
  5046. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
  5047. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  5048. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  5049. } else {
  5050. pr_debug("%s: giving up on INIT, attempts:%d "
  5051. "max_init_attempts:%d\n", __func__, attempts,
  5052. asoc->max_init_attempts);
  5053. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5054. SCTP_ERROR(ETIMEDOUT));
  5055. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  5056. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5057. return SCTP_DISPOSITION_DELETE_TCB;
  5058. }
  5059. return SCTP_DISPOSITION_CONSUME;
  5060. }
  5061. /*
  5062. * sctp_sf_t1_cookie_timer_expire
  5063. *
  5064. * Section: 4 Note: 2
  5065. * Verification Tag:
  5066. * Inputs
  5067. * (endpoint, asoc)
  5068. *
  5069. * RFC 2960 Section 4 Notes
  5070. * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
  5071. * COOKIE ECHO and re-start the T1-cookie timer without changing
  5072. * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
  5073. * After that, the endpoint MUST abort the initialization process and
  5074. * report the error to SCTP user.
  5075. *
  5076. * Outputs
  5077. * (timers, events)
  5078. *
  5079. */
  5080. enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
  5081. struct net *net,
  5082. const struct sctp_endpoint *ep,
  5083. const struct sctp_association *asoc,
  5084. const union sctp_subtype type,
  5085. void *arg,
  5086. struct sctp_cmd_seq *commands)
  5087. {
  5088. int attempts = asoc->init_err_counter + 1;
  5089. struct sctp_chunk *repl = NULL;
  5090. pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
  5091. SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
  5092. if (attempts <= asoc->max_init_attempts) {
  5093. repl = sctp_make_cookie_echo(asoc, NULL);
  5094. if (!repl)
  5095. return SCTP_DISPOSITION_NOMEM;
  5096. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
  5097. SCTP_CHUNK(repl));
  5098. /* Issue a sideeffect to do the needed accounting. */
  5099. sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
  5100. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  5101. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  5102. } else {
  5103. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5104. SCTP_ERROR(ETIMEDOUT));
  5105. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  5106. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5107. return SCTP_DISPOSITION_DELETE_TCB;
  5108. }
  5109. return SCTP_DISPOSITION_CONSUME;
  5110. }
  5111. /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
  5112. * with the updated last sequential TSN received from its peer.
  5113. *
  5114. * An endpoint should limit the number of retransmissions of the
  5115. * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
  5116. * If this threshold is exceeded the endpoint should destroy the TCB and
  5117. * MUST report the peer endpoint unreachable to the upper layer (and
  5118. * thus the association enters the CLOSED state). The reception of any
  5119. * packet from its peer (i.e. as the peer sends all of its queued DATA
  5120. * chunks) should clear the endpoint's retransmission count and restart
  5121. * the T2-Shutdown timer, giving its peer ample opportunity to transmit
  5122. * all of its queued DATA chunks that have not yet been sent.
  5123. */
  5124. enum sctp_disposition sctp_sf_t2_timer_expire(
  5125. struct net *net,
  5126. const struct sctp_endpoint *ep,
  5127. const struct sctp_association *asoc,
  5128. const union sctp_subtype type,
  5129. void *arg,
  5130. struct sctp_cmd_seq *commands)
  5131. {
  5132. struct sctp_chunk *reply = NULL;
  5133. pr_debug("%s: timer T2 expired\n", __func__);
  5134. SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
  5135. ((struct sctp_association *)asoc)->shutdown_retries++;
  5136. if (asoc->overall_error_count >= asoc->max_retrans) {
  5137. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5138. SCTP_ERROR(ETIMEDOUT));
  5139. /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  5140. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  5141. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5142. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  5143. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  5144. return SCTP_DISPOSITION_DELETE_TCB;
  5145. }
  5146. switch (asoc->state) {
  5147. case SCTP_STATE_SHUTDOWN_SENT:
  5148. reply = sctp_make_shutdown(asoc, NULL);
  5149. break;
  5150. case SCTP_STATE_SHUTDOWN_ACK_SENT:
  5151. reply = sctp_make_shutdown_ack(asoc, NULL);
  5152. break;
  5153. default:
  5154. BUG();
  5155. break;
  5156. }
  5157. if (!reply)
  5158. goto nomem;
  5159. /* Do some failure management (Section 8.2).
  5160. * If we remove the transport an SHUTDOWN was last sent to, don't
  5161. * do failure management.
  5162. */
  5163. if (asoc->shutdown_last_sent_to)
  5164. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
  5165. SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
  5166. /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
  5167. * the T2-shutdown timer.
  5168. */
  5169. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  5170. /* Restart the T2-shutdown timer. */
  5171. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  5172. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  5173. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  5174. return SCTP_DISPOSITION_CONSUME;
  5175. nomem:
  5176. return SCTP_DISPOSITION_NOMEM;
  5177. }
  5178. /*
  5179. * ADDIP Section 4.1 ASCONF CHunk Procedures
  5180. * If the T4 RTO timer expires the endpoint should do B1 to B5
  5181. */
  5182. enum sctp_disposition sctp_sf_t4_timer_expire(
  5183. struct net *net,
  5184. const struct sctp_endpoint *ep,
  5185. const struct sctp_association *asoc,
  5186. const union sctp_subtype type,
  5187. void *arg,
  5188. struct sctp_cmd_seq *commands)
  5189. {
  5190. struct sctp_chunk *chunk = asoc->addip_last_asconf;
  5191. struct sctp_transport *transport = chunk->transport;
  5192. SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
  5193. /* ADDIP 4.1 B1) Increment the error counters and perform path failure
  5194. * detection on the appropriate destination address as defined in
  5195. * RFC2960 [5] section 8.1 and 8.2.
  5196. */
  5197. if (transport)
  5198. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
  5199. SCTP_TRANSPORT(transport));
  5200. /* Reconfig T4 timer and transport. */
  5201. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
  5202. /* ADDIP 4.1 B2) Increment the association error counters and perform
  5203. * endpoint failure detection on the association as defined in
  5204. * RFC2960 [5] section 8.1 and 8.2.
  5205. * association error counter is incremented in SCTP_CMD_STRIKE.
  5206. */
  5207. if (asoc->overall_error_count >= asoc->max_retrans) {
  5208. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  5209. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  5210. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5211. SCTP_ERROR(ETIMEDOUT));
  5212. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  5213. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5214. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  5215. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  5216. return SCTP_DISPOSITION_ABORT;
  5217. }
  5218. /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
  5219. * the ASCONF chunk was sent by doubling the RTO timer value.
  5220. * This is done in SCTP_CMD_STRIKE.
  5221. */
  5222. /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
  5223. * choose an alternate destination address (please refer to RFC2960
  5224. * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
  5225. * chunk, it MUST be the same (including its serial number) as the last
  5226. * ASCONF sent.
  5227. */
  5228. sctp_chunk_hold(asoc->addip_last_asconf);
  5229. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  5230. SCTP_CHUNK(asoc->addip_last_asconf));
  5231. /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
  5232. * destination is selected, then the RTO used will be that of the new
  5233. * destination address.
  5234. */
  5235. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  5236. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  5237. return SCTP_DISPOSITION_CONSUME;
  5238. }
  5239. /* sctpimpguide-05 Section 2.12.2
  5240. * The sender of the SHUTDOWN MAY also start an overall guard timer
  5241. * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
  5242. * At the expiration of this timer the sender SHOULD abort the association
  5243. * by sending an ABORT chunk.
  5244. */
  5245. enum sctp_disposition sctp_sf_t5_timer_expire(
  5246. struct net *net,
  5247. const struct sctp_endpoint *ep,
  5248. const struct sctp_association *asoc,
  5249. const union sctp_subtype type,
  5250. void *arg,
  5251. struct sctp_cmd_seq *commands)
  5252. {
  5253. struct sctp_chunk *reply = NULL;
  5254. pr_debug("%s: timer T5 expired\n", __func__);
  5255. SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
  5256. reply = sctp_make_abort(asoc, NULL, 0);
  5257. if (!reply)
  5258. goto nomem;
  5259. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  5260. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5261. SCTP_ERROR(ETIMEDOUT));
  5262. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  5263. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5264. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  5265. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  5266. return SCTP_DISPOSITION_DELETE_TCB;
  5267. nomem:
  5268. return SCTP_DISPOSITION_NOMEM;
  5269. }
  5270. /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
  5271. * the association is automatically closed by starting the shutdown process.
  5272. * The work that needs to be done is same as when SHUTDOWN is initiated by
  5273. * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
  5274. */
  5275. enum sctp_disposition sctp_sf_autoclose_timer_expire(
  5276. struct net *net,
  5277. const struct sctp_endpoint *ep,
  5278. const struct sctp_association *asoc,
  5279. const union sctp_subtype type,
  5280. void *arg,
  5281. struct sctp_cmd_seq *commands)
  5282. {
  5283. enum sctp_disposition disposition;
  5284. SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
  5285. /* From 9.2 Shutdown of an Association
  5286. * Upon receipt of the SHUTDOWN primitive from its upper
  5287. * layer, the endpoint enters SHUTDOWN-PENDING state and
  5288. * remains there until all outstanding data has been
  5289. * acknowledged by its peer. The endpoint accepts no new data
  5290. * from its upper layer, but retransmits data to the far end
  5291. * if necessary to fill gaps.
  5292. */
  5293. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  5294. SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
  5295. disposition = SCTP_DISPOSITION_CONSUME;
  5296. if (sctp_outq_is_empty(&asoc->outqueue)) {
  5297. disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
  5298. arg, commands);
  5299. }
  5300. return disposition;
  5301. }
  5302. /*****************************************************************************
  5303. * These are sa state functions which could apply to all types of events.
  5304. ****************************************************************************/
  5305. /*
  5306. * This table entry is not implemented.
  5307. *
  5308. * Inputs
  5309. * (endpoint, asoc, chunk)
  5310. *
  5311. * The return value is the disposition of the chunk.
  5312. */
  5313. enum sctp_disposition sctp_sf_not_impl(struct net *net,
  5314. const struct sctp_endpoint *ep,
  5315. const struct sctp_association *asoc,
  5316. const union sctp_subtype type,
  5317. void *arg, struct sctp_cmd_seq *commands)
  5318. {
  5319. return SCTP_DISPOSITION_NOT_IMPL;
  5320. }
  5321. /*
  5322. * This table entry represents a bug.
  5323. *
  5324. * Inputs
  5325. * (endpoint, asoc, chunk)
  5326. *
  5327. * The return value is the disposition of the chunk.
  5328. */
  5329. enum sctp_disposition sctp_sf_bug(struct net *net,
  5330. const struct sctp_endpoint *ep,
  5331. const struct sctp_association *asoc,
  5332. const union sctp_subtype type,
  5333. void *arg, struct sctp_cmd_seq *commands)
  5334. {
  5335. return SCTP_DISPOSITION_BUG;
  5336. }
  5337. /*
  5338. * This table entry represents the firing of a timer in the wrong state.
  5339. * Since timer deletion cannot be guaranteed a timer 'may' end up firing
  5340. * when the association is in the wrong state. This event should
  5341. * be ignored, so as to prevent any rearming of the timer.
  5342. *
  5343. * Inputs
  5344. * (endpoint, asoc, chunk)
  5345. *
  5346. * The return value is the disposition of the chunk.
  5347. */
  5348. enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
  5349. const struct sctp_endpoint *ep,
  5350. const struct sctp_association *asoc,
  5351. const union sctp_subtype type,
  5352. void *arg,
  5353. struct sctp_cmd_seq *commands)
  5354. {
  5355. pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
  5356. return SCTP_DISPOSITION_CONSUME;
  5357. }
  5358. /********************************************************************
  5359. * 2nd Level Abstractions
  5360. ********************************************************************/
  5361. /* Pull the SACK chunk based on the SACK header. */
  5362. static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
  5363. {
  5364. struct sctp_sackhdr *sack;
  5365. __u16 num_dup_tsns;
  5366. unsigned int len;
  5367. __u16 num_blocks;
  5368. /* Protect ourselves from reading too far into
  5369. * the skb from a bogus sender.
  5370. */
  5371. sack = (struct sctp_sackhdr *) chunk->skb->data;
  5372. num_blocks = ntohs(sack->num_gap_ack_blocks);
  5373. num_dup_tsns = ntohs(sack->num_dup_tsns);
  5374. len = sizeof(struct sctp_sackhdr);
  5375. len += (num_blocks + num_dup_tsns) * sizeof(__u32);
  5376. if (len > chunk->skb->len)
  5377. return NULL;
  5378. skb_pull(chunk->skb, len);
  5379. return sack;
  5380. }
  5381. /* Create an ABORT packet to be sent as a response, with the specified
  5382. * error causes.
  5383. */
  5384. static struct sctp_packet *sctp_abort_pkt_new(
  5385. struct net *net,
  5386. const struct sctp_endpoint *ep,
  5387. const struct sctp_association *asoc,
  5388. struct sctp_chunk *chunk,
  5389. const void *payload, size_t paylen)
  5390. {
  5391. struct sctp_packet *packet;
  5392. struct sctp_chunk *abort;
  5393. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  5394. if (packet) {
  5395. /* Make an ABORT.
  5396. * The T bit will be set if the asoc is NULL.
  5397. */
  5398. abort = sctp_make_abort(asoc, chunk, paylen);
  5399. if (!abort) {
  5400. sctp_ootb_pkt_free(packet);
  5401. return NULL;
  5402. }
  5403. /* Reflect vtag if T-Bit is set */
  5404. if (sctp_test_T_bit(abort))
  5405. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  5406. /* Add specified error causes, i.e., payload, to the
  5407. * end of the chunk.
  5408. */
  5409. sctp_addto_chunk(abort, paylen, payload);
  5410. /* Set the skb to the belonging sock for accounting. */
  5411. abort->skb->sk = ep->base.sk;
  5412. sctp_packet_append_chunk(packet, abort);
  5413. }
  5414. return packet;
  5415. }
  5416. /* Allocate a packet for responding in the OOTB conditions. */
  5417. static struct sctp_packet *sctp_ootb_pkt_new(
  5418. struct net *net,
  5419. const struct sctp_association *asoc,
  5420. const struct sctp_chunk *chunk)
  5421. {
  5422. struct sctp_transport *transport;
  5423. struct sctp_packet *packet;
  5424. __u16 sport, dport;
  5425. __u32 vtag;
  5426. /* Get the source and destination port from the inbound packet. */
  5427. sport = ntohs(chunk->sctp_hdr->dest);
  5428. dport = ntohs(chunk->sctp_hdr->source);
  5429. /* The V-tag is going to be the same as the inbound packet if no
  5430. * association exists, otherwise, use the peer's vtag.
  5431. */
  5432. if (asoc) {
  5433. /* Special case the INIT-ACK as there is no peer's vtag
  5434. * yet.
  5435. */
  5436. switch (chunk->chunk_hdr->type) {
  5437. case SCTP_CID_INIT_ACK:
  5438. {
  5439. struct sctp_initack_chunk *initack;
  5440. initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
  5441. vtag = ntohl(initack->init_hdr.init_tag);
  5442. break;
  5443. }
  5444. default:
  5445. vtag = asoc->peer.i.init_tag;
  5446. break;
  5447. }
  5448. } else {
  5449. /* Special case the INIT and stale COOKIE_ECHO as there is no
  5450. * vtag yet.
  5451. */
  5452. switch (chunk->chunk_hdr->type) {
  5453. case SCTP_CID_INIT:
  5454. {
  5455. struct sctp_init_chunk *init;
  5456. init = (struct sctp_init_chunk *)chunk->chunk_hdr;
  5457. vtag = ntohl(init->init_hdr.init_tag);
  5458. break;
  5459. }
  5460. default:
  5461. vtag = ntohl(chunk->sctp_hdr->vtag);
  5462. break;
  5463. }
  5464. }
  5465. /* Make a transport for the bucket, Eliza... */
  5466. transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
  5467. if (!transport)
  5468. goto nomem;
  5469. /* Cache a route for the transport with the chunk's destination as
  5470. * the source address.
  5471. */
  5472. sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
  5473. sctp_sk(net->sctp.ctl_sock));
  5474. packet = &transport->packet;
  5475. sctp_packet_init(packet, transport, sport, dport);
  5476. sctp_packet_config(packet, vtag, 0);
  5477. return packet;
  5478. nomem:
  5479. return NULL;
  5480. }
  5481. /* Free the packet allocated earlier for responding in the OOTB condition. */
  5482. void sctp_ootb_pkt_free(struct sctp_packet *packet)
  5483. {
  5484. sctp_transport_free(packet->transport);
  5485. }
  5486. /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
  5487. static void sctp_send_stale_cookie_err(struct net *net,
  5488. const struct sctp_endpoint *ep,
  5489. const struct sctp_association *asoc,
  5490. const struct sctp_chunk *chunk,
  5491. struct sctp_cmd_seq *commands,
  5492. struct sctp_chunk *err_chunk)
  5493. {
  5494. struct sctp_packet *packet;
  5495. if (err_chunk) {
  5496. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  5497. if (packet) {
  5498. struct sctp_signed_cookie *cookie;
  5499. /* Override the OOTB vtag from the cookie. */
  5500. cookie = chunk->subh.cookie_hdr;
  5501. packet->vtag = cookie->c.peer_vtag;
  5502. /* Set the skb to the belonging sock for accounting. */
  5503. err_chunk->skb->sk = ep->base.sk;
  5504. sctp_packet_append_chunk(packet, err_chunk);
  5505. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  5506. SCTP_PACKET(packet));
  5507. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  5508. } else
  5509. sctp_chunk_free (err_chunk);
  5510. }
  5511. }
  5512. /* Process a data chunk */
  5513. static int sctp_eat_data(const struct sctp_association *asoc,
  5514. struct sctp_chunk *chunk,
  5515. struct sctp_cmd_seq *commands)
  5516. {
  5517. struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
  5518. struct sock *sk = asoc->base.sk;
  5519. struct net *net = sock_net(sk);
  5520. struct sctp_datahdr *data_hdr;
  5521. struct sctp_chunk *err;
  5522. enum sctp_verb deliver;
  5523. size_t datalen;
  5524. u8 ordered = 0;
  5525. u16 ssn, sid;
  5526. __u32 tsn;
  5527. int tmp;
  5528. data_hdr = (struct sctp_datahdr *)chunk->skb->data;
  5529. chunk->subh.data_hdr = data_hdr;
  5530. skb_pull(chunk->skb, sizeof(*data_hdr));
  5531. tsn = ntohl(data_hdr->tsn);
  5532. pr_debug("%s: TSN 0x%x\n", __func__, tsn);
  5533. /* ASSERT: Now skb->data is really the user data. */
  5534. /* Process ECN based congestion.
  5535. *
  5536. * Since the chunk structure is reused for all chunks within
  5537. * a packet, we use ecn_ce_done to track if we've already
  5538. * done CE processing for this packet.
  5539. *
  5540. * We need to do ECN processing even if we plan to discard the
  5541. * chunk later.
  5542. */
  5543. if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
  5544. struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
  5545. chunk->ecn_ce_done = 1;
  5546. if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
  5547. /* Do real work as sideffect. */
  5548. sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
  5549. SCTP_U32(tsn));
  5550. }
  5551. }
  5552. tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
  5553. if (tmp < 0) {
  5554. /* The TSN is too high--silently discard the chunk and
  5555. * count on it getting retransmitted later.
  5556. */
  5557. if (chunk->asoc)
  5558. chunk->asoc->stats.outofseqtsns++;
  5559. return SCTP_IERROR_HIGH_TSN;
  5560. } else if (tmp > 0) {
  5561. /* This is a duplicate. Record it. */
  5562. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
  5563. return SCTP_IERROR_DUP_TSN;
  5564. }
  5565. /* This is a new TSN. */
  5566. /* Discard if there is no room in the receive window.
  5567. * Actually, allow a little bit of overflow (up to a MTU).
  5568. */
  5569. datalen = ntohs(chunk->chunk_hdr->length);
  5570. datalen -= sizeof(struct sctp_data_chunk);
  5571. deliver = SCTP_CMD_CHUNK_ULP;
  5572. /* Think about partial delivery. */
  5573. if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
  5574. /* Even if we don't accept this chunk there is
  5575. * memory pressure.
  5576. */
  5577. sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
  5578. }
  5579. /* Spill over rwnd a little bit. Note: While allowed, this spill over
  5580. * seems a bit troublesome in that frag_point varies based on
  5581. * PMTU. In cases, such as loopback, this might be a rather
  5582. * large spill over.
  5583. */
  5584. if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
  5585. (datalen > asoc->rwnd + asoc->frag_point))) {
  5586. /* If this is the next TSN, consider reneging to make
  5587. * room. Note: Playing nice with a confused sender. A
  5588. * malicious sender can still eat up all our buffer
  5589. * space and in the future we may want to detect and
  5590. * do more drastic reneging.
  5591. */
  5592. if (sctp_tsnmap_has_gap(map) &&
  5593. (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
  5594. pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
  5595. deliver = SCTP_CMD_RENEGE;
  5596. } else {
  5597. pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
  5598. __func__, tsn, datalen, asoc->rwnd);
  5599. return SCTP_IERROR_IGNORE_TSN;
  5600. }
  5601. }
  5602. /*
  5603. * Also try to renege to limit our memory usage in the event that
  5604. * we are under memory pressure
  5605. * If we can't renege, don't worry about it, the sk_rmem_schedule
  5606. * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
  5607. * memory usage too much
  5608. */
  5609. if (*sk->sk_prot_creator->memory_pressure) {
  5610. if (sctp_tsnmap_has_gap(map) &&
  5611. (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
  5612. pr_debug("%s: under pressure, reneging for tsn:%u\n",
  5613. __func__, tsn);
  5614. deliver = SCTP_CMD_RENEGE;
  5615. }
  5616. }
  5617. /*
  5618. * Section 3.3.10.9 No User Data (9)
  5619. *
  5620. * Cause of error
  5621. * ---------------
  5622. * No User Data: This error cause is returned to the originator of a
  5623. * DATA chunk if a received DATA chunk has no user data.
  5624. */
  5625. if (unlikely(0 == datalen)) {
  5626. err = sctp_make_abort_no_data(asoc, chunk, tsn);
  5627. if (err) {
  5628. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  5629. SCTP_CHUNK(err));
  5630. }
  5631. /* We are going to ABORT, so we might as well stop
  5632. * processing the rest of the chunks in the packet.
  5633. */
  5634. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  5635. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5636. SCTP_ERROR(ECONNABORTED));
  5637. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  5638. SCTP_PERR(SCTP_ERROR_NO_DATA));
  5639. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  5640. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  5641. return SCTP_IERROR_NO_DATA;
  5642. }
  5643. chunk->data_accepted = 1;
  5644. /* Note: Some chunks may get overcounted (if we drop) or overcounted
  5645. * if we renege and the chunk arrives again.
  5646. */
  5647. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  5648. SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
  5649. if (chunk->asoc)
  5650. chunk->asoc->stats.iuodchunks++;
  5651. } else {
  5652. SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
  5653. if (chunk->asoc)
  5654. chunk->asoc->stats.iodchunks++;
  5655. ordered = 1;
  5656. }
  5657. /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
  5658. *
  5659. * If an endpoint receive a DATA chunk with an invalid stream
  5660. * identifier, it shall acknowledge the reception of the DATA chunk
  5661. * following the normal procedure, immediately send an ERROR chunk
  5662. * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
  5663. * and discard the DATA chunk.
  5664. */
  5665. sid = ntohs(data_hdr->stream);
  5666. if (sid >= asoc->stream.incnt) {
  5667. /* Mark tsn as received even though we drop it */
  5668. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
  5669. err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
  5670. &data_hdr->stream,
  5671. sizeof(data_hdr->stream),
  5672. sizeof(u16));
  5673. if (err)
  5674. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  5675. SCTP_CHUNK(err));
  5676. return SCTP_IERROR_BAD_STREAM;
  5677. }
  5678. /* Check to see if the SSN is possible for this TSN.
  5679. * The biggest gap we can record is 4K wide. Since SSNs wrap
  5680. * at an unsigned short, there is no way that an SSN can
  5681. * wrap and for a valid TSN. We can simply check if the current
  5682. * SSN is smaller then the next expected one. If it is, it wrapped
  5683. * and is invalid.
  5684. */
  5685. ssn = ntohs(data_hdr->ssn);
  5686. if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->stream, in, sid)))
  5687. return SCTP_IERROR_PROTO_VIOLATION;
  5688. /* Send the data up to the user. Note: Schedule the
  5689. * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
  5690. * chunk needs the updated rwnd.
  5691. */
  5692. sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
  5693. return SCTP_IERROR_NO_ERROR;
  5694. }