sm_statefuns.c 185 KB

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