sm_statefuns.c 169 KB

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