sm_statefuns.c 193 KB

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