sm_statefuns.c 169 KB

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