sm_statefuns.c 197 KB

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