sm_statefuns.c 190 KB

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