sm_statefuns.c 197 KB

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