sm_statefuns.c 197 KB

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