sm_statefuns.c 197 KB

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