sm_statefuns.c 165 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329
  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->param_flags & SPP_HB_ENABLE) {
  800. if (SCTP_DISPOSITION_NOMEM ==
  801. sctp_sf_heartbeat(ep, asoc, type, arg,
  802. commands))
  803. return SCTP_DISPOSITION_NOMEM;
  804. /* Set transport error counter and association error counter
  805. * when sending heartbeat.
  806. */
  807. sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_RESET,
  808. SCTP_TRANSPORT(transport));
  809. }
  810. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
  811. SCTP_TRANSPORT(transport));
  812. return SCTP_DISPOSITION_CONSUME;
  813. }
  814. /*
  815. * Process an heartbeat request.
  816. *
  817. * Section: 8.3 Path Heartbeat
  818. * The receiver of the HEARTBEAT should immediately respond with a
  819. * HEARTBEAT ACK that contains the Heartbeat Information field copied
  820. * from the received HEARTBEAT chunk.
  821. *
  822. * Verification Tag: 8.5 Verification Tag [Normal verification]
  823. * When receiving an SCTP packet, the endpoint MUST ensure that the
  824. * value in the Verification Tag field of the received SCTP packet
  825. * matches its own Tag. If the received Verification Tag value does not
  826. * match the receiver's own tag value, the receiver shall silently
  827. * discard the packet and shall not process it any further except for
  828. * those cases listed in Section 8.5.1 below.
  829. *
  830. * Inputs
  831. * (endpoint, asoc, chunk)
  832. *
  833. * Outputs
  834. * (asoc, reply_msg, msg_up, timers, counters)
  835. *
  836. * The return value is the disposition of the chunk.
  837. */
  838. sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
  839. const struct sctp_association *asoc,
  840. const sctp_subtype_t type,
  841. void *arg,
  842. sctp_cmd_seq_t *commands)
  843. {
  844. struct sctp_chunk *chunk = arg;
  845. struct sctp_chunk *reply;
  846. size_t paylen = 0;
  847. if (!sctp_vtag_verify(chunk, asoc))
  848. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  849. /* Make sure that the HEARTBEAT chunk has a valid length. */
  850. if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
  851. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  852. commands);
  853. /* 8.3 The receiver of the HEARTBEAT should immediately
  854. * respond with a HEARTBEAT ACK that contains the Heartbeat
  855. * Information field copied from the received HEARTBEAT chunk.
  856. */
  857. chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
  858. paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
  859. 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. NIP6_FMT "\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. NIPQUAD_FMT "\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->hbinterval + 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. /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
  2144. * When a peer sends a SHUTDOWN, SCTP delivers this notification to
  2145. * inform the application that it should cease sending data.
  2146. */
  2147. ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
  2148. if (!ev) {
  2149. disposition = SCTP_DISPOSITION_NOMEM;
  2150. goto out;
  2151. }
  2152. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  2153. /* Upon the reception of the SHUTDOWN, the peer endpoint shall
  2154. * - enter the SHUTDOWN-RECEIVED state,
  2155. * - stop accepting new data from its SCTP user
  2156. *
  2157. * [This is implicit in the new state.]
  2158. */
  2159. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  2160. SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
  2161. disposition = SCTP_DISPOSITION_CONSUME;
  2162. if (sctp_outq_is_empty(&asoc->outqueue)) {
  2163. disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
  2164. arg, commands);
  2165. }
  2166. if (SCTP_DISPOSITION_NOMEM == disposition)
  2167. goto out;
  2168. /* - verify, by checking the Cumulative TSN Ack field of the
  2169. * chunk, that all its outstanding DATA chunks have been
  2170. * received by the SHUTDOWN sender.
  2171. */
  2172. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
  2173. SCTP_U32(chunk->subh.shutdown_hdr->cum_tsn_ack));
  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. return SCTP_DISPOSITION_CONSUME;
  2394. discard_force:
  2395. /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
  2396. *
  2397. * When a packet arrives with duplicate DATA chunk(s) and with
  2398. * no new DATA chunk(s), the endpoint MUST immediately send a
  2399. * SACK with no delay. If a packet arrives with duplicate
  2400. * DATA chunk(s) bundled with new DATA chunks, the endpoint
  2401. * MAY immediately send a SACK. Normally receipt of duplicate
  2402. * DATA chunks will occur when the original SACK chunk was lost
  2403. * and the peer's RTO has expired. The duplicate TSN number(s)
  2404. * SHOULD be reported in the SACK as duplicate.
  2405. */
  2406. /* In our case, we split the MAY SACK advice up whether or not
  2407. * the last chunk is a duplicate.'
  2408. */
  2409. if (chunk->end_of_packet)
  2410. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  2411. return SCTP_DISPOSITION_DISCARD;
  2412. discard_noforce:
  2413. if (chunk->end_of_packet)
  2414. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
  2415. return SCTP_DISPOSITION_DISCARD;
  2416. consume:
  2417. return SCTP_DISPOSITION_CONSUME;
  2418. }
  2419. /*
  2420. * sctp_sf_eat_data_fast_4_4
  2421. *
  2422. * Section: 4 (4)
  2423. * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
  2424. * DATA chunks without delay.
  2425. *
  2426. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2427. * Inputs
  2428. * (endpoint, asoc, chunk)
  2429. *
  2430. * Outputs
  2431. * (asoc, reply_msg, msg_up, timers, counters)
  2432. *
  2433. * The return value is the disposition of the chunk.
  2434. */
  2435. sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
  2436. const struct sctp_association *asoc,
  2437. const sctp_subtype_t type,
  2438. void *arg,
  2439. sctp_cmd_seq_t *commands)
  2440. {
  2441. struct sctp_chunk *chunk = arg;
  2442. int error;
  2443. if (!sctp_vtag_verify(chunk, asoc)) {
  2444. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  2445. SCTP_NULL());
  2446. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2447. }
  2448. if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
  2449. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  2450. commands);
  2451. error = sctp_eat_data(asoc, chunk, commands );
  2452. switch (error) {
  2453. case SCTP_IERROR_NO_ERROR:
  2454. case SCTP_IERROR_HIGH_TSN:
  2455. case SCTP_IERROR_DUP_TSN:
  2456. case SCTP_IERROR_IGNORE_TSN:
  2457. case SCTP_IERROR_BAD_STREAM:
  2458. break;
  2459. case SCTP_IERROR_NO_DATA:
  2460. goto consume;
  2461. default:
  2462. BUG();
  2463. }
  2464. /* Go a head and force a SACK, since we are shutting down. */
  2465. /* Implementor's Guide.
  2466. *
  2467. * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
  2468. * respond to each received packet containing one or more DATA chunk(s)
  2469. * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
  2470. */
  2471. if (chunk->end_of_packet) {
  2472. /* We must delay the chunk creation since the cumulative
  2473. * TSN has not been updated yet.
  2474. */
  2475. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
  2476. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  2477. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  2478. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  2479. }
  2480. consume:
  2481. return SCTP_DISPOSITION_CONSUME;
  2482. }
  2483. /*
  2484. * Section: 6.2 Processing a Received SACK
  2485. * D) Any time a SACK arrives, the endpoint performs the following:
  2486. *
  2487. * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
  2488. * then drop the SACK. Since Cumulative TSN Ack is monotonically
  2489. * increasing, a SACK whose Cumulative TSN Ack is less than the
  2490. * Cumulative TSN Ack Point indicates an out-of-order SACK.
  2491. *
  2492. * ii) Set rwnd equal to the newly received a_rwnd minus the number
  2493. * of bytes still outstanding after processing the Cumulative TSN Ack
  2494. * and the Gap Ack Blocks.
  2495. *
  2496. * iii) If the SACK is missing a TSN that was previously
  2497. * acknowledged via a Gap Ack Block (e.g., the data receiver
  2498. * reneged on the data), then mark the corresponding DATA chunk
  2499. * as available for retransmit: Mark it as missing for fast
  2500. * retransmit as described in Section 7.2.4 and if no retransmit
  2501. * timer is running for the destination address to which the DATA
  2502. * chunk was originally transmitted, then T3-rtx is started for
  2503. * that destination address.
  2504. *
  2505. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2506. *
  2507. * Inputs
  2508. * (endpoint, asoc, chunk)
  2509. *
  2510. * Outputs
  2511. * (asoc, reply_msg, msg_up, timers, counters)
  2512. *
  2513. * The return value is the disposition of the chunk.
  2514. */
  2515. sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
  2516. const struct sctp_association *asoc,
  2517. const sctp_subtype_t type,
  2518. void *arg,
  2519. sctp_cmd_seq_t *commands)
  2520. {
  2521. struct sctp_chunk *chunk = arg;
  2522. sctp_sackhdr_t *sackh;
  2523. __u32 ctsn;
  2524. if (!sctp_vtag_verify(chunk, asoc))
  2525. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2526. /* Make sure that the SACK chunk has a valid length. */
  2527. if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
  2528. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  2529. commands);
  2530. /* Pull the SACK chunk from the data buffer */
  2531. sackh = sctp_sm_pull_sack(chunk);
  2532. /* Was this a bogus SACK? */
  2533. if (!sackh)
  2534. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2535. chunk->subh.sack_hdr = sackh;
  2536. ctsn = ntohl(sackh->cum_tsn_ack);
  2537. /* i) If Cumulative TSN Ack is less than the Cumulative TSN
  2538. * Ack Point, then drop the SACK. Since Cumulative TSN
  2539. * Ack is monotonically increasing, a SACK whose
  2540. * Cumulative TSN Ack is less than the Cumulative TSN Ack
  2541. * Point indicates an out-of-order SACK.
  2542. */
  2543. if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
  2544. SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
  2545. SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
  2546. return SCTP_DISPOSITION_DISCARD;
  2547. }
  2548. /* Return this SACK for further processing. */
  2549. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
  2550. /* Note: We do the rest of the work on the PROCESS_SACK
  2551. * sideeffect.
  2552. */
  2553. return SCTP_DISPOSITION_CONSUME;
  2554. }
  2555. /*
  2556. * Generate an ABORT in response to a packet.
  2557. *
  2558. * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
  2559. *
  2560. * 8) The receiver should respond to the sender of the OOTB packet with
  2561. * an ABORT. When sending the ABORT, the receiver of the OOTB packet
  2562. * MUST fill in the Verification Tag field of the outbound packet
  2563. * with the value found in the Verification Tag field of the OOTB
  2564. * packet and set the T-bit in the Chunk Flags to indicate that the
  2565. * Verification Tag is reflected. After sending this ABORT, the
  2566. * receiver of the OOTB packet shall discard the OOTB packet and take
  2567. * no further action.
  2568. *
  2569. * Verification Tag:
  2570. *
  2571. * The return value is the disposition of the chunk.
  2572. */
  2573. sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
  2574. const struct sctp_association *asoc,
  2575. const sctp_subtype_t type,
  2576. void *arg,
  2577. sctp_cmd_seq_t *commands)
  2578. {
  2579. struct sctp_packet *packet = NULL;
  2580. struct sctp_chunk *chunk = arg;
  2581. struct sctp_chunk *abort;
  2582. packet = sctp_ootb_pkt_new(asoc, chunk);
  2583. if (packet) {
  2584. /* Make an ABORT. The T bit will be set if the asoc
  2585. * is NULL.
  2586. */
  2587. abort = sctp_make_abort(asoc, chunk, 0);
  2588. if (!abort) {
  2589. sctp_ootb_pkt_free(packet);
  2590. return SCTP_DISPOSITION_NOMEM;
  2591. }
  2592. /* Reflect vtag if T-Bit is set */
  2593. if (sctp_test_T_bit(abort))
  2594. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  2595. /* Set the skb to the belonging sock for accounting. */
  2596. abort->skb->sk = ep->base.sk;
  2597. sctp_packet_append_chunk(packet, abort);
  2598. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  2599. SCTP_PACKET(packet));
  2600. SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
  2601. return SCTP_DISPOSITION_CONSUME;
  2602. }
  2603. return SCTP_DISPOSITION_NOMEM;
  2604. }
  2605. /*
  2606. * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
  2607. * event as ULP notification for each cause included in the chunk.
  2608. *
  2609. * API 5.3.1.3 - SCTP_REMOTE_ERROR
  2610. *
  2611. * The return value is the disposition of the chunk.
  2612. */
  2613. sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
  2614. const struct sctp_association *asoc,
  2615. const sctp_subtype_t type,
  2616. void *arg,
  2617. sctp_cmd_seq_t *commands)
  2618. {
  2619. struct sctp_chunk *chunk = arg;
  2620. struct sctp_ulpevent *ev;
  2621. if (!sctp_vtag_verify(chunk, asoc))
  2622. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2623. /* Make sure that the ERROR chunk has a valid length. */
  2624. if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
  2625. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  2626. commands);
  2627. while (chunk->chunk_end > chunk->skb->data) {
  2628. ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0,
  2629. GFP_ATOMIC);
  2630. if (!ev)
  2631. goto nomem;
  2632. if (!sctp_add_cmd(commands, SCTP_CMD_EVENT_ULP,
  2633. SCTP_ULPEVENT(ev))) {
  2634. sctp_ulpevent_free(ev);
  2635. goto nomem;
  2636. }
  2637. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
  2638. SCTP_CHUNK(chunk));
  2639. }
  2640. return SCTP_DISPOSITION_CONSUME;
  2641. nomem:
  2642. return SCTP_DISPOSITION_NOMEM;
  2643. }
  2644. /*
  2645. * Process an inbound SHUTDOWN ACK.
  2646. *
  2647. * From Section 9.2:
  2648. * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
  2649. * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
  2650. * peer, and remove all record of the association.
  2651. *
  2652. * The return value is the disposition.
  2653. */
  2654. sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
  2655. const struct sctp_association *asoc,
  2656. const sctp_subtype_t type,
  2657. void *arg,
  2658. sctp_cmd_seq_t *commands)
  2659. {
  2660. struct sctp_chunk *chunk = arg;
  2661. struct sctp_chunk *reply;
  2662. struct sctp_ulpevent *ev;
  2663. if (!sctp_vtag_verify(chunk, asoc))
  2664. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2665. /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
  2666. if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
  2667. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  2668. commands);
  2669. /* 10.2 H) SHUTDOWN COMPLETE notification
  2670. *
  2671. * When SCTP completes the shutdown procedures (section 9.2) this
  2672. * notification is passed to the upper layer.
  2673. */
  2674. ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
  2675. 0, 0, 0, GFP_ATOMIC);
  2676. if (!ev)
  2677. goto nomem;
  2678. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  2679. /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
  2680. * stop the T2-shutdown timer,
  2681. */
  2682. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2683. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  2684. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2685. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  2686. /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
  2687. reply = sctp_make_shutdown_complete(asoc, chunk);
  2688. if (!reply)
  2689. goto nomem;
  2690. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  2691. SCTP_STATE(SCTP_STATE_CLOSED));
  2692. SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
  2693. SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
  2694. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  2695. /* ...and remove all record of the association. */
  2696. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  2697. return SCTP_DISPOSITION_DELETE_TCB;
  2698. nomem:
  2699. return SCTP_DISPOSITION_NOMEM;
  2700. }
  2701. /*
  2702. * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
  2703. *
  2704. * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
  2705. * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
  2706. * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
  2707. * packet must fill in the Verification Tag field of the outbound
  2708. * packet with the Verification Tag received in the SHUTDOWN ACK and
  2709. * set the T-bit in the Chunk Flags to indicate that the Verification
  2710. * Tag is reflected.
  2711. *
  2712. * 8) The receiver should respond to the sender of the OOTB packet with
  2713. * an ABORT. When sending the ABORT, the receiver of the OOTB packet
  2714. * MUST fill in the Verification Tag field of the outbound packet
  2715. * with the value found in the Verification Tag field of the OOTB
  2716. * packet and set the T-bit in the Chunk Flags to indicate that the
  2717. * Verification Tag is reflected. After sending this ABORT, the
  2718. * receiver of the OOTB packet shall discard the OOTB packet and take
  2719. * no further action.
  2720. */
  2721. sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
  2722. const struct sctp_association *asoc,
  2723. const sctp_subtype_t type,
  2724. void *arg,
  2725. sctp_cmd_seq_t *commands)
  2726. {
  2727. struct sctp_chunk *chunk = arg;
  2728. struct sk_buff *skb = chunk->skb;
  2729. sctp_chunkhdr_t *ch;
  2730. __u8 *ch_end;
  2731. int ootb_shut_ack = 0;
  2732. SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
  2733. ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
  2734. do {
  2735. /* Break out if chunk length is less then minimal. */
  2736. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  2737. break;
  2738. ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
  2739. if (ch_end > skb->tail)
  2740. break;
  2741. if (SCTP_CID_SHUTDOWN_ACK == ch->type)
  2742. ootb_shut_ack = 1;
  2743. /* RFC 2960, Section 3.3.7
  2744. * Moreover, under any circumstances, an endpoint that
  2745. * receives an ABORT MUST NOT respond to that ABORT by
  2746. * sending an ABORT of its own.
  2747. */
  2748. if (SCTP_CID_ABORT == ch->type)
  2749. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2750. ch = (sctp_chunkhdr_t *) ch_end;
  2751. } while (ch_end < skb->tail);
  2752. if (ootb_shut_ack)
  2753. sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
  2754. else
  2755. sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
  2756. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2757. }
  2758. /*
  2759. * Handle an "Out of the blue" SHUTDOWN ACK.
  2760. *
  2761. * Section: 8.4 5, sctpimpguide 2.41.
  2762. *
  2763. * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
  2764. * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
  2765. * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
  2766. * packet must fill in the Verification Tag field of the outbound
  2767. * packet with the Verification Tag received in the SHUTDOWN ACK and
  2768. * set the T-bit in the Chunk Flags to indicate that the Verification
  2769. * Tag is reflected.
  2770. *
  2771. * Inputs
  2772. * (endpoint, asoc, type, arg, commands)
  2773. *
  2774. * Outputs
  2775. * (sctp_disposition_t)
  2776. *
  2777. * The return value is the disposition of the chunk.
  2778. */
  2779. static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
  2780. const struct sctp_association *asoc,
  2781. const sctp_subtype_t type,
  2782. void *arg,
  2783. sctp_cmd_seq_t *commands)
  2784. {
  2785. struct sctp_packet *packet = NULL;
  2786. struct sctp_chunk *chunk = arg;
  2787. struct sctp_chunk *shut;
  2788. packet = sctp_ootb_pkt_new(asoc, chunk);
  2789. if (packet) {
  2790. /* Make an SHUTDOWN_COMPLETE.
  2791. * The T bit will be set if the asoc is NULL.
  2792. */
  2793. shut = sctp_make_shutdown_complete(asoc, chunk);
  2794. if (!shut) {
  2795. sctp_ootb_pkt_free(packet);
  2796. return SCTP_DISPOSITION_NOMEM;
  2797. }
  2798. /* Reflect vtag if T-Bit is set */
  2799. if (sctp_test_T_bit(shut))
  2800. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  2801. /* Set the skb to the belonging sock for accounting. */
  2802. shut->skb->sk = ep->base.sk;
  2803. sctp_packet_append_chunk(packet, shut);
  2804. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  2805. SCTP_PACKET(packet));
  2806. SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
  2807. /* If the chunk length is invalid, we don't want to process
  2808. * the reset of the packet.
  2809. */
  2810. if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
  2811. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2812. return SCTP_DISPOSITION_CONSUME;
  2813. }
  2814. return SCTP_DISPOSITION_NOMEM;
  2815. }
  2816. /*
  2817. * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
  2818. *
  2819. * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
  2820. * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
  2821. * procedures in section 8.4 SHOULD be followed, in other words it
  2822. * should be treated as an Out Of The Blue packet.
  2823. * [This means that we do NOT check the Verification Tag on these
  2824. * chunks. --piggy ]
  2825. *
  2826. */
  2827. sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
  2828. const struct sctp_association *asoc,
  2829. const sctp_subtype_t type,
  2830. void *arg,
  2831. sctp_cmd_seq_t *commands)
  2832. {
  2833. /* Although we do have an association in this case, it corresponds
  2834. * to a restarted association. So the packet is treated as an OOTB
  2835. * packet and the state function that handles OOTB SHUTDOWN_ACK is
  2836. * called with a NULL association.
  2837. */
  2838. return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
  2839. }
  2840. /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
  2841. sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
  2842. const struct sctp_association *asoc,
  2843. const sctp_subtype_t type, void *arg,
  2844. sctp_cmd_seq_t *commands)
  2845. {
  2846. struct sctp_chunk *chunk = arg;
  2847. struct sctp_chunk *asconf_ack = NULL;
  2848. sctp_addiphdr_t *hdr;
  2849. __u32 serial;
  2850. if (!sctp_vtag_verify(chunk, asoc)) {
  2851. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  2852. SCTP_NULL());
  2853. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2854. }
  2855. /* Make sure that the ASCONF ADDIP chunk has a valid length. */
  2856. if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
  2857. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  2858. commands);
  2859. hdr = (sctp_addiphdr_t *)chunk->skb->data;
  2860. serial = ntohl(hdr->serial);
  2861. /* ADDIP 4.2 C1) Compare the value of the serial number to the value
  2862. * the endpoint stored in a new association variable
  2863. * 'Peer-Serial-Number'.
  2864. */
  2865. if (serial == asoc->peer.addip_serial + 1) {
  2866. /* ADDIP 4.2 C2) If the value found in the serial number is
  2867. * equal to the ('Peer-Serial-Number' + 1), the endpoint MUST
  2868. * do V1-V5.
  2869. */
  2870. asconf_ack = sctp_process_asconf((struct sctp_association *)
  2871. asoc, chunk);
  2872. if (!asconf_ack)
  2873. return SCTP_DISPOSITION_NOMEM;
  2874. } else if (serial == asoc->peer.addip_serial) {
  2875. /* ADDIP 4.2 C3) If the value found in the serial number is
  2876. * equal to the value stored in the 'Peer-Serial-Number'
  2877. * IMPLEMENTATION NOTE: As an optimization a receiver may wish
  2878. * to save the last ASCONF-ACK for some predetermined period of
  2879. * time and instead of re-processing the ASCONF (with the same
  2880. * serial number) it may just re-transmit the ASCONF-ACK.
  2881. */
  2882. if (asoc->addip_last_asconf_ack)
  2883. asconf_ack = asoc->addip_last_asconf_ack;
  2884. else
  2885. return SCTP_DISPOSITION_DISCARD;
  2886. } else {
  2887. /* ADDIP 4.2 C4) Otherwise, the ASCONF Chunk is discarded since
  2888. * it must be either a stale packet or from an attacker.
  2889. */
  2890. return SCTP_DISPOSITION_DISCARD;
  2891. }
  2892. /* ADDIP 4.2 C5) In both cases C2 and C3 the ASCONF-ACK MUST be sent
  2893. * back to the source address contained in the IP header of the ASCONF
  2894. * being responded to.
  2895. */
  2896. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
  2897. return SCTP_DISPOSITION_CONSUME;
  2898. }
  2899. /*
  2900. * ADDIP Section 4.3 General rules for address manipulation
  2901. * When building TLV parameters for the ASCONF Chunk that will add or
  2902. * delete IP addresses the D0 to D13 rules should be applied:
  2903. */
  2904. sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
  2905. const struct sctp_association *asoc,
  2906. const sctp_subtype_t type, void *arg,
  2907. sctp_cmd_seq_t *commands)
  2908. {
  2909. struct sctp_chunk *asconf_ack = arg;
  2910. struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
  2911. struct sctp_chunk *abort;
  2912. sctp_addiphdr_t *addip_hdr;
  2913. __u32 sent_serial, rcvd_serial;
  2914. if (!sctp_vtag_verify(asconf_ack, asoc)) {
  2915. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  2916. SCTP_NULL());
  2917. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  2918. }
  2919. /* Make sure that the ADDIP chunk has a valid length. */
  2920. if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
  2921. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  2922. commands);
  2923. addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
  2924. rcvd_serial = ntohl(addip_hdr->serial);
  2925. if (last_asconf) {
  2926. addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
  2927. sent_serial = ntohl(addip_hdr->serial);
  2928. } else {
  2929. sent_serial = asoc->addip_serial - 1;
  2930. }
  2931. /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
  2932. * equal to the next serial number to be used but no ASCONF chunk is
  2933. * outstanding the endpoint MUST ABORT the association. Note that a
  2934. * sequence number is greater than if it is no more than 2^^31-1
  2935. * larger than the current sequence number (using serial arithmetic).
  2936. */
  2937. if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
  2938. !(asoc->addip_last_asconf)) {
  2939. abort = sctp_make_abort(asoc, asconf_ack,
  2940. sizeof(sctp_errhdr_t));
  2941. if (abort) {
  2942. sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, NULL, 0);
  2943. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  2944. SCTP_CHUNK(abort));
  2945. }
  2946. /* We are going to ABORT, so we might as well stop
  2947. * processing the rest of the chunks in the packet.
  2948. */
  2949. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2950. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  2951. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
  2952. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  2953. SCTP_U32(SCTP_ERROR_ASCONF_ACK));
  2954. SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
  2955. SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
  2956. return SCTP_DISPOSITION_ABORT;
  2957. }
  2958. if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
  2959. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2960. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  2961. if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
  2962. asconf_ack))
  2963. return SCTP_DISPOSITION_CONSUME;
  2964. abort = sctp_make_abort(asoc, asconf_ack,
  2965. sizeof(sctp_errhdr_t));
  2966. if (abort) {
  2967. sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, NULL, 0);
  2968. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  2969. SCTP_CHUNK(abort));
  2970. }
  2971. /* We are going to ABORT, so we might as well stop
  2972. * processing the rest of the chunks in the packet.
  2973. */
  2974. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
  2975. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  2976. SCTP_U32(SCTP_ERROR_ASCONF_ACK));
  2977. SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
  2978. SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
  2979. return SCTP_DISPOSITION_ABORT;
  2980. }
  2981. return SCTP_DISPOSITION_DISCARD;
  2982. }
  2983. /*
  2984. * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
  2985. *
  2986. * When a FORWARD TSN chunk arrives, the data receiver MUST first update
  2987. * its cumulative TSN point to the value carried in the FORWARD TSN
  2988. * chunk, and then MUST further advance its cumulative TSN point locally
  2989. * if possible.
  2990. * After the above processing, the data receiver MUST stop reporting any
  2991. * missing TSNs earlier than or equal to the new cumulative TSN point.
  2992. *
  2993. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2994. *
  2995. * The return value is the disposition of the chunk.
  2996. */
  2997. sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
  2998. const struct sctp_association *asoc,
  2999. const sctp_subtype_t type,
  3000. void *arg,
  3001. sctp_cmd_seq_t *commands)
  3002. {
  3003. struct sctp_chunk *chunk = arg;
  3004. struct sctp_fwdtsn_hdr *fwdtsn_hdr;
  3005. __u16 len;
  3006. __u32 tsn;
  3007. if (!sctp_vtag_verify(chunk, asoc)) {
  3008. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3009. SCTP_NULL());
  3010. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  3011. }
  3012. /* Make sure that the FORWARD_TSN chunk has valid length. */
  3013. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
  3014. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  3015. commands);
  3016. fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
  3017. chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
  3018. len = ntohs(chunk->chunk_hdr->length);
  3019. len -= sizeof(struct sctp_chunkhdr);
  3020. skb_pull(chunk->skb, len);
  3021. tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
  3022. SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
  3023. /* The TSN is too high--silently discard the chunk and count on it
  3024. * getting retransmitted later.
  3025. */
  3026. if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
  3027. goto discard_noforce;
  3028. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
  3029. if (len > sizeof(struct sctp_fwdtsn_hdr))
  3030. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
  3031. SCTP_CHUNK(chunk));
  3032. /* Count this as receiving DATA. */
  3033. if (asoc->autoclose) {
  3034. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  3035. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  3036. }
  3037. /* FIXME: For now send a SACK, but DATA processing may
  3038. * send another.
  3039. */
  3040. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
  3041. return SCTP_DISPOSITION_CONSUME;
  3042. discard_noforce:
  3043. return SCTP_DISPOSITION_DISCARD;
  3044. }
  3045. sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
  3046. const struct sctp_endpoint *ep,
  3047. const struct sctp_association *asoc,
  3048. const sctp_subtype_t type,
  3049. void *arg,
  3050. sctp_cmd_seq_t *commands)
  3051. {
  3052. struct sctp_chunk *chunk = arg;
  3053. struct sctp_fwdtsn_hdr *fwdtsn_hdr;
  3054. __u16 len;
  3055. __u32 tsn;
  3056. if (!sctp_vtag_verify(chunk, asoc)) {
  3057. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3058. SCTP_NULL());
  3059. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  3060. }
  3061. /* Make sure that the FORWARD_TSN chunk has a valid length. */
  3062. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
  3063. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  3064. commands);
  3065. fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
  3066. chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
  3067. len = ntohs(chunk->chunk_hdr->length);
  3068. len -= sizeof(struct sctp_chunkhdr);
  3069. skb_pull(chunk->skb, len);
  3070. tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
  3071. SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
  3072. /* The TSN is too high--silently discard the chunk and count on it
  3073. * getting retransmitted later.
  3074. */
  3075. if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
  3076. goto gen_shutdown;
  3077. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
  3078. if (len > sizeof(struct sctp_fwdtsn_hdr))
  3079. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
  3080. SCTP_CHUNK(chunk));
  3081. /* Go a head and force a SACK, since we are shutting down. */
  3082. gen_shutdown:
  3083. /* Implementor's Guide.
  3084. *
  3085. * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
  3086. * respond to each received packet containing one or more DATA chunk(s)
  3087. * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
  3088. */
  3089. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
  3090. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  3091. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  3092. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  3093. return SCTP_DISPOSITION_CONSUME;
  3094. }
  3095. /*
  3096. * Process an unknown chunk.
  3097. *
  3098. * Section: 3.2. Also, 2.1 in the implementor's guide.
  3099. *
  3100. * Chunk Types are encoded such that the highest-order two bits specify
  3101. * the action that must be taken if the processing endpoint does not
  3102. * recognize the Chunk Type.
  3103. *
  3104. * 00 - Stop processing this SCTP packet and discard it, do not process
  3105. * any further chunks within it.
  3106. *
  3107. * 01 - Stop processing this SCTP packet and discard it, do not process
  3108. * any further chunks within it, and report the unrecognized
  3109. * chunk in an 'Unrecognized Chunk Type'.
  3110. *
  3111. * 10 - Skip this chunk and continue processing.
  3112. *
  3113. * 11 - Skip this chunk and continue processing, but report in an ERROR
  3114. * Chunk using the 'Unrecognized Chunk Type' cause of error.
  3115. *
  3116. * The return value is the disposition of the chunk.
  3117. */
  3118. sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
  3119. const struct sctp_association *asoc,
  3120. const sctp_subtype_t type,
  3121. void *arg,
  3122. sctp_cmd_seq_t *commands)
  3123. {
  3124. struct sctp_chunk *unk_chunk = arg;
  3125. struct sctp_chunk *err_chunk;
  3126. sctp_chunkhdr_t *hdr;
  3127. SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
  3128. if (!sctp_vtag_verify(unk_chunk, asoc))
  3129. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  3130. /* Make sure that the chunk has a valid length.
  3131. * Since we don't know the chunk type, we use a general
  3132. * chunkhdr structure to make a comparison.
  3133. */
  3134. if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
  3135. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  3136. commands);
  3137. switch (type.chunk & SCTP_CID_ACTION_MASK) {
  3138. case SCTP_CID_ACTION_DISCARD:
  3139. /* Discard the packet. */
  3140. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  3141. break;
  3142. case SCTP_CID_ACTION_DISCARD_ERR:
  3143. /* Discard the packet. */
  3144. sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  3145. /* Generate an ERROR chunk as response. */
  3146. hdr = unk_chunk->chunk_hdr;
  3147. err_chunk = sctp_make_op_error(asoc, unk_chunk,
  3148. SCTP_ERROR_UNKNOWN_CHUNK, hdr,
  3149. WORD_ROUND(ntohs(hdr->length)));
  3150. if (err_chunk) {
  3151. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3152. SCTP_CHUNK(err_chunk));
  3153. }
  3154. return SCTP_DISPOSITION_CONSUME;
  3155. break;
  3156. case SCTP_CID_ACTION_SKIP:
  3157. /* Skip the chunk. */
  3158. return SCTP_DISPOSITION_DISCARD;
  3159. break;
  3160. case SCTP_CID_ACTION_SKIP_ERR:
  3161. /* Generate an ERROR chunk as response. */
  3162. hdr = unk_chunk->chunk_hdr;
  3163. err_chunk = sctp_make_op_error(asoc, unk_chunk,
  3164. SCTP_ERROR_UNKNOWN_CHUNK, hdr,
  3165. WORD_ROUND(ntohs(hdr->length)));
  3166. if (err_chunk) {
  3167. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3168. SCTP_CHUNK(err_chunk));
  3169. }
  3170. /* Skip the chunk. */
  3171. return SCTP_DISPOSITION_CONSUME;
  3172. break;
  3173. default:
  3174. break;
  3175. }
  3176. return SCTP_DISPOSITION_DISCARD;
  3177. }
  3178. /*
  3179. * Discard the chunk.
  3180. *
  3181. * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
  3182. * [Too numerous to mention...]
  3183. * Verification Tag: No verification needed.
  3184. * Inputs
  3185. * (endpoint, asoc, chunk)
  3186. *
  3187. * Outputs
  3188. * (asoc, reply_msg, msg_up, timers, counters)
  3189. *
  3190. * The return value is the disposition of the chunk.
  3191. */
  3192. sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
  3193. const struct sctp_association *asoc,
  3194. const sctp_subtype_t type,
  3195. void *arg,
  3196. sctp_cmd_seq_t *commands)
  3197. {
  3198. SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
  3199. return SCTP_DISPOSITION_DISCARD;
  3200. }
  3201. /*
  3202. * Discard the whole packet.
  3203. *
  3204. * Section: 8.4 2)
  3205. *
  3206. * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
  3207. * silently discard the OOTB packet and take no further action.
  3208. *
  3209. * Verification Tag: No verification necessary
  3210. *
  3211. * Inputs
  3212. * (endpoint, asoc, chunk)
  3213. *
  3214. * Outputs
  3215. * (asoc, reply_msg, msg_up, timers, counters)
  3216. *
  3217. * The return value is the disposition of the chunk.
  3218. */
  3219. sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
  3220. const struct sctp_association *asoc,
  3221. const sctp_subtype_t type,
  3222. void *arg,
  3223. sctp_cmd_seq_t *commands)
  3224. {
  3225. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  3226. return SCTP_DISPOSITION_CONSUME;
  3227. }
  3228. /*
  3229. * The other end is violating protocol.
  3230. *
  3231. * Section: Not specified
  3232. * Verification Tag: Not specified
  3233. * Inputs
  3234. * (endpoint, asoc, chunk)
  3235. *
  3236. * Outputs
  3237. * (asoc, reply_msg, msg_up, timers, counters)
  3238. *
  3239. * We simply tag the chunk as a violation. The state machine will log
  3240. * the violation and continue.
  3241. */
  3242. sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
  3243. const struct sctp_association *asoc,
  3244. const sctp_subtype_t type,
  3245. void *arg,
  3246. sctp_cmd_seq_t *commands)
  3247. {
  3248. return SCTP_DISPOSITION_VIOLATION;
  3249. }
  3250. /*
  3251. * Handle a protocol violation when the chunk length is invalid.
  3252. * "Invalid" length is identified as smaller then the minimal length a
  3253. * given chunk can be. For example, a SACK chunk has invalid length
  3254. * if it's length is set to be smaller then the size of sctp_sack_chunk_t.
  3255. *
  3256. * We inform the other end by sending an ABORT with a Protocol Violation
  3257. * error code.
  3258. *
  3259. * Section: Not specified
  3260. * Verification Tag: Nothing to do
  3261. * Inputs
  3262. * (endpoint, asoc, chunk)
  3263. *
  3264. * Outputs
  3265. * (reply_msg, msg_up, counters)
  3266. *
  3267. * Generate an ABORT chunk and terminate the association.
  3268. */
  3269. static sctp_disposition_t sctp_sf_violation_chunklen(
  3270. const struct sctp_endpoint *ep,
  3271. const struct sctp_association *asoc,
  3272. const sctp_subtype_t type,
  3273. void *arg,
  3274. sctp_cmd_seq_t *commands)
  3275. {
  3276. struct sctp_chunk *chunk = arg;
  3277. struct sctp_chunk *abort = NULL;
  3278. char err_str[]="The following chunk had invalid length:";
  3279. /* Make the abort chunk. */
  3280. abort = sctp_make_abort_violation(asoc, chunk, err_str,
  3281. sizeof(err_str));
  3282. if (!abort)
  3283. goto nomem;
  3284. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  3285. SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
  3286. if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
  3287. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3288. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  3289. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  3290. SCTP_U32(SCTP_ERROR_PROTO_VIOLATION));
  3291. } else {
  3292. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  3293. SCTP_U32(SCTP_ERROR_PROTO_VIOLATION));
  3294. SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
  3295. }
  3296. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  3297. SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
  3298. return SCTP_DISPOSITION_ABORT;
  3299. nomem:
  3300. return SCTP_DISPOSITION_NOMEM;
  3301. }
  3302. /***************************************************************************
  3303. * These are the state functions for handling primitive (Section 10) events.
  3304. ***************************************************************************/
  3305. /*
  3306. * sctp_sf_do_prm_asoc
  3307. *
  3308. * Section: 10.1 ULP-to-SCTP
  3309. * B) Associate
  3310. *
  3311. * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
  3312. * outbound stream count)
  3313. * -> association id [,destination transport addr list] [,outbound stream
  3314. * count]
  3315. *
  3316. * This primitive allows the upper layer to initiate an association to a
  3317. * specific peer endpoint.
  3318. *
  3319. * The peer endpoint shall be specified by one of the transport addresses
  3320. * which defines the endpoint (see Section 1.4). If the local SCTP
  3321. * instance has not been initialized, the ASSOCIATE is considered an
  3322. * error.
  3323. * [This is not relevant for the kernel implementation since we do all
  3324. * initialization at boot time. It we hadn't initialized we wouldn't
  3325. * get anywhere near this code.]
  3326. *
  3327. * An association id, which is a local handle to the SCTP association,
  3328. * will be returned on successful establishment of the association. If
  3329. * SCTP is not able to open an SCTP association with the peer endpoint,
  3330. * an error is returned.
  3331. * [In the kernel implementation, the struct sctp_association needs to
  3332. * be created BEFORE causing this primitive to run.]
  3333. *
  3334. * Other association parameters may be returned, including the
  3335. * complete destination transport addresses of the peer as well as the
  3336. * outbound stream count of the local endpoint. One of the transport
  3337. * address from the returned destination addresses will be selected by
  3338. * the local endpoint as default primary path for sending SCTP packets
  3339. * to this peer. The returned "destination transport addr list" can
  3340. * be used by the ULP to change the default primary path or to force
  3341. * sending a packet to a specific transport address. [All of this
  3342. * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
  3343. * function.]
  3344. *
  3345. * Mandatory attributes:
  3346. *
  3347. * o local SCTP instance name - obtained from the INITIALIZE operation.
  3348. * [This is the argument asoc.]
  3349. * o destination transport addr - specified as one of the transport
  3350. * addresses of the peer endpoint with which the association is to be
  3351. * established.
  3352. * [This is asoc->peer.active_path.]
  3353. * o outbound stream count - the number of outbound streams the ULP
  3354. * would like to open towards this peer endpoint.
  3355. * [BUG: This is not currently implemented.]
  3356. * Optional attributes:
  3357. *
  3358. * None.
  3359. *
  3360. * The return value is a disposition.
  3361. */
  3362. sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
  3363. const struct sctp_association *asoc,
  3364. const sctp_subtype_t type,
  3365. void *arg,
  3366. sctp_cmd_seq_t *commands)
  3367. {
  3368. struct sctp_chunk *repl;
  3369. /* The comment below says that we enter COOKIE-WAIT AFTER
  3370. * sending the INIT, but that doesn't actually work in our
  3371. * implementation...
  3372. */
  3373. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  3374. SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
  3375. /* RFC 2960 5.1 Normal Establishment of an Association
  3376. *
  3377. * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
  3378. * must provide its Verification Tag (Tag_A) in the Initiate
  3379. * Tag field. Tag_A SHOULD be a random number in the range of
  3380. * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
  3381. */
  3382. repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
  3383. if (!repl)
  3384. goto nomem;
  3385. /* Cast away the const modifier, as we want to just
  3386. * rerun it through as a sideffect.
  3387. */
  3388. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC,
  3389. SCTP_ASOC((struct sctp_association *) asoc));
  3390. /* Choose transport for INIT. */
  3391. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
  3392. SCTP_CHUNK(repl));
  3393. /* After sending the INIT, "A" starts the T1-init timer and
  3394. * enters the COOKIE-WAIT state.
  3395. */
  3396. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  3397. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  3398. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  3399. return SCTP_DISPOSITION_CONSUME;
  3400. nomem:
  3401. return SCTP_DISPOSITION_NOMEM;
  3402. }
  3403. /*
  3404. * Process the SEND primitive.
  3405. *
  3406. * Section: 10.1 ULP-to-SCTP
  3407. * E) Send
  3408. *
  3409. * Format: SEND(association id, buffer address, byte count [,context]
  3410. * [,stream id] [,life time] [,destination transport address]
  3411. * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
  3412. * -> result
  3413. *
  3414. * This is the main method to send user data via SCTP.
  3415. *
  3416. * Mandatory attributes:
  3417. *
  3418. * o association id - local handle to the SCTP association
  3419. *
  3420. * o buffer address - the location where the user message to be
  3421. * transmitted is stored;
  3422. *
  3423. * o byte count - The size of the user data in number of bytes;
  3424. *
  3425. * Optional attributes:
  3426. *
  3427. * o context - an optional 32 bit integer that will be carried in the
  3428. * sending failure notification to the ULP if the transportation of
  3429. * this User Message fails.
  3430. *
  3431. * o stream id - to indicate which stream to send the data on. If not
  3432. * specified, stream 0 will be used.
  3433. *
  3434. * o life time - specifies the life time of the user data. The user data
  3435. * will not be sent by SCTP after the life time expires. This
  3436. * parameter can be used to avoid efforts to transmit stale
  3437. * user messages. SCTP notifies the ULP if the data cannot be
  3438. * initiated to transport (i.e. sent to the destination via SCTP's
  3439. * send primitive) within the life time variable. However, the
  3440. * user data will be transmitted if SCTP has attempted to transmit a
  3441. * chunk before the life time expired.
  3442. *
  3443. * o destination transport address - specified as one of the destination
  3444. * transport addresses of the peer endpoint to which this packet
  3445. * should be sent. Whenever possible, SCTP should use this destination
  3446. * transport address for sending the packets, instead of the current
  3447. * primary path.
  3448. *
  3449. * o unorder flag - this flag, if present, indicates that the user
  3450. * would like the data delivered in an unordered fashion to the peer
  3451. * (i.e., the U flag is set to 1 on all DATA chunks carrying this
  3452. * message).
  3453. *
  3454. * o no-bundle flag - instructs SCTP not to bundle this user data with
  3455. * other outbound DATA chunks. SCTP MAY still bundle even when
  3456. * this flag is present, when faced with network congestion.
  3457. *
  3458. * o payload protocol-id - A 32 bit unsigned integer that is to be
  3459. * passed to the peer indicating the type of payload protocol data
  3460. * being transmitted. This value is passed as opaque data by SCTP.
  3461. *
  3462. * The return value is the disposition.
  3463. */
  3464. sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
  3465. const struct sctp_association *asoc,
  3466. const sctp_subtype_t type,
  3467. void *arg,
  3468. sctp_cmd_seq_t *commands)
  3469. {
  3470. struct sctp_chunk *chunk = arg;
  3471. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
  3472. return SCTP_DISPOSITION_CONSUME;
  3473. }
  3474. /*
  3475. * Process the SHUTDOWN primitive.
  3476. *
  3477. * Section: 10.1:
  3478. * C) Shutdown
  3479. *
  3480. * Format: SHUTDOWN(association id)
  3481. * -> result
  3482. *
  3483. * Gracefully closes an association. Any locally queued user data
  3484. * will be delivered to the peer. The association will be terminated only
  3485. * after the peer acknowledges all the SCTP packets sent. A success code
  3486. * will be returned on successful termination of the association. If
  3487. * attempting to terminate the association results in a failure, an error
  3488. * code shall be returned.
  3489. *
  3490. * Mandatory attributes:
  3491. *
  3492. * o association id - local handle to the SCTP association
  3493. *
  3494. * Optional attributes:
  3495. *
  3496. * None.
  3497. *
  3498. * The return value is the disposition.
  3499. */
  3500. sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
  3501. const struct sctp_endpoint *ep,
  3502. const struct sctp_association *asoc,
  3503. const sctp_subtype_t type,
  3504. void *arg,
  3505. sctp_cmd_seq_t *commands)
  3506. {
  3507. int disposition;
  3508. /* From 9.2 Shutdown of an Association
  3509. * Upon receipt of the SHUTDOWN primitive from its upper
  3510. * layer, the endpoint enters SHUTDOWN-PENDING state and
  3511. * remains there until all outstanding data has been
  3512. * acknowledged by its peer. The endpoint accepts no new data
  3513. * from its upper layer, but retransmits data to the far end
  3514. * if necessary to fill gaps.
  3515. */
  3516. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  3517. SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
  3518. /* sctpimpguide-05 Section 2.12.2
  3519. * The sender of the SHUTDOWN MAY also start an overall guard timer
  3520. * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
  3521. */
  3522. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  3523. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  3524. disposition = SCTP_DISPOSITION_CONSUME;
  3525. if (sctp_outq_is_empty(&asoc->outqueue)) {
  3526. disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
  3527. arg, commands);
  3528. }
  3529. return disposition;
  3530. }
  3531. /*
  3532. * Process the ABORT primitive.
  3533. *
  3534. * Section: 10.1:
  3535. * C) Abort
  3536. *
  3537. * Format: Abort(association id [, cause code])
  3538. * -> result
  3539. *
  3540. * Ungracefully closes an association. Any locally queued user data
  3541. * will be discarded and an ABORT chunk is sent to the peer. A success code
  3542. * will be returned on successful abortion of the association. If
  3543. * attempting to abort the association results in a failure, an error
  3544. * code shall be returned.
  3545. *
  3546. * Mandatory attributes:
  3547. *
  3548. * o association id - local handle to the SCTP association
  3549. *
  3550. * Optional attributes:
  3551. *
  3552. * o cause code - reason of the abort to be passed to the peer
  3553. *
  3554. * None.
  3555. *
  3556. * The return value is the disposition.
  3557. */
  3558. sctp_disposition_t sctp_sf_do_9_1_prm_abort(
  3559. const struct sctp_endpoint *ep,
  3560. const struct sctp_association *asoc,
  3561. const sctp_subtype_t type,
  3562. void *arg,
  3563. sctp_cmd_seq_t *commands)
  3564. {
  3565. /* From 9.1 Abort of an Association
  3566. * Upon receipt of the ABORT primitive from its upper
  3567. * layer, the endpoint enters CLOSED state and
  3568. * discard all outstanding data has been
  3569. * acknowledged by its peer. The endpoint accepts no new data
  3570. * from its upper layer, but retransmits data to the far end
  3571. * if necessary to fill gaps.
  3572. */
  3573. struct msghdr *msg = arg;
  3574. struct sctp_chunk *abort;
  3575. sctp_disposition_t retval;
  3576. retval = SCTP_DISPOSITION_CONSUME;
  3577. /* Generate ABORT chunk to send the peer. */
  3578. abort = sctp_make_abort_user(asoc, NULL, msg);
  3579. if (!abort)
  3580. retval = SCTP_DISPOSITION_NOMEM;
  3581. else
  3582. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  3583. /* Even if we can't send the ABORT due to low memory delete the
  3584. * TCB. This is a departure from our typical NOMEM handling.
  3585. */
  3586. /* Delete the established association. */
  3587. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  3588. SCTP_U32(SCTP_ERROR_USER_ABORT));
  3589. SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
  3590. SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
  3591. return retval;
  3592. }
  3593. /* We tried an illegal operation on an association which is closed. */
  3594. sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
  3595. const struct sctp_association *asoc,
  3596. const sctp_subtype_t type,
  3597. void *arg,
  3598. sctp_cmd_seq_t *commands)
  3599. {
  3600. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
  3601. return SCTP_DISPOSITION_CONSUME;
  3602. }
  3603. /* We tried an illegal operation on an association which is shutting
  3604. * down.
  3605. */
  3606. sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
  3607. const struct sctp_association *asoc,
  3608. const sctp_subtype_t type,
  3609. void *arg,
  3610. sctp_cmd_seq_t *commands)
  3611. {
  3612. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
  3613. SCTP_ERROR(-ESHUTDOWN));
  3614. return SCTP_DISPOSITION_CONSUME;
  3615. }
  3616. /*
  3617. * sctp_cookie_wait_prm_shutdown
  3618. *
  3619. * Section: 4 Note: 2
  3620. * Verification Tag:
  3621. * Inputs
  3622. * (endpoint, asoc)
  3623. *
  3624. * The RFC does not explicitly address this issue, but is the route through the
  3625. * state table when someone issues a shutdown while in COOKIE_WAIT state.
  3626. *
  3627. * Outputs
  3628. * (timers)
  3629. */
  3630. sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
  3631. const struct sctp_endpoint *ep,
  3632. const struct sctp_association *asoc,
  3633. const sctp_subtype_t type,
  3634. void *arg,
  3635. sctp_cmd_seq_t *commands)
  3636. {
  3637. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3638. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  3639. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  3640. SCTP_STATE(SCTP_STATE_CLOSED));
  3641. SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
  3642. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  3643. return SCTP_DISPOSITION_DELETE_TCB;
  3644. }
  3645. /*
  3646. * sctp_cookie_echoed_prm_shutdown
  3647. *
  3648. * Section: 4 Note: 2
  3649. * Verification Tag:
  3650. * Inputs
  3651. * (endpoint, asoc)
  3652. *
  3653. * The RFC does not explcitly address this issue, but is the route through the
  3654. * state table when someone issues a shutdown while in COOKIE_ECHOED state.
  3655. *
  3656. * Outputs
  3657. * (timers)
  3658. */
  3659. sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
  3660. const struct sctp_endpoint *ep,
  3661. const struct sctp_association *asoc,
  3662. const sctp_subtype_t type,
  3663. void *arg, sctp_cmd_seq_t *commands)
  3664. {
  3665. /* There is a single T1 timer, so we should be able to use
  3666. * common function with the COOKIE-WAIT state.
  3667. */
  3668. return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
  3669. }
  3670. /*
  3671. * sctp_sf_cookie_wait_prm_abort
  3672. *
  3673. * Section: 4 Note: 2
  3674. * Verification Tag:
  3675. * Inputs
  3676. * (endpoint, asoc)
  3677. *
  3678. * The RFC does not explicitly address this issue, but is the route through the
  3679. * state table when someone issues an abort while in COOKIE_WAIT state.
  3680. *
  3681. * Outputs
  3682. * (timers)
  3683. */
  3684. sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
  3685. const struct sctp_endpoint *ep,
  3686. const struct sctp_association *asoc,
  3687. const sctp_subtype_t type,
  3688. void *arg,
  3689. sctp_cmd_seq_t *commands)
  3690. {
  3691. struct msghdr *msg = arg;
  3692. struct sctp_chunk *abort;
  3693. sctp_disposition_t retval;
  3694. /* Stop T1-init timer */
  3695. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3696. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  3697. retval = SCTP_DISPOSITION_CONSUME;
  3698. /* Generate ABORT chunk to send the peer */
  3699. abort = sctp_make_abort_user(asoc, NULL, msg);
  3700. if (!abort)
  3701. retval = SCTP_DISPOSITION_NOMEM;
  3702. else
  3703. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  3704. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  3705. SCTP_STATE(SCTP_STATE_CLOSED));
  3706. SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
  3707. /* Even if we can't send the ABORT due to low memory delete the
  3708. * TCB. This is a departure from our typical NOMEM handling.
  3709. */
  3710. /* Delete the established association. */
  3711. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  3712. SCTP_U32(SCTP_ERROR_USER_ABORT));
  3713. return retval;
  3714. }
  3715. /*
  3716. * sctp_sf_cookie_echoed_prm_abort
  3717. *
  3718. * Section: 4 Note: 3
  3719. * Verification Tag:
  3720. * Inputs
  3721. * (endpoint, asoc)
  3722. *
  3723. * The RFC does not explcitly address this issue, but is the route through the
  3724. * state table when someone issues an abort while in COOKIE_ECHOED state.
  3725. *
  3726. * Outputs
  3727. * (timers)
  3728. */
  3729. sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
  3730. const struct sctp_endpoint *ep,
  3731. const struct sctp_association *asoc,
  3732. const sctp_subtype_t type,
  3733. void *arg,
  3734. sctp_cmd_seq_t *commands)
  3735. {
  3736. /* There is a single T1 timer, so we should be able to use
  3737. * common function with the COOKIE-WAIT state.
  3738. */
  3739. return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
  3740. }
  3741. /*
  3742. * sctp_sf_shutdown_pending_prm_abort
  3743. *
  3744. * Inputs
  3745. * (endpoint, asoc)
  3746. *
  3747. * The RFC does not explicitly address this issue, but is the route through the
  3748. * state table when someone issues an abort while in SHUTDOWN-PENDING state.
  3749. *
  3750. * Outputs
  3751. * (timers)
  3752. */
  3753. sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
  3754. const struct sctp_endpoint *ep,
  3755. const struct sctp_association *asoc,
  3756. const sctp_subtype_t type,
  3757. void *arg,
  3758. sctp_cmd_seq_t *commands)
  3759. {
  3760. /* Stop the T5-shutdown guard timer. */
  3761. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3762. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  3763. return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
  3764. }
  3765. /*
  3766. * sctp_sf_shutdown_sent_prm_abort
  3767. *
  3768. * Inputs
  3769. * (endpoint, asoc)
  3770. *
  3771. * The RFC does not explicitly address this issue, but is the route through the
  3772. * state table when someone issues an abort while in SHUTDOWN-SENT state.
  3773. *
  3774. * Outputs
  3775. * (timers)
  3776. */
  3777. sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
  3778. const struct sctp_endpoint *ep,
  3779. const struct sctp_association *asoc,
  3780. const sctp_subtype_t type,
  3781. void *arg,
  3782. sctp_cmd_seq_t *commands)
  3783. {
  3784. /* Stop the T2-shutdown timer. */
  3785. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3786. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  3787. /* Stop the T5-shutdown guard timer. */
  3788. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3789. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  3790. return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
  3791. }
  3792. /*
  3793. * sctp_sf_cookie_echoed_prm_abort
  3794. *
  3795. * Inputs
  3796. * (endpoint, asoc)
  3797. *
  3798. * The RFC does not explcitly address this issue, but is the route through the
  3799. * state table when someone issues an abort while in COOKIE_ECHOED state.
  3800. *
  3801. * Outputs
  3802. * (timers)
  3803. */
  3804. sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
  3805. const struct sctp_endpoint *ep,
  3806. const struct sctp_association *asoc,
  3807. const sctp_subtype_t type,
  3808. void *arg,
  3809. sctp_cmd_seq_t *commands)
  3810. {
  3811. /* The same T2 timer, so we should be able to use
  3812. * common function with the SHUTDOWN-SENT state.
  3813. */
  3814. return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
  3815. }
  3816. /*
  3817. * Process the REQUESTHEARTBEAT primitive
  3818. *
  3819. * 10.1 ULP-to-SCTP
  3820. * J) Request Heartbeat
  3821. *
  3822. * Format: REQUESTHEARTBEAT(association id, destination transport address)
  3823. *
  3824. * -> result
  3825. *
  3826. * Instructs the local endpoint to perform a HeartBeat on the specified
  3827. * destination transport address of the given association. The returned
  3828. * result should indicate whether the transmission of the HEARTBEAT
  3829. * chunk to the destination address is successful.
  3830. *
  3831. * Mandatory attributes:
  3832. *
  3833. * o association id - local handle to the SCTP association
  3834. *
  3835. * o destination transport address - the transport address of the
  3836. * association on which a heartbeat should be issued.
  3837. */
  3838. sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
  3839. const struct sctp_endpoint *ep,
  3840. const struct sctp_association *asoc,
  3841. const sctp_subtype_t type,
  3842. void *arg,
  3843. sctp_cmd_seq_t *commands)
  3844. {
  3845. return sctp_sf_heartbeat(ep, asoc, type, (struct sctp_transport *)arg,
  3846. commands);
  3847. }
  3848. /*
  3849. * ADDIP Section 4.1 ASCONF Chunk Procedures
  3850. * When an endpoint has an ASCONF signaled change to be sent to the
  3851. * remote endpoint it should do A1 to A9
  3852. */
  3853. sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
  3854. const struct sctp_association *asoc,
  3855. const sctp_subtype_t type,
  3856. void *arg,
  3857. sctp_cmd_seq_t *commands)
  3858. {
  3859. struct sctp_chunk *chunk = arg;
  3860. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
  3861. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  3862. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  3863. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
  3864. return SCTP_DISPOSITION_CONSUME;
  3865. }
  3866. /*
  3867. * Ignore the primitive event
  3868. *
  3869. * The return value is the disposition of the primitive.
  3870. */
  3871. sctp_disposition_t sctp_sf_ignore_primitive(
  3872. const struct sctp_endpoint *ep,
  3873. const struct sctp_association *asoc,
  3874. const sctp_subtype_t type,
  3875. void *arg,
  3876. sctp_cmd_seq_t *commands)
  3877. {
  3878. SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
  3879. return SCTP_DISPOSITION_DISCARD;
  3880. }
  3881. /***************************************************************************
  3882. * These are the state functions for the OTHER events.
  3883. ***************************************************************************/
  3884. /*
  3885. * Start the shutdown negotiation.
  3886. *
  3887. * From Section 9.2:
  3888. * Once all its outstanding data has been acknowledged, the endpoint
  3889. * shall send a SHUTDOWN chunk to its peer including in the Cumulative
  3890. * TSN Ack field the last sequential TSN it has received from the peer.
  3891. * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
  3892. * state. If the timer expires, the endpoint must re-send the SHUTDOWN
  3893. * with the updated last sequential TSN received from its peer.
  3894. *
  3895. * The return value is the disposition.
  3896. */
  3897. sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
  3898. const struct sctp_endpoint *ep,
  3899. const struct sctp_association *asoc,
  3900. const sctp_subtype_t type,
  3901. void *arg,
  3902. sctp_cmd_seq_t *commands)
  3903. {
  3904. struct sctp_chunk *reply;
  3905. /* Once all its outstanding data has been acknowledged, the
  3906. * endpoint shall send a SHUTDOWN chunk to its peer including
  3907. * in the Cumulative TSN Ack field the last sequential TSN it
  3908. * has received from the peer.
  3909. */
  3910. reply = sctp_make_shutdown(asoc, NULL);
  3911. if (!reply)
  3912. goto nomem;
  3913. /* Set the transport for the SHUTDOWN chunk and the timeout for the
  3914. * T2-shutdown timer.
  3915. */
  3916. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  3917. /* It shall then start the T2-shutdown timer */
  3918. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  3919. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  3920. if (asoc->autoclose)
  3921. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3922. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  3923. /* and enter the SHUTDOWN-SENT state. */
  3924. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  3925. SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
  3926. /* sctp-implguide 2.10 Issues with Heartbeating and failover
  3927. *
  3928. * HEARTBEAT ... is discontinued after sending either SHUTDOWN
  3929. * or SHUTDOWN-ACK.
  3930. */
  3931. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
  3932. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  3933. return SCTP_DISPOSITION_CONSUME;
  3934. nomem:
  3935. return SCTP_DISPOSITION_NOMEM;
  3936. }
  3937. /*
  3938. * Generate a SHUTDOWN ACK now that everything is SACK'd.
  3939. *
  3940. * From Section 9.2:
  3941. *
  3942. * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
  3943. * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
  3944. * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
  3945. * endpoint must re-send the SHUTDOWN ACK.
  3946. *
  3947. * The return value is the disposition.
  3948. */
  3949. sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
  3950. const struct sctp_endpoint *ep,
  3951. const struct sctp_association *asoc,
  3952. const sctp_subtype_t type,
  3953. void *arg,
  3954. sctp_cmd_seq_t *commands)
  3955. {
  3956. struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
  3957. struct sctp_chunk *reply;
  3958. /* There are 2 ways of getting here:
  3959. * 1) called in response to a SHUTDOWN chunk
  3960. * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
  3961. *
  3962. * For the case (2), the arg parameter is set to NULL. We need
  3963. * to check that we have a chunk before accessing it's fields.
  3964. */
  3965. if (chunk) {
  3966. if (!sctp_vtag_verify(chunk, asoc))
  3967. return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
  3968. /* Make sure that the SHUTDOWN chunk has a valid length. */
  3969. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
  3970. return sctp_sf_violation_chunklen(ep, asoc, type, arg,
  3971. commands);
  3972. }
  3973. /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
  3974. * shall send a SHUTDOWN ACK ...
  3975. */
  3976. reply = sctp_make_shutdown_ack(asoc, chunk);
  3977. if (!reply)
  3978. goto nomem;
  3979. /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
  3980. * the T2-shutdown timer.
  3981. */
  3982. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  3983. /* and start/restart a T2-shutdown timer of its own, */
  3984. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  3985. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  3986. if (asoc->autoclose)
  3987. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3988. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  3989. /* Enter the SHUTDOWN-ACK-SENT state. */
  3990. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  3991. SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
  3992. /* sctp-implguide 2.10 Issues with Heartbeating and failover
  3993. *
  3994. * HEARTBEAT ... is discontinued after sending either SHUTDOWN
  3995. * or SHUTDOWN-ACK.
  3996. */
  3997. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
  3998. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  3999. return SCTP_DISPOSITION_CONSUME;
  4000. nomem:
  4001. return SCTP_DISPOSITION_NOMEM;
  4002. }
  4003. /*
  4004. * Ignore the event defined as other
  4005. *
  4006. * The return value is the disposition of the event.
  4007. */
  4008. sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
  4009. const struct sctp_association *asoc,
  4010. const sctp_subtype_t type,
  4011. void *arg,
  4012. sctp_cmd_seq_t *commands)
  4013. {
  4014. SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
  4015. return SCTP_DISPOSITION_DISCARD;
  4016. }
  4017. /************************************************************
  4018. * These are the state functions for handling timeout events.
  4019. ************************************************************/
  4020. /*
  4021. * RTX Timeout
  4022. *
  4023. * Section: 6.3.3 Handle T3-rtx Expiration
  4024. *
  4025. * Whenever the retransmission timer T3-rtx expires for a destination
  4026. * address, do the following:
  4027. * [See below]
  4028. *
  4029. * The return value is the disposition of the chunk.
  4030. */
  4031. sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
  4032. const struct sctp_association *asoc,
  4033. const sctp_subtype_t type,
  4034. void *arg,
  4035. sctp_cmd_seq_t *commands)
  4036. {
  4037. struct sctp_transport *transport = arg;
  4038. if (asoc->overall_error_count >= asoc->max_retrans) {
  4039. /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  4040. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4041. SCTP_U32(SCTP_ERROR_NO_ERROR));
  4042. SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
  4043. SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
  4044. return SCTP_DISPOSITION_DELETE_TCB;
  4045. }
  4046. /* E1) For the destination address for which the timer
  4047. * expires, adjust its ssthresh with rules defined in Section
  4048. * 7.2.3 and set the cwnd <- MTU.
  4049. */
  4050. /* E2) For the destination address for which the timer
  4051. * expires, set RTO <- RTO * 2 ("back off the timer"). The
  4052. * maximum value discussed in rule C7 above (RTO.max) may be
  4053. * used to provide an upper bound to this doubling operation.
  4054. */
  4055. /* E3) Determine how many of the earliest (i.e., lowest TSN)
  4056. * outstanding DATA chunks for the address for which the
  4057. * T3-rtx has expired will fit into a single packet, subject
  4058. * to the MTU constraint for the path corresponding to the
  4059. * destination transport address to which the retransmission
  4060. * is being sent (this may be different from the address for
  4061. * which the timer expires [see Section 6.4]). Call this
  4062. * value K. Bundle and retransmit those K DATA chunks in a
  4063. * single packet to the destination endpoint.
  4064. *
  4065. * Note: Any DATA chunks that were sent to the address for
  4066. * which the T3-rtx timer expired but did not fit in one MTU
  4067. * (rule E3 above), should be marked for retransmission and
  4068. * sent as soon as cwnd allows (normally when a SACK arrives).
  4069. */
  4070. /* NB: Rules E4 and F1 are implicit in R1. */
  4071. sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
  4072. /* Do some failure management (Section 8.2). */
  4073. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
  4074. return SCTP_DISPOSITION_CONSUME;
  4075. }
  4076. /*
  4077. * Generate delayed SACK on timeout
  4078. *
  4079. * Section: 6.2 Acknowledgement on Reception of DATA Chunks
  4080. *
  4081. * The guidelines on delayed acknowledgement algorithm specified in
  4082. * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
  4083. * acknowledgement SHOULD be generated for at least every second packet
  4084. * (not every second DATA chunk) received, and SHOULD be generated
  4085. * within 200 ms of the arrival of any unacknowledged DATA chunk. In
  4086. * some situations it may be beneficial for an SCTP transmitter to be
  4087. * more conservative than the algorithms detailed in this document
  4088. * allow. However, an SCTP transmitter MUST NOT be more aggressive than
  4089. * the following algorithms allow.
  4090. */
  4091. sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
  4092. const struct sctp_association *asoc,
  4093. const sctp_subtype_t type,
  4094. void *arg,
  4095. sctp_cmd_seq_t *commands)
  4096. {
  4097. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  4098. return SCTP_DISPOSITION_CONSUME;
  4099. }
  4100. /*
  4101. * sctp_sf_t1_init_timer_expire
  4102. *
  4103. * Section: 4 Note: 2
  4104. * Verification Tag:
  4105. * Inputs
  4106. * (endpoint, asoc)
  4107. *
  4108. * RFC 2960 Section 4 Notes
  4109. * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
  4110. * and re-start the T1-init timer without changing state. This MUST
  4111. * be repeated up to 'Max.Init.Retransmits' times. After that, the
  4112. * endpoint MUST abort the initialization process and report the
  4113. * error to SCTP user.
  4114. *
  4115. * Outputs
  4116. * (timers, events)
  4117. *
  4118. */
  4119. sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
  4120. const struct sctp_association *asoc,
  4121. const sctp_subtype_t type,
  4122. void *arg,
  4123. sctp_cmd_seq_t *commands)
  4124. {
  4125. struct sctp_chunk *repl = NULL;
  4126. struct sctp_bind_addr *bp;
  4127. int attempts = asoc->init_err_counter + 1;
  4128. SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
  4129. if (attempts <= asoc->max_init_attempts) {
  4130. bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
  4131. repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
  4132. if (!repl)
  4133. return SCTP_DISPOSITION_NOMEM;
  4134. /* Choose transport for INIT. */
  4135. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
  4136. SCTP_CHUNK(repl));
  4137. /* Issue a sideeffect to do the needed accounting. */
  4138. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
  4139. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  4140. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  4141. } else {
  4142. SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
  4143. " max_init_attempts: %d\n",
  4144. attempts, asoc->max_init_attempts);
  4145. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  4146. SCTP_U32(SCTP_ERROR_NO_ERROR));
  4147. return SCTP_DISPOSITION_DELETE_TCB;
  4148. }
  4149. return SCTP_DISPOSITION_CONSUME;
  4150. }
  4151. /*
  4152. * sctp_sf_t1_cookie_timer_expire
  4153. *
  4154. * Section: 4 Note: 2
  4155. * Verification Tag:
  4156. * Inputs
  4157. * (endpoint, asoc)
  4158. *
  4159. * RFC 2960 Section 4 Notes
  4160. * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
  4161. * COOKIE ECHO and re-start the T1-cookie timer without changing
  4162. * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
  4163. * After that, the endpoint MUST abort the initialization process and
  4164. * report the error to SCTP user.
  4165. *
  4166. * Outputs
  4167. * (timers, events)
  4168. *
  4169. */
  4170. sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
  4171. const struct sctp_association *asoc,
  4172. const sctp_subtype_t type,
  4173. void *arg,
  4174. sctp_cmd_seq_t *commands)
  4175. {
  4176. struct sctp_chunk *repl = NULL;
  4177. int attempts = asoc->init_err_counter + 1;
  4178. SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
  4179. if (attempts <= asoc->max_init_attempts) {
  4180. repl = sctp_make_cookie_echo(asoc, NULL);
  4181. if (!repl)
  4182. return SCTP_DISPOSITION_NOMEM;
  4183. /* Issue a sideeffect to do the needed accounting. */
  4184. sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
  4185. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  4186. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  4187. } else {
  4188. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  4189. SCTP_U32(SCTP_ERROR_NO_ERROR));
  4190. return SCTP_DISPOSITION_DELETE_TCB;
  4191. }
  4192. return SCTP_DISPOSITION_CONSUME;
  4193. }
  4194. /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
  4195. * with the updated last sequential TSN received from its peer.
  4196. *
  4197. * An endpoint should limit the number of retransmissions of the
  4198. * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
  4199. * If this threshold is exceeded the endpoint should destroy the TCB and
  4200. * MUST report the peer endpoint unreachable to the upper layer (and
  4201. * thus the association enters the CLOSED state). The reception of any
  4202. * packet from its peer (i.e. as the peer sends all of its queued DATA
  4203. * chunks) should clear the endpoint's retransmission count and restart
  4204. * the T2-Shutdown timer, giving its peer ample opportunity to transmit
  4205. * all of its queued DATA chunks that have not yet been sent.
  4206. */
  4207. sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
  4208. const struct sctp_association *asoc,
  4209. const sctp_subtype_t type,
  4210. void *arg,
  4211. sctp_cmd_seq_t *commands)
  4212. {
  4213. struct sctp_chunk *reply = NULL;
  4214. SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
  4215. if (asoc->overall_error_count >= asoc->max_retrans) {
  4216. /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  4217. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4218. SCTP_U32(SCTP_ERROR_NO_ERROR));
  4219. SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
  4220. SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
  4221. return SCTP_DISPOSITION_DELETE_TCB;
  4222. }
  4223. switch (asoc->state) {
  4224. case SCTP_STATE_SHUTDOWN_SENT:
  4225. reply = sctp_make_shutdown(asoc, NULL);
  4226. break;
  4227. case SCTP_STATE_SHUTDOWN_ACK_SENT:
  4228. reply = sctp_make_shutdown_ack(asoc, NULL);
  4229. break;
  4230. default:
  4231. BUG();
  4232. break;
  4233. };
  4234. if (!reply)
  4235. goto nomem;
  4236. /* Do some failure management (Section 8.2). */
  4237. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
  4238. SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
  4239. /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
  4240. * the T2-shutdown timer.
  4241. */
  4242. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  4243. /* Restart the T2-shutdown timer. */
  4244. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  4245. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  4246. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  4247. return SCTP_DISPOSITION_CONSUME;
  4248. nomem:
  4249. return SCTP_DISPOSITION_NOMEM;
  4250. }
  4251. /*
  4252. * ADDIP Section 4.1 ASCONF CHunk Procedures
  4253. * If the T4 RTO timer expires the endpoint should do B1 to B5
  4254. */
  4255. sctp_disposition_t sctp_sf_t4_timer_expire(
  4256. const struct sctp_endpoint *ep,
  4257. const struct sctp_association *asoc,
  4258. const sctp_subtype_t type,
  4259. void *arg,
  4260. sctp_cmd_seq_t *commands)
  4261. {
  4262. struct sctp_chunk *chunk = asoc->addip_last_asconf;
  4263. struct sctp_transport *transport = chunk->transport;
  4264. /* ADDIP 4.1 B1) Increment the error counters and perform path failure
  4265. * detection on the appropriate destination address as defined in
  4266. * RFC2960 [5] section 8.1 and 8.2.
  4267. */
  4268. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
  4269. /* Reconfig T4 timer and transport. */
  4270. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
  4271. /* ADDIP 4.1 B2) Increment the association error counters and perform
  4272. * endpoint failure detection on the association as defined in
  4273. * RFC2960 [5] section 8.1 and 8.2.
  4274. * association error counter is incremented in SCTP_CMD_STRIKE.
  4275. */
  4276. if (asoc->overall_error_count >= asoc->max_retrans) {
  4277. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4278. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  4279. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4280. SCTP_U32(SCTP_ERROR_NO_ERROR));
  4281. SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
  4282. SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
  4283. return SCTP_DISPOSITION_ABORT;
  4284. }
  4285. /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
  4286. * the ASCONF chunk was sent by doubling the RTO timer value.
  4287. * This is done in SCTP_CMD_STRIKE.
  4288. */
  4289. /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
  4290. * choose an alternate destination address (please refer to RFC2960
  4291. * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
  4292. * chunk, it MUST be the same (including its serial number) as the last
  4293. * ASCONF sent.
  4294. */
  4295. sctp_chunk_hold(asoc->addip_last_asconf);
  4296. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  4297. SCTP_CHUNK(asoc->addip_last_asconf));
  4298. /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
  4299. * destination is selected, then the RTO used will be that of the new
  4300. * destination address.
  4301. */
  4302. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  4303. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  4304. return SCTP_DISPOSITION_CONSUME;
  4305. }
  4306. /* sctpimpguide-05 Section 2.12.2
  4307. * The sender of the SHUTDOWN MAY also start an overall guard timer
  4308. * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
  4309. * At the expiration of this timer the sender SHOULD abort the association
  4310. * by sending an ABORT chunk.
  4311. */
  4312. sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
  4313. const struct sctp_association *asoc,
  4314. const sctp_subtype_t type,
  4315. void *arg,
  4316. sctp_cmd_seq_t *commands)
  4317. {
  4318. struct sctp_chunk *reply = NULL;
  4319. SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
  4320. reply = sctp_make_abort(asoc, NULL, 0);
  4321. if (!reply)
  4322. goto nomem;
  4323. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  4324. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4325. SCTP_U32(SCTP_ERROR_NO_ERROR));
  4326. return SCTP_DISPOSITION_DELETE_TCB;
  4327. nomem:
  4328. return SCTP_DISPOSITION_NOMEM;
  4329. }
  4330. /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
  4331. * the association is automatically closed by starting the shutdown process.
  4332. * The work that needs to be done is same as when SHUTDOWN is initiated by
  4333. * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
  4334. */
  4335. sctp_disposition_t sctp_sf_autoclose_timer_expire(
  4336. const struct sctp_endpoint *ep,
  4337. const struct sctp_association *asoc,
  4338. const sctp_subtype_t type,
  4339. void *arg,
  4340. sctp_cmd_seq_t *commands)
  4341. {
  4342. int disposition;
  4343. /* From 9.2 Shutdown of an Association
  4344. * Upon receipt of the SHUTDOWN primitive from its upper
  4345. * layer, the endpoint enters SHUTDOWN-PENDING state and
  4346. * remains there until all outstanding data has been
  4347. * acknowledged by its peer. The endpoint accepts no new data
  4348. * from its upper layer, but retransmits data to the far end
  4349. * if necessary to fill gaps.
  4350. */
  4351. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4352. SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
  4353. /* sctpimpguide-05 Section 2.12.2
  4354. * The sender of the SHUTDOWN MAY also start an overall guard timer
  4355. * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
  4356. */
  4357. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  4358. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  4359. disposition = SCTP_DISPOSITION_CONSUME;
  4360. if (sctp_outq_is_empty(&asoc->outqueue)) {
  4361. disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
  4362. arg, commands);
  4363. }
  4364. return disposition;
  4365. }
  4366. /*****************************************************************************
  4367. * These are sa state functions which could apply to all types of events.
  4368. ****************************************************************************/
  4369. /*
  4370. * This table entry is not implemented.
  4371. *
  4372. * Inputs
  4373. * (endpoint, asoc, chunk)
  4374. *
  4375. * The return value is the disposition of the chunk.
  4376. */
  4377. sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
  4378. const struct sctp_association *asoc,
  4379. const sctp_subtype_t type,
  4380. void *arg,
  4381. sctp_cmd_seq_t *commands)
  4382. {
  4383. return SCTP_DISPOSITION_NOT_IMPL;
  4384. }
  4385. /*
  4386. * This table entry represents a bug.
  4387. *
  4388. * Inputs
  4389. * (endpoint, asoc, chunk)
  4390. *
  4391. * The return value is the disposition of the chunk.
  4392. */
  4393. sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
  4394. const struct sctp_association *asoc,
  4395. const sctp_subtype_t type,
  4396. void *arg,
  4397. sctp_cmd_seq_t *commands)
  4398. {
  4399. return SCTP_DISPOSITION_BUG;
  4400. }
  4401. /*
  4402. * This table entry represents the firing of a timer in the wrong state.
  4403. * Since timer deletion cannot be guaranteed a timer 'may' end up firing
  4404. * when the association is in the wrong state. This event should
  4405. * be ignored, so as to prevent any rearming of the timer.
  4406. *
  4407. * Inputs
  4408. * (endpoint, asoc, chunk)
  4409. *
  4410. * The return value is the disposition of the chunk.
  4411. */
  4412. sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
  4413. const struct sctp_association *asoc,
  4414. const sctp_subtype_t type,
  4415. void *arg,
  4416. sctp_cmd_seq_t *commands)
  4417. {
  4418. SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
  4419. return SCTP_DISPOSITION_CONSUME;
  4420. }
  4421. /********************************************************************
  4422. * 2nd Level Abstractions
  4423. ********************************************************************/
  4424. /* Pull the SACK chunk based on the SACK header. */
  4425. static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
  4426. {
  4427. struct sctp_sackhdr *sack;
  4428. unsigned int len;
  4429. __u16 num_blocks;
  4430. __u16 num_dup_tsns;
  4431. /* Protect ourselves from reading too far into
  4432. * the skb from a bogus sender.
  4433. */
  4434. sack = (struct sctp_sackhdr *) chunk->skb->data;
  4435. num_blocks = ntohs(sack->num_gap_ack_blocks);
  4436. num_dup_tsns = ntohs(sack->num_dup_tsns);
  4437. len = sizeof(struct sctp_sackhdr);
  4438. len += (num_blocks + num_dup_tsns) * sizeof(__u32);
  4439. if (len > chunk->skb->len)
  4440. return NULL;
  4441. skb_pull(chunk->skb, len);
  4442. return sack;
  4443. }
  4444. /* Create an ABORT packet to be sent as a response, with the specified
  4445. * error causes.
  4446. */
  4447. static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
  4448. const struct sctp_association *asoc,
  4449. struct sctp_chunk *chunk,
  4450. const void *payload,
  4451. size_t paylen)
  4452. {
  4453. struct sctp_packet *packet;
  4454. struct sctp_chunk *abort;
  4455. packet = sctp_ootb_pkt_new(asoc, chunk);
  4456. if (packet) {
  4457. /* Make an ABORT.
  4458. * The T bit will be set if the asoc is NULL.
  4459. */
  4460. abort = sctp_make_abort(asoc, chunk, paylen);
  4461. if (!abort) {
  4462. sctp_ootb_pkt_free(packet);
  4463. return NULL;
  4464. }
  4465. /* Reflect vtag if T-Bit is set */
  4466. if (sctp_test_T_bit(abort))
  4467. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  4468. /* Add specified error causes, i.e., payload, to the
  4469. * end of the chunk.
  4470. */
  4471. sctp_addto_chunk(abort, paylen, payload);
  4472. /* Set the skb to the belonging sock for accounting. */
  4473. abort->skb->sk = ep->base.sk;
  4474. sctp_packet_append_chunk(packet, abort);
  4475. }
  4476. return packet;
  4477. }
  4478. /* Allocate a packet for responding in the OOTB conditions. */
  4479. static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
  4480. const struct sctp_chunk *chunk)
  4481. {
  4482. struct sctp_packet *packet;
  4483. struct sctp_transport *transport;
  4484. __u16 sport;
  4485. __u16 dport;
  4486. __u32 vtag;
  4487. /* Get the source and destination port from the inbound packet. */
  4488. sport = ntohs(chunk->sctp_hdr->dest);
  4489. dport = ntohs(chunk->sctp_hdr->source);
  4490. /* The V-tag is going to be the same as the inbound packet if no
  4491. * association exists, otherwise, use the peer's vtag.
  4492. */
  4493. if (asoc) {
  4494. vtag = asoc->peer.i.init_tag;
  4495. } else {
  4496. /* Special case the INIT and stale COOKIE_ECHO as there is no
  4497. * vtag yet.
  4498. */
  4499. switch(chunk->chunk_hdr->type) {
  4500. case SCTP_CID_INIT:
  4501. {
  4502. sctp_init_chunk_t *init;
  4503. init = (sctp_init_chunk_t *)chunk->chunk_hdr;
  4504. vtag = ntohl(init->init_hdr.init_tag);
  4505. break;
  4506. }
  4507. default:
  4508. vtag = ntohl(chunk->sctp_hdr->vtag);
  4509. break;
  4510. }
  4511. }
  4512. /* Make a transport for the bucket, Eliza... */
  4513. transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
  4514. if (!transport)
  4515. goto nomem;
  4516. /* Cache a route for the transport with the chunk's destination as
  4517. * the source address.
  4518. */
  4519. sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
  4520. sctp_sk(sctp_get_ctl_sock()));
  4521. packet = sctp_packet_init(&transport->packet, transport, sport, dport);
  4522. packet = sctp_packet_config(packet, vtag, 0);
  4523. return packet;
  4524. nomem:
  4525. return NULL;
  4526. }
  4527. /* Free the packet allocated earlier for responding in the OOTB condition. */
  4528. void sctp_ootb_pkt_free(struct sctp_packet *packet)
  4529. {
  4530. sctp_transport_free(packet->transport);
  4531. }
  4532. /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
  4533. static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
  4534. const struct sctp_association *asoc,
  4535. const struct sctp_chunk *chunk,
  4536. sctp_cmd_seq_t *commands,
  4537. struct sctp_chunk *err_chunk)
  4538. {
  4539. struct sctp_packet *packet;
  4540. if (err_chunk) {
  4541. packet = sctp_ootb_pkt_new(asoc, chunk);
  4542. if (packet) {
  4543. struct sctp_signed_cookie *cookie;
  4544. /* Override the OOTB vtag from the cookie. */
  4545. cookie = chunk->subh.cookie_hdr;
  4546. packet->vtag = cookie->c.peer_vtag;
  4547. /* Set the skb to the belonging sock for accounting. */
  4548. err_chunk->skb->sk = ep->base.sk;
  4549. sctp_packet_append_chunk(packet, err_chunk);
  4550. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  4551. SCTP_PACKET(packet));
  4552. SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
  4553. } else
  4554. sctp_chunk_free (err_chunk);
  4555. }
  4556. }
  4557. /* Process a data chunk */
  4558. static int sctp_eat_data(const struct sctp_association *asoc,
  4559. struct sctp_chunk *chunk,
  4560. sctp_cmd_seq_t *commands)
  4561. {
  4562. sctp_datahdr_t *data_hdr;
  4563. struct sctp_chunk *err;
  4564. size_t datalen;
  4565. sctp_verb_t deliver;
  4566. int tmp;
  4567. __u32 tsn;
  4568. int account_value;
  4569. struct sock *sk = asoc->base.sk;
  4570. data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
  4571. skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
  4572. tsn = ntohl(data_hdr->tsn);
  4573. SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
  4574. /* ASSERT: Now skb->data is really the user data. */
  4575. /*
  4576. * if we are established, and we have used up our receive
  4577. * buffer memory, drop the frame
  4578. */
  4579. if (asoc->state == SCTP_STATE_ESTABLISHED) {
  4580. /*
  4581. * If the receive buffer policy is 1, then each
  4582. * association can allocate up to sk_rcvbuf bytes
  4583. * otherwise, all the associations in aggregate
  4584. * may allocate up to sk_rcvbuf bytes
  4585. */
  4586. if (asoc->ep->rcvbuf_policy)
  4587. account_value = atomic_read(&asoc->rmem_alloc);
  4588. else
  4589. account_value = atomic_read(&sk->sk_rmem_alloc);
  4590. if (account_value > sk->sk_rcvbuf)
  4591. return SCTP_IERROR_IGNORE_TSN;
  4592. }
  4593. /* Process ECN based congestion.
  4594. *
  4595. * Since the chunk structure is reused for all chunks within
  4596. * a packet, we use ecn_ce_done to track if we've already
  4597. * done CE processing for this packet.
  4598. *
  4599. * We need to do ECN processing even if we plan to discard the
  4600. * chunk later.
  4601. */
  4602. if (!chunk->ecn_ce_done) {
  4603. struct sctp_af *af;
  4604. chunk->ecn_ce_done = 1;
  4605. af = sctp_get_af_specific(
  4606. ipver2af(chunk->skb->nh.iph->version));
  4607. if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
  4608. /* Do real work as sideffect. */
  4609. sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
  4610. SCTP_U32(tsn));
  4611. }
  4612. }
  4613. tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
  4614. if (tmp < 0) {
  4615. /* The TSN is too high--silently discard the chunk and
  4616. * count on it getting retransmitted later.
  4617. */
  4618. return SCTP_IERROR_HIGH_TSN;
  4619. } else if (tmp > 0) {
  4620. /* This is a duplicate. Record it. */
  4621. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
  4622. return SCTP_IERROR_DUP_TSN;
  4623. }
  4624. /* This is a new TSN. */
  4625. /* Discard if there is no room in the receive window.
  4626. * Actually, allow a little bit of overflow (up to a MTU).
  4627. */
  4628. datalen = ntohs(chunk->chunk_hdr->length);
  4629. datalen -= sizeof(sctp_data_chunk_t);
  4630. deliver = SCTP_CMD_CHUNK_ULP;
  4631. /* Think about partial delivery. */
  4632. if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
  4633. /* Even if we don't accept this chunk there is
  4634. * memory pressure.
  4635. */
  4636. sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
  4637. }
  4638. /* Spill over rwnd a little bit. Note: While allowed, this spill over
  4639. * seems a bit troublesome in that frag_point varies based on
  4640. * PMTU. In cases, such as loopback, this might be a rather
  4641. * large spill over.
  4642. */
  4643. if (!asoc->rwnd || asoc->rwnd_over ||
  4644. (datalen > asoc->rwnd + asoc->frag_point)) {
  4645. /* If this is the next TSN, consider reneging to make
  4646. * room. Note: Playing nice with a confused sender. A
  4647. * malicious sender can still eat up all our buffer
  4648. * space and in the future we may want to detect and
  4649. * do more drastic reneging.
  4650. */
  4651. if (sctp_tsnmap_has_gap(&asoc->peer.tsn_map) &&
  4652. (sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1) == tsn) {
  4653. SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
  4654. deliver = SCTP_CMD_RENEGE;
  4655. } else {
  4656. SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
  4657. "rwnd: %d\n", tsn, datalen,
  4658. asoc->rwnd);
  4659. return SCTP_IERROR_IGNORE_TSN;
  4660. }
  4661. }
  4662. /*
  4663. * Section 3.3.10.9 No User Data (9)
  4664. *
  4665. * Cause of error
  4666. * ---------------
  4667. * No User Data: This error cause is returned to the originator of a
  4668. * DATA chunk if a received DATA chunk has no user data.
  4669. */
  4670. if (unlikely(0 == datalen)) {
  4671. err = sctp_make_abort_no_data(asoc, chunk, tsn);
  4672. if (err) {
  4673. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  4674. SCTP_CHUNK(err));
  4675. }
  4676. /* We are going to ABORT, so we might as well stop
  4677. * processing the rest of the chunks in the packet.
  4678. */
  4679. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
  4680. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4681. SCTP_U32(SCTP_ERROR_NO_DATA));
  4682. SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
  4683. SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
  4684. return SCTP_IERROR_NO_DATA;
  4685. }
  4686. /* If definately accepting the DATA chunk, record its TSN, otherwise
  4687. * wait for renege processing.
  4688. */
  4689. if (SCTP_CMD_CHUNK_ULP == deliver)
  4690. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
  4691. /* Note: Some chunks may get overcounted (if we drop) or overcounted
  4692. * if we renege and the chunk arrives again.
  4693. */
  4694. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
  4695. SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
  4696. else
  4697. SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
  4698. /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
  4699. *
  4700. * If an endpoint receive a DATA chunk with an invalid stream
  4701. * identifier, it shall acknowledge the reception of the DATA chunk
  4702. * following the normal procedure, immediately send an ERROR chunk
  4703. * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
  4704. * and discard the DATA chunk.
  4705. */
  4706. if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
  4707. err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
  4708. &data_hdr->stream,
  4709. sizeof(data_hdr->stream));
  4710. if (err)
  4711. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  4712. SCTP_CHUNK(err));
  4713. return SCTP_IERROR_BAD_STREAM;
  4714. }
  4715. /* Send the data up to the user. Note: Schedule the
  4716. * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
  4717. * chunk needs the updated rwnd.
  4718. */
  4719. sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
  4720. return SCTP_IERROR_NO_ERROR;
  4721. }