sm_statefuns.c 185 KB

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