sm_statefuns.c 163 KB

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