sm_statefuns.c 189 KB

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