sm_statefuns.c 197 KB

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