sm_statefuns.c 200 KB

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