sm_statefuns.c 185 KB

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