sm_statefuns.c 204 KB

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