sm_statefuns.c 195 KB

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