sm_statefuns.c 169 KB

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