sm_statefuns.c 164 KB

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