sm_statefuns.c 193 KB

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