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