sm_statefuns.c 193 KB

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