sm_statefuns.c 183 KB

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