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