sm_statefuns.c 166 KB

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