sm_statefuns.c 167 KB

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