sm_statefuns.c 171 KB

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