sm_statefuns.c 205 KB

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