sm_statefuns.c 190 KB

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