sm_statefuns.c 197 KB

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