sm_statefuns.c 169 KB

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