sm_statefuns.c 165 KB

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