sm_statefuns.c 201 KB

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