sm_statefuns.c 185 KB

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