sm_statefuns.c 169 KB

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