sm_statefuns.c 162 KB

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