sm_statefuns.c 196 KB

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