sm_statefuns.c 171 KB

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