sm_statefuns.c 186 KB

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