sm_statefuns.c 193 KB

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