sm_statefuns.c 191 KB

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