sm_statefuns.c 168 KB

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