ngtcp2_conn.c 404 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "ngtcp2_conn.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "ngtcp2_macro.h"
  29. #include "ngtcp2_log.h"
  30. #include "ngtcp2_cid.h"
  31. #include "ngtcp2_conv.h"
  32. #include "ngtcp2_vec.h"
  33. #include "ngtcp2_addr.h"
  34. #include "ngtcp2_path.h"
  35. #include "ngtcp2_rcvry.h"
  36. #include "ngtcp2_unreachable.h"
  37. #include "ngtcp2_net.h"
  38. #include "ngtcp2_transport_params.h"
  39. #include "ngtcp2_settings.h"
  40. #include "ngtcp2_tstamp.h"
  41. #include "ngtcp2_frame_chain.h"
  42. /* NGTCP2_FLOW_WINDOW_RTT_FACTOR is the factor of RTT when flow
  43. control window auto-tuning is triggered. */
  44. #define NGTCP2_FLOW_WINDOW_RTT_FACTOR 2
  45. /* NGTCP2_FLOW_WINDOW_SCALING_FACTOR is the growth factor of flow
  46. control window. */
  47. #define NGTCP2_FLOW_WINDOW_SCALING_FACTOR 2
  48. /* NGTCP2_MIN_COALESCED_PAYLOADLEN is the minimum length of QUIC
  49. packet payload that should be coalesced to a long packet. */
  50. #define NGTCP2_MIN_COALESCED_PAYLOADLEN 128
  51. /* NGTCP2_MAX_ACK_PER_PKT is the maximum number of ACK frame per an
  52. incoming QUIC packet to process. ACK frames that exceed this limit
  53. are not processed. */
  54. #define NGTCP2_MAX_ACK_PER_PKT 1
  55. ngtcp2_objalloc_def(strm, ngtcp2_strm, oplent)
  56. /*
  57. * conn_local_stream returns nonzero if |stream_id| indicates that it
  58. * is the stream initiated by local endpoint.
  59. */
  60. static int conn_local_stream(ngtcp2_conn *conn, int64_t stream_id) {
  61. return (uint8_t)(stream_id & 1) == conn->server;
  62. }
  63. /*
  64. * bidi_stream returns nonzero if |stream_id| is a bidirectional
  65. * stream ID.
  66. */
  67. static int bidi_stream(int64_t stream_id) { return (stream_id & 0x2) == 0; }
  68. static void conn_update_timestamp(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  69. assert(conn->log.last_ts <= ts);
  70. assert(conn->qlog.last_ts <= ts);
  71. conn->log.last_ts = ts;
  72. conn->qlog.last_ts = ts;
  73. }
  74. /*
  75. * conn_is_tls_handshake_completed returns nonzero if TLS handshake
  76. * has completed and 1 RTT keys are available.
  77. */
  78. static int conn_is_tls_handshake_completed(ngtcp2_conn *conn) {
  79. return (conn->flags & NGTCP2_CONN_FLAG_TLS_HANDSHAKE_COMPLETED) &&
  80. conn->pktns.crypto.rx.ckm && conn->pktns.crypto.tx.ckm;
  81. }
  82. static int conn_call_recv_client_initial(ngtcp2_conn *conn,
  83. const ngtcp2_cid *dcid) {
  84. int rv;
  85. assert(conn->callbacks.recv_client_initial);
  86. rv = conn->callbacks.recv_client_initial(conn, dcid, conn->user_data);
  87. if (rv != 0) {
  88. return NGTCP2_ERR_CALLBACK_FAILURE;
  89. }
  90. return 0;
  91. }
  92. static int conn_call_handshake_completed(ngtcp2_conn *conn) {
  93. int rv;
  94. if (!conn->callbacks.handshake_completed) {
  95. return 0;
  96. }
  97. rv = conn->callbacks.handshake_completed(conn, conn->user_data);
  98. if (rv != 0) {
  99. return NGTCP2_ERR_CALLBACK_FAILURE;
  100. }
  101. return 0;
  102. }
  103. static int conn_call_recv_stream_data(ngtcp2_conn *conn, ngtcp2_strm *strm,
  104. uint32_t flags, uint64_t offset,
  105. const uint8_t *data, size_t datalen) {
  106. int rv;
  107. if (!conn->callbacks.recv_stream_data) {
  108. return 0;
  109. }
  110. rv = conn->callbacks.recv_stream_data(conn, flags, strm->stream_id, offset,
  111. data, datalen, conn->user_data,
  112. strm->stream_user_data);
  113. if (rv != 0) {
  114. return NGTCP2_ERR_CALLBACK_FAILURE;
  115. }
  116. return 0;
  117. }
  118. static int conn_call_recv_crypto_data(ngtcp2_conn *conn,
  119. ngtcp2_encryption_level encryption_level,
  120. uint64_t offset, const uint8_t *data,
  121. size_t datalen) {
  122. int rv;
  123. assert(conn->callbacks.recv_crypto_data);
  124. rv = conn->callbacks.recv_crypto_data(conn, encryption_level, offset, data,
  125. datalen, conn->user_data);
  126. switch (rv) {
  127. case 0:
  128. case NGTCP2_ERR_CRYPTO:
  129. case NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM:
  130. case NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM:
  131. case NGTCP2_ERR_TRANSPORT_PARAM:
  132. case NGTCP2_ERR_PROTO:
  133. case NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE:
  134. case NGTCP2_ERR_NOMEM:
  135. case NGTCP2_ERR_CALLBACK_FAILURE:
  136. return rv;
  137. default:
  138. return NGTCP2_ERR_CALLBACK_FAILURE;
  139. }
  140. }
  141. static int conn_call_stream_open(ngtcp2_conn *conn, ngtcp2_strm *strm) {
  142. int rv;
  143. if (!conn->callbacks.stream_open) {
  144. return 0;
  145. }
  146. rv = conn->callbacks.stream_open(conn, strm->stream_id, conn->user_data);
  147. if (rv != 0) {
  148. return NGTCP2_ERR_CALLBACK_FAILURE;
  149. }
  150. return 0;
  151. }
  152. static int conn_call_stream_close(ngtcp2_conn *conn, ngtcp2_strm *strm) {
  153. int rv;
  154. uint32_t flags = NGTCP2_STREAM_CLOSE_FLAG_NONE;
  155. if (!conn->callbacks.stream_close) {
  156. return 0;
  157. }
  158. if (strm->flags & NGTCP2_STRM_FLAG_APP_ERROR_CODE_SET) {
  159. flags |= NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET;
  160. }
  161. rv = conn->callbacks.stream_close(conn, flags, strm->stream_id,
  162. strm->app_error_code, conn->user_data,
  163. strm->stream_user_data);
  164. if (rv != 0) {
  165. return NGTCP2_ERR_CALLBACK_FAILURE;
  166. }
  167. return 0;
  168. }
  169. static int conn_call_stream_reset(ngtcp2_conn *conn, int64_t stream_id,
  170. uint64_t final_size, uint64_t app_error_code,
  171. void *stream_user_data) {
  172. int rv;
  173. if (!conn->callbacks.stream_reset) {
  174. return 0;
  175. }
  176. rv = conn->callbacks.stream_reset(conn, stream_id, final_size, app_error_code,
  177. conn->user_data, stream_user_data);
  178. if (rv != 0) {
  179. return NGTCP2_ERR_CALLBACK_FAILURE;
  180. }
  181. return 0;
  182. }
  183. static int conn_call_extend_max_local_streams_bidi(ngtcp2_conn *conn,
  184. uint64_t max_streams) {
  185. int rv;
  186. if (!conn->callbacks.extend_max_local_streams_bidi) {
  187. return 0;
  188. }
  189. rv = conn->callbacks.extend_max_local_streams_bidi(conn, max_streams,
  190. conn->user_data);
  191. if (rv != 0) {
  192. return NGTCP2_ERR_CALLBACK_FAILURE;
  193. }
  194. return 0;
  195. }
  196. static int conn_call_extend_max_local_streams_uni(ngtcp2_conn *conn,
  197. uint64_t max_streams) {
  198. int rv;
  199. if (!conn->callbacks.extend_max_local_streams_uni) {
  200. return 0;
  201. }
  202. rv = conn->callbacks.extend_max_local_streams_uni(conn, max_streams,
  203. conn->user_data);
  204. if (rv != 0) {
  205. return NGTCP2_ERR_CALLBACK_FAILURE;
  206. }
  207. return 0;
  208. }
  209. static int conn_call_get_new_connection_id(ngtcp2_conn *conn, ngtcp2_cid *cid,
  210. uint8_t *token, size_t cidlen) {
  211. int rv;
  212. assert(conn->callbacks.get_new_connection_id);
  213. rv = conn->callbacks.get_new_connection_id(conn, cid, token, cidlen,
  214. conn->user_data);
  215. if (rv != 0) {
  216. return NGTCP2_ERR_CALLBACK_FAILURE;
  217. }
  218. return 0;
  219. }
  220. static int conn_call_remove_connection_id(ngtcp2_conn *conn,
  221. const ngtcp2_cid *cid) {
  222. int rv;
  223. if (!conn->callbacks.remove_connection_id) {
  224. return 0;
  225. }
  226. rv = conn->callbacks.remove_connection_id(conn, cid, conn->user_data);
  227. if (rv != 0) {
  228. return NGTCP2_ERR_CALLBACK_FAILURE;
  229. }
  230. return 0;
  231. }
  232. static int conn_call_path_validation(ngtcp2_conn *conn, const ngtcp2_pv *pv,
  233. ngtcp2_path_validation_result res) {
  234. int rv;
  235. uint32_t flags = NGTCP2_PATH_VALIDATION_FLAG_NONE;
  236. const ngtcp2_path *old_path = NULL;
  237. if (!conn->callbacks.path_validation) {
  238. return 0;
  239. }
  240. if (pv->flags & NGTCP2_PV_FLAG_PREFERRED_ADDR) {
  241. flags |= NGTCP2_PATH_VALIDATION_FLAG_PREFERRED_ADDR;
  242. }
  243. if (pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) {
  244. old_path = &pv->fallback_dcid.ps.path;
  245. }
  246. if (conn->server && old_path &&
  247. (ngtcp2_addr_compare(&pv->dcid.ps.path.remote, &old_path->remote) &
  248. (NGTCP2_ADDR_COMPARE_FLAG_ADDR | NGTCP2_ADDR_COMPARE_FLAG_FAMILY))) {
  249. flags |= NGTCP2_PATH_VALIDATION_FLAG_NEW_TOKEN;
  250. }
  251. rv = conn->callbacks.path_validation(conn, flags, &pv->dcid.ps.path, old_path,
  252. res, conn->user_data);
  253. if (rv != 0) {
  254. return NGTCP2_ERR_CALLBACK_FAILURE;
  255. }
  256. return 0;
  257. }
  258. static int conn_call_select_preferred_addr(ngtcp2_conn *conn,
  259. ngtcp2_path *dest) {
  260. int rv;
  261. if (!conn->callbacks.select_preferred_addr) {
  262. return 0;
  263. }
  264. assert(conn->remote.transport_params);
  265. assert(conn->remote.transport_params->preferred_addr_present);
  266. rv = conn->callbacks.select_preferred_addr(
  267. conn, dest, &conn->remote.transport_params->preferred_addr,
  268. conn->user_data);
  269. if (rv != 0) {
  270. return NGTCP2_ERR_CALLBACK_FAILURE;
  271. }
  272. return 0;
  273. }
  274. static int conn_call_extend_max_remote_streams_bidi(ngtcp2_conn *conn,
  275. uint64_t max_streams) {
  276. int rv;
  277. if (!conn->callbacks.extend_max_remote_streams_bidi) {
  278. return 0;
  279. }
  280. rv = conn->callbacks.extend_max_remote_streams_bidi(conn, max_streams,
  281. conn->user_data);
  282. if (rv != 0) {
  283. return NGTCP2_ERR_CALLBACK_FAILURE;
  284. }
  285. return 0;
  286. }
  287. static int conn_call_extend_max_remote_streams_uni(ngtcp2_conn *conn,
  288. uint64_t max_streams) {
  289. int rv;
  290. if (!conn->callbacks.extend_max_remote_streams_uni) {
  291. return 0;
  292. }
  293. rv = conn->callbacks.extend_max_remote_streams_uni(conn, max_streams,
  294. conn->user_data);
  295. if (rv != 0) {
  296. return NGTCP2_ERR_CALLBACK_FAILURE;
  297. }
  298. return 0;
  299. }
  300. static int conn_call_extend_max_stream_data(ngtcp2_conn *conn,
  301. ngtcp2_strm *strm,
  302. int64_t stream_id,
  303. uint64_t datalen) {
  304. int rv;
  305. if (!conn->callbacks.extend_max_stream_data) {
  306. return 0;
  307. }
  308. rv = conn->callbacks.extend_max_stream_data(
  309. conn, stream_id, datalen, conn->user_data, strm->stream_user_data);
  310. if (rv != 0) {
  311. return NGTCP2_ERR_CALLBACK_FAILURE;
  312. }
  313. return 0;
  314. }
  315. static int conn_call_dcid_status(ngtcp2_conn *conn,
  316. ngtcp2_connection_id_status_type type,
  317. const ngtcp2_dcid *dcid) {
  318. int rv;
  319. if (!conn->callbacks.dcid_status) {
  320. return 0;
  321. }
  322. rv = conn->callbacks.dcid_status(
  323. conn, type, dcid->seq, &dcid->cid,
  324. (dcid->flags & NGTCP2_DCID_FLAG_TOKEN_PRESENT) ? dcid->token : NULL,
  325. conn->user_data);
  326. if (rv != 0) {
  327. return NGTCP2_ERR_CALLBACK_FAILURE;
  328. }
  329. return 0;
  330. }
  331. static int conn_call_activate_dcid(ngtcp2_conn *conn, const ngtcp2_dcid *dcid) {
  332. return conn_call_dcid_status(conn, NGTCP2_CONNECTION_ID_STATUS_TYPE_ACTIVATE,
  333. dcid);
  334. }
  335. static int conn_call_deactivate_dcid(ngtcp2_conn *conn,
  336. const ngtcp2_dcid *dcid) {
  337. return conn_call_dcid_status(
  338. conn, NGTCP2_CONNECTION_ID_STATUS_TYPE_DEACTIVATE, dcid);
  339. }
  340. static int conn_call_stream_stop_sending(ngtcp2_conn *conn, int64_t stream_id,
  341. uint64_t app_error_code,
  342. void *stream_user_data) {
  343. int rv;
  344. if (!conn->callbacks.stream_stop_sending) {
  345. return 0;
  346. }
  347. rv = conn->callbacks.stream_stop_sending(conn, stream_id, app_error_code,
  348. conn->user_data, stream_user_data);
  349. if (rv != 0) {
  350. return NGTCP2_ERR_CALLBACK_FAILURE;
  351. }
  352. return 0;
  353. }
  354. static void conn_call_delete_crypto_aead_ctx(ngtcp2_conn *conn,
  355. ngtcp2_crypto_aead_ctx *aead_ctx) {
  356. if (!aead_ctx->native_handle) {
  357. return;
  358. }
  359. assert(conn->callbacks.delete_crypto_aead_ctx);
  360. conn->callbacks.delete_crypto_aead_ctx(conn, aead_ctx, conn->user_data);
  361. }
  362. static void
  363. conn_call_delete_crypto_cipher_ctx(ngtcp2_conn *conn,
  364. ngtcp2_crypto_cipher_ctx *cipher_ctx) {
  365. if (!cipher_ctx->native_handle) {
  366. return;
  367. }
  368. assert(conn->callbacks.delete_crypto_cipher_ctx);
  369. conn->callbacks.delete_crypto_cipher_ctx(conn, cipher_ctx, conn->user_data);
  370. }
  371. static int conn_call_client_initial(ngtcp2_conn *conn) {
  372. int rv;
  373. assert(conn->callbacks.client_initial);
  374. rv = conn->callbacks.client_initial(conn, conn->user_data);
  375. if (rv != 0) {
  376. return NGTCP2_ERR_CALLBACK_FAILURE;
  377. }
  378. return 0;
  379. }
  380. static int conn_call_get_path_challenge_data(ngtcp2_conn *conn, uint8_t *data) {
  381. int rv;
  382. assert(conn->callbacks.get_path_challenge_data);
  383. rv = conn->callbacks.get_path_challenge_data(conn, data, conn->user_data);
  384. if (rv != 0) {
  385. return NGTCP2_ERR_CALLBACK_FAILURE;
  386. }
  387. return 0;
  388. }
  389. static int conn_call_recv_version_negotiation(ngtcp2_conn *conn,
  390. const ngtcp2_pkt_hd *hd,
  391. const uint32_t *sv, size_t nsv) {
  392. int rv;
  393. if (!conn->callbacks.recv_version_negotiation) {
  394. return 0;
  395. }
  396. rv = conn->callbacks.recv_version_negotiation(conn, hd, sv, nsv,
  397. conn->user_data);
  398. if (rv != 0) {
  399. return NGTCP2_ERR_CALLBACK_FAILURE;
  400. }
  401. return 0;
  402. }
  403. static int conn_call_recv_retry(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd) {
  404. int rv;
  405. assert(conn->callbacks.recv_retry);
  406. rv = conn->callbacks.recv_retry(conn, hd, conn->user_data);
  407. if (rv != 0) {
  408. return NGTCP2_ERR_CALLBACK_FAILURE;
  409. }
  410. return 0;
  411. }
  412. static int
  413. conn_call_recv_stateless_reset(ngtcp2_conn *conn,
  414. const ngtcp2_pkt_stateless_reset *sr) {
  415. int rv;
  416. if (!conn->callbacks.recv_stateless_reset) {
  417. return 0;
  418. }
  419. rv = conn->callbacks.recv_stateless_reset(conn, sr, conn->user_data);
  420. if (rv != 0) {
  421. return NGTCP2_ERR_CALLBACK_FAILURE;
  422. }
  423. return 0;
  424. }
  425. static int conn_call_recv_new_token(ngtcp2_conn *conn, const uint8_t *token,
  426. size_t tokenlen) {
  427. int rv;
  428. if (!conn->callbacks.recv_new_token) {
  429. return 0;
  430. }
  431. rv = conn->callbacks.recv_new_token(conn, token, tokenlen, conn->user_data);
  432. if (rv != 0) {
  433. return NGTCP2_ERR_CALLBACK_FAILURE;
  434. }
  435. return 0;
  436. }
  437. static int conn_call_handshake_confirmed(ngtcp2_conn *conn) {
  438. int rv;
  439. if (!conn->callbacks.handshake_confirmed) {
  440. return 0;
  441. }
  442. rv = conn->callbacks.handshake_confirmed(conn, conn->user_data);
  443. if (rv != 0) {
  444. return NGTCP2_ERR_CALLBACK_FAILURE;
  445. }
  446. return 0;
  447. }
  448. static int conn_call_recv_datagram(ngtcp2_conn *conn,
  449. const ngtcp2_datagram *fr) {
  450. const uint8_t *data;
  451. size_t datalen;
  452. int rv;
  453. uint32_t flags = NGTCP2_DATAGRAM_FLAG_NONE;
  454. if (!conn->callbacks.recv_datagram) {
  455. return 0;
  456. }
  457. if (fr->datacnt) {
  458. assert(fr->datacnt == 1);
  459. data = fr->data->base;
  460. datalen = fr->data->len;
  461. } else {
  462. data = NULL;
  463. datalen = 0;
  464. }
  465. if (!conn_is_tls_handshake_completed(conn)) {
  466. flags |= NGTCP2_DATAGRAM_FLAG_0RTT;
  467. }
  468. rv =
  469. conn->callbacks.recv_datagram(conn, flags, data, datalen, conn->user_data);
  470. if (rv != 0) {
  471. return NGTCP2_ERR_CALLBACK_FAILURE;
  472. }
  473. return 0;
  474. }
  475. static int
  476. conn_call_update_key(ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
  477. ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_iv,
  478. ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_iv,
  479. const uint8_t *current_rx_secret,
  480. const uint8_t *current_tx_secret, size_t secretlen) {
  481. int rv;
  482. assert(conn->callbacks.update_key);
  483. rv = conn->callbacks.update_key(
  484. conn, rx_secret, tx_secret, rx_aead_ctx, rx_iv, tx_aead_ctx, tx_iv,
  485. current_rx_secret, current_tx_secret, secretlen, conn->user_data);
  486. if (rv != 0) {
  487. return NGTCP2_ERR_CALLBACK_FAILURE;
  488. }
  489. return 0;
  490. }
  491. static int conn_call_version_negotiation(ngtcp2_conn *conn, uint32_t version,
  492. const ngtcp2_cid *dcid) {
  493. int rv;
  494. assert(conn->callbacks.version_negotiation);
  495. rv =
  496. conn->callbacks.version_negotiation(conn, version, dcid, conn->user_data);
  497. if (rv != 0) {
  498. return NGTCP2_ERR_CALLBACK_FAILURE;
  499. }
  500. return 0;
  501. }
  502. static int conn_call_recv_rx_key(ngtcp2_conn *conn,
  503. ngtcp2_encryption_level level) {
  504. int rv;
  505. if (!conn->callbacks.recv_rx_key) {
  506. return 0;
  507. }
  508. rv = conn->callbacks.recv_rx_key(conn, level, conn->user_data);
  509. if (rv != 0) {
  510. return NGTCP2_ERR_CALLBACK_FAILURE;
  511. }
  512. return 0;
  513. }
  514. static int conn_call_recv_tx_key(ngtcp2_conn *conn,
  515. ngtcp2_encryption_level level) {
  516. int rv;
  517. if (!conn->callbacks.recv_tx_key) {
  518. return 0;
  519. }
  520. rv = conn->callbacks.recv_tx_key(conn, level, conn->user_data);
  521. if (rv != 0) {
  522. return NGTCP2_ERR_CALLBACK_FAILURE;
  523. }
  524. return 0;
  525. }
  526. static void pktns_init(ngtcp2_pktns *pktns, ngtcp2_pktns_id pktns_id,
  527. ngtcp2_rst *rst, ngtcp2_cc *cc, int64_t initial_pkt_num,
  528. ngtcp2_log *log, ngtcp2_qlog *qlog,
  529. ngtcp2_objalloc *rtb_entry_objalloc,
  530. ngtcp2_objalloc *frc_objalloc, const ngtcp2_mem *mem) {
  531. memset(pktns, 0, sizeof(*pktns));
  532. ngtcp2_gaptr_init(&pktns->rx.pngap, mem);
  533. pktns->tx.last_pkt_num = initial_pkt_num - 1;
  534. pktns->tx.non_ack_pkt_start_ts = UINT64_MAX;
  535. pktns->rx.max_pkt_num = -1;
  536. pktns->rx.max_ack_eliciting_pkt_num = -1;
  537. pktns->id = pktns_id;
  538. ngtcp2_acktr_init(&pktns->acktr, log, mem);
  539. ngtcp2_strm_init(&pktns->crypto.strm, 0, NGTCP2_STRM_FLAG_NONE, 0, 0, NULL,
  540. frc_objalloc, mem);
  541. ngtcp2_rtb_init(&pktns->rtb, rst, cc, initial_pkt_num, log, qlog,
  542. rtb_entry_objalloc, frc_objalloc, mem);
  543. }
  544. static int pktns_new(ngtcp2_pktns **ppktns, ngtcp2_pktns_id pktns_id,
  545. ngtcp2_rst *rst, ngtcp2_cc *cc, int64_t initial_pkt_num,
  546. ngtcp2_log *log, ngtcp2_qlog *qlog,
  547. ngtcp2_objalloc *rtb_entry_objalloc,
  548. ngtcp2_objalloc *frc_objalloc, const ngtcp2_mem *mem) {
  549. *ppktns = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_pktns));
  550. if (*ppktns == NULL) {
  551. return NGTCP2_ERR_NOMEM;
  552. }
  553. pktns_init(*ppktns, pktns_id, rst, cc, initial_pkt_num, log, qlog,
  554. rtb_entry_objalloc, frc_objalloc, mem);
  555. return 0;
  556. }
  557. static int cycle_less(const ngtcp2_pq_entry *lhs, const ngtcp2_pq_entry *rhs) {
  558. ngtcp2_strm *ls = ngtcp2_struct_of(lhs, ngtcp2_strm, pe);
  559. ngtcp2_strm *rs = ngtcp2_struct_of(rhs, ngtcp2_strm, pe);
  560. if (ls->cycle == rs->cycle) {
  561. return ls->stream_id < rs->stream_id;
  562. }
  563. return rs->cycle - ls->cycle <= 1;
  564. }
  565. static void delete_buffed_pkts(ngtcp2_pkt_chain *pc, const ngtcp2_mem *mem) {
  566. ngtcp2_pkt_chain *next;
  567. for (; pc;) {
  568. next = pc->next;
  569. ngtcp2_pkt_chain_del(pc, mem);
  570. pc = next;
  571. }
  572. }
  573. static void delete_buf_chain(ngtcp2_buf_chain *bufchain,
  574. const ngtcp2_mem *mem) {
  575. ngtcp2_buf_chain *next;
  576. for (; bufchain;) {
  577. next = bufchain->next;
  578. ngtcp2_buf_chain_del(bufchain, mem);
  579. bufchain = next;
  580. }
  581. }
  582. static void pktns_free(ngtcp2_pktns *pktns, const ngtcp2_mem *mem) {
  583. delete_buf_chain(pktns->crypto.tx.data, mem);
  584. delete_buffed_pkts(pktns->rx.buffed_pkts, mem);
  585. ngtcp2_frame_chain_list_objalloc_del(pktns->tx.frq, pktns->rtb.frc_objalloc,
  586. mem);
  587. ngtcp2_crypto_km_del(pktns->crypto.rx.ckm, mem);
  588. ngtcp2_crypto_km_del(pktns->crypto.tx.ckm, mem);
  589. ngtcp2_rtb_free(&pktns->rtb);
  590. ngtcp2_strm_free(&pktns->crypto.strm);
  591. ngtcp2_acktr_free(&pktns->acktr);
  592. ngtcp2_gaptr_free(&pktns->rx.pngap);
  593. }
  594. static void pktns_del(ngtcp2_pktns *pktns, const ngtcp2_mem *mem) {
  595. if (pktns == NULL) {
  596. return;
  597. }
  598. pktns_free(pktns, mem);
  599. ngtcp2_mem_free(mem, pktns);
  600. }
  601. static int cid_less(const ngtcp2_ksl_key *lhs, const ngtcp2_ksl_key *rhs) {
  602. return ngtcp2_cid_less(lhs, rhs);
  603. }
  604. ngtcp2_ksl_search_def(cid_less, cid_less)
  605. static int retired_ts_less(const ngtcp2_pq_entry *lhs,
  606. const ngtcp2_pq_entry *rhs) {
  607. const ngtcp2_scid *a = ngtcp2_struct_of(lhs, ngtcp2_scid, pe);
  608. const ngtcp2_scid *b = ngtcp2_struct_of(rhs, ngtcp2_scid, pe);
  609. return a->retired_ts < b->retired_ts;
  610. }
  611. /*
  612. * conn_reset_conn_stat_cc resets congestion state in |cstat|.
  613. */
  614. static void conn_reset_conn_stat_cc(ngtcp2_conn *conn,
  615. ngtcp2_conn_stat *cstat) {
  616. cstat->latest_rtt = 0;
  617. cstat->min_rtt = UINT64_MAX;
  618. cstat->smoothed_rtt = conn->local.settings.initial_rtt;
  619. cstat->rttvar = conn->local.settings.initial_rtt / 2;
  620. cstat->first_rtt_sample_ts = UINT64_MAX;
  621. cstat->pto_count = 0;
  622. cstat->loss_detection_timer = UINT64_MAX;
  623. cstat->max_tx_udp_payload_size =
  624. ngtcp2_conn_get_path_max_tx_udp_payload_size(conn);
  625. cstat->cwnd = ngtcp2_cc_compute_initcwnd(cstat->max_tx_udp_payload_size);
  626. cstat->ssthresh = UINT64_MAX;
  627. cstat->congestion_recovery_start_ts = UINT64_MAX;
  628. cstat->bytes_in_flight = 0;
  629. cstat->delivery_rate_sec = 0;
  630. cstat->pacing_interval = 0;
  631. cstat->send_quantum = 64 * 1024;
  632. }
  633. /*
  634. * reset_conn_stat_recovery resets the fields related to the recovery
  635. * function
  636. */
  637. static void reset_conn_stat_recovery(ngtcp2_conn_stat *cstat) {
  638. /* Initializes them with UINT64_MAX. */
  639. memset(cstat->loss_time, 0xff, sizeof(cstat->loss_time));
  640. memset(cstat->last_tx_pkt_ts, 0xff, sizeof(cstat->last_tx_pkt_ts));
  641. }
  642. /*
  643. * conn_reset_conn_stat resets |cstat|. The following fields are not
  644. * reset: initial_rtt and max_udp_payload_size.
  645. */
  646. static void conn_reset_conn_stat(ngtcp2_conn *conn, ngtcp2_conn_stat *cstat) {
  647. conn_reset_conn_stat_cc(conn, cstat);
  648. reset_conn_stat_recovery(cstat);
  649. }
  650. static void delete_scid(ngtcp2_ksl *scids, const ngtcp2_mem *mem) {
  651. ngtcp2_ksl_it it;
  652. for (it = ngtcp2_ksl_begin(scids); !ngtcp2_ksl_it_end(&it);
  653. ngtcp2_ksl_it_next(&it)) {
  654. ngtcp2_mem_free(mem, ngtcp2_ksl_it_get(&it));
  655. }
  656. }
  657. /*
  658. * compute_pto computes PTO.
  659. */
  660. static ngtcp2_duration compute_pto(ngtcp2_duration smoothed_rtt,
  661. ngtcp2_duration rttvar,
  662. ngtcp2_duration max_ack_delay) {
  663. ngtcp2_duration var = ngtcp2_max_uint64(4 * rttvar, NGTCP2_GRANULARITY);
  664. return smoothed_rtt + var + max_ack_delay;
  665. }
  666. /*
  667. * conn_compute_initial_pto computes PTO using the initial RTT.
  668. */
  669. static ngtcp2_duration conn_compute_initial_pto(ngtcp2_conn *conn,
  670. ngtcp2_pktns *pktns) {
  671. ngtcp2_duration initial_rtt = conn->local.settings.initial_rtt;
  672. ngtcp2_duration max_ack_delay;
  673. if (pktns->id == NGTCP2_PKTNS_ID_APPLICATION &&
  674. conn->remote.transport_params) {
  675. max_ack_delay = conn->remote.transport_params->max_ack_delay;
  676. } else {
  677. max_ack_delay = 0;
  678. }
  679. return compute_pto(initial_rtt, initial_rtt / 2, max_ack_delay);
  680. }
  681. /*
  682. * conn_compute_pto computes the current PTO.
  683. */
  684. static ngtcp2_duration conn_compute_pto(ngtcp2_conn *conn,
  685. ngtcp2_pktns *pktns) {
  686. ngtcp2_conn_stat *cstat = &conn->cstat;
  687. ngtcp2_duration max_ack_delay;
  688. if (pktns->id == NGTCP2_PKTNS_ID_APPLICATION &&
  689. conn->remote.transport_params) {
  690. max_ack_delay = conn->remote.transport_params->max_ack_delay;
  691. } else {
  692. max_ack_delay = 0;
  693. }
  694. return compute_pto(cstat->smoothed_rtt, cstat->rttvar, max_ack_delay);
  695. }
  696. ngtcp2_duration ngtcp2_conn_compute_pto(ngtcp2_conn *conn,
  697. ngtcp2_pktns *pktns) {
  698. return conn_compute_pto(conn, pktns);
  699. }
  700. /*
  701. * conn_compute_pv_timeout_pto returns path validation timeout using
  702. * the given |pto|.
  703. */
  704. static ngtcp2_duration conn_compute_pv_timeout_pto(ngtcp2_conn *conn,
  705. ngtcp2_duration pto) {
  706. ngtcp2_duration initial_pto = conn_compute_initial_pto(conn, &conn->pktns);
  707. return 3 * ngtcp2_max_uint64(pto, initial_pto);
  708. }
  709. /*
  710. * conn_compute_pv_timeout returns path validation timeout.
  711. */
  712. static ngtcp2_duration conn_compute_pv_timeout(ngtcp2_conn *conn) {
  713. return conn_compute_pv_timeout_pto(conn,
  714. conn_compute_pto(conn, &conn->pktns));
  715. }
  716. static void conn_handle_tx_ecn(ngtcp2_conn *conn, ngtcp2_pkt_info *pi,
  717. uint16_t *prtb_entry_flags, ngtcp2_pktns *pktns,
  718. const ngtcp2_pkt_hd *hd, ngtcp2_tstamp ts) {
  719. assert(pi);
  720. if (pi->ecn != NGTCP2_ECN_NOT_ECT) {
  721. /* We have already made a transition of validation state and
  722. deceided to send UDP datagram with ECN bit set. Coalesced QUIC
  723. packets also bear ECN bits set. */
  724. if (pktns->tx.ecn.start_pkt_num == INT64_MAX) {
  725. pktns->tx.ecn.start_pkt_num = hd->pkt_num;
  726. }
  727. ++pktns->tx.ecn.validation_pkt_sent;
  728. if (prtb_entry_flags) {
  729. *prtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ECN;
  730. }
  731. ++pktns->tx.ecn.ect0;
  732. return;
  733. }
  734. switch (conn->tx.ecn.state) {
  735. case NGTCP2_ECN_STATE_TESTING:
  736. if (conn->tx.ecn.validation_start_ts == UINT64_MAX) {
  737. assert(0 == pktns->tx.ecn.validation_pkt_sent);
  738. assert(0 == pktns->tx.ecn.validation_pkt_lost);
  739. conn->tx.ecn.validation_start_ts = ts;
  740. } else if (ts - conn->tx.ecn.validation_start_ts >=
  741. 3 * conn_compute_pto(conn, pktns)) {
  742. conn->tx.ecn.state = NGTCP2_ECN_STATE_UNKNOWN;
  743. break;
  744. }
  745. if (pktns->tx.ecn.start_pkt_num == INT64_MAX) {
  746. pktns->tx.ecn.start_pkt_num = hd->pkt_num;
  747. }
  748. ++pktns->tx.ecn.validation_pkt_sent;
  749. if (++conn->tx.ecn.dgram_sent == NGTCP2_ECN_MAX_NUM_VALIDATION_PKTS) {
  750. conn->tx.ecn.state = NGTCP2_ECN_STATE_UNKNOWN;
  751. }
  752. /* fall through */
  753. case NGTCP2_ECN_STATE_CAPABLE:
  754. /* pi is provided per UDP datagram. */
  755. assert(NGTCP2_ECN_NOT_ECT == pi->ecn);
  756. pi->ecn = NGTCP2_ECN_ECT_0;
  757. if (prtb_entry_flags) {
  758. *prtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ECN;
  759. }
  760. ++pktns->tx.ecn.ect0;
  761. break;
  762. case NGTCP2_ECN_STATE_UNKNOWN:
  763. case NGTCP2_ECN_STATE_FAILED:
  764. break;
  765. default:
  766. ngtcp2_unreachable();
  767. }
  768. }
  769. static void conn_reset_ecn_validation_state(ngtcp2_conn *conn) {
  770. ngtcp2_pktns *in_pktns = conn->in_pktns;
  771. ngtcp2_pktns *hs_pktns = conn->hs_pktns;
  772. ngtcp2_pktns *pktns = &conn->pktns;
  773. conn->tx.ecn.state = NGTCP2_ECN_STATE_TESTING;
  774. conn->tx.ecn.validation_start_ts = UINT64_MAX;
  775. conn->tx.ecn.dgram_sent = 0;
  776. if (in_pktns) {
  777. in_pktns->tx.ecn.start_pkt_num = INT64_MAX;
  778. in_pktns->tx.ecn.validation_pkt_sent = 0;
  779. in_pktns->tx.ecn.validation_pkt_lost = 0;
  780. }
  781. if (hs_pktns) {
  782. hs_pktns->tx.ecn.start_pkt_num = INT64_MAX;
  783. hs_pktns->tx.ecn.validation_pkt_sent = 0;
  784. hs_pktns->tx.ecn.validation_pkt_lost = 0;
  785. }
  786. pktns->tx.ecn.start_pkt_num = INT64_MAX;
  787. pktns->tx.ecn.validation_pkt_sent = 0;
  788. pktns->tx.ecn.validation_pkt_lost = 0;
  789. }
  790. /* server_default_available_versions is the default available_versions
  791. field sent by server. */
  792. static uint8_t server_default_available_versions[] = {0, 0, 0, 1};
  793. /*
  794. * available_versions_init writes |versions| of length |versionslen|
  795. * in network byte order to the buffer pointed by |buf|, suitable for
  796. * sending in available_versions field of version_information QUIC
  797. * transport parameter. This function returns the pointer to the one
  798. * beyond the last byte written.
  799. */
  800. static void *available_versions_init(void *buf, const uint32_t *versions,
  801. size_t versionslen) {
  802. size_t i;
  803. for (i = 0; i < versionslen; ++i) {
  804. buf = ngtcp2_put_uint32be(buf, versions[i]);
  805. }
  806. return 0;
  807. }
  808. static void
  809. conn_set_local_transport_params(ngtcp2_conn *conn,
  810. const ngtcp2_transport_params *params) {
  811. ngtcp2_transport_params *p = &conn->local.transport_params;
  812. uint32_t chosen_version = p->version_info.chosen_version;
  813. *p = *params;
  814. if (conn->server) {
  815. p->version_info.chosen_version = chosen_version;
  816. } else {
  817. p->version_info.chosen_version = conn->client_chosen_version;
  818. }
  819. p->version_info.available_versions = conn->vneg.available_versions;
  820. p->version_info.available_versionslen = conn->vneg.available_versionslen;
  821. p->version_info_present = 1;
  822. }
  823. static size_t buflen_align(size_t buflen) {
  824. return (buflen + 0x7) & (size_t)~0x7;
  825. }
  826. static void *buf_align(void *buf) {
  827. return (void *)((uintptr_t)((uint8_t *)buf + 0x7) & (uintptr_t)~0x7);
  828. }
  829. static void *buf_advance(void *buf, size_t n) { return (uint8_t *)buf + n; }
  830. static int conn_new(ngtcp2_conn **pconn, const ngtcp2_cid *dcid,
  831. const ngtcp2_cid *scid, const ngtcp2_path *path,
  832. uint32_t client_chosen_version, int callbacks_version,
  833. const ngtcp2_callbacks *callbacks, int settings_version,
  834. const ngtcp2_settings *settings,
  835. int transport_params_version,
  836. const ngtcp2_transport_params *params,
  837. const ngtcp2_mem *mem, void *user_data, int server) {
  838. int rv;
  839. ngtcp2_scid *scident;
  840. void *buf, *tokenbuf;
  841. size_t buflen;
  842. uint8_t fixed_bit_byte;
  843. size_t i;
  844. uint32_t *preferred_versions;
  845. ngtcp2_settings settingsbuf;
  846. ngtcp2_transport_params paramsbuf;
  847. (void)callbacks_version;
  848. (void)settings_version;
  849. settings =
  850. ngtcp2_settings_convert_to_latest(&settingsbuf, settings_version, settings);
  851. params = ngtcp2_transport_params_convert_to_latest(
  852. &paramsbuf, transport_params_version, params);
  853. assert(settings->max_window <= NGTCP2_MAX_VARINT);
  854. assert(settings->max_stream_window <= NGTCP2_MAX_VARINT);
  855. assert(settings->max_tx_udp_payload_size);
  856. assert(settings->max_tx_udp_payload_size <= NGTCP2_HARD_MAX_UDP_PAYLOAD_SIZE);
  857. assert(settings->initial_pkt_num <= INT32_MAX);
  858. assert(params->active_connection_id_limit >=
  859. NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT);
  860. assert(params->active_connection_id_limit <= NGTCP2_MAX_DCID_POOL_SIZE);
  861. assert(params->initial_max_data <= NGTCP2_MAX_VARINT);
  862. assert(params->initial_max_stream_data_bidi_local <= NGTCP2_MAX_VARINT);
  863. assert(params->initial_max_stream_data_bidi_remote <= NGTCP2_MAX_VARINT);
  864. assert(params->initial_max_stream_data_uni <= NGTCP2_MAX_VARINT);
  865. assert((server && params->original_dcid_present) ||
  866. (!server && !params->original_dcid_present));
  867. assert(!params->initial_scid_present);
  868. assert(server || !params->stateless_reset_token_present);
  869. assert(server || !params->preferred_addr_present);
  870. assert(server || !params->retry_scid_present);
  871. assert(params->max_idle_timeout != UINT64_MAX);
  872. assert(params->max_ack_delay < (1 << 14) * NGTCP2_MILLISECONDS);
  873. assert(server || callbacks->client_initial);
  874. assert(!server || callbacks->recv_client_initial);
  875. assert(callbacks->recv_crypto_data);
  876. assert(callbacks->encrypt);
  877. assert(callbacks->decrypt);
  878. assert(callbacks->hp_mask);
  879. assert(server || callbacks->recv_retry);
  880. assert(callbacks->rand);
  881. assert(callbacks->get_new_connection_id);
  882. assert(callbacks->update_key);
  883. assert(callbacks->delete_crypto_aead_ctx);
  884. assert(callbacks->delete_crypto_cipher_ctx);
  885. assert(callbacks->get_path_challenge_data);
  886. assert(!server || !ngtcp2_is_reserved_version(client_chosen_version));
  887. for (i = 0; i < settings->pmtud_probeslen; ++i) {
  888. assert(settings->pmtud_probes[i] > NGTCP2_MAX_UDP_PAYLOAD_SIZE);
  889. }
  890. if (mem == NULL) {
  891. mem = ngtcp2_mem_default();
  892. }
  893. buflen = sizeof(ngtcp2_conn);
  894. if (settings->qlog_write) {
  895. buflen = buflen_align(buflen);
  896. buflen += NGTCP2_QLOG_BUFLEN;
  897. }
  898. if (settings->pmtud_probeslen) {
  899. buflen = buflen_align(buflen);
  900. buflen += sizeof(settings->pmtud_probes[0]) * settings->pmtud_probeslen;
  901. }
  902. if (settings->preferred_versionslen) {
  903. buflen = buflen_align(buflen);
  904. buflen +=
  905. sizeof(settings->preferred_versions[0]) * settings->preferred_versionslen;
  906. }
  907. if (settings->available_versionslen) {
  908. buflen = buflen_align(buflen);
  909. buflen +=
  910. sizeof(settings->available_versions[0]) * settings->available_versionslen;
  911. } else if (server) {
  912. if (settings->preferred_versionslen) {
  913. buflen = buflen_align(buflen);
  914. buflen += sizeof(settings->preferred_versions[0]) *
  915. settings->preferred_versionslen;
  916. }
  917. } else if (!ngtcp2_is_reserved_version(client_chosen_version)) {
  918. buflen = buflen_align(buflen);
  919. buflen += sizeof(client_chosen_version);
  920. }
  921. buf = ngtcp2_mem_calloc(mem, 1, buflen);
  922. if (buf == NULL) {
  923. return NGTCP2_ERR_NOMEM;
  924. }
  925. *pconn = buf;
  926. buf = buf_advance(buf, sizeof(ngtcp2_conn));
  927. (*pconn)->server = server;
  928. ngtcp2_objalloc_frame_chain_init(&(*pconn)->frc_objalloc, 16, mem);
  929. ngtcp2_objalloc_rtb_entry_init(&(*pconn)->rtb_entry_objalloc, 16, mem);
  930. ngtcp2_objalloc_strm_init(&(*pconn)->strm_objalloc, 16, mem);
  931. ngtcp2_static_ringbuf_dcid_bound_init(&(*pconn)->dcid.bound);
  932. ngtcp2_static_ringbuf_dcid_unused_init(&(*pconn)->dcid.unused);
  933. ngtcp2_static_ringbuf_dcid_retired_init(&(*pconn)->dcid.retired);
  934. ngtcp2_gaptr_init(&(*pconn)->dcid.seqgap, mem);
  935. ngtcp2_ksl_init(&(*pconn)->scid.set, cid_less, ksl_cid_less_search,
  936. sizeof(ngtcp2_cid), mem);
  937. ngtcp2_pq_init(&(*pconn)->scid.used, retired_ts_less, mem);
  938. ngtcp2_map_init(&(*pconn)->strms, mem);
  939. ngtcp2_pq_init(&(*pconn)->tx.strmq, cycle_less, mem);
  940. ngtcp2_idtr_init(&(*pconn)->remote.bidi.idtr, mem);
  941. ngtcp2_idtr_init(&(*pconn)->remote.uni.idtr, mem);
  942. ngtcp2_static_ringbuf_path_challenge_init(&(*pconn)->rx.path_challenge);
  943. ngtcp2_log_init(&(*pconn)->log, scid, settings->log_printf,
  944. settings->initial_ts, user_data);
  945. ngtcp2_qlog_init(&(*pconn)->qlog, settings->qlog_write, settings->initial_ts,
  946. user_data);
  947. if ((*pconn)->qlog.write) {
  948. buf = buf_align(buf);
  949. ngtcp2_buf_init(&(*pconn)->qlog.buf, buf, NGTCP2_QLOG_BUFLEN);
  950. buf = buf_advance(buf, NGTCP2_QLOG_BUFLEN);
  951. }
  952. (*pconn)->local.settings = *settings;
  953. if (settings->tokenlen) {
  954. tokenbuf = ngtcp2_mem_malloc(mem, settings->tokenlen);
  955. if (tokenbuf == NULL) {
  956. rv = NGTCP2_ERR_NOMEM;
  957. goto fail_token;
  958. }
  959. memcpy(tokenbuf, settings->token, settings->tokenlen);
  960. (*pconn)->local.settings.token = tokenbuf;
  961. } else {
  962. (*pconn)->local.settings.token = NULL;
  963. }
  964. if (settings->pmtud_probeslen) {
  965. (*pconn)->local.settings.pmtud_probes = buf_align(buf);
  966. buf = ngtcp2_cpymem(
  967. (uint16_t *)(*pconn)->local.settings.pmtud_probes, settings->pmtud_probes,
  968. sizeof(settings->pmtud_probes[0]) * settings->pmtud_probeslen);
  969. }
  970. if (!(*pconn)->local.settings.original_version) {
  971. (*pconn)->local.settings.original_version = client_chosen_version;
  972. }
  973. ngtcp2_dcid_init(&(*pconn)->dcid.current, 0, dcid, NULL);
  974. ngtcp2_dcid_set_path(&(*pconn)->dcid.current, path);
  975. rv = ngtcp2_gaptr_push(&(*pconn)->dcid.seqgap, 0, 1);
  976. if (rv != 0) {
  977. goto fail_seqgap_push;
  978. }
  979. conn_reset_conn_stat(*pconn, &(*pconn)->cstat);
  980. (*pconn)->cstat.initial_rtt = settings->initial_rtt;
  981. ngtcp2_rst_init(&(*pconn)->rst);
  982. (*pconn)->cc_algo = settings->cc_algo;
  983. switch (settings->cc_algo) {
  984. case NGTCP2_CC_ALGO_RENO:
  985. ngtcp2_cc_reno_init(&(*pconn)->reno, &(*pconn)->log);
  986. break;
  987. case NGTCP2_CC_ALGO_CUBIC:
  988. ngtcp2_cc_cubic_init(&(*pconn)->cubic, &(*pconn)->log, &(*pconn)->rst);
  989. break;
  990. case NGTCP2_CC_ALGO_BBR:
  991. ngtcp2_cc_bbr_init(&(*pconn)->bbr, &(*pconn)->log, &(*pconn)->cstat,
  992. &(*pconn)->rst, settings->initial_ts, callbacks->rand,
  993. &settings->rand_ctx);
  994. break;
  995. default:
  996. ngtcp2_unreachable();
  997. }
  998. rv = pktns_new(&(*pconn)->in_pktns, NGTCP2_PKTNS_ID_INITIAL, &(*pconn)->rst,
  999. &(*pconn)->cc, settings->initial_pkt_num, &(*pconn)->log,
  1000. &(*pconn)->qlog, &(*pconn)->rtb_entry_objalloc,
  1001. &(*pconn)->frc_objalloc, mem);
  1002. if (rv != 0) {
  1003. goto fail_in_pktns_init;
  1004. }
  1005. rv = pktns_new(&(*pconn)->hs_pktns, NGTCP2_PKTNS_ID_HANDSHAKE, &(*pconn)->rst,
  1006. &(*pconn)->cc, settings->initial_pkt_num, &(*pconn)->log,
  1007. &(*pconn)->qlog, &(*pconn)->rtb_entry_objalloc,
  1008. &(*pconn)->frc_objalloc, mem);
  1009. if (rv != 0) {
  1010. goto fail_hs_pktns_init;
  1011. }
  1012. pktns_init(&(*pconn)->pktns, NGTCP2_PKTNS_ID_APPLICATION, &(*pconn)->rst,
  1013. &(*pconn)->cc, settings->initial_pkt_num, &(*pconn)->log,
  1014. &(*pconn)->qlog, &(*pconn)->rtb_entry_objalloc,
  1015. &(*pconn)->frc_objalloc, mem);
  1016. scident = ngtcp2_mem_malloc(mem, sizeof(*scident));
  1017. if (scident == NULL) {
  1018. rv = NGTCP2_ERR_NOMEM;
  1019. goto fail_scident;
  1020. }
  1021. /* Set stateless reset token later if it is available in the local
  1022. transport parameters */
  1023. ngtcp2_scid_init(scident, 0, scid);
  1024. rv = ngtcp2_ksl_insert(&(*pconn)->scid.set, NULL, &scident->cid, scident);
  1025. if (rv != 0) {
  1026. goto fail_scid_set_insert;
  1027. }
  1028. scident = NULL;
  1029. if (settings->preferred_versionslen) {
  1030. if (!server && !ngtcp2_is_reserved_version(client_chosen_version)) {
  1031. for (i = 0; i < settings->preferred_versionslen; ++i) {
  1032. if (settings->preferred_versions[i] == client_chosen_version) {
  1033. break;
  1034. }
  1035. }
  1036. assert(i < settings->preferred_versionslen);
  1037. }
  1038. preferred_versions = buf_align(buf);
  1039. buf = buf_advance(preferred_versions, sizeof(preferred_versions[0]) *
  1040. settings->preferred_versionslen);
  1041. for (i = 0; i < settings->preferred_versionslen; ++i) {
  1042. assert(ngtcp2_is_supported_version(settings->preferred_versions[i]));
  1043. preferred_versions[i] = settings->preferred_versions[i];
  1044. }
  1045. (*pconn)->vneg.preferred_versions = preferred_versions;
  1046. (*pconn)->vneg.preferred_versionslen = settings->preferred_versionslen;
  1047. }
  1048. (*pconn)->local.settings.preferred_versions = NULL;
  1049. (*pconn)->local.settings.preferred_versionslen = 0;
  1050. if (settings->available_versionslen) {
  1051. if (!server && !ngtcp2_is_reserved_version(client_chosen_version)) {
  1052. for (i = 0; i < settings->available_versionslen; ++i) {
  1053. if (settings->available_versions[i] == client_chosen_version) {
  1054. break;
  1055. }
  1056. }
  1057. assert(i < settings->available_versionslen);
  1058. }
  1059. for (i = 0; i < settings->available_versionslen; ++i) {
  1060. assert(ngtcp2_is_reserved_version(settings->available_versions[i]) ||
  1061. ngtcp2_is_supported_version(settings->available_versions[i]));
  1062. }
  1063. (*pconn)->vneg.available_versions = buf_align(buf);
  1064. (*pconn)->vneg.available_versionslen =
  1065. sizeof(uint32_t) * settings->available_versionslen;
  1066. buf = available_versions_init((*pconn)->vneg.available_versions,
  1067. settings->available_versions,
  1068. settings->available_versionslen);
  1069. } else if (server) {
  1070. if (settings->preferred_versionslen) {
  1071. (*pconn)->vneg.available_versions = buf_align(buf);
  1072. (*pconn)->vneg.available_versionslen =
  1073. sizeof(uint32_t) * settings->preferred_versionslen;
  1074. buf = available_versions_init((*pconn)->vneg.available_versions,
  1075. settings->preferred_versions,
  1076. settings->preferred_versionslen);
  1077. } else {
  1078. (*pconn)->vneg.available_versions = server_default_available_versions;
  1079. (*pconn)->vneg.available_versionslen =
  1080. sizeof(server_default_available_versions);
  1081. }
  1082. } else if (!ngtcp2_is_reserved_version(client_chosen_version)) {
  1083. (*pconn)->vneg.available_versions = buf_align(buf);
  1084. (*pconn)->vneg.available_versionslen = sizeof(uint32_t);
  1085. buf = available_versions_init((*pconn)->vneg.available_versions,
  1086. &client_chosen_version, 1);
  1087. }
  1088. (*pconn)->local.settings.available_versions = NULL;
  1089. (*pconn)->local.settings.available_versionslen = 0;
  1090. (*pconn)->client_chosen_version = client_chosen_version;
  1091. conn_set_local_transport_params(*pconn, params);
  1092. callbacks->rand(&fixed_bit_byte, 1, &settings->rand_ctx);
  1093. if (fixed_bit_byte & 1) {
  1094. (*pconn)->flags |= NGTCP2_CONN_FLAG_CLEAR_FIXED_BIT;
  1095. }
  1096. (*pconn)->keep_alive.last_ts = UINT64_MAX;
  1097. (*pconn)->keep_alive.timeout = UINT64_MAX;
  1098. (*pconn)->oscid = *scid;
  1099. (*pconn)->callbacks = *callbacks;
  1100. (*pconn)->mem = mem;
  1101. (*pconn)->user_data = user_data;
  1102. (*pconn)->idle_ts = settings->initial_ts;
  1103. (*pconn)->crypto.key_update.confirmed_ts = UINT64_MAX;
  1104. (*pconn)->tx.last_max_data_ts = UINT64_MAX;
  1105. (*pconn)->tx.pacing.next_ts = UINT64_MAX;
  1106. (*pconn)->tx.last_blocked_offset = UINT64_MAX;
  1107. (*pconn)->early.discard_started_ts = UINT64_MAX;
  1108. conn_reset_ecn_validation_state(*pconn);
  1109. ngtcp2_qlog_start(&(*pconn)->qlog,
  1110. server
  1111. ? ((*pconn)->local.transport_params.retry_scid_present
  1112. ? &(*pconn)->local.transport_params.retry_scid
  1113. : &(*pconn)->local.transport_params.original_dcid)
  1114. : dcid,
  1115. server);
  1116. return 0;
  1117. fail_scid_set_insert:
  1118. ngtcp2_mem_free(mem, scident);
  1119. fail_scident:
  1120. pktns_del((*pconn)->hs_pktns, mem);
  1121. fail_hs_pktns_init:
  1122. pktns_del((*pconn)->in_pktns, mem);
  1123. fail_in_pktns_init:
  1124. ngtcp2_gaptr_free(&(*pconn)->dcid.seqgap);
  1125. fail_seqgap_push:
  1126. ngtcp2_mem_free(mem, (uint8_t *)(*pconn)->local.settings.token);
  1127. fail_token:
  1128. ngtcp2_mem_free(mem, *pconn);
  1129. return rv;
  1130. }
  1131. int ngtcp2_conn_client_new_versioned(
  1132. ngtcp2_conn **pconn, const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
  1133. const ngtcp2_path *path, uint32_t client_chosen_version,
  1134. int callbacks_version, const ngtcp2_callbacks *callbacks,
  1135. int settings_version, const ngtcp2_settings *settings,
  1136. int transport_params_version, const ngtcp2_transport_params *params,
  1137. const ngtcp2_mem *mem, void *user_data) {
  1138. int rv;
  1139. rv = conn_new(pconn, dcid, scid, path, client_chosen_version,
  1140. callbacks_version, callbacks, settings_version, settings,
  1141. transport_params_version, params, mem, user_data, 0);
  1142. if (rv != 0) {
  1143. return rv;
  1144. }
  1145. (*pconn)->rcid = *dcid;
  1146. (*pconn)->state = NGTCP2_CS_CLIENT_INITIAL;
  1147. (*pconn)->local.bidi.next_stream_id = 0;
  1148. (*pconn)->local.uni.next_stream_id = 2;
  1149. rv = ngtcp2_conn_commit_local_transport_params(*pconn);
  1150. if (rv != 0) {
  1151. ngtcp2_conn_del(*pconn);
  1152. return rv;
  1153. }
  1154. return 0;
  1155. }
  1156. int ngtcp2_conn_server_new_versioned(
  1157. ngtcp2_conn **pconn, const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
  1158. const ngtcp2_path *path, uint32_t client_chosen_version,
  1159. int callbacks_version, const ngtcp2_callbacks *callbacks,
  1160. int settings_version, const ngtcp2_settings *settings,
  1161. int transport_params_version, const ngtcp2_transport_params *params,
  1162. const ngtcp2_mem *mem, void *user_data) {
  1163. int rv;
  1164. rv = conn_new(pconn, dcid, scid, path, client_chosen_version,
  1165. callbacks_version, callbacks, settings_version, settings,
  1166. transport_params_version, params, mem, user_data, 1);
  1167. if (rv != 0) {
  1168. return rv;
  1169. }
  1170. (*pconn)->state = NGTCP2_CS_SERVER_INITIAL;
  1171. (*pconn)->local.bidi.next_stream_id = 1;
  1172. (*pconn)->local.uni.next_stream_id = 3;
  1173. if ((*pconn)->local.settings.tokenlen) {
  1174. /* Usage of token lifts amplification limit */
  1175. (*pconn)->dcid.current.flags |= NGTCP2_DCID_FLAG_PATH_VALIDATED;
  1176. }
  1177. return 0;
  1178. }
  1179. /*
  1180. * conn_fc_credits returns the number of bytes allowed to be sent to
  1181. * the given stream. Both connection and stream level flow control
  1182. * credits are considered.
  1183. */
  1184. static uint64_t conn_fc_credits(ngtcp2_conn *conn, ngtcp2_strm *strm) {
  1185. return ngtcp2_min_uint64(strm->tx.max_offset - strm->tx.offset,
  1186. conn->tx.max_offset - conn->tx.offset);
  1187. }
  1188. /*
  1189. * conn_enforce_flow_control returns the number of bytes allowed to be
  1190. * sent to the given stream. |len| might be shorted because of
  1191. * available flow control credits.
  1192. */
  1193. static uint64_t conn_enforce_flow_control(ngtcp2_conn *conn, ngtcp2_strm *strm,
  1194. uint64_t len) {
  1195. uint64_t fc_credits = conn_fc_credits(conn, strm);
  1196. return ngtcp2_min_uint64(len, fc_credits);
  1197. }
  1198. static int delete_strms_each(void *data, void *ptr) {
  1199. ngtcp2_conn *conn = ptr;
  1200. ngtcp2_strm *s = data;
  1201. ngtcp2_strm_free(s);
  1202. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, s);
  1203. return 0;
  1204. }
  1205. static void conn_vneg_crypto_free(ngtcp2_conn *conn) {
  1206. if (conn->vneg.rx.ckm) {
  1207. conn_call_delete_crypto_aead_ctx(conn, &conn->vneg.rx.ckm->aead_ctx);
  1208. }
  1209. conn_call_delete_crypto_cipher_ctx(conn, &conn->vneg.rx.hp_ctx);
  1210. if (conn->vneg.tx.ckm) {
  1211. conn_call_delete_crypto_aead_ctx(conn, &conn->vneg.tx.ckm->aead_ctx);
  1212. }
  1213. conn_call_delete_crypto_cipher_ctx(conn, &conn->vneg.tx.hp_ctx);
  1214. ngtcp2_crypto_km_del(conn->vneg.rx.ckm, conn->mem);
  1215. ngtcp2_crypto_km_del(conn->vneg.tx.ckm, conn->mem);
  1216. }
  1217. void ngtcp2_conn_del(ngtcp2_conn *conn) {
  1218. if (conn == NULL) {
  1219. return;
  1220. }
  1221. ngtcp2_qlog_end(&conn->qlog);
  1222. if (conn->early.ckm) {
  1223. conn_call_delete_crypto_aead_ctx(conn, &conn->early.ckm->aead_ctx);
  1224. }
  1225. conn_call_delete_crypto_cipher_ctx(conn, &conn->early.hp_ctx);
  1226. if (conn->crypto.key_update.old_rx_ckm) {
  1227. conn_call_delete_crypto_aead_ctx(
  1228. conn, &conn->crypto.key_update.old_rx_ckm->aead_ctx);
  1229. }
  1230. if (conn->crypto.key_update.new_rx_ckm) {
  1231. conn_call_delete_crypto_aead_ctx(
  1232. conn, &conn->crypto.key_update.new_rx_ckm->aead_ctx);
  1233. }
  1234. if (conn->crypto.key_update.new_tx_ckm) {
  1235. conn_call_delete_crypto_aead_ctx(
  1236. conn, &conn->crypto.key_update.new_tx_ckm->aead_ctx);
  1237. }
  1238. if (conn->pktns.crypto.rx.ckm) {
  1239. conn_call_delete_crypto_aead_ctx(conn,
  1240. &conn->pktns.crypto.rx.ckm->aead_ctx);
  1241. }
  1242. conn_call_delete_crypto_cipher_ctx(conn, &conn->pktns.crypto.rx.hp_ctx);
  1243. if (conn->pktns.crypto.tx.ckm) {
  1244. conn_call_delete_crypto_aead_ctx(conn,
  1245. &conn->pktns.crypto.tx.ckm->aead_ctx);
  1246. }
  1247. conn_call_delete_crypto_cipher_ctx(conn, &conn->pktns.crypto.tx.hp_ctx);
  1248. if (conn->hs_pktns) {
  1249. if (conn->hs_pktns->crypto.rx.ckm) {
  1250. conn_call_delete_crypto_aead_ctx(
  1251. conn, &conn->hs_pktns->crypto.rx.ckm->aead_ctx);
  1252. }
  1253. conn_call_delete_crypto_cipher_ctx(conn, &conn->hs_pktns->crypto.rx.hp_ctx);
  1254. if (conn->hs_pktns->crypto.tx.ckm) {
  1255. conn_call_delete_crypto_aead_ctx(
  1256. conn, &conn->hs_pktns->crypto.tx.ckm->aead_ctx);
  1257. }
  1258. conn_call_delete_crypto_cipher_ctx(conn, &conn->hs_pktns->crypto.tx.hp_ctx);
  1259. }
  1260. if (conn->in_pktns) {
  1261. if (conn->in_pktns->crypto.rx.ckm) {
  1262. conn_call_delete_crypto_aead_ctx(
  1263. conn, &conn->in_pktns->crypto.rx.ckm->aead_ctx);
  1264. }
  1265. conn_call_delete_crypto_cipher_ctx(conn, &conn->in_pktns->crypto.rx.hp_ctx);
  1266. if (conn->in_pktns->crypto.tx.ckm) {
  1267. conn_call_delete_crypto_aead_ctx(
  1268. conn, &conn->in_pktns->crypto.tx.ckm->aead_ctx);
  1269. }
  1270. conn_call_delete_crypto_cipher_ctx(conn, &conn->in_pktns->crypto.tx.hp_ctx);
  1271. }
  1272. conn_call_delete_crypto_aead_ctx(conn, &conn->crypto.retry_aead_ctx);
  1273. ngtcp2_transport_params_del(conn->remote.transport_params, conn->mem);
  1274. ngtcp2_transport_params_del(conn->remote.pending_transport_params, conn->mem);
  1275. conn_vneg_crypto_free(conn);
  1276. ngtcp2_mem_free(conn->mem, conn->crypto.decrypt_buf.base);
  1277. ngtcp2_mem_free(conn->mem, conn->crypto.decrypt_hp_buf.base);
  1278. ngtcp2_mem_free(conn->mem, (uint8_t *)conn->local.settings.token);
  1279. ngtcp2_crypto_km_del(conn->crypto.key_update.old_rx_ckm, conn->mem);
  1280. ngtcp2_crypto_km_del(conn->crypto.key_update.new_rx_ckm, conn->mem);
  1281. ngtcp2_crypto_km_del(conn->crypto.key_update.new_tx_ckm, conn->mem);
  1282. ngtcp2_crypto_km_del(conn->early.ckm, conn->mem);
  1283. pktns_free(&conn->pktns, conn->mem);
  1284. pktns_del(conn->hs_pktns, conn->mem);
  1285. pktns_del(conn->in_pktns, conn->mem);
  1286. ngtcp2_pmtud_del(conn->pmtud);
  1287. ngtcp2_pv_del(conn->pv);
  1288. ngtcp2_mem_free(conn->mem, (uint8_t *)conn->rx.ccerr.reason);
  1289. ngtcp2_idtr_free(&conn->remote.uni.idtr);
  1290. ngtcp2_idtr_free(&conn->remote.bidi.idtr);
  1291. ngtcp2_mem_free(conn->mem, conn->tx.ack);
  1292. ngtcp2_pq_free(&conn->tx.strmq);
  1293. ngtcp2_map_each(&conn->strms, delete_strms_each, (void *)conn);
  1294. ngtcp2_map_free(&conn->strms);
  1295. ngtcp2_pq_free(&conn->scid.used);
  1296. delete_scid(&conn->scid.set, conn->mem);
  1297. ngtcp2_ksl_free(&conn->scid.set);
  1298. ngtcp2_gaptr_free(&conn->dcid.seqgap);
  1299. ngtcp2_objalloc_free(&conn->strm_objalloc);
  1300. ngtcp2_objalloc_free(&conn->rtb_entry_objalloc);
  1301. ngtcp2_objalloc_free(&conn->frc_objalloc);
  1302. ngtcp2_mem_free(conn->mem, conn);
  1303. }
  1304. /*
  1305. * conn_ensure_ack_ranges makes sure that conn->tx.ack->ack.ranges can
  1306. * contain at least |n| additional ngtcp2_ack_range.
  1307. *
  1308. * This function returns 0 if it succeeds, or one of the following
  1309. * negative error codes:
  1310. *
  1311. * NGTCP2_ERR_NOMEM
  1312. * Out of memory.
  1313. */
  1314. static int conn_ensure_ack_ranges(ngtcp2_conn *conn, size_t n) {
  1315. ngtcp2_frame *fr;
  1316. size_t max = conn->tx.max_ack_ranges;
  1317. if (n <= max) {
  1318. return 0;
  1319. }
  1320. max *= 2;
  1321. assert(max >= n);
  1322. fr = ngtcp2_mem_realloc(conn->mem, conn->tx.ack,
  1323. sizeof(ngtcp2_ack) + sizeof(ngtcp2_ack_range) * max);
  1324. if (fr == NULL) {
  1325. return NGTCP2_ERR_NOMEM;
  1326. }
  1327. conn->tx.ack = fr;
  1328. conn->tx.max_ack_ranges = max;
  1329. return 0;
  1330. }
  1331. /*
  1332. * conn_compute_ack_delay computes ACK delay for outgoing protected
  1333. * ACK.
  1334. */
  1335. static ngtcp2_duration conn_compute_ack_delay(ngtcp2_conn *conn) {
  1336. return ngtcp2_min_uint64(
  1337. conn->local.transport_params.max_ack_delay,
  1338. ngtcp2_max_uint64(conn->cstat.smoothed_rtt / 8, NGTCP2_NANOSECONDS));
  1339. }
  1340. int ngtcp2_conn_create_ack_frame(ngtcp2_conn *conn, ngtcp2_frame **pfr,
  1341. ngtcp2_pktns *pktns, uint8_t type,
  1342. ngtcp2_tstamp ts, ngtcp2_duration ack_delay,
  1343. uint64_t ack_delay_exponent) {
  1344. /* TODO Measure an actual size of ACK blocks to find the best
  1345. default value. */
  1346. const size_t initial_max_ack_ranges = 8;
  1347. int64_t last_pkt_num;
  1348. ngtcp2_acktr *acktr = &pktns->acktr;
  1349. ngtcp2_ack_range *range;
  1350. ngtcp2_ksl_it it;
  1351. ngtcp2_acktr_entry *rpkt;
  1352. ngtcp2_ack *ack;
  1353. size_t range_idx;
  1354. ngtcp2_tstamp largest_ack_ts;
  1355. int rv;
  1356. if (acktr->flags & NGTCP2_ACKTR_FLAG_IMMEDIATE_ACK) {
  1357. ack_delay = 0;
  1358. }
  1359. if (!ngtcp2_acktr_require_active_ack(acktr, ack_delay, ts)) {
  1360. return 0;
  1361. }
  1362. it = ngtcp2_acktr_get(acktr);
  1363. if (ngtcp2_ksl_it_end(&it)) {
  1364. ngtcp2_acktr_commit_ack(acktr);
  1365. return 0;
  1366. }
  1367. if (conn->tx.ack == NULL) {
  1368. conn->tx.ack = ngtcp2_mem_malloc(
  1369. conn->mem,
  1370. sizeof(ngtcp2_ack) + sizeof(ngtcp2_ack_range) * initial_max_ack_ranges);
  1371. if (conn->tx.ack == NULL) {
  1372. return NGTCP2_ERR_NOMEM;
  1373. }
  1374. conn->tx.max_ack_ranges = initial_max_ack_ranges;
  1375. }
  1376. ack = &conn->tx.ack->ack;
  1377. if (pktns->rx.ecn.ect0 || pktns->rx.ecn.ect1 || pktns->rx.ecn.ce) {
  1378. ack->type = NGTCP2_FRAME_ACK_ECN;
  1379. ack->ecn.ect0 = pktns->rx.ecn.ect0;
  1380. ack->ecn.ect1 = pktns->rx.ecn.ect1;
  1381. ack->ecn.ce = pktns->rx.ecn.ce;
  1382. } else {
  1383. ack->type = NGTCP2_FRAME_ACK;
  1384. }
  1385. ack->rangecnt = 0;
  1386. rpkt = ngtcp2_ksl_it_get(&it);
  1387. if (rpkt->pkt_num == pktns->rx.max_pkt_num) {
  1388. last_pkt_num = rpkt->pkt_num - (int64_t)(rpkt->len - 1);
  1389. largest_ack_ts = rpkt->tstamp;
  1390. ack->largest_ack = rpkt->pkt_num;
  1391. ack->first_ack_range = rpkt->len - 1;
  1392. ngtcp2_ksl_it_next(&it);
  1393. } else if (rpkt->pkt_num + 1 == pktns->rx.max_pkt_num) {
  1394. last_pkt_num = rpkt->pkt_num - (int64_t)(rpkt->len - 1);
  1395. largest_ack_ts = pktns->rx.max_pkt_ts;
  1396. ack->largest_ack = pktns->rx.max_pkt_num;
  1397. ack->first_ack_range = rpkt->len;
  1398. ngtcp2_ksl_it_next(&it);
  1399. } else {
  1400. assert(rpkt->pkt_num < pktns->rx.max_pkt_num);
  1401. last_pkt_num = pktns->rx.max_pkt_num;
  1402. largest_ack_ts = pktns->rx.max_pkt_ts;
  1403. ack->largest_ack = pktns->rx.max_pkt_num;
  1404. ack->first_ack_range = 0;
  1405. }
  1406. if (type == NGTCP2_PKT_1RTT) {
  1407. ack->ack_delay_unscaled = ts - largest_ack_ts;
  1408. ack->ack_delay = ack->ack_delay_unscaled / NGTCP2_MICROSECONDS /
  1409. (1ULL << ack_delay_exponent);
  1410. } else {
  1411. ack->ack_delay_unscaled = 0;
  1412. ack->ack_delay = 0;
  1413. }
  1414. for (; !ngtcp2_ksl_it_end(&it); ngtcp2_ksl_it_next(&it)) {
  1415. if (ack->rangecnt == NGTCP2_MAX_ACK_RANGES) {
  1416. break;
  1417. }
  1418. rpkt = ngtcp2_ksl_it_get(&it);
  1419. range_idx = ack->rangecnt++;
  1420. rv = conn_ensure_ack_ranges(conn, ack->rangecnt);
  1421. if (rv != 0) {
  1422. return rv;
  1423. }
  1424. ack = &conn->tx.ack->ack;
  1425. range = &ack->ranges[range_idx];
  1426. range->gap = (uint64_t)(last_pkt_num - rpkt->pkt_num - 2);
  1427. range->len = rpkt->len - 1;
  1428. last_pkt_num = rpkt->pkt_num - (int64_t)(rpkt->len - 1);
  1429. }
  1430. /* TODO Just remove entries which cannot fit into a single ACK frame
  1431. for now. */
  1432. if (!ngtcp2_ksl_it_end(&it)) {
  1433. ngtcp2_acktr_forget(acktr, ngtcp2_ksl_it_get(&it));
  1434. }
  1435. *pfr = conn->tx.ack;
  1436. return 0;
  1437. }
  1438. /*
  1439. * conn_ppe_write_frame writes |fr| to |ppe|. If |hd_logged| is not
  1440. * NULL and |*hd_logged| is zero, packet header is logged, and 1 is
  1441. * assigned to |*hd_logged|.
  1442. *
  1443. * This function returns 0 if it succeeds, or one of the following
  1444. * negative error codes:
  1445. *
  1446. * NGTCP2_ERR_NOBUF
  1447. * Buffer is too small.
  1448. */
  1449. static int conn_ppe_write_frame_hd_log(ngtcp2_conn *conn, ngtcp2_ppe *ppe,
  1450. int *hd_logged, const ngtcp2_pkt_hd *hd,
  1451. ngtcp2_frame *fr) {
  1452. int rv;
  1453. rv = ngtcp2_ppe_encode_frame(ppe, fr);
  1454. if (rv != 0) {
  1455. assert(NGTCP2_ERR_NOBUF == rv);
  1456. return rv;
  1457. }
  1458. if (hd_logged && !*hd_logged) {
  1459. *hd_logged = 1;
  1460. ngtcp2_log_tx_pkt_hd(&conn->log, hd);
  1461. ngtcp2_qlog_pkt_sent_start(&conn->qlog);
  1462. }
  1463. ngtcp2_log_tx_fr(&conn->log, hd, fr);
  1464. ngtcp2_qlog_write_frame(&conn->qlog, fr);
  1465. return 0;
  1466. }
  1467. /*
  1468. * conn_ppe_write_frame writes |fr| to |ppe|.
  1469. *
  1470. * This function returns 0 if it succeeds, or one of the following
  1471. * negative error codes:
  1472. *
  1473. * NGTCP2_ERR_NOBUF
  1474. * Buffer is too small.
  1475. */
  1476. static int conn_ppe_write_frame(ngtcp2_conn *conn, ngtcp2_ppe *ppe,
  1477. const ngtcp2_pkt_hd *hd, ngtcp2_frame *fr) {
  1478. return conn_ppe_write_frame_hd_log(conn, ppe, NULL, hd, fr);
  1479. }
  1480. /*
  1481. * conn_on_pkt_sent is called when new non-ACK-only packet is sent.
  1482. *
  1483. * This function returns 0 if it succeeds, or one of the following
  1484. * negative error codes:
  1485. *
  1486. * NGTCP2_ERR_NOMEM
  1487. * Out of memory
  1488. */
  1489. static int conn_on_pkt_sent(ngtcp2_conn *conn, ngtcp2_pktns *pktns,
  1490. ngtcp2_rtb_entry *ent) {
  1491. ngtcp2_rtb *rtb = &pktns->rtb;
  1492. int rv;
  1493. /* This function implements OnPacketSent, but it handles only
  1494. non-ACK-only packet. */
  1495. rv = ngtcp2_rtb_add(rtb, ent, &conn->cstat);
  1496. if (rv != 0) {
  1497. return rv;
  1498. }
  1499. if (ent->flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) {
  1500. conn->cstat.last_tx_pkt_ts[pktns->id] = ent->ts;
  1501. }
  1502. ngtcp2_conn_set_loss_detection_timer(conn, ent->ts);
  1503. return 0;
  1504. }
  1505. /*
  1506. * pktns_select_pkt_numlen selects shortest packet number encoding for
  1507. * the next packet number based on the largest acknowledged packet
  1508. * number. It returns the number of bytes to encode the packet
  1509. * number.
  1510. */
  1511. static size_t pktns_select_pkt_numlen(ngtcp2_pktns *pktns) {
  1512. int64_t pkt_num = pktns->tx.last_pkt_num + 1;
  1513. ngtcp2_rtb *rtb = &pktns->rtb;
  1514. int64_t n = pkt_num - rtb->largest_acked_tx_pkt_num;
  1515. if (NGTCP2_MAX_PKT_NUM / 2 < n) {
  1516. return 4;
  1517. }
  1518. n = n * 2 - 1;
  1519. if (n > 0xffffff) {
  1520. return 4;
  1521. }
  1522. if (n > 0xffff) {
  1523. return 3;
  1524. }
  1525. if (n > 0xff) {
  1526. return 2;
  1527. }
  1528. return 1;
  1529. }
  1530. /*
  1531. * conn_get_cwnd returns cwnd for the current path.
  1532. */
  1533. static uint64_t conn_get_cwnd(ngtcp2_conn *conn) {
  1534. return conn->pv && (conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE)
  1535. ? ngtcp2_cc_compute_initcwnd(conn->cstat.max_tx_udp_payload_size)
  1536. : conn->cstat.cwnd;
  1537. }
  1538. /*
  1539. * conn_cwnd_is_zero returns nonzero if the number of bytes the local
  1540. * endpoint can sent at this time is zero.
  1541. */
  1542. static int conn_cwnd_is_zero(ngtcp2_conn *conn) {
  1543. uint64_t bytes_in_flight = conn->cstat.bytes_in_flight;
  1544. uint64_t cwnd = conn_get_cwnd(conn);
  1545. if (bytes_in_flight >= cwnd) {
  1546. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_LDC,
  1547. "cwnd limited bytes_in_flight=%lu cwnd=%lu",
  1548. bytes_in_flight, cwnd);
  1549. }
  1550. return bytes_in_flight >= cwnd;
  1551. }
  1552. /*
  1553. * conn_retry_early_payloadlen returns the estimated wire length of
  1554. * the first STREAM frame of 0-RTT packet which should be
  1555. * retransmitted due to Retry packet.
  1556. */
  1557. static uint64_t conn_retry_early_payloadlen(ngtcp2_conn *conn) {
  1558. ngtcp2_frame_chain *frc;
  1559. ngtcp2_strm *strm;
  1560. uint64_t len;
  1561. if (conn->flags & NGTCP2_CONN_FLAG_EARLY_DATA_REJECTED) {
  1562. return 0;
  1563. }
  1564. for (; !ngtcp2_pq_empty(&conn->tx.strmq);) {
  1565. strm = ngtcp2_conn_tx_strmq_top(conn);
  1566. if (ngtcp2_strm_streamfrq_empty(strm)) {
  1567. ngtcp2_conn_tx_strmq_pop(conn);
  1568. continue;
  1569. }
  1570. frc = ngtcp2_strm_streamfrq_top(strm);
  1571. len = ngtcp2_vec_len(frc->fr.stream.data, frc->fr.stream.datacnt) +
  1572. NGTCP2_STREAM_OVERHEAD;
  1573. /* Take the min because in conn_should_pad_pkt we take max in
  1574. order to deal with unbreakable DATAGRAM. */
  1575. return ngtcp2_min_uint64(len, NGTCP2_MIN_COALESCED_PAYLOADLEN);
  1576. }
  1577. return 0;
  1578. }
  1579. /*
  1580. * conn_verify_dcid verifies that destination connection ID in |hd| is
  1581. * valid for the connection. If it is successfully verified and the
  1582. * remote endpoint uses new DCID in the packet, nonzero value is
  1583. * assigned to |*pnew_cid_used| if it is not NULL. Otherwise 0 is
  1584. * assigned to it.
  1585. *
  1586. * This function returns 0 if it succeeds, or one of the following
  1587. * negative error codes:
  1588. *
  1589. * NGTCP2_ERR_NOMEM
  1590. * Out of memory.
  1591. * NGTCP2_ERR_INVALID_ARGUMENT
  1592. * |dcid| is not known to the local endpoint.
  1593. */
  1594. static int conn_verify_dcid(ngtcp2_conn *conn, int *pnew_cid_used,
  1595. const ngtcp2_pkt_hd *hd) {
  1596. ngtcp2_ksl_it it;
  1597. ngtcp2_scid *scid;
  1598. int rv;
  1599. it = ngtcp2_ksl_lower_bound(&conn->scid.set, &hd->dcid);
  1600. if (ngtcp2_ksl_it_end(&it)) {
  1601. return NGTCP2_ERR_INVALID_ARGUMENT;
  1602. }
  1603. scid = ngtcp2_ksl_it_get(&it);
  1604. if (!ngtcp2_cid_eq(&scid->cid, &hd->dcid)) {
  1605. return NGTCP2_ERR_INVALID_ARGUMENT;
  1606. }
  1607. if (!(scid->flags & NGTCP2_SCID_FLAG_USED)) {
  1608. scid->flags |= NGTCP2_SCID_FLAG_USED;
  1609. if (scid->pe.index == NGTCP2_PQ_BAD_INDEX) {
  1610. rv = ngtcp2_pq_push(&conn->scid.used, &scid->pe);
  1611. if (rv != 0) {
  1612. return rv;
  1613. }
  1614. }
  1615. if (pnew_cid_used) {
  1616. *pnew_cid_used = 1;
  1617. }
  1618. } else if (pnew_cid_used) {
  1619. *pnew_cid_used = 0;
  1620. }
  1621. return 0;
  1622. }
  1623. /*
  1624. * conn_should_pad_pkt returns nonzero if the packet should be padded.
  1625. * |type| is the type of packet. |left| is the space left in packet
  1626. * buffer. |write_datalen| is the number of bytes which will be sent
  1627. * in the next, coalesced 0-RTT packet.
  1628. */
  1629. static int conn_should_pad_pkt(ngtcp2_conn *conn, uint8_t type, size_t left,
  1630. uint64_t write_datalen, int ack_eliciting,
  1631. int require_padding) {
  1632. uint64_t min_payloadlen;
  1633. if (type == NGTCP2_PKT_INITIAL) {
  1634. if (conn->server) {
  1635. if (!ack_eliciting) {
  1636. return 0;
  1637. }
  1638. if ((conn->hs_pktns->crypto.tx.ckm &&
  1639. (conn->hs_pktns->rtb.probe_pkt_left ||
  1640. !ngtcp2_strm_streamfrq_empty(&conn->hs_pktns->crypto.strm) ||
  1641. !ngtcp2_acktr_empty(&conn->hs_pktns->acktr))) ||
  1642. conn->pktns.crypto.tx.ckm) {
  1643. /* If we have something to send in Handshake or 1RTT packet,
  1644. then add PADDING in that packet. */
  1645. min_payloadlen = NGTCP2_MIN_COALESCED_PAYLOADLEN;
  1646. } else {
  1647. return 1;
  1648. }
  1649. } else {
  1650. if (conn->hs_pktns->crypto.tx.ckm &&
  1651. (conn->hs_pktns->rtb.probe_pkt_left ||
  1652. !ngtcp2_strm_streamfrq_empty(&conn->hs_pktns->crypto.strm) ||
  1653. !ngtcp2_acktr_empty(&conn->hs_pktns->acktr))) {
  1654. /* If we have something to send in Handshake packet, then add
  1655. PADDING in Handshake packet. */
  1656. min_payloadlen = NGTCP2_MIN_COALESCED_PAYLOADLEN;
  1657. } else if (conn->early.ckm && write_datalen > 0) {
  1658. /* If we have something to send in 0RTT packet, then add
  1659. PADDING in that packet. Take maximum in case that
  1660. write_datalen includes DATAGRAM which cannot be split. */
  1661. min_payloadlen =
  1662. ngtcp2_max_uint64(write_datalen, NGTCP2_MIN_COALESCED_PAYLOADLEN);
  1663. } else {
  1664. return 1;
  1665. }
  1666. }
  1667. } else {
  1668. assert(type == NGTCP2_PKT_HANDSHAKE);
  1669. if (!require_padding) {
  1670. return 0;
  1671. }
  1672. if (!conn->pktns.crypto.tx.ckm) {
  1673. return 1;
  1674. }
  1675. min_payloadlen = NGTCP2_MIN_COALESCED_PAYLOADLEN;
  1676. }
  1677. /* TODO the next packet type should be taken into account */
  1678. return left <
  1679. /* TODO Assuming that pkt_num is encoded in 1 byte. */
  1680. NGTCP2_MIN_LONG_HEADERLEN + conn->dcid.current.cid.datalen +
  1681. conn->oscid.datalen + NGTCP2_PKT_LENGTHLEN - 1 + min_payloadlen +
  1682. NGTCP2_MAX_AEAD_OVERHEAD;
  1683. }
  1684. static void conn_restart_timer_on_write(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  1685. conn->idle_ts = ts;
  1686. conn->flags &= (uint32_t)~NGTCP2_CONN_FLAG_RESTART_IDLE_TIMER_ON_WRITE;
  1687. }
  1688. static void conn_restart_timer_on_read(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  1689. conn->idle_ts = ts;
  1690. conn->flags |= NGTCP2_CONN_FLAG_RESTART_IDLE_TIMER_ON_WRITE;
  1691. }
  1692. /*
  1693. * conn_keep_alive_enabled returns nonzero if keep-alive is enabled.
  1694. */
  1695. static int conn_keep_alive_enabled(ngtcp2_conn *conn) {
  1696. return conn->keep_alive.last_ts != UINT64_MAX &&
  1697. conn->keep_alive.timeout != UINT64_MAX;
  1698. }
  1699. /*
  1700. * conn_keep_alive_expired returns nonzero if keep-alive timer has
  1701. * expired.
  1702. */
  1703. static int conn_keep_alive_expired(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  1704. return ngtcp2_tstamp_elapsed(conn->keep_alive.last_ts,
  1705. conn->keep_alive.timeout, ts);
  1706. }
  1707. /*
  1708. * conn_keep_alive_expiry returns the expiry time of keep-alive timer.
  1709. */
  1710. static ngtcp2_tstamp conn_keep_alive_expiry(ngtcp2_conn *conn) {
  1711. if ((conn->flags & NGTCP2_CONN_FLAG_KEEP_ALIVE_CANCELLED) ||
  1712. !(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED) ||
  1713. !conn_keep_alive_enabled(conn) ||
  1714. conn->keep_alive.last_ts >= UINT64_MAX - conn->keep_alive.timeout) {
  1715. return UINT64_MAX;
  1716. }
  1717. return conn->keep_alive.last_ts + conn->keep_alive.timeout;
  1718. }
  1719. /*
  1720. * conn_cancel_expired_keep_alive_timer cancels the expired keep-alive
  1721. * timer.
  1722. */
  1723. static void conn_cancel_expired_keep_alive_timer(ngtcp2_conn *conn,
  1724. ngtcp2_tstamp ts) {
  1725. if (!(conn->flags & NGTCP2_CONN_FLAG_KEEP_ALIVE_CANCELLED) &&
  1726. conn_keep_alive_expired(conn, ts)) {
  1727. conn->flags |= NGTCP2_CONN_FLAG_KEEP_ALIVE_CANCELLED;
  1728. }
  1729. }
  1730. /*
  1731. * conn_update_keep_alive_last_ts updates the base time point of
  1732. * keep-alive timer.
  1733. */
  1734. static void conn_update_keep_alive_last_ts(ngtcp2_conn *conn,
  1735. ngtcp2_tstamp ts) {
  1736. conn->keep_alive.last_ts = ts;
  1737. conn->flags &= (uint32_t)~NGTCP2_CONN_FLAG_KEEP_ALIVE_CANCELLED;
  1738. }
  1739. void ngtcp2_conn_set_keep_alive_timeout(ngtcp2_conn *conn,
  1740. ngtcp2_duration timeout) {
  1741. if (timeout == 0) {
  1742. timeout = UINT64_MAX;
  1743. }
  1744. conn->keep_alive.timeout = timeout;
  1745. }
  1746. /*
  1747. * NGTCP2_PKT_PACING_OVERHEAD defines overhead of userspace event
  1748. * loop. Packet pacing might require sub milliseconds packet spacing,
  1749. * but userspace event loop might not offer such precision.
  1750. * Typically, if delay is 0.5 microseconds, the actual delay after
  1751. * which we can send packet is well over 1 millisecond when event loop
  1752. * is involved (which includes other stuff, like reading packets etc
  1753. * in a typical single threaded use case).
  1754. */
  1755. #define NGTCP2_PKT_PACING_OVERHEAD NGTCP2_MILLISECONDS
  1756. static void conn_cancel_expired_pkt_tx_timer(ngtcp2_conn *conn,
  1757. ngtcp2_tstamp ts) {
  1758. if (conn->tx.pacing.next_ts == UINT64_MAX) {
  1759. return;
  1760. }
  1761. if (conn->tx.pacing.next_ts > ts + NGTCP2_PKT_PACING_OVERHEAD) {
  1762. return;
  1763. }
  1764. conn->tx.pacing.next_ts = UINT64_MAX;
  1765. }
  1766. static int conn_pacing_pkt_tx_allowed(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  1767. return conn->tx.pacing.next_ts == UINT64_MAX ||
  1768. conn->tx.pacing.next_ts <= ts + NGTCP2_PKT_PACING_OVERHEAD;
  1769. }
  1770. static uint8_t conn_pkt_flags(ngtcp2_conn *conn) {
  1771. if (conn->remote.transport_params &&
  1772. conn->remote.transport_params->grease_quic_bit &&
  1773. (conn->flags & NGTCP2_CONN_FLAG_CLEAR_FIXED_BIT)) {
  1774. return NGTCP2_PKT_FLAG_FIXED_BIT_CLEAR;
  1775. }
  1776. return NGTCP2_PKT_FLAG_NONE;
  1777. }
  1778. static uint8_t conn_pkt_flags_long(ngtcp2_conn *conn) {
  1779. return NGTCP2_PKT_FLAG_LONG_FORM | conn_pkt_flags(conn);
  1780. }
  1781. static uint8_t conn_pkt_flags_short(ngtcp2_conn *conn) {
  1782. return (uint8_t)(conn_pkt_flags(conn) | ((conn->pktns.crypto.tx.ckm->flags &
  1783. NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE)
  1784. ? NGTCP2_PKT_FLAG_KEY_PHASE
  1785. : NGTCP2_PKT_FLAG_NONE));
  1786. }
  1787. static size_t conn_min_pktlen(ngtcp2_conn *conn);
  1788. /*
  1789. * conn_write_handshake_pkt writes handshake packet in the buffer
  1790. * pointed by |dest| whose length is |destlen|. |dgram_offset| is the
  1791. * offset in UDP datagram payload where this QUIC packet is positioned
  1792. * at. |type| specifies long packet type. It should be either
  1793. * NGTCP2_PKT_INITIAL or NGTCP2_PKT_HANDSHAKE_PKT.
  1794. *
  1795. * |write_datalen| is the minimum length of application data ready to
  1796. * send in subsequent 0RTT packet.
  1797. *
  1798. * This function returns the number of bytes written in |dest| if it
  1799. * succeeds, or one of the following negative error codes:
  1800. *
  1801. * NGTCP2_ERR_NOMEM
  1802. * Out of memory.
  1803. * NGTCP2_ERR_CALLBACK_FAILURE
  1804. * User-defined callback function failed.
  1805. */
  1806. static ngtcp2_ssize
  1807. conn_write_handshake_pkt(ngtcp2_conn *conn, ngtcp2_pkt_info *pi, uint8_t *dest,
  1808. size_t destlen, size_t dgram_offset, uint8_t type,
  1809. uint8_t flags, uint64_t write_datalen,
  1810. ngtcp2_tstamp ts) {
  1811. int rv;
  1812. ngtcp2_ppe ppe;
  1813. ngtcp2_pkt_hd hd;
  1814. ngtcp2_frame_chain *frq = NULL, **pfrc = &frq;
  1815. ngtcp2_frame_chain *nfrc;
  1816. ngtcp2_frame *ackfr = NULL, lfr;
  1817. ngtcp2_ssize spktlen;
  1818. ngtcp2_crypto_cc cc;
  1819. ngtcp2_rtb_entry *rtbent;
  1820. ngtcp2_pktns *pktns;
  1821. size_t left;
  1822. uint16_t rtb_entry_flags = NGTCP2_RTB_ENTRY_FLAG_NONE;
  1823. int require_padding = (flags & NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING) != 0;
  1824. int pkt_empty = 1;
  1825. int min_padded = 0;
  1826. int padded = 0;
  1827. int hd_logged = 0;
  1828. uint64_t crypto_offset;
  1829. ngtcp2_ssize num_reclaimed;
  1830. uint32_t version;
  1831. switch (type) {
  1832. case NGTCP2_PKT_INITIAL:
  1833. if (!conn->in_pktns) {
  1834. return 0;
  1835. }
  1836. assert(conn->in_pktns->crypto.tx.ckm);
  1837. pktns = conn->in_pktns;
  1838. version = conn->negotiated_version ? conn->negotiated_version
  1839. : conn->client_chosen_version;
  1840. if (version == conn->client_chosen_version) {
  1841. cc.ckm = pktns->crypto.tx.ckm;
  1842. cc.hp_ctx = pktns->crypto.tx.hp_ctx;
  1843. } else {
  1844. assert(conn->vneg.version == version);
  1845. cc.ckm = conn->vneg.tx.ckm;
  1846. cc.hp_ctx = conn->vneg.tx.hp_ctx;
  1847. }
  1848. break;
  1849. case NGTCP2_PKT_HANDSHAKE:
  1850. if (!conn->hs_pktns || !conn->hs_pktns->crypto.tx.ckm) {
  1851. return 0;
  1852. }
  1853. pktns = conn->hs_pktns;
  1854. version = conn->negotiated_version;
  1855. cc.ckm = pktns->crypto.tx.ckm;
  1856. cc.hp_ctx = pktns->crypto.tx.hp_ctx;
  1857. break;
  1858. default:
  1859. ngtcp2_unreachable();
  1860. }
  1861. cc.aead = pktns->crypto.ctx.aead;
  1862. cc.hp = pktns->crypto.ctx.hp;
  1863. cc.encrypt = conn->callbacks.encrypt;
  1864. cc.hp_mask = conn->callbacks.hp_mask;
  1865. ngtcp2_pkt_hd_init(
  1866. &hd, conn_pkt_flags_long(conn), type, &conn->dcid.current.cid, &conn->oscid,
  1867. pktns->tx.last_pkt_num + 1, pktns_select_pkt_numlen(pktns), version, 0);
  1868. if (!conn->server && type == NGTCP2_PKT_INITIAL &&
  1869. conn->local.settings.tokenlen) {
  1870. hd.token = conn->local.settings.token;
  1871. hd.tokenlen = conn->local.settings.tokenlen;
  1872. }
  1873. ngtcp2_ppe_init(&ppe, dest, destlen, dgram_offset, &cc);
  1874. rv = ngtcp2_ppe_encode_hd(&ppe, &hd);
  1875. if (rv != 0) {
  1876. assert(NGTCP2_ERR_NOBUF == rv);
  1877. return 0;
  1878. }
  1879. if (!ngtcp2_ppe_ensure_hp_sample(&ppe)) {
  1880. return 0;
  1881. }
  1882. rv = ngtcp2_conn_create_ack_frame(conn, &ackfr, pktns, type, ts,
  1883. /* ack_delay = */ 0,
  1884. NGTCP2_DEFAULT_ACK_DELAY_EXPONENT);
  1885. if (rv != 0) {
  1886. ngtcp2_frame_chain_list_objalloc_del(frq, &conn->frc_objalloc, conn->mem);
  1887. return rv;
  1888. }
  1889. if (ackfr) {
  1890. rv = conn_ppe_write_frame_hd_log(conn, &ppe, &hd_logged, &hd, ackfr);
  1891. if (rv != 0) {
  1892. assert(NGTCP2_ERR_NOBUF == rv);
  1893. } else {
  1894. ngtcp2_acktr_commit_ack(&pktns->acktr);
  1895. ngtcp2_acktr_add_ack(&pktns->acktr, hd.pkt_num, ackfr->ack.largest_ack);
  1896. pkt_empty = 0;
  1897. }
  1898. }
  1899. /* Server requires at least NGTCP2_MAX_UDP_PAYLOAD_SIZE bytes in
  1900. order to send ack-eliciting Initial packet. */
  1901. if (!conn->server || type != NGTCP2_PKT_INITIAL ||
  1902. destlen >= NGTCP2_MAX_UDP_PAYLOAD_SIZE) {
  1903. build_pkt:
  1904. for (; !ngtcp2_strm_streamfrq_empty(&pktns->crypto.strm);) {
  1905. left = ngtcp2_ppe_left(&ppe);
  1906. crypto_offset = ngtcp2_strm_streamfrq_unacked_offset(&pktns->crypto.strm);
  1907. if (crypto_offset == (uint64_t)-1) {
  1908. ngtcp2_strm_streamfrq_clear(&pktns->crypto.strm);
  1909. break;
  1910. }
  1911. left = ngtcp2_pkt_crypto_max_datalen(crypto_offset, left, left);
  1912. if (left == (size_t)-1) {
  1913. break;
  1914. }
  1915. rv = ngtcp2_strm_streamfrq_pop(&pktns->crypto.strm, &nfrc, left);
  1916. if (rv != 0) {
  1917. assert(ngtcp2_err_is_fatal(rv));
  1918. ngtcp2_frame_chain_list_objalloc_del(frq, &conn->frc_objalloc,
  1919. conn->mem);
  1920. return rv;
  1921. }
  1922. if (nfrc == NULL) {
  1923. break;
  1924. }
  1925. rv = conn_ppe_write_frame_hd_log(conn, &ppe, &hd_logged, &hd, &nfrc->fr);
  1926. if (rv != 0) {
  1927. ngtcp2_unreachable();
  1928. }
  1929. *pfrc = nfrc;
  1930. pfrc = &(*pfrc)->next;
  1931. pkt_empty = 0;
  1932. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  1933. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  1934. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  1935. }
  1936. if (!(rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) &&
  1937. pktns->rtb.num_retransmittable && pktns->rtb.probe_pkt_left) {
  1938. num_reclaimed = ngtcp2_rtb_reclaim_on_pto(&pktns->rtb, conn, pktns, 1);
  1939. if (num_reclaimed < 0) {
  1940. ngtcp2_frame_chain_list_objalloc_del(frq, &conn->frc_objalloc,
  1941. conn->mem);
  1942. return rv;
  1943. }
  1944. if (num_reclaimed) {
  1945. goto build_pkt;
  1946. }
  1947. /* We had pktns->rtb.num_retransmittable > 0 but the contents of
  1948. those packets have been acknowledged (i.e., retransmission in
  1949. another packet). For server, in this case, we don't have to
  1950. send any probe packet. Client needs to send probe packets
  1951. until it knows that server has completed address validation or
  1952. handshake has been confirmed. */
  1953. if (pktns->rtb.num_pto_eliciting == 0 &&
  1954. (conn->server ||
  1955. (conn->flags & (NGTCP2_CONN_FLAG_SERVER_ADDR_VERIFIED |
  1956. NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED)))) {
  1957. pktns->rtb.probe_pkt_left = 0;
  1958. ngtcp2_conn_set_loss_detection_timer(conn, ts);
  1959. /* TODO If packet is empty, we should return now if cwnd is
  1960. zero. */
  1961. }
  1962. }
  1963. if (!(rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) &&
  1964. pktns->rtb.probe_pkt_left) {
  1965. lfr.type = NGTCP2_FRAME_PING;
  1966. rv = conn_ppe_write_frame_hd_log(conn, &ppe, &hd_logged, &hd, &lfr);
  1967. if (rv != 0) {
  1968. assert(rv == NGTCP2_ERR_NOBUF);
  1969. } else {
  1970. rtb_entry_flags |=
  1971. NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING | NGTCP2_RTB_ENTRY_FLAG_PROBE;
  1972. pkt_empty = 0;
  1973. }
  1974. }
  1975. if (!pkt_empty) {
  1976. if (!(rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING)) {
  1977. if (ngtcp2_tstamp_elapsed(pktns->tx.non_ack_pkt_start_ts,
  1978. conn->cstat.smoothed_rtt, ts)) {
  1979. lfr.type = NGTCP2_FRAME_PING;
  1980. rv = conn_ppe_write_frame_hd_log(conn, &ppe, &hd_logged, &hd, &lfr);
  1981. if (rv != 0) {
  1982. assert(rv == NGTCP2_ERR_NOBUF);
  1983. } else {
  1984. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING;
  1985. pktns->tx.non_ack_pkt_start_ts = UINT64_MAX;
  1986. pkt_empty = 0;
  1987. }
  1988. } else if (pktns->tx.non_ack_pkt_start_ts == UINT64_MAX) {
  1989. pktns->tx.non_ack_pkt_start_ts = ts;
  1990. }
  1991. } else {
  1992. pktns->tx.non_ack_pkt_start_ts = UINT64_MAX;
  1993. }
  1994. }
  1995. }
  1996. if (pkt_empty && !require_padding) {
  1997. return 0;
  1998. }
  1999. /* If we cannot write another packet, then we need to add padding to
  2000. Initial here. */
  2001. if (conn_should_pad_pkt(
  2002. conn, type, ngtcp2_ppe_left(&ppe), write_datalen,
  2003. (rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) != 0,
  2004. require_padding)) {
  2005. lfr.type = NGTCP2_FRAME_PADDING;
  2006. lfr.padding.len = ngtcp2_ppe_dgram_padding(&ppe);
  2007. } else if (pkt_empty) {
  2008. return 0;
  2009. } else {
  2010. lfr.type = NGTCP2_FRAME_PADDING;
  2011. lfr.padding.len = ngtcp2_ppe_padding_size(&ppe, conn_min_pktlen(conn));
  2012. min_padded = 1;
  2013. }
  2014. if (lfr.padding.len) {
  2015. if (!min_padded ||
  2016. (rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING)) {
  2017. padded = 1;
  2018. }
  2019. ngtcp2_log_tx_fr(&conn->log, &hd, &lfr);
  2020. ngtcp2_qlog_write_frame(&conn->qlog, &lfr);
  2021. }
  2022. spktlen = ngtcp2_ppe_final(&ppe, NULL);
  2023. if (spktlen < 0) {
  2024. assert(ngtcp2_err_is_fatal((int)spktlen));
  2025. ngtcp2_frame_chain_list_objalloc_del(frq, &conn->frc_objalloc, conn->mem);
  2026. return spktlen;
  2027. }
  2028. ngtcp2_qlog_pkt_sent_end(&conn->qlog, &hd, (size_t)spktlen);
  2029. if ((rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) || padded) {
  2030. if (pi) {
  2031. conn_handle_tx_ecn(conn, pi, &rtb_entry_flags, pktns, &hd, ts);
  2032. }
  2033. rv =
  2034. ngtcp2_rtb_entry_objalloc_new(&rtbent, &hd, frq, ts, (size_t)spktlen,
  2035. rtb_entry_flags, &conn->rtb_entry_objalloc);
  2036. if (rv != 0) {
  2037. assert(ngtcp2_err_is_fatal(rv));
  2038. ngtcp2_frame_chain_list_objalloc_del(frq, &conn->frc_objalloc, conn->mem);
  2039. return rv;
  2040. }
  2041. rv = conn_on_pkt_sent(conn, pktns, rtbent);
  2042. if (rv != 0) {
  2043. ngtcp2_rtb_entry_objalloc_del(rtbent, &conn->rtb_entry_objalloc,
  2044. &conn->frc_objalloc, conn->mem);
  2045. return rv;
  2046. }
  2047. if ((rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) &&
  2048. (conn->flags & NGTCP2_CONN_FLAG_RESTART_IDLE_TIMER_ON_WRITE)) {
  2049. conn_restart_timer_on_write(conn, ts);
  2050. }
  2051. } else if (pi && conn->tx.ecn.state == NGTCP2_ECN_STATE_CAPABLE) {
  2052. conn_handle_tx_ecn(conn, pi, NULL, pktns, &hd, ts);
  2053. }
  2054. if (pktns->rtb.probe_pkt_left &&
  2055. (rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING)) {
  2056. --pktns->rtb.probe_pkt_left;
  2057. }
  2058. conn_update_keep_alive_last_ts(conn, ts);
  2059. conn->dcid.current.bytes_sent += (uint64_t)spktlen;
  2060. conn->tx.pacing.pktlen += (size_t)spktlen;
  2061. ngtcp2_qlog_metrics_updated(&conn->qlog, &conn->cstat);
  2062. ++pktns->tx.last_pkt_num;
  2063. return spktlen;
  2064. }
  2065. /*
  2066. * conn_write_ack_pkt writes QUIC packet for type |type| which only
  2067. * includes ACK frame in the buffer pointed by |dest| whose length is
  2068. * |destlen|.
  2069. *
  2070. * This function returns the number of bytes written in |dest| if it
  2071. * succeeds, or one of the following negative error codes:
  2072. *
  2073. * NGTCP2_ERR_CALLBACK_FAILURE
  2074. * User-defined callback function failed.
  2075. * NGTCP2_ERR_NOMEM
  2076. * Out of memory.
  2077. */
  2078. static ngtcp2_ssize conn_write_ack_pkt(ngtcp2_conn *conn, ngtcp2_pkt_info *pi,
  2079. uint8_t *dest, size_t destlen,
  2080. uint8_t type, ngtcp2_tstamp ts) {
  2081. int rv;
  2082. ngtcp2_frame *ackfr;
  2083. ngtcp2_pktns *pktns;
  2084. ngtcp2_duration ack_delay;
  2085. uint64_t ack_delay_exponent;
  2086. ngtcp2_ssize spktlen;
  2087. assert(!(conn->flags & NGTCP2_CONN_FLAG_PPE_PENDING));
  2088. switch (type) {
  2089. case NGTCP2_PKT_INITIAL:
  2090. assert(conn->server);
  2091. pktns = conn->in_pktns;
  2092. ack_delay = 0;
  2093. ack_delay_exponent = NGTCP2_DEFAULT_ACK_DELAY_EXPONENT;
  2094. break;
  2095. case NGTCP2_PKT_HANDSHAKE:
  2096. pktns = conn->hs_pktns;
  2097. ack_delay = 0;
  2098. ack_delay_exponent = NGTCP2_DEFAULT_ACK_DELAY_EXPONENT;
  2099. break;
  2100. case NGTCP2_PKT_1RTT:
  2101. pktns = &conn->pktns;
  2102. ack_delay = conn_compute_ack_delay(conn);
  2103. ack_delay_exponent = conn->local.transport_params.ack_delay_exponent;
  2104. break;
  2105. default:
  2106. ngtcp2_unreachable();
  2107. }
  2108. if (!pktns->crypto.tx.ckm) {
  2109. return 0;
  2110. }
  2111. ackfr = NULL;
  2112. rv = ngtcp2_conn_create_ack_frame(conn, &ackfr, pktns, type, ts, ack_delay,
  2113. ack_delay_exponent);
  2114. if (rv != 0) {
  2115. return rv;
  2116. }
  2117. if (!ackfr) {
  2118. return 0;
  2119. }
  2120. spktlen = ngtcp2_conn_write_single_frame_pkt(
  2121. conn, pi, dest, destlen, type, NGTCP2_WRITE_PKT_FLAG_NONE,
  2122. &conn->dcid.current.cid, ackfr, NGTCP2_RTB_ENTRY_FLAG_NONE, NULL, ts);
  2123. if (spktlen <= 0) {
  2124. return spktlen;
  2125. }
  2126. conn->dcid.current.bytes_sent += (uint64_t)spktlen;
  2127. return spktlen;
  2128. }
  2129. static void conn_discard_pktns(ngtcp2_conn *conn, ngtcp2_pktns **ppktns,
  2130. ngtcp2_tstamp ts) {
  2131. ngtcp2_pktns *pktns = *ppktns;
  2132. uint64_t bytes_in_flight;
  2133. bytes_in_flight = pktns->rtb.cc_bytes_in_flight;
  2134. assert(conn->cstat.bytes_in_flight >= bytes_in_flight);
  2135. conn->cstat.bytes_in_flight -= bytes_in_flight;
  2136. conn->cstat.pto_count = 0;
  2137. conn->cstat.last_tx_pkt_ts[pktns->id] = UINT64_MAX;
  2138. conn->cstat.loss_time[pktns->id] = UINT64_MAX;
  2139. conn_call_delete_crypto_aead_ctx(conn, &pktns->crypto.rx.ckm->aead_ctx);
  2140. conn_call_delete_crypto_cipher_ctx(conn, &pktns->crypto.rx.hp_ctx);
  2141. conn_call_delete_crypto_aead_ctx(conn, &pktns->crypto.tx.ckm->aead_ctx);
  2142. conn_call_delete_crypto_cipher_ctx(conn, &pktns->crypto.tx.hp_ctx);
  2143. pktns_del(pktns, conn->mem);
  2144. *ppktns = NULL;
  2145. ngtcp2_conn_set_loss_detection_timer(conn, ts);
  2146. }
  2147. void ngtcp2_conn_discard_initial_state(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  2148. if (!conn->in_pktns) {
  2149. return;
  2150. }
  2151. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  2152. "discarding Initial packet number space");
  2153. conn_discard_pktns(conn, &conn->in_pktns, ts);
  2154. conn_vneg_crypto_free(conn);
  2155. memset(&conn->vneg.rx, 0, sizeof(conn->vneg.rx));
  2156. memset(&conn->vneg.tx, 0, sizeof(conn->vneg.tx));
  2157. }
  2158. void ngtcp2_conn_discard_handshake_state(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  2159. if (!conn->hs_pktns) {
  2160. return;
  2161. }
  2162. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  2163. "discarding Handshake packet number space");
  2164. conn_discard_pktns(conn, &conn->hs_pktns, ts);
  2165. }
  2166. /*
  2167. * conn_discard_early_key discards early key.
  2168. */
  2169. static void conn_discard_early_key(ngtcp2_conn *conn) {
  2170. assert(conn->early.ckm);
  2171. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON, "discarding early key");
  2172. conn_call_delete_crypto_aead_ctx(conn, &conn->early.ckm->aead_ctx);
  2173. conn_call_delete_crypto_cipher_ctx(conn, &conn->early.hp_ctx);
  2174. memset(&conn->early.hp_ctx, 0, sizeof(conn->early.hp_ctx));
  2175. ngtcp2_crypto_km_del(conn->early.ckm, conn->mem);
  2176. conn->early.ckm = NULL;
  2177. }
  2178. /*
  2179. * conn_write_handshake_ack_pkts writes packets which contain ACK
  2180. * frame only. This function writes at most 2 packets for each
  2181. * Initial and Handshake packet.
  2182. */
  2183. static ngtcp2_ssize conn_write_handshake_ack_pkts(ngtcp2_conn *conn,
  2184. ngtcp2_pkt_info *pi,
  2185. uint8_t *dest, size_t destlen,
  2186. ngtcp2_tstamp ts) {
  2187. ngtcp2_ssize res = 0, nwrite = 0;
  2188. /* In the most cases, client sends ACK in conn_write_handshake_pkt.
  2189. This function is only called when it is CWND limited or pacing
  2190. limited. It is not required for client to send ACK for server
  2191. Initial. This is because once it gets server Initial, it gets
  2192. Handshake tx key and discards Initial key. The only good reason
  2193. to send ACK is give server RTT measurement early. */
  2194. if (conn->server && conn->in_pktns) {
  2195. nwrite =
  2196. conn_write_ack_pkt(conn, pi, dest, destlen, NGTCP2_PKT_INITIAL, ts);
  2197. if (nwrite < 0) {
  2198. assert(nwrite != NGTCP2_ERR_NOBUF);
  2199. return nwrite;
  2200. }
  2201. res += nwrite;
  2202. dest += nwrite;
  2203. destlen -= (size_t)nwrite;
  2204. }
  2205. if (conn->hs_pktns->crypto.tx.ckm) {
  2206. nwrite =
  2207. conn_write_ack_pkt(conn, pi, dest, destlen, NGTCP2_PKT_HANDSHAKE, ts);
  2208. if (nwrite < 0) {
  2209. assert(nwrite != NGTCP2_ERR_NOBUF);
  2210. return nwrite;
  2211. }
  2212. res += nwrite;
  2213. if (!conn->server && nwrite) {
  2214. ngtcp2_conn_discard_initial_state(conn, ts);
  2215. }
  2216. }
  2217. return res;
  2218. }
  2219. /*
  2220. * conn_write_client_initial writes Initial packet in the buffer
  2221. * pointed by |dest| whose length is |destlen|.
  2222. *
  2223. * This function returns the number of bytes written in |dest| if it
  2224. * succeeds, or one of the following negative error codes:
  2225. *
  2226. * NGTCP2_ERR_NOMEM
  2227. * Out of memory.
  2228. * NGTCP2_ERR_CALLBACK_FAILURE
  2229. * User-defined callback function failed.
  2230. */
  2231. static ngtcp2_ssize conn_write_client_initial(ngtcp2_conn *conn,
  2232. ngtcp2_pkt_info *pi,
  2233. uint8_t *dest, size_t destlen,
  2234. uint64_t early_datalen,
  2235. ngtcp2_tstamp ts) {
  2236. int rv;
  2237. rv = conn_call_client_initial(conn);
  2238. if (rv != 0) {
  2239. return rv;
  2240. }
  2241. return conn_write_handshake_pkt(
  2242. conn, pi, dest, destlen, 0, NGTCP2_PKT_INITIAL, NGTCP2_WRITE_PKT_FLAG_NONE,
  2243. early_datalen, ts);
  2244. }
  2245. /*
  2246. * dcid_tx_left returns the maximum number of bytes that server is
  2247. * allowed to send to an unvalidated path associated to |dcid|.
  2248. */
  2249. static uint64_t dcid_tx_left(ngtcp2_dcid *dcid) {
  2250. if (dcid->flags & NGTCP2_DCID_FLAG_PATH_VALIDATED) {
  2251. return SIZE_MAX;
  2252. }
  2253. /* From QUIC spec: Prior to validating the client address, servers
  2254. MUST NOT send more than three times as many bytes as the number
  2255. of bytes they have received. */
  2256. assert(dcid->bytes_recv * 3 >= dcid->bytes_sent);
  2257. return dcid->bytes_recv * 3 - dcid->bytes_sent;
  2258. }
  2259. /*
  2260. * conn_server_tx_left returns the maximum number of bytes that server
  2261. * is allowed to send to an unvalidated path.
  2262. */
  2263. static uint64_t conn_server_tx_left(ngtcp2_conn *conn, ngtcp2_dcid *dcid) {
  2264. assert(conn->server);
  2265. /* If pv->dcid has the current path, use conn->dcid.current. This
  2266. is because conn->dcid.current gets update for bytes_recv and
  2267. bytes_sent. */
  2268. if (ngtcp2_path_eq(&dcid->ps.path, &conn->dcid.current.ps.path)) {
  2269. return dcid_tx_left(&conn->dcid.current);
  2270. }
  2271. return dcid_tx_left(dcid);
  2272. }
  2273. /*
  2274. * conn_write_handshake_pkts writes Initial and Handshake packets in
  2275. * the buffer pointed by |dest| whose length is |destlen|.
  2276. *
  2277. * This function returns the number of bytes written in |dest| if it
  2278. * succeeds, or one of the following negative error codes:
  2279. *
  2280. * NGTCP2_ERR_NOMEM
  2281. * Out of memory.
  2282. * NGTCP2_ERR_CALLBACK_FAILURE
  2283. * User-defined callback function failed.
  2284. */
  2285. static ngtcp2_ssize conn_write_handshake_pkts(ngtcp2_conn *conn,
  2286. ngtcp2_pkt_info *pi,
  2287. uint8_t *dest, size_t destlen,
  2288. uint64_t write_datalen,
  2289. ngtcp2_tstamp ts) {
  2290. ngtcp2_ssize nwrite;
  2291. ngtcp2_ssize res = 0;
  2292. ngtcp2_rtb_entry *rtbent;
  2293. uint8_t wflags = NGTCP2_WRITE_PKT_FLAG_NONE;
  2294. ngtcp2_conn_stat *cstat = &conn->cstat;
  2295. ngtcp2_ksl_it it;
  2296. /* As a client, we would like to discard Initial packet number space
  2297. when sending the first Handshake packet. When sending Handshake
  2298. packet, it should be one of 1) sending ACK, 2) sending PTO probe
  2299. packet, or 3) sending CRYPTO. If we have pending acknowledgement
  2300. for Initial, then do not discard Initial packet number space.
  2301. Otherwise, if either 1) or 2) is satisfied, discard Initial
  2302. packet number space. When sending Handshake CRYPTO, it indicates
  2303. that client has received Handshake CRYPTO from server. Initial
  2304. packet number space is discarded because 1) is met. If there is
  2305. pending Initial ACK, Initial packet number space is discarded
  2306. after writing the first Handshake packet.
  2307. */
  2308. if (!conn->server && conn->hs_pktns->crypto.tx.ckm && conn->in_pktns &&
  2309. !ngtcp2_acktr_require_active_ack(&conn->in_pktns->acktr,
  2310. /* max_ack_delay = */ 0, ts) &&
  2311. (ngtcp2_acktr_require_active_ack(&conn->hs_pktns->acktr,
  2312. /* max_ack_delay = */ 0, ts) ||
  2313. conn->hs_pktns->rtb.probe_pkt_left)) {
  2314. /* Discard Initial state here so that Handshake packet is not
  2315. padded. */
  2316. ngtcp2_conn_discard_initial_state(conn, ts);
  2317. } else if (conn->in_pktns) {
  2318. nwrite =
  2319. conn_write_handshake_pkt(conn, pi, dest, destlen, 0, NGTCP2_PKT_INITIAL,
  2320. NGTCP2_WRITE_PKT_FLAG_NONE, write_datalen, ts);
  2321. if (nwrite < 0) {
  2322. assert(nwrite != NGTCP2_ERR_NOBUF);
  2323. return nwrite;
  2324. }
  2325. if (nwrite == 0) {
  2326. if (conn->server &&
  2327. (conn->in_pktns->rtb.probe_pkt_left ||
  2328. !ngtcp2_strm_streamfrq_empty(&conn->in_pktns->crypto.strm))) {
  2329. if (cstat->loss_detection_timer != UINT64_MAX &&
  2330. conn_server_tx_left(conn, &conn->dcid.current) <
  2331. NGTCP2_MAX_UDP_PAYLOAD_SIZE) {
  2332. ngtcp2_log_info(
  2333. &conn->log, NGTCP2_LOG_EVENT_LDC,
  2334. "loss detection timer canceled due to amplification limit");
  2335. ngtcp2_conn_cancel_loss_detection_timer(conn);
  2336. }
  2337. return 0;
  2338. }
  2339. } else {
  2340. res += nwrite;
  2341. dest += nwrite;
  2342. destlen -= (size_t)nwrite;
  2343. /* If initial packet size is at least
  2344. NGTCP2_MAX_UDP_PAYLOAD_SIZE, no extra padding is needed in a
  2345. subsequent packet. */
  2346. if (nwrite < NGTCP2_MAX_UDP_PAYLOAD_SIZE) {
  2347. if (conn->server) {
  2348. it = ngtcp2_rtb_head(&conn->in_pktns->rtb);
  2349. if (!ngtcp2_ksl_it_end(&it)) {
  2350. rtbent = ngtcp2_ksl_it_get(&it);
  2351. if (rtbent->flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) {
  2352. wflags |= NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING;
  2353. }
  2354. }
  2355. } else {
  2356. wflags |= NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING;
  2357. }
  2358. }
  2359. }
  2360. }
  2361. nwrite =
  2362. conn_write_handshake_pkt(conn, pi, dest, destlen, (size_t)res,
  2363. NGTCP2_PKT_HANDSHAKE, wflags, write_datalen, ts);
  2364. if (nwrite < 0) {
  2365. assert(nwrite != NGTCP2_ERR_NOBUF);
  2366. return nwrite;
  2367. }
  2368. res += nwrite;
  2369. if (!conn->server && conn->hs_pktns->crypto.tx.ckm && nwrite) {
  2370. /* We don't need to send further Initial packet if we have
  2371. Handshake key and sent something with it. So discard initial
  2372. state here. */
  2373. ngtcp2_conn_discard_initial_state(conn, ts);
  2374. }
  2375. return res;
  2376. }
  2377. /*
  2378. * conn_initial_stream_rx_offset returns the initial maximum offset of
  2379. * data for a stream denoted by |stream_id|.
  2380. */
  2381. static uint64_t conn_initial_stream_rx_offset(ngtcp2_conn *conn,
  2382. int64_t stream_id) {
  2383. int local_stream = conn_local_stream(conn, stream_id);
  2384. if (bidi_stream(stream_id)) {
  2385. if (local_stream) {
  2386. return conn->local.transport_params.initial_max_stream_data_bidi_local;
  2387. }
  2388. return conn->local.transport_params.initial_max_stream_data_bidi_remote;
  2389. }
  2390. if (local_stream) {
  2391. return 0;
  2392. }
  2393. return conn->local.transport_params.initial_max_stream_data_uni;
  2394. }
  2395. /*
  2396. * conn_should_send_max_stream_data returns nonzero if MAX_STREAM_DATA
  2397. * frame should be send for |strm|.
  2398. */
  2399. static int conn_should_send_max_stream_data(ngtcp2_conn *conn,
  2400. ngtcp2_strm *strm) {
  2401. uint64_t inc = strm->rx.unsent_max_offset - strm->rx.max_offset;
  2402. (void)conn;
  2403. return strm->rx.window < 4 * inc;
  2404. }
  2405. /*
  2406. * conn_should_send_max_data returns nonzero if MAX_DATA frame should
  2407. * be sent.
  2408. */
  2409. static int conn_should_send_max_data(ngtcp2_conn *conn) {
  2410. uint64_t inc = conn->rx.unsent_max_offset - conn->rx.max_offset;
  2411. return conn->rx.window < 4 * inc;
  2412. }
  2413. /*
  2414. * conn_required_num_new_connection_id returns the number of
  2415. * additional connection ID the local endpoint has to provide to the
  2416. * remote endpoint.
  2417. */
  2418. static size_t conn_required_num_new_connection_id(ngtcp2_conn *conn) {
  2419. uint64_t n;
  2420. size_t len = ngtcp2_ksl_len(&conn->scid.set);
  2421. size_t lim;
  2422. if (len >= NGTCP2_MAX_SCID_POOL_SIZE) {
  2423. return 0;
  2424. }
  2425. assert(NGTCP2_MAX_SCID_POOL_SIZE >= conn->scid.num_in_flight);
  2426. lim = NGTCP2_MAX_SCID_POOL_SIZE - conn->scid.num_in_flight;
  2427. if (lim == 0) {
  2428. return 0;
  2429. }
  2430. assert(conn->remote.transport_params);
  2431. assert(conn->remote.transport_params->active_connection_id_limit);
  2432. /* len includes retired CID. We don't provide extra CID if doing so
  2433. exceeds NGTCP2_MAX_SCID_POOL_SIZE. */
  2434. n = conn->remote.transport_params->active_connection_id_limit +
  2435. conn->scid.num_retired;
  2436. n = ngtcp2_min_uint64(NGTCP2_MAX_SCID_POOL_SIZE, n) - len;
  2437. return (size_t)ngtcp2_min_uint64(lim, n);
  2438. }
  2439. /*
  2440. * conn_enqueue_new_connection_id generates additional connection IDs
  2441. * and prepares to send them to the remote endpoint.
  2442. *
  2443. * This function returns 0 if it succeeds, or one of the following
  2444. * negative error codes:
  2445. *
  2446. * NGTCP2_ERR_NOMEM
  2447. * Out of memory.
  2448. * NGTCP2_ERR_CALLBACK_FAILURE
  2449. * User-defined callback function failed.
  2450. */
  2451. static int conn_enqueue_new_connection_id(ngtcp2_conn *conn) {
  2452. size_t i, need = conn_required_num_new_connection_id(conn);
  2453. size_t cidlen = conn->oscid.datalen;
  2454. ngtcp2_cid cid;
  2455. uint64_t seq;
  2456. int rv;
  2457. uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN];
  2458. ngtcp2_frame_chain *nfrc;
  2459. ngtcp2_pktns *pktns = &conn->pktns;
  2460. ngtcp2_scid *scid;
  2461. ngtcp2_ksl_it it;
  2462. for (i = 0; i < need; ++i) {
  2463. rv = conn_call_get_new_connection_id(conn, &cid, token, cidlen);
  2464. if (rv != 0) {
  2465. return rv;
  2466. }
  2467. if (cid.datalen != cidlen) {
  2468. return NGTCP2_ERR_CALLBACK_FAILURE;
  2469. }
  2470. /* Assert uniqueness */
  2471. it = ngtcp2_ksl_lower_bound(&conn->scid.set, &cid);
  2472. if (!ngtcp2_ksl_it_end(&it) &&
  2473. ngtcp2_cid_eq(ngtcp2_ksl_it_key(&it), &cid)) {
  2474. return NGTCP2_ERR_CALLBACK_FAILURE;
  2475. }
  2476. seq = ++conn->scid.last_seq;
  2477. scid = ngtcp2_mem_malloc(conn->mem, sizeof(*scid));
  2478. if (scid == NULL) {
  2479. return NGTCP2_ERR_NOMEM;
  2480. }
  2481. ngtcp2_scid_init(scid, seq, &cid);
  2482. rv = ngtcp2_ksl_insert(&conn->scid.set, NULL, &scid->cid, scid);
  2483. if (rv != 0) {
  2484. ngtcp2_mem_free(conn->mem, scid);
  2485. return rv;
  2486. }
  2487. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  2488. if (rv != 0) {
  2489. return rv;
  2490. }
  2491. nfrc->fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
  2492. nfrc->fr.new_connection_id.seq = seq;
  2493. nfrc->fr.new_connection_id.retire_prior_to = 0;
  2494. nfrc->fr.new_connection_id.cid = cid;
  2495. memcpy(nfrc->fr.new_connection_id.stateless_reset_token, token,
  2496. sizeof(token));
  2497. nfrc->next = pktns->tx.frq;
  2498. pktns->tx.frq = nfrc;
  2499. assert(NGTCP2_MAX_SCID_POOL_SIZE > conn->scid.num_in_flight);
  2500. ++conn->scid.num_in_flight;
  2501. }
  2502. return 0;
  2503. }
  2504. /*
  2505. * conn_remove_retired_connection_id removes the already retired
  2506. * connection ID. It waits PTO before actually removing a connection
  2507. * ID after it receives RETIRE_CONNECTION_ID from peer to catch
  2508. * reordered packets.
  2509. *
  2510. * This function returns 0 if it succeeds, or one of the following
  2511. * negative error codes:
  2512. *
  2513. * NGTCP2_ERR_NOMEM
  2514. * Out of memory.
  2515. * NGTCP2_ERR_CALLBACK_FAILURE
  2516. * User-defined callback function failed.
  2517. */
  2518. static int conn_remove_retired_connection_id(ngtcp2_conn *conn,
  2519. ngtcp2_duration pto,
  2520. ngtcp2_tstamp ts) {
  2521. ngtcp2_duration timeout = pto;
  2522. ngtcp2_scid *scid;
  2523. ngtcp2_dcid *dcid;
  2524. int rv;
  2525. for (; !ngtcp2_pq_empty(&conn->scid.used);) {
  2526. scid = ngtcp2_struct_of(ngtcp2_pq_top(&conn->scid.used), ngtcp2_scid, pe);
  2527. if (!ngtcp2_tstamp_elapsed(scid->retired_ts, timeout, ts)) {
  2528. break;
  2529. }
  2530. assert(scid->flags & NGTCP2_SCID_FLAG_RETIRED);
  2531. rv = conn_call_remove_connection_id(conn, &scid->cid);
  2532. if (rv != 0) {
  2533. return rv;
  2534. }
  2535. ngtcp2_ksl_remove(&conn->scid.set, NULL, &scid->cid);
  2536. ngtcp2_pq_pop(&conn->scid.used);
  2537. ngtcp2_mem_free(conn->mem, scid);
  2538. assert(conn->scid.num_retired);
  2539. --conn->scid.num_retired;
  2540. }
  2541. for (; ngtcp2_ringbuf_len(&conn->dcid.retired.rb);) {
  2542. dcid = ngtcp2_ringbuf_get(&conn->dcid.retired.rb, 0);
  2543. if (dcid->retired_ts + timeout >= ts) {
  2544. break;
  2545. }
  2546. rv = conn_call_deactivate_dcid(conn, dcid);
  2547. if (rv != 0) {
  2548. return rv;
  2549. }
  2550. ngtcp2_ringbuf_pop_front(&conn->dcid.retired.rb);
  2551. }
  2552. return 0;
  2553. }
  2554. /*
  2555. * conn_min_pktlen returns the minimum length of packet this endpoint
  2556. * sends. It may underestimate the length because this does not take
  2557. * into account header protection sample.
  2558. */
  2559. static size_t conn_min_pktlen(ngtcp2_conn *conn) {
  2560. return conn->oscid.datalen + NGTCP2_MIN_PKT_EXPANDLEN;
  2561. }
  2562. /*
  2563. * conn_handle_unconfirmed_key_update_from_remote deals with key
  2564. * update which has not been confirmed yet and initiated by the remote
  2565. * endpoint.
  2566. *
  2567. * If key update was initiated by the remote endpoint, acknowledging a
  2568. * packet encrypted with the new key completes key update procedure.
  2569. */
  2570. static void conn_handle_unconfirmed_key_update_from_remote(ngtcp2_conn *conn,
  2571. int64_t largest_ack,
  2572. ngtcp2_tstamp ts) {
  2573. if (!(conn->flags & NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED) ||
  2574. (conn->flags & NGTCP2_CONN_FLAG_KEY_UPDATE_INITIATOR) ||
  2575. largest_ack < conn->pktns.crypto.rx.ckm->pkt_num) {
  2576. return;
  2577. }
  2578. conn->flags &= (uint32_t)~NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED;
  2579. conn->crypto.key_update.confirmed_ts = ts;
  2580. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CRY, "key update confirmed");
  2581. }
  2582. static uint64_t conn_tx_strmq_first_cycle(ngtcp2_conn *conn);
  2583. /*
  2584. * strm_should_send_stream_data_blocked returns nonzero if
  2585. * STREAM_DATA_BLOCKED frame should be sent to |strm|.
  2586. */
  2587. static int strm_should_send_stream_data_blocked(ngtcp2_strm *strm) {
  2588. return strm->tx.offset == strm->tx.max_offset &&
  2589. strm->tx.last_blocked_offset != strm->tx.max_offset;
  2590. }
  2591. /*
  2592. * conn_should_send_data_blocked returns nonzero if DATA_BLOCKED frame
  2593. * should be sent.
  2594. */
  2595. static int conn_should_send_data_blocked(ngtcp2_conn *conn) {
  2596. return conn->tx.offset == conn->tx.max_offset &&
  2597. conn->tx.last_blocked_offset != conn->tx.max_offset;
  2598. }
  2599. /*
  2600. * conn_reset_ppe_pending clears NGTCP2_CONN_FLAG_PPE_PENDING flag and
  2601. * nullifies conn->pkt.
  2602. */
  2603. static void conn_reset_ppe_pending(ngtcp2_conn *conn) {
  2604. conn->flags &= (uint32_t)~NGTCP2_CONN_FLAG_PPE_PENDING;
  2605. memset(&conn->pkt, 0, sizeof(conn->pkt));
  2606. }
  2607. /*
  2608. * conn_write_pkt writes a protected packet in the buffer pointed by
  2609. * |dest| whose length if |destlen|. |dgram_offset| is the offset in
  2610. * UDP datagram payload where this QUIC packet is positioned at.
  2611. * |type| specifies the type of packet. It can be NGTCP2_PKT_1RTT or
  2612. * NGTCP2_PKT_0RTT.
  2613. *
  2614. * This function can send new stream data. In order to send stream
  2615. * data, specify the underlying stream and parameters to
  2616. * |vmsg|->stream. If |vmsg|->stream.fin is set to nonzero, it
  2617. * signals that the given data is the final portion of the stream.
  2618. * |vmsg|->stream.data vector of length |vmsg|->stream.datacnt
  2619. * specifies stream data to send. The number of bytes sent to the
  2620. * stream is assigned to *|vmsg|->stream.pdatalen. If 0 length STREAM
  2621. * data is sent, 0 is assigned to it. The caller should initialize
  2622. * *|vmsg|->stream.pdatalen to -1.
  2623. *
  2624. * If |require_padding| is nonzero, padding bytes are added to occupy
  2625. * the remaining packet payload.
  2626. *
  2627. * This function returns the number of bytes written in |dest| if it
  2628. * succeeds, or one of the following negative error codes:
  2629. *
  2630. * NGTCP2_ERR_NOMEM
  2631. * Out of memory.
  2632. * NGTCP2_ERR_CALLBACK_FAILURE
  2633. * User-defined callback function failed.
  2634. * NGTCP2_ERR_STREAM_DATA_BLOCKED
  2635. * Stream data could not be written because of flow control.
  2636. */
  2637. static ngtcp2_ssize conn_write_pkt(ngtcp2_conn *conn, ngtcp2_pkt_info *pi,
  2638. uint8_t *dest, size_t destlen,
  2639. size_t dgram_offset, ngtcp2_vmsg *vmsg,
  2640. uint8_t type, uint8_t flags,
  2641. ngtcp2_tstamp ts) {
  2642. int rv = 0;
  2643. ngtcp2_crypto_cc *cc = &conn->pkt.cc;
  2644. ngtcp2_ppe *ppe = &conn->pkt.ppe;
  2645. ngtcp2_pkt_hd *hd = &conn->pkt.hd;
  2646. ngtcp2_frame *ackfr = NULL, lfr;
  2647. ngtcp2_ssize nwrite;
  2648. ngtcp2_frame_chain **pfrc, *nfrc, *frc;
  2649. ngtcp2_rtb_entry *ent;
  2650. ngtcp2_strm *strm;
  2651. int pkt_empty = 1;
  2652. uint64_t ndatalen = 0;
  2653. int send_stream = 0;
  2654. int stream_blocked = 0;
  2655. int send_datagram = 0;
  2656. ngtcp2_pktns *pktns = &conn->pktns;
  2657. size_t left;
  2658. uint64_t datalen = 0;
  2659. ngtcp2_vec data[NGTCP2_MAX_STREAM_DATACNT];
  2660. size_t datacnt;
  2661. uint16_t rtb_entry_flags = NGTCP2_RTB_ENTRY_FLAG_NONE;
  2662. int hd_logged = 0;
  2663. ngtcp2_path_challenge_entry *pcent;
  2664. uint8_t hd_flags = NGTCP2_PKT_FLAG_NONE;
  2665. int require_padding = (flags & NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING) != 0;
  2666. int write_more = (flags & NGTCP2_WRITE_PKT_FLAG_MORE) != 0;
  2667. int ppe_pending = (conn->flags & NGTCP2_CONN_FLAG_PPE_PENDING) != 0;
  2668. size_t min_pktlen = conn_min_pktlen(conn);
  2669. int min_padded = 0;
  2670. int padded = 0;
  2671. ngtcp2_cc_pkt cc_pkt;
  2672. uint64_t crypto_offset;
  2673. uint64_t stream_offset;
  2674. ngtcp2_ssize num_reclaimed;
  2675. int fin;
  2676. uint64_t target_max_data;
  2677. ngtcp2_conn_stat *cstat = &conn->cstat;
  2678. uint64_t delta;
  2679. const ngtcp2_cid *scid = NULL;
  2680. int keep_alive_expired = 0;
  2681. uint32_t version = 0;
  2682. /* Return 0 if destlen is less than minimum packet length which can
  2683. trigger Stateless Reset */
  2684. if (destlen < min_pktlen) {
  2685. return 0;
  2686. }
  2687. if (vmsg) {
  2688. switch (vmsg->type) {
  2689. case NGTCP2_VMSG_TYPE_STREAM:
  2690. datalen = ngtcp2_vec_len(vmsg->stream.data, vmsg->stream.datacnt);
  2691. ndatalen = conn_enforce_flow_control(conn, vmsg->stream.strm, datalen);
  2692. /* 0 length STREAM frame is allowed */
  2693. if (ndatalen || datalen == 0) {
  2694. send_stream = 1;
  2695. } else {
  2696. stream_blocked = 1;
  2697. }
  2698. break;
  2699. case NGTCP2_VMSG_TYPE_DATAGRAM:
  2700. datalen = ngtcp2_vec_len(vmsg->datagram.data, vmsg->datagram.datacnt);
  2701. send_datagram = 1;
  2702. break;
  2703. default:
  2704. break;
  2705. }
  2706. }
  2707. if (!ppe_pending) {
  2708. switch (type) {
  2709. case NGTCP2_PKT_1RTT:
  2710. hd_flags = conn_pkt_flags_short(conn);
  2711. scid = NULL;
  2712. cc->aead = pktns->crypto.ctx.aead;
  2713. cc->hp = pktns->crypto.ctx.hp;
  2714. cc->ckm = pktns->crypto.tx.ckm;
  2715. cc->hp_ctx = pktns->crypto.tx.hp_ctx;
  2716. assert(conn->negotiated_version);
  2717. version = conn->negotiated_version;
  2718. /* transport parameter is only valid after handshake completion
  2719. which means we don't know how many connection ID that remote
  2720. peer can accept before handshake completion. Because server
  2721. can use remote transport parameters sending stream data in
  2722. 0.5 RTT, it is also allowed to use remote transport
  2723. parameters here. */
  2724. if (conn->oscid.datalen &&
  2725. (conn->server || conn_is_tls_handshake_completed(conn))) {
  2726. rv = conn_enqueue_new_connection_id(conn);
  2727. if (rv != 0) {
  2728. return rv;
  2729. }
  2730. }
  2731. break;
  2732. case NGTCP2_PKT_0RTT:
  2733. assert(!conn->server);
  2734. if (!conn->early.ckm) {
  2735. return 0;
  2736. }
  2737. hd_flags = conn_pkt_flags_long(conn);
  2738. scid = &conn->oscid;
  2739. cc->aead = conn->early.ctx.aead;
  2740. cc->hp = conn->early.ctx.hp;
  2741. cc->ckm = conn->early.ckm;
  2742. cc->hp_ctx = conn->early.hp_ctx;
  2743. version = conn->client_chosen_version;
  2744. break;
  2745. default:
  2746. /* Unreachable */
  2747. ngtcp2_unreachable();
  2748. }
  2749. cc->encrypt = conn->callbacks.encrypt;
  2750. cc->hp_mask = conn->callbacks.hp_mask;
  2751. if (conn_should_send_max_data(conn)) {
  2752. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  2753. if (rv != 0) {
  2754. return rv;
  2755. }
  2756. if (conn->local.settings.max_window &&
  2757. conn->tx.last_max_data_ts != UINT64_MAX &&
  2758. ts - conn->tx.last_max_data_ts <
  2759. NGTCP2_FLOW_WINDOW_RTT_FACTOR * cstat->smoothed_rtt &&
  2760. conn->local.settings.max_window > conn->rx.window) {
  2761. target_max_data = NGTCP2_FLOW_WINDOW_SCALING_FACTOR * conn->rx.window;
  2762. if (target_max_data > conn->local.settings.max_window) {
  2763. target_max_data = conn->local.settings.max_window;
  2764. }
  2765. delta = target_max_data - conn->rx.window;
  2766. if (conn->rx.unsent_max_offset + delta > NGTCP2_MAX_VARINT) {
  2767. delta = NGTCP2_MAX_VARINT - conn->rx.unsent_max_offset;
  2768. }
  2769. conn->rx.window = target_max_data;
  2770. } else {
  2771. delta = 0;
  2772. }
  2773. conn->tx.last_max_data_ts = ts;
  2774. nfrc->fr.type = NGTCP2_FRAME_MAX_DATA;
  2775. nfrc->fr.max_data.max_data = conn->rx.unsent_max_offset + delta;
  2776. nfrc->next = pktns->tx.frq;
  2777. pktns->tx.frq = nfrc;
  2778. conn->rx.max_offset = conn->rx.unsent_max_offset =
  2779. nfrc->fr.max_data.max_data;
  2780. }
  2781. if (stream_blocked && conn_should_send_max_data(conn)) {
  2782. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  2783. if (rv != 0) {
  2784. return rv;
  2785. }
  2786. nfrc->fr.type = NGTCP2_FRAME_DATA_BLOCKED;
  2787. nfrc->fr.data_blocked.offset = conn->tx.max_offset;
  2788. nfrc->next = pktns->tx.frq;
  2789. pktns->tx.frq = nfrc;
  2790. conn->tx.last_blocked_offset = conn->tx.max_offset;
  2791. }
  2792. if (stream_blocked && !ngtcp2_strm_is_tx_queued(vmsg->stream.strm) &&
  2793. strm_should_send_stream_data_blocked(vmsg->stream.strm)) {
  2794. assert(vmsg);
  2795. assert(vmsg->type == NGTCP2_VMSG_TYPE_STREAM);
  2796. vmsg->stream.strm->cycle = conn_tx_strmq_first_cycle(conn);
  2797. rv = ngtcp2_conn_tx_strmq_push(conn, vmsg->stream.strm);
  2798. if (rv != 0) {
  2799. return rv;
  2800. }
  2801. }
  2802. ngtcp2_pkt_hd_init(hd, hd_flags, type, &conn->dcid.current.cid, scid,
  2803. pktns->tx.last_pkt_num + 1,
  2804. pktns_select_pkt_numlen(pktns), version, 0);
  2805. ngtcp2_ppe_init(ppe, dest, destlen, dgram_offset, cc);
  2806. rv = ngtcp2_ppe_encode_hd(ppe, hd);
  2807. if (rv != 0) {
  2808. assert(NGTCP2_ERR_NOBUF == rv);
  2809. return 0;
  2810. }
  2811. if (!ngtcp2_ppe_ensure_hp_sample(ppe)) {
  2812. return 0;
  2813. }
  2814. if (ngtcp2_ringbuf_len(&conn->rx.path_challenge.rb)) {
  2815. pcent = ngtcp2_ringbuf_get(&conn->rx.path_challenge.rb, 0);
  2816. /* PATH_RESPONSE is bound to the path that the corresponding
  2817. PATH_CHALLENGE is received. */
  2818. if (ngtcp2_path_eq(&conn->dcid.current.ps.path, &pcent->ps.path)) {
  2819. lfr.type = NGTCP2_FRAME_PATH_RESPONSE;
  2820. memcpy(lfr.path_response.data, pcent->data,
  2821. sizeof(lfr.path_response.data));
  2822. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &lfr);
  2823. if (rv != 0) {
  2824. assert(NGTCP2_ERR_NOBUF == rv);
  2825. } else {
  2826. ngtcp2_ringbuf_pop_front(&conn->rx.path_challenge.rb);
  2827. pkt_empty = 0;
  2828. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING;
  2829. require_padding = require_padding || !conn->server ||
  2830. destlen >= NGTCP2_MAX_UDP_PAYLOAD_SIZE;
  2831. /* We don't retransmit PATH_RESPONSE. */
  2832. }
  2833. }
  2834. }
  2835. rv = ngtcp2_conn_create_ack_frame(
  2836. conn, &ackfr, pktns, type, ts, conn_compute_ack_delay(conn),
  2837. conn->local.transport_params.ack_delay_exponent);
  2838. if (rv != 0) {
  2839. assert(ngtcp2_err_is_fatal(rv));
  2840. return rv;
  2841. }
  2842. if (ackfr) {
  2843. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, ackfr);
  2844. if (rv != 0) {
  2845. assert(NGTCP2_ERR_NOBUF == rv);
  2846. } else {
  2847. ngtcp2_acktr_commit_ack(&pktns->acktr);
  2848. ngtcp2_acktr_add_ack(&pktns->acktr, hd->pkt_num,
  2849. ackfr->ack.largest_ack);
  2850. assert(NGTCP2_PKT_1RTT == type);
  2851. conn_handle_unconfirmed_key_update_from_remote(
  2852. conn, ackfr->ack.largest_ack, ts);
  2853. pkt_empty = 0;
  2854. }
  2855. }
  2856. build_pkt:
  2857. for (pfrc = &pktns->tx.frq; *pfrc;) {
  2858. if ((*pfrc)->binder &&
  2859. ((*pfrc)->binder->flags & NGTCP2_FRAME_CHAIN_BINDER_FLAG_ACK)) {
  2860. frc = *pfrc;
  2861. *pfrc = (*pfrc)->next;
  2862. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  2863. continue;
  2864. }
  2865. switch ((*pfrc)->fr.type) {
  2866. case NGTCP2_FRAME_RESET_STREAM:
  2867. strm =
  2868. ngtcp2_conn_find_stream(conn, (*pfrc)->fr.reset_stream.stream_id);
  2869. if (strm == NULL ||
  2870. !ngtcp2_strm_require_retransmit_reset_stream(strm)) {
  2871. frc = *pfrc;
  2872. *pfrc = (*pfrc)->next;
  2873. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  2874. continue;
  2875. }
  2876. break;
  2877. case NGTCP2_FRAME_STOP_SENDING:
  2878. strm =
  2879. ngtcp2_conn_find_stream(conn, (*pfrc)->fr.stop_sending.stream_id);
  2880. if (strm == NULL ||
  2881. !ngtcp2_strm_require_retransmit_stop_sending(strm)) {
  2882. frc = *pfrc;
  2883. *pfrc = (*pfrc)->next;
  2884. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  2885. continue;
  2886. }
  2887. break;
  2888. case NGTCP2_FRAME_STREAM:
  2889. ngtcp2_unreachable();
  2890. case NGTCP2_FRAME_MAX_STREAMS_BIDI:
  2891. if ((*pfrc)->fr.max_streams.max_streams <
  2892. conn->remote.bidi.max_streams) {
  2893. frc = *pfrc;
  2894. *pfrc = (*pfrc)->next;
  2895. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  2896. continue;
  2897. }
  2898. break;
  2899. case NGTCP2_FRAME_MAX_STREAMS_UNI:
  2900. if ((*pfrc)->fr.max_streams.max_streams <
  2901. conn->remote.uni.max_streams) {
  2902. frc = *pfrc;
  2903. *pfrc = (*pfrc)->next;
  2904. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  2905. continue;
  2906. }
  2907. break;
  2908. case NGTCP2_FRAME_MAX_STREAM_DATA:
  2909. strm =
  2910. ngtcp2_conn_find_stream(conn, (*pfrc)->fr.max_stream_data.stream_id);
  2911. if (strm == NULL || !ngtcp2_strm_require_retransmit_max_stream_data(
  2912. strm, &(*pfrc)->fr.max_stream_data)) {
  2913. frc = *pfrc;
  2914. *pfrc = (*pfrc)->next;
  2915. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  2916. continue;
  2917. }
  2918. break;
  2919. case NGTCP2_FRAME_MAX_DATA:
  2920. if ((*pfrc)->fr.max_data.max_data < conn->rx.max_offset) {
  2921. frc = *pfrc;
  2922. *pfrc = (*pfrc)->next;
  2923. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  2924. continue;
  2925. }
  2926. break;
  2927. case NGTCP2_FRAME_STREAM_DATA_BLOCKED:
  2928. strm = ngtcp2_conn_find_stream(
  2929. conn, (*pfrc)->fr.stream_data_blocked.stream_id);
  2930. if (strm == NULL || !ngtcp2_strm_require_retransmit_stream_data_blocked(
  2931. strm, &(*pfrc)->fr.stream_data_blocked)) {
  2932. frc = *pfrc;
  2933. *pfrc = (*pfrc)->next;
  2934. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  2935. continue;
  2936. }
  2937. break;
  2938. case NGTCP2_FRAME_DATA_BLOCKED:
  2939. if ((*pfrc)->fr.data_blocked.offset != conn->tx.max_offset) {
  2940. frc = *pfrc;
  2941. *pfrc = (*pfrc)->next;
  2942. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  2943. continue;
  2944. }
  2945. break;
  2946. case NGTCP2_FRAME_CRYPTO:
  2947. ngtcp2_unreachable();
  2948. }
  2949. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &(*pfrc)->fr);
  2950. if (rv != 0) {
  2951. assert(NGTCP2_ERR_NOBUF == rv);
  2952. break;
  2953. }
  2954. pkt_empty = 0;
  2955. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  2956. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  2957. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  2958. pfrc = &(*pfrc)->next;
  2959. }
  2960. if (*pfrc == NULL) {
  2961. for (; !ngtcp2_strm_streamfrq_empty(&pktns->crypto.strm);) {
  2962. left = ngtcp2_ppe_left(ppe);
  2963. crypto_offset =
  2964. ngtcp2_strm_streamfrq_unacked_offset(&pktns->crypto.strm);
  2965. if (crypto_offset == (uint64_t)-1) {
  2966. ngtcp2_strm_streamfrq_clear(&pktns->crypto.strm);
  2967. break;
  2968. }
  2969. left = ngtcp2_pkt_crypto_max_datalen(crypto_offset, left, left);
  2970. if (left == (size_t)-1) {
  2971. break;
  2972. }
  2973. rv = ngtcp2_strm_streamfrq_pop(&pktns->crypto.strm, &nfrc, left);
  2974. if (rv != 0) {
  2975. assert(ngtcp2_err_is_fatal(rv));
  2976. return rv;
  2977. }
  2978. if (nfrc == NULL) {
  2979. break;
  2980. }
  2981. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  2982. if (rv != 0) {
  2983. ngtcp2_unreachable();
  2984. }
  2985. *pfrc = nfrc;
  2986. pfrc = &(*pfrc)->next;
  2987. pkt_empty = 0;
  2988. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  2989. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  2990. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  2991. }
  2992. }
  2993. if (*pfrc == NULL) {
  2994. for (; !ngtcp2_pq_empty(&conn->tx.strmq);) {
  2995. strm = ngtcp2_conn_tx_strmq_top(conn);
  2996. if (strm->flags & NGTCP2_STRM_FLAG_SEND_RESET_STREAM) {
  2997. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  2998. if (rv != 0) {
  2999. return rv;
  3000. }
  3001. nfrc->fr.type = NGTCP2_FRAME_RESET_STREAM;
  3002. nfrc->fr.reset_stream.stream_id = strm->stream_id;
  3003. nfrc->fr.reset_stream.app_error_code =
  3004. strm->tx.reset_stream_app_error_code;
  3005. nfrc->fr.reset_stream.final_size = strm->tx.offset;
  3006. *pfrc = nfrc;
  3007. strm->flags &= ~NGTCP2_STRM_FLAG_SEND_RESET_STREAM;
  3008. rv =
  3009. conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  3010. if (rv != 0) {
  3011. assert(NGTCP2_ERR_NOBUF == rv);
  3012. break;
  3013. }
  3014. pkt_empty = 0;
  3015. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3016. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3017. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3018. pfrc = &(*pfrc)->next;
  3019. }
  3020. if (strm->flags & NGTCP2_STRM_FLAG_SEND_STOP_SENDING) {
  3021. if ((strm->flags & NGTCP2_STRM_FLAG_SHUT_RD) &&
  3022. ngtcp2_strm_rx_offset(strm) == strm->rx.last_offset) {
  3023. strm->flags &= ~NGTCP2_STRM_FLAG_SEND_STOP_SENDING;
  3024. } else {
  3025. rv = conn_call_stream_stop_sending(
  3026. conn, strm->stream_id, strm->tx.stop_sending_app_error_code,
  3027. strm->stream_user_data);
  3028. if (rv != 0) {
  3029. assert(ngtcp2_err_is_fatal(rv));
  3030. return rv;
  3031. }
  3032. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  3033. if (rv != 0) {
  3034. return rv;
  3035. }
  3036. nfrc->fr.type = NGTCP2_FRAME_STOP_SENDING;
  3037. nfrc->fr.stop_sending.stream_id = strm->stream_id;
  3038. nfrc->fr.stop_sending.app_error_code =
  3039. strm->tx.stop_sending_app_error_code;
  3040. *pfrc = nfrc;
  3041. strm->flags &= ~NGTCP2_STRM_FLAG_SEND_STOP_SENDING;
  3042. rv =
  3043. conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  3044. if (rv != 0) {
  3045. assert(NGTCP2_ERR_NOBUF == rv);
  3046. break;
  3047. }
  3048. pkt_empty = 0;
  3049. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3050. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3051. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3052. pfrc = &(*pfrc)->next;
  3053. }
  3054. }
  3055. if (!(strm->flags & NGTCP2_STRM_FLAG_SHUT_WR) &&
  3056. strm_should_send_stream_data_blocked(strm)) {
  3057. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  3058. if (rv != 0) {
  3059. return rv;
  3060. }
  3061. nfrc->fr.type = NGTCP2_FRAME_STREAM_DATA_BLOCKED;
  3062. nfrc->fr.stream_data_blocked.stream_id = strm->stream_id;
  3063. nfrc->fr.stream_data_blocked.offset = strm->tx.max_offset;
  3064. *pfrc = nfrc;
  3065. strm->tx.last_blocked_offset = strm->tx.max_offset;
  3066. rv =
  3067. conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  3068. if (rv != 0) {
  3069. assert(NGTCP2_ERR_NOBUF == rv);
  3070. break;
  3071. }
  3072. pkt_empty = 0;
  3073. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3074. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3075. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3076. pfrc = &(*pfrc)->next;
  3077. }
  3078. if (!(strm->flags &
  3079. (NGTCP2_STRM_FLAG_SHUT_RD | NGTCP2_STRM_FLAG_STOP_SENDING)) &&
  3080. conn_should_send_max_stream_data(conn, strm)) {
  3081. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  3082. if (rv != 0) {
  3083. assert(ngtcp2_err_is_fatal(rv));
  3084. return rv;
  3085. }
  3086. if (conn->local.settings.max_stream_window &&
  3087. strm->tx.last_max_stream_data_ts != UINT64_MAX &&
  3088. ts - strm->tx.last_max_stream_data_ts <
  3089. NGTCP2_FLOW_WINDOW_RTT_FACTOR * cstat->smoothed_rtt &&
  3090. conn->local.settings.max_stream_window > strm->rx.window) {
  3091. target_max_data =
  3092. NGTCP2_FLOW_WINDOW_SCALING_FACTOR * strm->rx.window;
  3093. if (target_max_data > conn->local.settings.max_stream_window) {
  3094. target_max_data = conn->local.settings.max_stream_window;
  3095. }
  3096. delta = target_max_data - strm->rx.window;
  3097. if (strm->rx.unsent_max_offset + delta > NGTCP2_MAX_VARINT) {
  3098. delta = NGTCP2_MAX_VARINT - strm->rx.unsent_max_offset;
  3099. }
  3100. strm->rx.window = target_max_data;
  3101. } else {
  3102. delta = 0;
  3103. }
  3104. strm->tx.last_max_stream_data_ts = ts;
  3105. nfrc->fr.type = NGTCP2_FRAME_MAX_STREAM_DATA;
  3106. nfrc->fr.max_stream_data.stream_id = strm->stream_id;
  3107. nfrc->fr.max_stream_data.max_stream_data =
  3108. strm->rx.unsent_max_offset + delta;
  3109. *pfrc = nfrc;
  3110. strm->rx.max_offset = strm->rx.unsent_max_offset =
  3111. nfrc->fr.max_stream_data.max_stream_data;
  3112. rv =
  3113. conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  3114. if (rv != 0) {
  3115. assert(NGTCP2_ERR_NOBUF == rv);
  3116. break;
  3117. }
  3118. pkt_empty = 0;
  3119. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3120. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3121. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3122. pfrc = &(*pfrc)->next;
  3123. }
  3124. if (ngtcp2_strm_streamfrq_empty(strm)) {
  3125. ngtcp2_conn_tx_strmq_pop(conn);
  3126. continue;
  3127. }
  3128. stream_offset = ngtcp2_strm_streamfrq_unacked_offset(strm);
  3129. if (stream_offset == (uint64_t)-1) {
  3130. ngtcp2_strm_streamfrq_clear(strm);
  3131. ngtcp2_conn_tx_strmq_pop(conn);
  3132. continue;
  3133. }
  3134. left = ngtcp2_ppe_left(ppe);
  3135. left = ngtcp2_pkt_stream_max_datalen(strm->stream_id, stream_offset,
  3136. left, left);
  3137. if (left == (size_t)-1) {
  3138. break;
  3139. }
  3140. rv = ngtcp2_strm_streamfrq_pop(strm, &nfrc, left);
  3141. if (rv != 0) {
  3142. assert(ngtcp2_err_is_fatal(rv));
  3143. return rv;
  3144. }
  3145. if (nfrc == NULL) {
  3146. /* TODO Why? */
  3147. break;
  3148. }
  3149. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  3150. if (rv != 0) {
  3151. ngtcp2_unreachable();
  3152. }
  3153. *pfrc = nfrc;
  3154. pfrc = &(*pfrc)->next;
  3155. pkt_empty = 0;
  3156. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3157. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3158. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3159. if (ngtcp2_strm_streamfrq_empty(strm)) {
  3160. ngtcp2_conn_tx_strmq_pop(conn);
  3161. continue;
  3162. }
  3163. ngtcp2_conn_tx_strmq_pop(conn);
  3164. ++strm->cycle;
  3165. rv = ngtcp2_conn_tx_strmq_push(conn, strm);
  3166. if (rv != 0) {
  3167. assert(ngtcp2_err_is_fatal(rv));
  3168. return rv;
  3169. }
  3170. }
  3171. }
  3172. /* Write MAX_STREAMS after RESET_STREAM so that we can extend
  3173. stream ID space in one packet. */
  3174. if (*pfrc == NULL &&
  3175. conn->remote.bidi.unsent_max_streams > conn->remote.bidi.max_streams) {
  3176. rv = conn_call_extend_max_remote_streams_bidi(
  3177. conn, conn->remote.bidi.unsent_max_streams);
  3178. if (rv != 0) {
  3179. assert(ngtcp2_err_is_fatal(rv));
  3180. return rv;
  3181. }
  3182. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  3183. if (rv != 0) {
  3184. assert(ngtcp2_err_is_fatal(rv));
  3185. return rv;
  3186. }
  3187. nfrc->fr.type = NGTCP2_FRAME_MAX_STREAMS_BIDI;
  3188. nfrc->fr.max_streams.max_streams = conn->remote.bidi.unsent_max_streams;
  3189. *pfrc = nfrc;
  3190. conn->remote.bidi.max_streams = conn->remote.bidi.unsent_max_streams;
  3191. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &(*pfrc)->fr);
  3192. if (rv != 0) {
  3193. assert(NGTCP2_ERR_NOBUF == rv);
  3194. } else {
  3195. pkt_empty = 0;
  3196. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3197. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3198. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3199. pfrc = &(*pfrc)->next;
  3200. }
  3201. }
  3202. if (*pfrc == NULL &&
  3203. conn->remote.uni.unsent_max_streams > conn->remote.uni.max_streams) {
  3204. rv = conn_call_extend_max_remote_streams_uni(
  3205. conn, conn->remote.uni.unsent_max_streams);
  3206. if (rv != 0) {
  3207. assert(ngtcp2_err_is_fatal(rv));
  3208. return rv;
  3209. }
  3210. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  3211. if (rv != 0) {
  3212. assert(ngtcp2_err_is_fatal(rv));
  3213. return rv;
  3214. }
  3215. nfrc->fr.type = NGTCP2_FRAME_MAX_STREAMS_UNI;
  3216. nfrc->fr.max_streams.max_streams = conn->remote.uni.unsent_max_streams;
  3217. *pfrc = nfrc;
  3218. conn->remote.uni.max_streams = conn->remote.uni.unsent_max_streams;
  3219. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &(*pfrc)->fr);
  3220. if (rv != 0) {
  3221. assert(NGTCP2_ERR_NOBUF == rv);
  3222. } else {
  3223. pkt_empty = 0;
  3224. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3225. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3226. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3227. pfrc = &(*pfrc)->next;
  3228. }
  3229. }
  3230. if (pktns->tx.frq == NULL && !send_stream && !send_datagram &&
  3231. !(rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) &&
  3232. pktns->rtb.num_retransmittable && pktns->rtb.probe_pkt_left) {
  3233. num_reclaimed = ngtcp2_rtb_reclaim_on_pto(&pktns->rtb, conn, pktns, 1);
  3234. if (num_reclaimed < 0) {
  3235. return rv;
  3236. }
  3237. if (num_reclaimed) {
  3238. goto build_pkt;
  3239. }
  3240. /* We had pktns->rtb.num_retransmittable > 0 but we were unable
  3241. to reclaim any frame. In this case, we do not have to send
  3242. any probe packet. */
  3243. if (pktns->rtb.num_pto_eliciting == 0) {
  3244. pktns->rtb.probe_pkt_left = 0;
  3245. ngtcp2_conn_set_loss_detection_timer(conn, ts);
  3246. if (pkt_empty && conn_cwnd_is_zero(conn) && !require_padding) {
  3247. return 0;
  3248. }
  3249. }
  3250. }
  3251. } else {
  3252. pfrc = conn->pkt.pfrc;
  3253. rtb_entry_flags |= conn->pkt.rtb_entry_flags;
  3254. pkt_empty = conn->pkt.pkt_empty;
  3255. hd_logged = conn->pkt.hd_logged;
  3256. }
  3257. left = ngtcp2_ppe_left(ppe);
  3258. if (*pfrc == NULL && send_stream &&
  3259. (ndatalen = ngtcp2_pkt_stream_max_datalen(
  3260. vmsg->stream.strm->stream_id, vmsg->stream.strm->tx.offset, ndatalen,
  3261. left)) != (size_t)-1 &&
  3262. (ndatalen || datalen == 0)) {
  3263. datacnt = ngtcp2_vec_copy_at_most(data, NGTCP2_MAX_STREAM_DATACNT,
  3264. vmsg->stream.data, vmsg->stream.datacnt,
  3265. (size_t)ndatalen);
  3266. ndatalen = ngtcp2_vec_len(data, datacnt);
  3267. assert((datacnt == 0 && datalen == 0) || (datacnt && datalen));
  3268. rv = ngtcp2_frame_chain_stream_datacnt_objalloc_new(
  3269. &nfrc, datacnt, &conn->frc_objalloc, conn->mem);
  3270. if (rv != 0) {
  3271. assert(ngtcp2_err_is_fatal(rv));
  3272. return rv;
  3273. }
  3274. nfrc->fr.stream.type = NGTCP2_FRAME_STREAM;
  3275. nfrc->fr.stream.flags = 0;
  3276. nfrc->fr.stream.stream_id = vmsg->stream.strm->stream_id;
  3277. nfrc->fr.stream.offset = vmsg->stream.strm->tx.offset;
  3278. nfrc->fr.stream.datacnt = datacnt;
  3279. ngtcp2_vec_copy(nfrc->fr.stream.data, data, datacnt);
  3280. fin = (vmsg->stream.flags & NGTCP2_WRITE_STREAM_FLAG_FIN) &&
  3281. ndatalen == datalen;
  3282. nfrc->fr.stream.fin = (uint8_t)fin;
  3283. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  3284. if (rv != 0) {
  3285. ngtcp2_unreachable();
  3286. }
  3287. *pfrc = nfrc;
  3288. pfrc = &(*pfrc)->next;
  3289. pkt_empty = 0;
  3290. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3291. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3292. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3293. vmsg->stream.strm->tx.offset += ndatalen;
  3294. conn->tx.offset += ndatalen;
  3295. vmsg->stream.strm->flags |= NGTCP2_STRM_FLAG_ANY_SENT;
  3296. if (fin) {
  3297. ngtcp2_strm_shutdown(vmsg->stream.strm, NGTCP2_STRM_FLAG_SHUT_WR);
  3298. }
  3299. if (vmsg->stream.pdatalen) {
  3300. *vmsg->stream.pdatalen = (ngtcp2_ssize)ndatalen;
  3301. }
  3302. } else {
  3303. send_stream = 0;
  3304. }
  3305. if (vmsg && vmsg->type == NGTCP2_VMSG_TYPE_STREAM &&
  3306. ((stream_blocked && *pfrc == NULL) ||
  3307. (send_stream &&
  3308. !(vmsg->stream.strm->flags & NGTCP2_STRM_FLAG_SHUT_WR)))) {
  3309. if (conn_should_send_data_blocked(conn)) {
  3310. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  3311. if (rv != 0) {
  3312. assert(ngtcp2_err_is_fatal(rv));
  3313. return rv;
  3314. }
  3315. nfrc->fr.type = NGTCP2_FRAME_DATA_BLOCKED;
  3316. nfrc->fr.data_blocked.offset = conn->tx.offset;
  3317. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  3318. if (rv != 0) {
  3319. assert(NGTCP2_ERR_NOBUF == rv);
  3320. /* We cannot add nfrc to pktns->tx.frq here. */
  3321. ngtcp2_frame_chain_objalloc_del(nfrc, &conn->frc_objalloc, conn->mem);
  3322. } else {
  3323. *pfrc = nfrc;
  3324. pfrc = &(*pfrc)->next;
  3325. pkt_empty = 0;
  3326. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3327. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3328. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3329. conn->tx.last_blocked_offset = conn->tx.max_offset;
  3330. }
  3331. }
  3332. strm = vmsg->stream.strm;
  3333. if (strm_should_send_stream_data_blocked(strm)) {
  3334. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  3335. if (rv != 0) {
  3336. assert(ngtcp2_err_is_fatal(rv));
  3337. return rv;
  3338. }
  3339. nfrc->fr.type = NGTCP2_FRAME_STREAM_DATA_BLOCKED;
  3340. nfrc->fr.stream_data_blocked.stream_id = strm->stream_id;
  3341. nfrc->fr.stream_data_blocked.offset = strm->tx.max_offset;
  3342. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  3343. if (rv != 0) {
  3344. assert(NGTCP2_ERR_NOBUF == rv);
  3345. /* We cannot add nfrc to pktns->tx.frq here. */
  3346. ngtcp2_frame_chain_objalloc_del(nfrc, &conn->frc_objalloc, conn->mem);
  3347. if (!ngtcp2_strm_is_tx_queued(strm)) {
  3348. strm->cycle = conn_tx_strmq_first_cycle(conn);
  3349. rv = ngtcp2_conn_tx_strmq_push(conn, strm);
  3350. if (rv != 0) {
  3351. return rv;
  3352. }
  3353. }
  3354. } else {
  3355. *pfrc = nfrc;
  3356. pfrc = &(*pfrc)->next;
  3357. pkt_empty = 0;
  3358. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3359. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3360. NGTCP2_RTB_ENTRY_FLAG_RETRANSMITTABLE;
  3361. strm->tx.last_blocked_offset = strm->tx.max_offset;
  3362. }
  3363. }
  3364. }
  3365. if (*pfrc == NULL && send_datagram &&
  3366. left >= ngtcp2_pkt_datagram_framelen((size_t)datalen)) {
  3367. if (conn->callbacks.ack_datagram || conn->callbacks.lost_datagram) {
  3368. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  3369. if (rv != 0) {
  3370. assert(ngtcp2_err_is_fatal(rv));
  3371. return rv;
  3372. }
  3373. nfrc->fr.datagram.type = NGTCP2_FRAME_DATAGRAM_LEN;
  3374. nfrc->fr.datagram.dgram_id = vmsg->datagram.dgram_id;
  3375. nfrc->fr.datagram.datacnt = vmsg->datagram.datacnt;
  3376. nfrc->fr.datagram.data = (ngtcp2_vec *)vmsg->datagram.data;
  3377. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &nfrc->fr);
  3378. assert(rv == 0);
  3379. /* Because DATAGRAM will not be retransmitted, we do not use
  3380. data anymore. Just nullify it. The only reason to keep
  3381. track a frame is keep dgram_id to pass it to
  3382. ngtcp2_ack_datagram or ngtcp2_lost_datagram callbacks. */
  3383. nfrc->fr.datagram.datacnt = 0;
  3384. nfrc->fr.datagram.data = NULL;
  3385. *pfrc = nfrc;
  3386. pfrc = &(*pfrc)->next;
  3387. } else {
  3388. lfr.datagram.type = NGTCP2_FRAME_DATAGRAM_LEN;
  3389. lfr.datagram.datacnt = vmsg->datagram.datacnt;
  3390. lfr.datagram.data = (ngtcp2_vec *)vmsg->datagram.data;
  3391. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &lfr);
  3392. assert(rv == 0);
  3393. }
  3394. pkt_empty = 0;
  3395. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING |
  3396. NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3397. NGTCP2_RTB_ENTRY_FLAG_DATAGRAM;
  3398. if (vmsg->datagram.paccepted) {
  3399. *vmsg->datagram.paccepted = 1;
  3400. }
  3401. } else {
  3402. send_datagram = 0;
  3403. }
  3404. if (pkt_empty) {
  3405. if (*pfrc == NULL && rv == 0 && stream_blocked &&
  3406. (write_more || !require_padding) &&
  3407. ngtcp2_conn_get_max_data_left(conn)) {
  3408. if (write_more) {
  3409. conn->pkt.pfrc = pfrc;
  3410. conn->pkt.pkt_empty = pkt_empty;
  3411. conn->pkt.rtb_entry_flags = rtb_entry_flags;
  3412. conn->pkt.hd_logged = hd_logged;
  3413. conn->flags |= NGTCP2_CONN_FLAG_PPE_PENDING;
  3414. }
  3415. return NGTCP2_ERR_STREAM_DATA_BLOCKED;
  3416. }
  3417. keep_alive_expired =
  3418. type == NGTCP2_PKT_1RTT && conn_keep_alive_expired(conn, ts);
  3419. if (conn->pktns.rtb.probe_pkt_left == 0 && !keep_alive_expired &&
  3420. !require_padding) {
  3421. conn_reset_ppe_pending(conn);
  3422. return 0;
  3423. }
  3424. } else if (write_more) {
  3425. conn->pkt.pfrc = pfrc;
  3426. conn->pkt.pkt_empty = pkt_empty;
  3427. conn->pkt.rtb_entry_flags = rtb_entry_flags;
  3428. conn->pkt.hd_logged = hd_logged;
  3429. conn->flags |= NGTCP2_CONN_FLAG_PPE_PENDING;
  3430. assert(vmsg);
  3431. switch (vmsg->type) {
  3432. case NGTCP2_VMSG_TYPE_STREAM:
  3433. if (send_stream) {
  3434. if (ngtcp2_ppe_left(ppe)) {
  3435. return NGTCP2_ERR_WRITE_MORE;
  3436. }
  3437. break;
  3438. }
  3439. if (*pfrc == NULL && ngtcp2_conn_get_max_data_left(conn) &&
  3440. stream_blocked) {
  3441. return NGTCP2_ERR_STREAM_DATA_BLOCKED;
  3442. }
  3443. break;
  3444. case NGTCP2_VMSG_TYPE_DATAGRAM:
  3445. if (send_datagram && ngtcp2_ppe_left(ppe)) {
  3446. return NGTCP2_ERR_WRITE_MORE;
  3447. }
  3448. /* If DATAGRAM cannot be written due to insufficient space,
  3449. continue to create a packet with the hope that application
  3450. calls ngtcp2_conn_writev_datagram again. */
  3451. break;
  3452. default:
  3453. ngtcp2_unreachable();
  3454. }
  3455. }
  3456. if (!(rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING)) {
  3457. if (ngtcp2_tstamp_elapsed(pktns->tx.non_ack_pkt_start_ts,
  3458. cstat->smoothed_rtt, ts) ||
  3459. keep_alive_expired || conn->pktns.rtb.probe_pkt_left) {
  3460. lfr.type = NGTCP2_FRAME_PING;
  3461. rv = conn_ppe_write_frame_hd_log(conn, ppe, &hd_logged, hd, &lfr);
  3462. if (rv != 0) {
  3463. assert(rv == NGTCP2_ERR_NOBUF);
  3464. /* TODO If buffer is too small, PING cannot be written if
  3465. packet is still empty. */
  3466. } else {
  3467. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING;
  3468. if (conn->pktns.rtb.probe_pkt_left) {
  3469. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_PROBE;
  3470. } else {
  3471. rtb_entry_flags |= NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING;
  3472. }
  3473. pktns->tx.non_ack_pkt_start_ts = UINT64_MAX;
  3474. pkt_empty = 0;
  3475. }
  3476. } else if (pktns->tx.non_ack_pkt_start_ts == UINT64_MAX) {
  3477. pktns->tx.non_ack_pkt_start_ts = ts;
  3478. }
  3479. } else {
  3480. pktns->tx.non_ack_pkt_start_ts = UINT64_MAX;
  3481. }
  3482. /* TODO Push STREAM frame back to ngtcp2_strm if there is an error
  3483. before ngtcp2_rtb_entry is safely created and added. */
  3484. if (require_padding) {
  3485. lfr.padding.len = ngtcp2_ppe_dgram_padding(ppe);
  3486. } else {
  3487. lfr.padding.len = ngtcp2_ppe_padding_size(ppe, min_pktlen);
  3488. min_padded = 1;
  3489. }
  3490. if (lfr.padding.len) {
  3491. if (!min_padded ||
  3492. (rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING)) {
  3493. padded = 1;
  3494. }
  3495. lfr.type = NGTCP2_FRAME_PADDING;
  3496. ngtcp2_log_tx_fr(&conn->log, hd, &lfr);
  3497. ngtcp2_qlog_write_frame(&conn->qlog, &lfr);
  3498. }
  3499. nwrite = ngtcp2_ppe_final(ppe, NULL);
  3500. if (nwrite < 0) {
  3501. assert(ngtcp2_err_is_fatal((int)nwrite));
  3502. return nwrite;
  3503. }
  3504. ++cc->ckm->use_count;
  3505. ngtcp2_qlog_pkt_sent_end(&conn->qlog, hd, (size_t)nwrite);
  3506. /* TODO ack-eliciting vs needs-tracking */
  3507. /* probe packet needs tracking but it does not need ACK, could be lost. */
  3508. if ((rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) || padded) {
  3509. if (pi) {
  3510. conn_handle_tx_ecn(conn, pi, &rtb_entry_flags, pktns, hd, ts);
  3511. }
  3512. rv =
  3513. ngtcp2_rtb_entry_objalloc_new(&ent, hd, NULL, ts, (size_t)nwrite,
  3514. rtb_entry_flags, &conn->rtb_entry_objalloc);
  3515. if (rv != 0) {
  3516. assert(ngtcp2_err_is_fatal(rv));
  3517. return rv;
  3518. }
  3519. if (*pfrc != pktns->tx.frq) {
  3520. ent->frc = pktns->tx.frq;
  3521. pktns->tx.frq = *pfrc;
  3522. *pfrc = NULL;
  3523. }
  3524. if ((rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) &&
  3525. pktns->rtb.num_ack_eliciting == 0 && conn->cc.event) {
  3526. conn->cc.event(&conn->cc, &conn->cstat, NGTCP2_CC_EVENT_TYPE_TX_START,
  3527. ts);
  3528. }
  3529. rv = conn_on_pkt_sent(conn, pktns, ent);
  3530. if (rv != 0) {
  3531. assert(ngtcp2_err_is_fatal(rv));
  3532. ngtcp2_rtb_entry_objalloc_del(ent, &conn->rtb_entry_objalloc,
  3533. &conn->frc_objalloc, conn->mem);
  3534. return rv;
  3535. }
  3536. if (rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) {
  3537. if (conn->cc.on_pkt_sent) {
  3538. conn->cc.on_pkt_sent(
  3539. &conn->cc, &conn->cstat,
  3540. ngtcp2_cc_pkt_init(&cc_pkt, hd->pkt_num, (size_t)nwrite,
  3541. NGTCP2_PKTNS_ID_APPLICATION, ts, ent->rst.lost,
  3542. ent->rst.tx_in_flight, ent->rst.is_app_limited));
  3543. }
  3544. if (conn->flags & NGTCP2_CONN_FLAG_RESTART_IDLE_TIMER_ON_WRITE) {
  3545. conn_restart_timer_on_write(conn, ts);
  3546. }
  3547. }
  3548. } else if (pi && conn->tx.ecn.state == NGTCP2_ECN_STATE_CAPABLE) {
  3549. conn_handle_tx_ecn(conn, pi, NULL, pktns, hd, ts);
  3550. }
  3551. conn_reset_ppe_pending(conn);
  3552. if (pktns->rtb.probe_pkt_left &&
  3553. (rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING)) {
  3554. --pktns->rtb.probe_pkt_left;
  3555. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON, "probe pkt size=%td",
  3556. nwrite);
  3557. }
  3558. conn_update_keep_alive_last_ts(conn, ts);
  3559. conn->dcid.current.bytes_sent += (uint64_t)nwrite;
  3560. conn->tx.pacing.pktlen += (size_t)nwrite;
  3561. ngtcp2_qlog_metrics_updated(&conn->qlog, &conn->cstat);
  3562. ++pktns->tx.last_pkt_num;
  3563. return nwrite;
  3564. }
  3565. ngtcp2_ssize ngtcp2_conn_write_single_frame_pkt(
  3566. ngtcp2_conn *conn, ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen,
  3567. uint8_t type, uint8_t flags, const ngtcp2_cid *dcid, ngtcp2_frame *fr,
  3568. uint16_t rtb_entry_flags, const ngtcp2_path *path, ngtcp2_tstamp ts) {
  3569. int rv;
  3570. ngtcp2_ppe ppe;
  3571. ngtcp2_pkt_hd hd;
  3572. ngtcp2_frame lfr;
  3573. ngtcp2_ssize nwrite;
  3574. ngtcp2_crypto_cc cc;
  3575. ngtcp2_pktns *pktns;
  3576. uint8_t hd_flags;
  3577. ngtcp2_rtb_entry *rtbent;
  3578. int padded = 0;
  3579. const ngtcp2_cid *scid;
  3580. uint32_t version;
  3581. switch (type) {
  3582. case NGTCP2_PKT_INITIAL:
  3583. pktns = conn->in_pktns;
  3584. hd_flags = conn_pkt_flags_long(conn);
  3585. scid = &conn->oscid;
  3586. version = conn->negotiated_version ? conn->negotiated_version
  3587. : conn->client_chosen_version;
  3588. if (version == conn->client_chosen_version) {
  3589. cc.ckm = pktns->crypto.tx.ckm;
  3590. cc.hp_ctx = pktns->crypto.tx.hp_ctx;
  3591. } else {
  3592. assert(version == conn->vneg.version);
  3593. cc.ckm = conn->vneg.tx.ckm;
  3594. cc.hp_ctx = conn->vneg.tx.hp_ctx;
  3595. }
  3596. break;
  3597. case NGTCP2_PKT_HANDSHAKE:
  3598. pktns = conn->hs_pktns;
  3599. hd_flags = conn_pkt_flags_long(conn);
  3600. scid = &conn->oscid;
  3601. version = conn->negotiated_version;
  3602. cc.ckm = pktns->crypto.tx.ckm;
  3603. cc.hp_ctx = pktns->crypto.tx.hp_ctx;
  3604. break;
  3605. case NGTCP2_PKT_1RTT:
  3606. pktns = &conn->pktns;
  3607. hd_flags = conn_pkt_flags_short(conn);
  3608. scid = NULL;
  3609. version = conn->negotiated_version;
  3610. cc.ckm = pktns->crypto.tx.ckm;
  3611. cc.hp_ctx = pktns->crypto.tx.hp_ctx;
  3612. break;
  3613. default:
  3614. /* We don't support 0-RTT packet in this function. */
  3615. ngtcp2_unreachable();
  3616. }
  3617. cc.aead = pktns->crypto.ctx.aead;
  3618. cc.hp = pktns->crypto.ctx.hp;
  3619. cc.encrypt = conn->callbacks.encrypt;
  3620. cc.hp_mask = conn->callbacks.hp_mask;
  3621. ngtcp2_pkt_hd_init(&hd, hd_flags, type, dcid, scid,
  3622. pktns->tx.last_pkt_num + 1, pktns_select_pkt_numlen(pktns),
  3623. version, 0);
  3624. ngtcp2_ppe_init(&ppe, dest, destlen, 0, &cc);
  3625. rv = ngtcp2_ppe_encode_hd(&ppe, &hd);
  3626. if (rv != 0) {
  3627. assert(NGTCP2_ERR_NOBUF == rv);
  3628. return 0;
  3629. }
  3630. if (!ngtcp2_ppe_ensure_hp_sample(&ppe)) {
  3631. return 0;
  3632. }
  3633. ngtcp2_log_tx_pkt_hd(&conn->log, &hd);
  3634. ngtcp2_qlog_pkt_sent_start(&conn->qlog);
  3635. rv = conn_ppe_write_frame(conn, &ppe, &hd, fr);
  3636. if (rv != 0) {
  3637. assert(NGTCP2_ERR_NOBUF == rv);
  3638. return 0;
  3639. }
  3640. lfr.type = NGTCP2_FRAME_PADDING;
  3641. if (flags & NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING_FULL) {
  3642. lfr.padding.len = ngtcp2_ppe_dgram_padding_size(&ppe, destlen);
  3643. } else if (flags & NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING) {
  3644. lfr.padding.len = ngtcp2_ppe_dgram_padding(&ppe);
  3645. } else {
  3646. switch (fr->type) {
  3647. case NGTCP2_FRAME_PATH_CHALLENGE:
  3648. case NGTCP2_FRAME_PATH_RESPONSE:
  3649. if (!conn->server || destlen >= NGTCP2_MAX_UDP_PAYLOAD_SIZE) {
  3650. lfr.padding.len = ngtcp2_ppe_dgram_padding(&ppe);
  3651. } else {
  3652. lfr.padding.len = 0;
  3653. }
  3654. break;
  3655. default:
  3656. lfr.padding.len = ngtcp2_ppe_padding_size(&ppe, conn_min_pktlen(conn));
  3657. }
  3658. }
  3659. if (lfr.padding.len) {
  3660. padded = 1;
  3661. ngtcp2_log_tx_fr(&conn->log, &hd, &lfr);
  3662. ngtcp2_qlog_write_frame(&conn->qlog, &lfr);
  3663. }
  3664. nwrite = ngtcp2_ppe_final(&ppe, NULL);
  3665. if (nwrite < 0) {
  3666. return nwrite;
  3667. }
  3668. if (type == NGTCP2_PKT_1RTT) {
  3669. ++cc.ckm->use_count;
  3670. }
  3671. ngtcp2_qlog_pkt_sent_end(&conn->qlog, &hd, (size_t)nwrite);
  3672. /* Do this when we are sure that there is no error. */
  3673. switch (fr->type) {
  3674. case NGTCP2_FRAME_ACK:
  3675. case NGTCP2_FRAME_ACK_ECN:
  3676. ngtcp2_acktr_commit_ack(&pktns->acktr);
  3677. ngtcp2_acktr_add_ack(&pktns->acktr, hd.pkt_num, fr->ack.largest_ack);
  3678. if (type == NGTCP2_PKT_1RTT) {
  3679. conn_handle_unconfirmed_key_update_from_remote(conn, fr->ack.largest_ack,
  3680. ts);
  3681. }
  3682. if (!(flags & (NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING_FULL |
  3683. NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING))) {
  3684. padded = 0;
  3685. }
  3686. break;
  3687. }
  3688. if (((rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) || padded) &&
  3689. (!path || ngtcp2_path_eq(&conn->dcid.current.ps.path, path))) {
  3690. if (pi && (conn->tx.ecn.state == NGTCP2_ECN_STATE_CAPABLE ||
  3691. !(rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_PMTUD_PROBE))) {
  3692. conn_handle_tx_ecn(conn, pi, &rtb_entry_flags, pktns, &hd, ts);
  3693. }
  3694. rv =
  3695. ngtcp2_rtb_entry_objalloc_new(&rtbent, &hd, NULL, ts, (size_t)nwrite,
  3696. rtb_entry_flags, &conn->rtb_entry_objalloc);
  3697. if (rv != 0) {
  3698. return rv;
  3699. }
  3700. rv = conn_on_pkt_sent(conn, pktns, rtbent);
  3701. if (rv != 0) {
  3702. ngtcp2_rtb_entry_objalloc_del(rtbent, &conn->rtb_entry_objalloc,
  3703. &conn->frc_objalloc, conn->mem);
  3704. return rv;
  3705. }
  3706. if (rtb_entry_flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING) {
  3707. if (conn->flags & NGTCP2_CONN_FLAG_RESTART_IDLE_TIMER_ON_WRITE) {
  3708. conn_restart_timer_on_write(conn, ts);
  3709. }
  3710. if (pktns->rtb.probe_pkt_left && path &&
  3711. ngtcp2_path_eq(&conn->dcid.current.ps.path, path)) {
  3712. --pktns->rtb.probe_pkt_left;
  3713. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON, "probe pkt size=%td",
  3714. nwrite);
  3715. }
  3716. }
  3717. } else if (pi && conn->tx.ecn.state == NGTCP2_ECN_STATE_CAPABLE) {
  3718. conn_handle_tx_ecn(conn, pi, NULL, pktns, &hd, ts);
  3719. }
  3720. if (path && ngtcp2_path_eq(&conn->dcid.current.ps.path, path)) {
  3721. conn_update_keep_alive_last_ts(conn, ts);
  3722. }
  3723. if (!padded) {
  3724. switch (fr->type) {
  3725. case NGTCP2_FRAME_ACK:
  3726. case NGTCP2_FRAME_ACK_ECN:
  3727. break;
  3728. default:
  3729. conn->tx.pacing.pktlen += (size_t)nwrite;
  3730. }
  3731. } else {
  3732. conn->tx.pacing.pktlen += (size_t)nwrite;
  3733. }
  3734. ngtcp2_qlog_metrics_updated(&conn->qlog, &conn->cstat);
  3735. ++pktns->tx.last_pkt_num;
  3736. return nwrite;
  3737. }
  3738. /*
  3739. * conn_process_early_rtb makes any pending 0RTT packet 1RTT packet.
  3740. */
  3741. static void conn_process_early_rtb(ngtcp2_conn *conn) {
  3742. ngtcp2_rtb_entry *ent;
  3743. ngtcp2_rtb *rtb = &conn->pktns.rtb;
  3744. ngtcp2_ksl_it it;
  3745. for (it = ngtcp2_rtb_head(rtb); !ngtcp2_ksl_it_end(&it);
  3746. ngtcp2_ksl_it_next(&it)) {
  3747. ent = ngtcp2_ksl_it_get(&it);
  3748. if ((ent->hd.flags & NGTCP2_PKT_FLAG_LONG_FORM) == 0 ||
  3749. ent->hd.type != NGTCP2_PKT_0RTT) {
  3750. continue;
  3751. }
  3752. /* 0-RTT packet is retransmitted as a 1RTT packet. */
  3753. ent->hd.flags &= (uint8_t)~NGTCP2_PKT_FLAG_LONG_FORM;
  3754. ent->hd.type = NGTCP2_PKT_1RTT;
  3755. }
  3756. }
  3757. /*
  3758. * conn_handshake_remnants_left returns nonzero if there may be
  3759. * handshake packets the local endpoint has to send, including new
  3760. * packets and lost ones.
  3761. */
  3762. static int conn_handshake_remnants_left(ngtcp2_conn *conn) {
  3763. ngtcp2_pktns *in_pktns = conn->in_pktns;
  3764. ngtcp2_pktns *hs_pktns = conn->hs_pktns;
  3765. return !conn_is_tls_handshake_completed(conn) ||
  3766. (in_pktns && (in_pktns->rtb.num_pto_eliciting ||
  3767. !ngtcp2_strm_streamfrq_empty(&in_pktns->crypto.strm))) ||
  3768. (hs_pktns && (hs_pktns->rtb.num_pto_eliciting ||
  3769. !ngtcp2_strm_streamfrq_empty(&hs_pktns->crypto.strm)));
  3770. }
  3771. /*
  3772. * conn_retire_dcid_seq retires destination connection ID denoted by
  3773. * |seq|.
  3774. *
  3775. * This function returns 0 if it succeeds, or one of the following
  3776. * negative error codes:
  3777. *
  3778. * NGTCP2_ERR_NOMEM
  3779. * Out of memory.
  3780. * NGTCP2_ERR_CONNECTION_ID_LIMIT
  3781. * The number of unacknowledged retirement exceeds the limit.
  3782. */
  3783. static int conn_retire_dcid_seq(ngtcp2_conn *conn, uint64_t seq) {
  3784. ngtcp2_pktns *pktns = &conn->pktns;
  3785. ngtcp2_frame_chain *nfrc;
  3786. int rv;
  3787. if (ngtcp2_conn_check_retired_dcid_tracked(conn, seq)) {
  3788. return 0;
  3789. }
  3790. rv = ngtcp2_conn_track_retired_dcid_seq(conn, seq);
  3791. if (rv != 0) {
  3792. return rv;
  3793. }
  3794. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  3795. if (rv != 0) {
  3796. return rv;
  3797. }
  3798. nfrc->fr.type = NGTCP2_FRAME_RETIRE_CONNECTION_ID;
  3799. nfrc->fr.retire_connection_id.seq = seq;
  3800. nfrc->next = pktns->tx.frq;
  3801. pktns->tx.frq = nfrc;
  3802. return 0;
  3803. }
  3804. /*
  3805. * conn_retire_dcid retires |dcid|.
  3806. *
  3807. * This function returns 0 if it succeeds, or one of the following
  3808. * negative error codes:
  3809. *
  3810. * NGTCP2_ERR_NOMEM
  3811. * Out of memory
  3812. */
  3813. static int conn_retire_dcid(ngtcp2_conn *conn, const ngtcp2_dcid *dcid,
  3814. ngtcp2_tstamp ts) {
  3815. ngtcp2_ringbuf *rb = &conn->dcid.retired.rb;
  3816. ngtcp2_dcid *dest, *stale_dcid;
  3817. int rv;
  3818. assert(dcid->cid.datalen);
  3819. if (ngtcp2_ringbuf_full(rb)) {
  3820. stale_dcid = ngtcp2_ringbuf_get(rb, 0);
  3821. rv = conn_call_deactivate_dcid(conn, stale_dcid);
  3822. if (rv != 0) {
  3823. return rv;
  3824. }
  3825. ngtcp2_ringbuf_pop_front(rb);
  3826. }
  3827. dest = ngtcp2_ringbuf_push_back(rb);
  3828. ngtcp2_dcid_copy(dest, dcid);
  3829. dest->retired_ts = ts;
  3830. return conn_retire_dcid_seq(conn, dcid->seq);
  3831. }
  3832. /*
  3833. * conn_bind_dcid stores the DCID to |*pdcid| bound to |path|. If
  3834. * such DCID is not found, bind the new DCID to |path| and stores it
  3835. * to |*pdcid|. If a remote endpoint uses zero-length connection ID,
  3836. * the pointer to conn->dcid.current is assigned to |*pdcid|.
  3837. *
  3838. * This function returns 0 if it succeeds, or one of the following
  3839. * negative error codes:
  3840. *
  3841. * NGTCP2_ERR_CONN_ID_BLOCKED
  3842. * No unused DCID is available
  3843. * NGTCP2_ERR_NOMEM
  3844. * Out of memory
  3845. */
  3846. static int conn_bind_dcid(ngtcp2_conn *conn, ngtcp2_dcid **pdcid,
  3847. const ngtcp2_path *path, ngtcp2_tstamp ts) {
  3848. ngtcp2_dcid *dcid, *ndcid;
  3849. ngtcp2_cid cid;
  3850. size_t i, len;
  3851. int rv;
  3852. assert(!ngtcp2_path_eq(&conn->dcid.current.ps.path, path));
  3853. assert(!conn->pv || !ngtcp2_path_eq(&conn->pv->dcid.ps.path, path));
  3854. assert(!conn->pv || !(conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) ||
  3855. !ngtcp2_path_eq(&conn->pv->fallback_dcid.ps.path, path));
  3856. len = ngtcp2_ringbuf_len(&conn->dcid.bound.rb);
  3857. for (i = 0; i < len; ++i) {
  3858. dcid = ngtcp2_ringbuf_get(&conn->dcid.bound.rb, i);
  3859. if (ngtcp2_path_eq(&dcid->ps.path, path)) {
  3860. *pdcid = dcid;
  3861. return 0;
  3862. }
  3863. }
  3864. if (conn->dcid.current.cid.datalen == 0) {
  3865. ndcid = ngtcp2_ringbuf_push_back(&conn->dcid.bound.rb);
  3866. ngtcp2_cid_zero(&cid);
  3867. ngtcp2_dcid_init(ndcid, ++conn->dcid.zerolen_seq, &cid, NULL);
  3868. ngtcp2_dcid_set_path(ndcid, path);
  3869. *pdcid = ndcid;
  3870. return 0;
  3871. }
  3872. if (ngtcp2_ringbuf_len(&conn->dcid.unused.rb) == 0) {
  3873. return NGTCP2_ERR_CONN_ID_BLOCKED;
  3874. }
  3875. if (ngtcp2_ringbuf_full(&conn->dcid.bound.rb)) {
  3876. dcid = ngtcp2_ringbuf_get(&conn->dcid.bound.rb, 0);
  3877. rv = conn_retire_dcid(conn, dcid, ts);
  3878. if (rv != 0) {
  3879. return rv;
  3880. }
  3881. }
  3882. dcid = ngtcp2_ringbuf_get(&conn->dcid.unused.rb, 0);
  3883. ndcid = ngtcp2_ringbuf_push_back(&conn->dcid.bound.rb);
  3884. ngtcp2_dcid_copy(ndcid, dcid);
  3885. ndcid->bound_ts = ts;
  3886. ngtcp2_dcid_set_path(ndcid, path);
  3887. ngtcp2_ringbuf_pop_front(&conn->dcid.unused.rb);
  3888. *pdcid = ndcid;
  3889. return 0;
  3890. }
  3891. static int conn_start_pmtud(ngtcp2_conn *conn) {
  3892. int rv;
  3893. size_t hard_max_udp_payload_size;
  3894. assert(!conn->local.settings.no_pmtud);
  3895. assert(!conn->pmtud);
  3896. assert(conn_is_tls_handshake_completed(conn));
  3897. assert(conn->remote.transport_params);
  3898. assert(conn->remote.transport_params->max_udp_payload_size >=
  3899. NGTCP2_MAX_UDP_PAYLOAD_SIZE);
  3900. hard_max_udp_payload_size = (size_t)ngtcp2_min_uint64(
  3901. conn->remote.transport_params->max_udp_payload_size,
  3902. (uint64_t)conn->local.settings.max_tx_udp_payload_size);
  3903. rv =
  3904. ngtcp2_pmtud_new(&conn->pmtud, conn->dcid.current.max_udp_payload_size,
  3905. hard_max_udp_payload_size, conn->pktns.tx.last_pkt_num + 1,
  3906. conn->local.settings.pmtud_probes,
  3907. conn->local.settings.pmtud_probeslen, conn->mem);
  3908. if (rv != 0) {
  3909. return rv;
  3910. }
  3911. if (ngtcp2_pmtud_finished(conn->pmtud)) {
  3912. ngtcp2_conn_stop_pmtud(conn);
  3913. }
  3914. return 0;
  3915. }
  3916. int ngtcp2_conn_start_pmtud(ngtcp2_conn *conn) {
  3917. return conn_start_pmtud(conn);
  3918. }
  3919. void ngtcp2_conn_stop_pmtud(ngtcp2_conn *conn) {
  3920. if (!conn->pmtud) {
  3921. return;
  3922. }
  3923. ngtcp2_pmtud_del(conn->pmtud);
  3924. conn->pmtud = NULL;
  3925. }
  3926. static ngtcp2_ssize conn_write_pmtud_probe(ngtcp2_conn *conn,
  3927. ngtcp2_pkt_info *pi, uint8_t *dest,
  3928. size_t destlen, ngtcp2_tstamp ts) {
  3929. size_t probelen;
  3930. ngtcp2_ssize nwrite;
  3931. ngtcp2_frame lfr;
  3932. assert(conn->pmtud);
  3933. assert(!ngtcp2_pmtud_finished(conn->pmtud));
  3934. if (!ngtcp2_pmtud_require_probe(conn->pmtud)) {
  3935. return 0;
  3936. }
  3937. probelen = ngtcp2_pmtud_probelen(conn->pmtud);
  3938. if (probelen > destlen) {
  3939. return 0;
  3940. }
  3941. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  3942. "sending PMTUD probe packet len=%zu", probelen);
  3943. lfr.type = NGTCP2_FRAME_PING;
  3944. nwrite = ngtcp2_conn_write_single_frame_pkt(
  3945. conn, pi, dest, probelen, NGTCP2_PKT_1RTT,
  3946. NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING_FULL, &conn->dcid.current.cid, &lfr,
  3947. NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING | NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING |
  3948. NGTCP2_RTB_ENTRY_FLAG_PMTUD_PROBE,
  3949. NULL, ts);
  3950. if (nwrite < 0) {
  3951. return nwrite;
  3952. }
  3953. assert(nwrite);
  3954. ngtcp2_pmtud_probe_sent(conn->pmtud, conn_compute_pto(conn, &conn->pktns),
  3955. ts);
  3956. return nwrite;
  3957. }
  3958. /*
  3959. * conn_stop_pv stops the path validation which is currently running.
  3960. * This function does nothing if no path validation is currently being
  3961. * performed.
  3962. *
  3963. * This function returns 0 if it succeeds, or one of the following
  3964. * negative error codes:
  3965. *
  3966. * NGTCP2_ERR_NOMEM
  3967. * Out of memory
  3968. */
  3969. static int conn_stop_pv(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  3970. int rv = 0;
  3971. ngtcp2_pv *pv = conn->pv;
  3972. if (pv == NULL) {
  3973. return 0;
  3974. }
  3975. if (pv->dcid.cid.datalen && pv->dcid.seq != conn->dcid.current.seq) {
  3976. rv = conn_retire_dcid(conn, &pv->dcid, ts);
  3977. if (rv != 0) {
  3978. goto fin;
  3979. }
  3980. }
  3981. if ((pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  3982. pv->fallback_dcid.cid.datalen &&
  3983. pv->fallback_dcid.seq != conn->dcid.current.seq &&
  3984. pv->fallback_dcid.seq != pv->dcid.seq) {
  3985. rv = conn_retire_dcid(conn, &pv->fallback_dcid, ts);
  3986. if (rv != 0) {
  3987. goto fin;
  3988. }
  3989. }
  3990. fin:
  3991. ngtcp2_pv_del(pv);
  3992. conn->pv = NULL;
  3993. return rv;
  3994. }
  3995. /*
  3996. * conn_abort_pv aborts the current path validation and frees
  3997. * resources allocated for it. This function assumes that conn->pv is
  3998. * not NULL.
  3999. *
  4000. * This function returns 0 if it succeeds, or one of the following
  4001. * negative error codes:
  4002. *
  4003. * NGTCP2_ERR_NOMEM
  4004. * Out of memory
  4005. * NGTCP2_ERR_CALLBACK_FAILURE
  4006. * User-defined callback function failed.
  4007. */
  4008. static int conn_abort_pv(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  4009. ngtcp2_pv *pv = conn->pv;
  4010. int rv;
  4011. assert(pv);
  4012. if (!(pv->flags & NGTCP2_PV_FLAG_DONT_CARE)) {
  4013. rv = conn_call_path_validation(conn, pv,
  4014. NGTCP2_PATH_VALIDATION_RESULT_ABORTED);
  4015. if (rv != 0) {
  4016. return rv;
  4017. }
  4018. }
  4019. return conn_stop_pv(conn, ts);
  4020. }
  4021. static size_t conn_shape_udp_payload(ngtcp2_conn *conn, const ngtcp2_dcid *dcid,
  4022. size_t payloadlen) {
  4023. if (conn->remote.transport_params &&
  4024. conn->remote.transport_params->max_udp_payload_size) {
  4025. assert(conn->remote.transport_params->max_udp_payload_size >=
  4026. NGTCP2_MAX_UDP_PAYLOAD_SIZE);
  4027. payloadlen = (size_t)ngtcp2_min_uint64(
  4028. (uint64_t)payloadlen,
  4029. conn->remote.transport_params->max_udp_payload_size);
  4030. }
  4031. payloadlen =
  4032. ngtcp2_min_size(payloadlen, conn->local.settings.max_tx_udp_payload_size);
  4033. if (conn->local.settings.no_tx_udp_payload_size_shaping) {
  4034. return payloadlen;
  4035. }
  4036. return ngtcp2_min_size(payloadlen, dcid->max_udp_payload_size);
  4037. }
  4038. static void conn_reset_congestion_state(ngtcp2_conn *conn, ngtcp2_tstamp ts);
  4039. /*
  4040. * conn_on_path_validation_failed is called when path validation
  4041. * fails. This function may delete |pv|.
  4042. *
  4043. * This function returns 0 if it succeeds, or one of the following
  4044. * negative error codes:
  4045. *
  4046. * NGTCP2_ERR_NOMEM
  4047. * Out of memory
  4048. * NGTCP2_ERR_CALLBACK_FAILURE
  4049. * User-defined callback function failed.
  4050. */
  4051. static int conn_on_path_validation_failed(ngtcp2_conn *conn, ngtcp2_pv *pv,
  4052. ngtcp2_tstamp ts) {
  4053. int rv;
  4054. if (!(pv->flags & NGTCP2_PV_FLAG_DONT_CARE)) {
  4055. rv = conn_call_path_validation(conn, pv,
  4056. NGTCP2_PATH_VALIDATION_RESULT_FAILURE);
  4057. if (rv != 0) {
  4058. return rv;
  4059. }
  4060. }
  4061. if (pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) {
  4062. ngtcp2_dcid_copy(&conn->dcid.current, &pv->fallback_dcid);
  4063. conn_reset_congestion_state(conn, ts);
  4064. }
  4065. return conn_stop_pv(conn, ts);
  4066. }
  4067. /*
  4068. * conn_write_path_challenge writes a packet which includes
  4069. * PATH_CHALLENGE frame into |dest| of length |destlen|.
  4070. *
  4071. * This function returns the number of bytes written to |dest|, or one
  4072. * of the following negative error codes:
  4073. *
  4074. * NGTCP2_ERR_NOMEM
  4075. * Out of memory
  4076. * NGTCP2_ERR_CALLBACK_FAILURE
  4077. * User-defined callback function failed.
  4078. */
  4079. static ngtcp2_ssize conn_write_path_challenge(ngtcp2_conn *conn,
  4080. ngtcp2_path *path,
  4081. ngtcp2_pkt_info *pi,
  4082. uint8_t *dest, size_t destlen,
  4083. ngtcp2_tstamp ts) {
  4084. ngtcp2_ssize nwrite;
  4085. ngtcp2_tstamp expiry;
  4086. ngtcp2_pv *pv = conn->pv;
  4087. ngtcp2_frame lfr;
  4088. ngtcp2_duration timeout, initial_pto;
  4089. uint8_t flags;
  4090. uint64_t tx_left;
  4091. int rv;
  4092. if (ngtcp2_pv_validation_timed_out(pv, ts)) {
  4093. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PTV,
  4094. "path validation was timed out");
  4095. rv = conn_on_path_validation_failed(conn, pv, ts);
  4096. if (rv != 0) {
  4097. return rv;
  4098. }
  4099. /* We might set path to the one which we just failed validate.
  4100. Set it to the current path here. */
  4101. if (path) {
  4102. ngtcp2_path_copy(path, &conn->dcid.current.ps.path);
  4103. }
  4104. return 0;
  4105. }
  4106. ngtcp2_pv_handle_entry_expiry(pv, ts);
  4107. if (!ngtcp2_pv_should_send_probe(pv)) {
  4108. return 0;
  4109. }
  4110. rv = conn_call_get_path_challenge_data(conn, lfr.path_challenge.data);
  4111. if (rv != 0) {
  4112. return rv;
  4113. }
  4114. lfr.type = NGTCP2_FRAME_PATH_CHALLENGE;
  4115. initial_pto = conn_compute_initial_pto(conn, &conn->pktns);
  4116. timeout = conn_compute_pto(conn, &conn->pktns);
  4117. timeout = ngtcp2_max_uint64(timeout, initial_pto);
  4118. expiry = ts + timeout * (1ULL << pv->round);
  4119. destlen = ngtcp2_min_size(destlen, NGTCP2_MAX_UDP_PAYLOAD_SIZE);
  4120. if (conn->server) {
  4121. if (!(pv->dcid.flags & NGTCP2_DCID_FLAG_PATH_VALIDATED)) {
  4122. tx_left = conn_server_tx_left(conn, &pv->dcid);
  4123. destlen = (size_t)ngtcp2_min_uint64((uint64_t)destlen, tx_left);
  4124. if (destlen == 0) {
  4125. return 0;
  4126. }
  4127. }
  4128. if (destlen < NGTCP2_MAX_UDP_PAYLOAD_SIZE) {
  4129. flags = NGTCP2_PV_ENTRY_FLAG_UNDERSIZED;
  4130. } else {
  4131. flags = NGTCP2_PV_ENTRY_FLAG_NONE;
  4132. }
  4133. } else {
  4134. flags = NGTCP2_PV_ENTRY_FLAG_NONE;
  4135. }
  4136. ngtcp2_pv_add_entry(pv, lfr.path_challenge.data, expiry, flags, ts);
  4137. nwrite = ngtcp2_conn_write_single_frame_pkt(
  4138. conn, pi, dest, destlen, NGTCP2_PKT_1RTT, NGTCP2_WRITE_PKT_FLAG_NONE,
  4139. &pv->dcid.cid, &lfr,
  4140. NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING | NGTCP2_RTB_ENTRY_FLAG_PTO_ELICITING,
  4141. &pv->dcid.ps.path, ts);
  4142. if (nwrite <= 0) {
  4143. return nwrite;
  4144. }
  4145. if (path) {
  4146. ngtcp2_path_copy(path, &pv->dcid.ps.path);
  4147. }
  4148. if (ngtcp2_path_eq(&pv->dcid.ps.path, &conn->dcid.current.ps.path)) {
  4149. conn->dcid.current.bytes_sent += (uint64_t)nwrite;
  4150. } else {
  4151. pv->dcid.bytes_sent += (uint64_t)nwrite;
  4152. }
  4153. return nwrite;
  4154. }
  4155. /*
  4156. * conn_write_path_response writes a packet which includes
  4157. * PATH_RESPONSE frame into |dest| of length |destlen|.
  4158. *
  4159. * This function returns the number of bytes written to |dest|, or one
  4160. * of the following negative error codes:
  4161. *
  4162. * NGTCP2_ERR_NOMEM
  4163. * Out of memory
  4164. * NGTCP2_ERR_CALLBACK_FAILURE
  4165. * User-defined callback function failed.
  4166. */
  4167. static ngtcp2_ssize conn_write_path_response(ngtcp2_conn *conn,
  4168. ngtcp2_path *path,
  4169. ngtcp2_pkt_info *pi, uint8_t *dest,
  4170. size_t destlen, ngtcp2_tstamp ts) {
  4171. ngtcp2_pv *pv = conn->pv;
  4172. ngtcp2_path_challenge_entry *pcent = NULL;
  4173. ngtcp2_dcid *dcid = NULL;
  4174. ngtcp2_frame lfr;
  4175. ngtcp2_ssize nwrite;
  4176. int rv;
  4177. uint64_t tx_left;
  4178. for (; ngtcp2_ringbuf_len(&conn->rx.path_challenge.rb);) {
  4179. pcent = ngtcp2_ringbuf_get(&conn->rx.path_challenge.rb, 0);
  4180. if (ngtcp2_path_eq(&conn->dcid.current.ps.path, &pcent->ps.path)) {
  4181. /* Send PATH_RESPONSE from conn_write_pkt. */
  4182. return 0;
  4183. }
  4184. if (pv) {
  4185. if (ngtcp2_path_eq(&pv->dcid.ps.path, &pcent->ps.path)) {
  4186. dcid = &pv->dcid;
  4187. break;
  4188. }
  4189. if ((pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  4190. ngtcp2_path_eq(&pv->fallback_dcid.ps.path, &pcent->ps.path)) {
  4191. dcid = &pv->fallback_dcid;
  4192. break;
  4193. }
  4194. }
  4195. if (conn->server) {
  4196. break;
  4197. }
  4198. /* Client does not expect to respond to path validation against
  4199. unknown path */
  4200. ngtcp2_ringbuf_pop_front(&conn->rx.path_challenge.rb);
  4201. pcent = NULL;
  4202. }
  4203. if (pcent == NULL) {
  4204. return 0;
  4205. }
  4206. if (dcid == NULL) {
  4207. /* client is expected to have |path| in conn->dcid.current or
  4208. conn->pv. */
  4209. assert(conn->server);
  4210. rv = conn_bind_dcid(conn, &dcid, &pcent->ps.path, ts);
  4211. if (rv != 0) {
  4212. if (ngtcp2_err_is_fatal(rv)) {
  4213. return rv;
  4214. }
  4215. return 0;
  4216. }
  4217. }
  4218. destlen = ngtcp2_min_size(destlen, NGTCP2_MAX_UDP_PAYLOAD_SIZE);
  4219. if (conn->server && !(dcid->flags & NGTCP2_DCID_FLAG_PATH_VALIDATED)) {
  4220. tx_left = conn_server_tx_left(conn, dcid);
  4221. destlen = (size_t)ngtcp2_min_uint64((uint64_t)destlen, tx_left);
  4222. if (destlen == 0) {
  4223. return 0;
  4224. }
  4225. }
  4226. lfr.type = NGTCP2_FRAME_PATH_RESPONSE;
  4227. memcpy(lfr.path_response.data, pcent->data, sizeof(lfr.path_response.data));
  4228. nwrite = ngtcp2_conn_write_single_frame_pkt(
  4229. conn, pi, dest, destlen, NGTCP2_PKT_1RTT, NGTCP2_WRITE_PKT_FLAG_NONE,
  4230. &dcid->cid, &lfr, NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING, &pcent->ps.path, ts);
  4231. if (nwrite <= 0) {
  4232. return nwrite;
  4233. }
  4234. if (path) {
  4235. ngtcp2_path_copy(path, &pcent->ps.path);
  4236. }
  4237. ngtcp2_ringbuf_pop_front(&conn->rx.path_challenge.rb);
  4238. dcid->bytes_sent += (uint64_t)nwrite;
  4239. return nwrite;
  4240. }
  4241. ngtcp2_ssize ngtcp2_conn_write_pkt_versioned(ngtcp2_conn *conn,
  4242. ngtcp2_path *path,
  4243. int pkt_info_version,
  4244. ngtcp2_pkt_info *pi, uint8_t *dest,
  4245. size_t destlen, ngtcp2_tstamp ts) {
  4246. return ngtcp2_conn_writev_stream_versioned(
  4247. conn, path, pkt_info_version, pi, dest, destlen,
  4248. /* pdatalen = */ NULL, NGTCP2_WRITE_STREAM_FLAG_NONE,
  4249. /* stream_id = */ -1,
  4250. /* datav = */ NULL, /* datavcnt = */ 0, ts);
  4251. }
  4252. /*
  4253. * conn_on_version_negotiation is called when Version Negotiation
  4254. * packet is received. The function decodes the data in the buffer
  4255. * pointed by |payload| whose length is |payloadlen| as Version
  4256. * Negotiation packet payload. The packet header is given in |hd|.
  4257. *
  4258. * This function returns 0 if it succeeds, or one of the following
  4259. * negative error codes:
  4260. *
  4261. * NGTCP2_ERR_NOMEM
  4262. * Out of memory.
  4263. * NGTCP2_ERR_CALLBACK_FAILURE
  4264. * User-defined callback function failed.
  4265. * NGTCP2_ERR_INVALID_ARGUMENT
  4266. * Packet payload is badly formatted.
  4267. */
  4268. static int conn_on_version_negotiation(ngtcp2_conn *conn,
  4269. const ngtcp2_pkt_hd *hd,
  4270. const uint8_t *payload,
  4271. size_t payloadlen) {
  4272. uint32_t sv[16];
  4273. uint32_t *p;
  4274. int rv = 0;
  4275. size_t nsv;
  4276. size_t i;
  4277. if (payloadlen % sizeof(uint32_t)) {
  4278. return NGTCP2_ERR_INVALID_ARGUMENT;
  4279. }
  4280. /* Version Negotiation packet is ignored if client has reacted upon
  4281. Version Negotiation packet. */
  4282. if (conn->local.settings.original_version != conn->client_chosen_version) {
  4283. return NGTCP2_ERR_INVALID_ARGUMENT;
  4284. }
  4285. if (payloadlen > sizeof(sv)) {
  4286. p = ngtcp2_mem_malloc(conn->mem, payloadlen);
  4287. if (p == NULL) {
  4288. return NGTCP2_ERR_NOMEM;
  4289. }
  4290. } else {
  4291. p = sv;
  4292. }
  4293. nsv = ngtcp2_pkt_decode_version_negotiation(p, payload, payloadlen);
  4294. ngtcp2_log_rx_vn(&conn->log, hd, p, nsv);
  4295. ngtcp2_qlog_version_negotiation_pkt_received(&conn->qlog, hd, p, nsv);
  4296. if (!ngtcp2_is_reserved_version(conn->local.settings.original_version)) {
  4297. for (i = 0; i < nsv; ++i) {
  4298. if (p[i] == conn->local.settings.original_version) {
  4299. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  4300. "ignore Version Negotiation because it contains the "
  4301. "original version");
  4302. rv = NGTCP2_ERR_INVALID_ARGUMENT;
  4303. goto fin;
  4304. }
  4305. }
  4306. }
  4307. rv = conn_call_recv_version_negotiation(conn, hd, p, nsv);
  4308. if (rv != 0) {
  4309. goto fin;
  4310. }
  4311. fin:
  4312. if (p != sv) {
  4313. ngtcp2_mem_free(conn->mem, p);
  4314. }
  4315. return rv;
  4316. }
  4317. static uint64_t conn_tx_strmq_first_cycle(ngtcp2_conn *conn) {
  4318. ngtcp2_strm *strm;
  4319. if (ngtcp2_pq_empty(&conn->tx.strmq)) {
  4320. return 0;
  4321. }
  4322. strm = ngtcp2_struct_of(ngtcp2_pq_top(&conn->tx.strmq), ngtcp2_strm, pe);
  4323. return strm->cycle;
  4324. }
  4325. uint64_t ngtcp2_conn_tx_strmq_first_cycle(ngtcp2_conn *conn) {
  4326. ngtcp2_strm *strm;
  4327. if (ngtcp2_pq_empty(&conn->tx.strmq)) {
  4328. return 0;
  4329. }
  4330. strm = ngtcp2_struct_of(ngtcp2_pq_top(&conn->tx.strmq), ngtcp2_strm, pe);
  4331. return strm->cycle;
  4332. }
  4333. /*
  4334. * conn_on_retry is called when Retry packet is received. The
  4335. * function decodes the data in the buffer pointed by |pkt| whose
  4336. * length is |pktlen| as Retry packet. The length of long packet
  4337. * header is given in |hdpktlen|. |pkt| includes packet header.
  4338. *
  4339. * This function returns 0 if it succeeds, or one of the following
  4340. * negative error codes:
  4341. *
  4342. * NGTCP2_ERR_NOMEM
  4343. * Out of memory.
  4344. * NGTCP2_ERR_CALLBACK_FAILURE
  4345. * User-defined callback function failed.
  4346. * NGTCP2_ERR_INVALID_ARGUMENT
  4347. * Packet payload is badly formatted.
  4348. * NGTCP2_ERR_PROTO
  4349. * ODCID does not match; or Token is empty.
  4350. */
  4351. static int conn_on_retry(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd,
  4352. size_t hdpktlen, const uint8_t *pkt, size_t pktlen,
  4353. ngtcp2_tstamp ts) {
  4354. int rv;
  4355. ngtcp2_pkt_retry retry;
  4356. ngtcp2_pktns *in_pktns = conn->in_pktns;
  4357. ngtcp2_rtb *rtb = &conn->pktns.rtb;
  4358. ngtcp2_rtb *in_rtb;
  4359. uint8_t cidbuf[sizeof(retry.odcid.data) * 2 + 1];
  4360. uint8_t *token;
  4361. if (!in_pktns || conn->flags & NGTCP2_CONN_FLAG_RECV_RETRY) {
  4362. return 0;
  4363. }
  4364. in_rtb = &in_pktns->rtb;
  4365. rv = ngtcp2_pkt_decode_retry(&retry, pkt + hdpktlen, pktlen - hdpktlen);
  4366. if (rv != 0) {
  4367. return rv;
  4368. }
  4369. retry.odcid = conn->dcid.current.cid;
  4370. rv = ngtcp2_pkt_verify_retry_tag(
  4371. conn->client_chosen_version, &retry, pkt, pktlen, conn->callbacks.encrypt,
  4372. &conn->crypto.retry_aead, &conn->crypto.retry_aead_ctx);
  4373. if (rv != 0) {
  4374. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  4375. "unable to verify Retry packet integrity");
  4376. return rv;
  4377. }
  4378. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT, "odcid=0x%s",
  4379. (const char *)ngtcp2_encode_hex(cidbuf, retry.odcid.data,
  4380. retry.odcid.datalen));
  4381. if (retry.tokenlen == 0) {
  4382. return NGTCP2_ERR_PROTO;
  4383. }
  4384. if (ngtcp2_cid_eq(&conn->dcid.current.cid, &hd->scid)) {
  4385. return 0;
  4386. }
  4387. ngtcp2_qlog_retry_pkt_received(&conn->qlog, hd, &retry);
  4388. /* DCID must be updated before invoking callback because client
  4389. generates new initial keys there. */
  4390. conn->dcid.current.cid = hd->scid;
  4391. conn->retry_scid = hd->scid;
  4392. conn->flags |= NGTCP2_CONN_FLAG_RECV_RETRY;
  4393. rv = conn_call_recv_retry(conn, hd);
  4394. if (rv != 0) {
  4395. return rv;
  4396. }
  4397. conn->state = NGTCP2_CS_CLIENT_INITIAL;
  4398. /* Just freeing memory is dangerous because we might free twice. */
  4399. rv = ngtcp2_rtb_remove_all(rtb, conn, &conn->pktns, &conn->cstat);
  4400. if (rv != 0) {
  4401. return rv;
  4402. }
  4403. rv = ngtcp2_rtb_remove_all(in_rtb, conn, in_pktns, &conn->cstat);
  4404. if (rv != 0) {
  4405. return rv;
  4406. }
  4407. ngtcp2_mem_free(conn->mem, (uint8_t *)conn->local.settings.token);
  4408. conn->local.settings.token = NULL;
  4409. conn->local.settings.tokenlen = 0;
  4410. token = ngtcp2_mem_malloc(conn->mem, retry.tokenlen);
  4411. if (token == NULL) {
  4412. return NGTCP2_ERR_NOMEM;
  4413. }
  4414. ngtcp2_cpymem(token, retry.token, retry.tokenlen);
  4415. conn->local.settings.token = token;
  4416. conn->local.settings.tokenlen = retry.tokenlen;
  4417. reset_conn_stat_recovery(&conn->cstat);
  4418. conn_reset_congestion_state(conn, ts);
  4419. conn_reset_ecn_validation_state(conn);
  4420. return 0;
  4421. }
  4422. int ngtcp2_conn_detect_lost_pkt(ngtcp2_conn *conn, ngtcp2_pktns *pktns,
  4423. ngtcp2_conn_stat *cstat, ngtcp2_tstamp ts) {
  4424. return ngtcp2_rtb_detect_lost_pkt(&pktns->rtb, conn, pktns, cstat, ts);
  4425. }
  4426. /*
  4427. * conn_recv_ack processes received ACK frame |fr|. |pkt_ts| is the
  4428. * timestamp when packet is received. |ts| should be the current
  4429. * time. Usually they are the same, but for buffered packets,
  4430. * |pkt_ts| would be earlier than |ts|. This function needs to be
  4431. * called after |fr| is validated by ngtcp2_pkt_validate_ack.
  4432. *
  4433. * This function returns 0 if it succeeds, or one of the following
  4434. * negative error codes:
  4435. *
  4436. * NGTCP2_ERR_NOMEM
  4437. * Out of memory
  4438. * NGTCP2_ERR_PROTO
  4439. * |fr| acknowledges a packet this endpoint has not sent.
  4440. * NGTCP2_ERR_CALLBACK_FAILURE
  4441. * User callback failed.
  4442. */
  4443. static int conn_recv_ack(ngtcp2_conn *conn, ngtcp2_pktns *pktns, ngtcp2_ack *fr,
  4444. ngtcp2_tstamp pkt_ts, ngtcp2_tstamp ts) {
  4445. ngtcp2_ssize num_acked;
  4446. ngtcp2_conn_stat *cstat = &conn->cstat;
  4447. if (pktns->tx.last_pkt_num < fr->largest_ack) {
  4448. return NGTCP2_ERR_PROTO;
  4449. }
  4450. ngtcp2_acktr_recv_ack(&pktns->acktr, fr);
  4451. num_acked =
  4452. ngtcp2_rtb_recv_ack(&pktns->rtb, fr, &conn->cstat, conn, pktns, pkt_ts, ts);
  4453. if (num_acked < 0) {
  4454. assert(ngtcp2_err_is_fatal((int)num_acked));
  4455. return (int)num_acked;
  4456. }
  4457. if (num_acked == 0) {
  4458. return 0;
  4459. }
  4460. pktns->rtb.probe_pkt_left = 0;
  4461. if (cstat->pto_count &&
  4462. (conn->server || (conn->flags & NGTCP2_CONN_FLAG_SERVER_ADDR_VERIFIED))) {
  4463. /* Reset PTO count but no less than 2 to avoid frequent probe
  4464. packet transmission. */
  4465. cstat->pto_count = ngtcp2_min_size(cstat->pto_count, 2);
  4466. }
  4467. ngtcp2_conn_set_loss_detection_timer(conn, ts);
  4468. return 0;
  4469. }
  4470. /*
  4471. * conn_assign_recved_ack_delay_unscaled assigns
  4472. * fr->ack_delay_unscaled.
  4473. */
  4474. static void assign_recved_ack_delay_unscaled(ngtcp2_ack *fr,
  4475. uint64_t ack_delay_exponent) {
  4476. fr->ack_delay_unscaled =
  4477. fr->ack_delay * (1ULL << ack_delay_exponent) * NGTCP2_MICROSECONDS;
  4478. }
  4479. /*
  4480. * conn_recv_max_stream_data processes received MAX_STREAM_DATA frame
  4481. * |fr|.
  4482. *
  4483. * This function returns 0 if it succeeds, or one of the following
  4484. * negative error codes:
  4485. *
  4486. * NGTCP2_ERR_STREAM_STATE
  4487. * Stream ID indicates that it is a local stream, and the local
  4488. * endpoint has not initiated it; or stream is peer initiated
  4489. * unidirectional stream.
  4490. * NGTCP2_ERR_STREAM_LIMIT
  4491. * Stream ID exceeds allowed limit.
  4492. * NGTCP2_ERR_NOMEM
  4493. * Out of memory.
  4494. */
  4495. static int conn_recv_max_stream_data(ngtcp2_conn *conn,
  4496. const ngtcp2_max_stream_data *fr) {
  4497. ngtcp2_strm *strm;
  4498. ngtcp2_idtr *idtr;
  4499. int local_stream = conn_local_stream(conn, fr->stream_id);
  4500. int bidi = bidi_stream(fr->stream_id);
  4501. int rv;
  4502. if (bidi) {
  4503. if (local_stream) {
  4504. if (conn->local.bidi.next_stream_id <= fr->stream_id) {
  4505. return NGTCP2_ERR_STREAM_STATE;
  4506. }
  4507. } else if (conn->remote.bidi.max_streams <
  4508. ngtcp2_ord_stream_id(fr->stream_id)) {
  4509. return NGTCP2_ERR_STREAM_LIMIT;
  4510. }
  4511. idtr = &conn->remote.bidi.idtr;
  4512. } else {
  4513. if (!local_stream || conn->local.uni.next_stream_id <= fr->stream_id) {
  4514. return NGTCP2_ERR_STREAM_STATE;
  4515. }
  4516. idtr = &conn->remote.uni.idtr;
  4517. }
  4518. strm = ngtcp2_conn_find_stream(conn, fr->stream_id);
  4519. if (strm == NULL) {
  4520. if (local_stream) {
  4521. /* Stream has been closed. */
  4522. return 0;
  4523. }
  4524. rv = ngtcp2_idtr_open(idtr, fr->stream_id);
  4525. if (rv != 0) {
  4526. if (ngtcp2_err_is_fatal(rv)) {
  4527. return rv;
  4528. }
  4529. assert(rv == NGTCP2_ERR_STREAM_IN_USE);
  4530. /* Stream has been closed. */
  4531. return 0;
  4532. }
  4533. strm = ngtcp2_objalloc_strm_get(&conn->strm_objalloc);
  4534. if (strm == NULL) {
  4535. return NGTCP2_ERR_NOMEM;
  4536. }
  4537. rv = ngtcp2_conn_init_stream(conn, strm, fr->stream_id, NULL);
  4538. if (rv != 0) {
  4539. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, strm);
  4540. return rv;
  4541. }
  4542. rv = conn_call_stream_open(conn, strm);
  4543. if (rv != 0) {
  4544. return rv;
  4545. }
  4546. }
  4547. if (strm->tx.max_offset < fr->max_stream_data) {
  4548. strm->tx.max_offset = fr->max_stream_data;
  4549. /* Don't call callback if stream is half-closed local */
  4550. if (strm->flags & NGTCP2_STRM_FLAG_SHUT_WR) {
  4551. return 0;
  4552. }
  4553. rv = conn_call_extend_max_stream_data(conn, strm, fr->stream_id,
  4554. fr->max_stream_data);
  4555. if (rv != 0) {
  4556. return rv;
  4557. }
  4558. }
  4559. return 0;
  4560. }
  4561. /*
  4562. * conn_recv_max_data processes received MAX_DATA frame |fr|.
  4563. */
  4564. static void conn_recv_max_data(ngtcp2_conn *conn, const ngtcp2_max_data *fr) {
  4565. conn->tx.max_offset = ngtcp2_max_uint64(conn->tx.max_offset, fr->max_data);
  4566. }
  4567. /*
  4568. * conn_buffer_pkt buffers |pkt| of length |pktlen|, chaining it from
  4569. * |*ppc|.
  4570. *
  4571. * This function returns 0 if it succeeds, or one of the following
  4572. * negative error codes:
  4573. *
  4574. * NGTCP2_ERR_NOMEM
  4575. * Out of memory.
  4576. */
  4577. static int conn_buffer_pkt(ngtcp2_conn *conn, ngtcp2_pktns *pktns,
  4578. const ngtcp2_path *path, const ngtcp2_pkt_info *pi,
  4579. const uint8_t *pkt, size_t pktlen, size_t dgramlen,
  4580. ngtcp2_tstamp ts) {
  4581. int rv;
  4582. ngtcp2_pkt_chain **ppc = &pktns->rx.buffed_pkts, *pc;
  4583. size_t i;
  4584. for (i = 0; *ppc && i < NGTCP2_MAX_NUM_BUFFED_RX_PKTS;
  4585. ppc = &(*ppc)->next, ++i)
  4586. ;
  4587. if (i == NGTCP2_MAX_NUM_BUFFED_RX_PKTS) {
  4588. return 0;
  4589. }
  4590. rv =
  4591. ngtcp2_pkt_chain_new(&pc, path, pi, pkt, pktlen, dgramlen, ts, conn->mem);
  4592. if (rv != 0) {
  4593. return rv;
  4594. }
  4595. *ppc = pc;
  4596. return 0;
  4597. }
  4598. static int ensure_decrypt_buffer(ngtcp2_vec *vec, size_t n, size_t initial,
  4599. const ngtcp2_mem *mem) {
  4600. uint8_t *nbuf;
  4601. size_t len;
  4602. if (vec->len >= n) {
  4603. return 0;
  4604. }
  4605. len = vec->len == 0 ? initial : vec->len * 2;
  4606. for (; len < n; len *= 2)
  4607. ;
  4608. nbuf = ngtcp2_mem_realloc(mem, vec->base, len);
  4609. if (nbuf == NULL) {
  4610. return NGTCP2_ERR_NOMEM;
  4611. }
  4612. vec->base = nbuf;
  4613. vec->len = len;
  4614. return 0;
  4615. }
  4616. /*
  4617. * conn_ensure_decrypt_hp_buffer ensures that
  4618. * conn->crypto.decrypt_hp_buf has at least |n| bytes space.
  4619. *
  4620. * This function returns 0 if it succeeds, or one of the following
  4621. * negative error codes:
  4622. *
  4623. * NGTCP2_ERR_NOMEM
  4624. * Out of memory.
  4625. */
  4626. static int conn_ensure_decrypt_hp_buffer(ngtcp2_conn *conn, size_t n) {
  4627. return ensure_decrypt_buffer(&conn->crypto.decrypt_hp_buf, n, 256, conn->mem);
  4628. }
  4629. /*
  4630. * conn_ensure_decrypt_buffer ensures that conn->crypto.decrypt_buf
  4631. * has at least |n| bytes space.
  4632. *
  4633. * This function returns 0 if it succeeds, or one of the following
  4634. * negative error codes:
  4635. *
  4636. * NGTCP2_ERR_NOMEM
  4637. * Out of memory.
  4638. */
  4639. static int conn_ensure_decrypt_buffer(ngtcp2_conn *conn, size_t n) {
  4640. return ensure_decrypt_buffer(&conn->crypto.decrypt_buf, n, 2048, conn->mem);
  4641. }
  4642. /*
  4643. * decrypt_pkt decrypts the data pointed by |payload| whose length is
  4644. * |payloadlen|, and writes plaintext data to the buffer pointed by
  4645. * |dest|. The buffer pointed by |aad| is the Additional
  4646. * Authenticated Data, and its length is |aadlen|. |pkt_num| is used
  4647. * to create a nonce. |ckm| is the cryptographic key, and iv to use.
  4648. * |decrypt| is a callback function which actually decrypts a packet.
  4649. *
  4650. * This function returns the number of bytes written in |dest| if it
  4651. * succeeds, or one of the following negative error codes:
  4652. *
  4653. * NGTCP2_ERR_CALLBACK_FAILURE
  4654. * User callback failed.
  4655. * NGTCP2_ERR_DECRYPT
  4656. * Failed to decrypt a packet.
  4657. */
  4658. static ngtcp2_ssize decrypt_pkt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
  4659. const uint8_t *payload, size_t payloadlen,
  4660. const uint8_t *aad, size_t aadlen,
  4661. int64_t pkt_num, ngtcp2_crypto_km *ckm,
  4662. ngtcp2_decrypt decrypt) {
  4663. /* TODO nonce is limited to 64 bytes. */
  4664. uint8_t nonce[64];
  4665. int rv;
  4666. assert(sizeof(nonce) >= ckm->iv.len);
  4667. ngtcp2_crypto_create_nonce(nonce, ckm->iv.base, ckm->iv.len, pkt_num);
  4668. rv = decrypt(dest, aead, &ckm->aead_ctx, payload, payloadlen, nonce,
  4669. ckm->iv.len, aad, aadlen);
  4670. if (rv != 0) {
  4671. if (rv == NGTCP2_ERR_DECRYPT) {
  4672. return rv;
  4673. }
  4674. return NGTCP2_ERR_CALLBACK_FAILURE;
  4675. }
  4676. assert(payloadlen >= aead->max_overhead);
  4677. return (ngtcp2_ssize)(payloadlen - aead->max_overhead);
  4678. }
  4679. /*
  4680. * decrypt_hp decryptes packet header. The packet number starts at
  4681. * |pkt| + |pkt_num_offset|. The entire plaintext QUIC packet header
  4682. * will be written to the buffer pointed by |dest| whose capacity is
  4683. * |destlen|.
  4684. *
  4685. * This function returns the number of bytes written to |dest|, or one
  4686. * of the following negative error codes:
  4687. *
  4688. * NGTCP2_ERR_PROTO
  4689. * Packet is badly formatted
  4690. * NGTCP2_ERR_CALLBACK_FAILURE
  4691. * User-defined callback function failed; or it does not return
  4692. * expected result.
  4693. */
  4694. static ngtcp2_ssize
  4695. decrypt_hp(ngtcp2_pkt_hd *hd, uint8_t *dest, const ngtcp2_crypto_cipher *hp,
  4696. const uint8_t *pkt, size_t pktlen, size_t pkt_num_offset,
  4697. const ngtcp2_crypto_cipher_ctx *hp_ctx, ngtcp2_hp_mask hp_mask) {
  4698. size_t sample_offset;
  4699. uint8_t *p = dest;
  4700. uint8_t mask[NGTCP2_HP_SAMPLELEN];
  4701. size_t i;
  4702. int rv;
  4703. assert(hp_mask);
  4704. if (pkt_num_offset + 4 + NGTCP2_HP_SAMPLELEN > pktlen) {
  4705. return NGTCP2_ERR_PROTO;
  4706. }
  4707. p = ngtcp2_cpymem(p, pkt, pkt_num_offset);
  4708. sample_offset = pkt_num_offset + 4;
  4709. rv = hp_mask(mask, hp, hp_ctx, pkt + sample_offset);
  4710. if (rv != 0) {
  4711. return NGTCP2_ERR_CALLBACK_FAILURE;
  4712. }
  4713. if (hd->flags & NGTCP2_PKT_FLAG_LONG_FORM) {
  4714. dest[0] = (uint8_t)(dest[0] ^ (mask[0] & 0x0f));
  4715. } else {
  4716. dest[0] = (uint8_t)(dest[0] ^ (mask[0] & 0x1f));
  4717. if (dest[0] & NGTCP2_SHORT_KEY_PHASE_BIT) {
  4718. hd->flags |= NGTCP2_PKT_FLAG_KEY_PHASE;
  4719. }
  4720. }
  4721. hd->pkt_numlen = (size_t)((dest[0] & NGTCP2_PKT_NUMLEN_MASK) + 1);
  4722. for (i = 0; i < hd->pkt_numlen; ++i) {
  4723. *p++ = *(pkt + pkt_num_offset + i) ^ mask[i + 1];
  4724. }
  4725. hd->pkt_num = ngtcp2_get_pkt_num(p - hd->pkt_numlen, hd->pkt_numlen);
  4726. return p - dest;
  4727. }
  4728. /*
  4729. * conn_emit_pending_crypto_data delivers pending stream data to the
  4730. * application due to packet reordering.
  4731. *
  4732. * This function returns 0 if it succeeds, or one of the following
  4733. * negative error codes:
  4734. *
  4735. * NGTCP2_ERR_CALLBACK_FAILURE
  4736. * User callback failed
  4737. * NGTCP2_ERR_CRYPTO
  4738. * TLS backend reported error
  4739. */
  4740. static int
  4741. conn_emit_pending_crypto_data(ngtcp2_conn *conn,
  4742. ngtcp2_encryption_level encryption_level,
  4743. ngtcp2_strm *strm, uint64_t rx_offset) {
  4744. size_t datalen;
  4745. const uint8_t *data;
  4746. int rv;
  4747. uint64_t offset;
  4748. if (!strm->rx.rob) {
  4749. return 0;
  4750. }
  4751. for (;;) {
  4752. datalen = ngtcp2_rob_data_at(strm->rx.rob, &data, rx_offset);
  4753. if (datalen == 0) {
  4754. assert(rx_offset == ngtcp2_strm_rx_offset(strm));
  4755. return 0;
  4756. }
  4757. offset = rx_offset;
  4758. rx_offset += datalen;
  4759. rv =
  4760. conn_call_recv_crypto_data(conn, encryption_level, offset, data, datalen);
  4761. if (rv != 0) {
  4762. return rv;
  4763. }
  4764. ngtcp2_rob_pop(strm->rx.rob, rx_offset - datalen, datalen);
  4765. }
  4766. }
  4767. /*
  4768. * conn_recv_connection_close is called when CONNECTION_CLOSE or
  4769. * APPLICATION_CLOSE frame is received.
  4770. */
  4771. static int conn_recv_connection_close(ngtcp2_conn *conn,
  4772. ngtcp2_connection_close *fr) {
  4773. ngtcp2_ccerr *ccerr = &conn->rx.ccerr;
  4774. conn->state = NGTCP2_CS_DRAINING;
  4775. if (fr->type == NGTCP2_FRAME_CONNECTION_CLOSE) {
  4776. ccerr->type = NGTCP2_CCERR_TYPE_TRANSPORT;
  4777. } else {
  4778. ccerr->type = NGTCP2_CCERR_TYPE_APPLICATION;
  4779. }
  4780. ccerr->error_code = fr->error_code;
  4781. ccerr->frame_type = fr->frame_type;
  4782. if (!fr->reasonlen) {
  4783. ccerr->reasonlen = 0;
  4784. return 0;
  4785. }
  4786. if (ccerr->reason == NULL) {
  4787. ccerr->reason = ngtcp2_mem_malloc(conn->mem, NGTCP2_CCERR_MAX_REASONLEN);
  4788. if (ccerr->reason == NULL) {
  4789. return NGTCP2_ERR_NOMEM;
  4790. }
  4791. }
  4792. ccerr->reasonlen = ngtcp2_min_size(fr->reasonlen, NGTCP2_CCERR_MAX_REASONLEN);
  4793. ngtcp2_cpymem((uint8_t *)ccerr->reason, fr->reason, ccerr->reasonlen);
  4794. return 0;
  4795. }
  4796. static void conn_recv_path_challenge(ngtcp2_conn *conn, const ngtcp2_path *path,
  4797. ngtcp2_path_challenge *fr) {
  4798. ngtcp2_path_challenge_entry *ent;
  4799. /* client only responds to PATH_CHALLENGE from the current path or
  4800. path which client is migrating to. */
  4801. if (!conn->server && !ngtcp2_path_eq(&conn->dcid.current.ps.path, path) &&
  4802. (!conn->pv || !ngtcp2_path_eq(&conn->pv->dcid.ps.path, path))) {
  4803. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  4804. "discard PATH_CHALLENGE from the path which is not current "
  4805. "or endpoint is migrating to");
  4806. return;
  4807. }
  4808. ent = ngtcp2_ringbuf_push_front(&conn->rx.path_challenge.rb);
  4809. ngtcp2_path_challenge_entry_init(ent, path, fr->data);
  4810. }
  4811. /*
  4812. * conn_reset_congestion_state resets congestion state.
  4813. */
  4814. static void conn_reset_congestion_state(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  4815. conn_reset_conn_stat_cc(conn, &conn->cstat);
  4816. if (conn->cc.reset) {
  4817. conn->cc.reset(&conn->cc, &conn->cstat, ts);
  4818. }
  4819. if (conn->hs_pktns) {
  4820. ngtcp2_rtb_reset_cc_state(&conn->hs_pktns->rtb,
  4821. conn->hs_pktns->tx.last_pkt_num + 1);
  4822. }
  4823. ngtcp2_rtb_reset_cc_state(&conn->pktns.rtb, conn->pktns.tx.last_pkt_num + 1);
  4824. ngtcp2_rst_init(&conn->rst);
  4825. conn->tx.pacing.next_ts = UINT64_MAX;
  4826. }
  4827. static int conn_recv_path_response(ngtcp2_conn *conn, ngtcp2_path_response *fr,
  4828. ngtcp2_tstamp ts) {
  4829. int rv;
  4830. ngtcp2_pv *pv = conn->pv, *npv;
  4831. uint8_t ent_flags;
  4832. if (!pv) {
  4833. return 0;
  4834. }
  4835. rv = ngtcp2_pv_validate(pv, &ent_flags, fr->data);
  4836. if (rv != 0) {
  4837. assert(!ngtcp2_err_is_fatal(rv));
  4838. return 0;
  4839. }
  4840. if (!(pv->flags & NGTCP2_PV_FLAG_DONT_CARE)) {
  4841. if (pv->dcid.seq != conn->dcid.current.seq) {
  4842. assert(!conn->server);
  4843. assert(conn->dcid.current.cid.datalen);
  4844. rv = conn_retire_dcid(conn, &conn->dcid.current, ts);
  4845. if (rv != 0) {
  4846. return rv;
  4847. }
  4848. ngtcp2_dcid_copy(&conn->dcid.current, &pv->dcid);
  4849. conn_reset_congestion_state(conn, ts);
  4850. conn_reset_ecn_validation_state(conn);
  4851. }
  4852. assert(ngtcp2_path_eq(&pv->dcid.ps.path, &conn->dcid.current.ps.path));
  4853. conn->dcid.current.flags |= NGTCP2_DCID_FLAG_PATH_VALIDATED;
  4854. if (!conn->local.settings.no_pmtud) {
  4855. ngtcp2_conn_stop_pmtud(conn);
  4856. if (!(ent_flags & NGTCP2_PV_ENTRY_FLAG_UNDERSIZED)) {
  4857. rv = conn_start_pmtud(conn);
  4858. if (rv != 0) {
  4859. return rv;
  4860. }
  4861. }
  4862. }
  4863. if (!(ent_flags & NGTCP2_PV_ENTRY_FLAG_UNDERSIZED)) {
  4864. rv = conn_call_path_validation(conn, pv,
  4865. NGTCP2_PATH_VALIDATION_RESULT_SUCCESS);
  4866. if (rv != 0) {
  4867. return rv;
  4868. }
  4869. }
  4870. }
  4871. if (pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) {
  4872. if (ent_flags & NGTCP2_PV_ENTRY_FLAG_UNDERSIZED) {
  4873. assert(conn->server);
  4874. /* Validate path again */
  4875. rv = ngtcp2_pv_new(&npv, &pv->dcid, conn_compute_pv_timeout(conn),
  4876. NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE, &conn->log,
  4877. conn->mem);
  4878. if (rv != 0) {
  4879. return rv;
  4880. }
  4881. npv->dcid.flags |= NGTCP2_DCID_FLAG_PATH_VALIDATED;
  4882. ngtcp2_dcid_copy(&npv->fallback_dcid, &pv->fallback_dcid);
  4883. npv->fallback_pto = pv->fallback_pto;
  4884. } else {
  4885. rv = ngtcp2_pv_new(&npv, &pv->fallback_dcid,
  4886. conn_compute_pv_timeout_pto(conn, pv->fallback_pto),
  4887. NGTCP2_PV_FLAG_DONT_CARE, &conn->log, conn->mem);
  4888. if (rv != 0) {
  4889. return rv;
  4890. }
  4891. }
  4892. /* Unset the flag bit so that conn_stop_pv does not retire
  4893. DCID. */
  4894. pv->flags &= (uint8_t)~NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE;
  4895. rv = conn_stop_pv(conn, ts);
  4896. if (rv != 0) {
  4897. ngtcp2_pv_del(npv);
  4898. return rv;
  4899. }
  4900. conn->pv = npv;
  4901. return 0;
  4902. }
  4903. return conn_stop_pv(conn, ts);
  4904. }
  4905. /*
  4906. * pktns_pkt_num_is_duplicate returns nonzero if |pkt_num| is
  4907. * duplicated packet number.
  4908. */
  4909. static int pktns_pkt_num_is_duplicate(ngtcp2_pktns *pktns, int64_t pkt_num) {
  4910. return ngtcp2_gaptr_is_pushed(&pktns->rx.pngap, (uint64_t)pkt_num, 1);
  4911. }
  4912. /*
  4913. * pktns_commit_recv_pkt_num marks packet number |pkt_num| as
  4914. * received.
  4915. */
  4916. static int pktns_commit_recv_pkt_num(ngtcp2_pktns *pktns, int64_t pkt_num,
  4917. int ack_eliciting, ngtcp2_tstamp ts) {
  4918. int rv;
  4919. ngtcp2_range r;
  4920. rv = ngtcp2_gaptr_push(&pktns->rx.pngap, (uint64_t)pkt_num, 1);
  4921. if (rv != 0) {
  4922. return rv;
  4923. }
  4924. if (ngtcp2_ksl_len(&pktns->rx.pngap.gap) > 256) {
  4925. ngtcp2_gaptr_drop_first_gap(&pktns->rx.pngap);
  4926. }
  4927. if (ack_eliciting) {
  4928. if (pktns->rx.max_ack_eliciting_pkt_num != -1) {
  4929. if (pkt_num < pktns->rx.max_ack_eliciting_pkt_num) {
  4930. ngtcp2_acktr_immediate_ack(&pktns->acktr);
  4931. } else if (pkt_num != pktns->rx.max_ack_eliciting_pkt_num + 1) {
  4932. r = ngtcp2_gaptr_get_first_gap_after(
  4933. &pktns->rx.pngap, (uint64_t)pktns->rx.max_ack_eliciting_pkt_num);
  4934. if (r.begin < (uint64_t)pkt_num) {
  4935. ngtcp2_acktr_immediate_ack(&pktns->acktr);
  4936. }
  4937. }
  4938. }
  4939. if (pktns->rx.max_ack_eliciting_pkt_num < pkt_num) {
  4940. pktns->rx.max_ack_eliciting_pkt_num = pkt_num;
  4941. }
  4942. }
  4943. if (pktns->rx.max_pkt_num < pkt_num) {
  4944. pktns->rx.max_pkt_num = pkt_num;
  4945. pktns->rx.max_pkt_ts = ts;
  4946. }
  4947. return 0;
  4948. }
  4949. /*
  4950. * verify_token verifies |hd| contains |token| in its token field. It
  4951. * returns 0 if it succeeds, or NGTCP2_ERR_PROTO.
  4952. */
  4953. static int verify_token(const uint8_t *token, size_t tokenlen,
  4954. const ngtcp2_pkt_hd *hd) {
  4955. if (tokenlen == hd->tokenlen && ngtcp2_cmemeq(token, hd->token, tokenlen)) {
  4956. return 0;
  4957. }
  4958. return NGTCP2_ERR_PROTO;
  4959. }
  4960. static void pktns_increase_ecn_counts(ngtcp2_pktns *pktns,
  4961. const ngtcp2_pkt_info *pi) {
  4962. switch (pi->ecn & NGTCP2_ECN_MASK) {
  4963. case NGTCP2_ECN_ECT_0:
  4964. ++pktns->rx.ecn.ect0;
  4965. break;
  4966. case NGTCP2_ECN_ECT_1:
  4967. ++pktns->rx.ecn.ect1;
  4968. break;
  4969. case NGTCP2_ECN_CE:
  4970. ++pktns->rx.ecn.ce;
  4971. break;
  4972. }
  4973. }
  4974. /*
  4975. * vneg_available_versions_includes returns nonzero if
  4976. * |available_versions| of length |available_versionslen| includes
  4977. * |version|. |available_versions| is the wire image of
  4978. * available_versions field of version_information transport
  4979. * parameter, and each version is encoded in network byte order.
  4980. */
  4981. static int vneg_available_versions_includes(const uint8_t *available_versions,
  4982. size_t available_versionslen,
  4983. uint32_t version) {
  4984. size_t i;
  4985. uint32_t v;
  4986. assert(!(available_versionslen & 0x3));
  4987. if (available_versionslen == 0) {
  4988. return 0;
  4989. }
  4990. for (i = 0; i < available_versionslen; i += sizeof(uint32_t)) {
  4991. available_versions = ngtcp2_get_uint32be(&v, available_versions);
  4992. if (version == v) {
  4993. return 1;
  4994. }
  4995. }
  4996. return 0;
  4997. }
  4998. /*
  4999. * conn_verify_fixed_bit verifies that fixed bit in |hd| is
  5000. * acceptable.
  5001. *
  5002. * This function returns 0 if it succeeds, or one of the following
  5003. * negative error codes:
  5004. *
  5005. * NGTCP2_ERR_INVALID_ARGUMENT
  5006. * Clearing fixed bit is not permitted.
  5007. */
  5008. static int conn_verify_fixed_bit(ngtcp2_conn *conn, ngtcp2_pkt_hd *hd) {
  5009. if (!(hd->flags & NGTCP2_PKT_FLAG_FIXED_BIT_CLEAR)) {
  5010. return 0;
  5011. }
  5012. if (conn->server) {
  5013. switch (hd->type) {
  5014. case NGTCP2_PKT_INITIAL:
  5015. case NGTCP2_PKT_0RTT:
  5016. case NGTCP2_PKT_HANDSHAKE:
  5017. /* RFC 9287 requires that a token from NEW_TOKEN. */
  5018. if (!(conn->flags & NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED) &&
  5019. (conn->local.settings.token_type != NGTCP2_TOKEN_TYPE_NEW_TOKEN ||
  5020. !conn->local.settings.tokenlen)) {
  5021. return NGTCP2_ERR_INVALID_ARGUMENT;
  5022. }
  5023. break;
  5024. }
  5025. }
  5026. /* TODO we have no information that we enabled grease_quic_bit in
  5027. the previous connection. */
  5028. if (!conn->local.transport_params.grease_quic_bit) {
  5029. return NGTCP2_ERR_INVALID_ARGUMENT;
  5030. }
  5031. return 0;
  5032. }
  5033. static int conn_recv_crypto(ngtcp2_conn *conn,
  5034. ngtcp2_encryption_level encryption_level,
  5035. ngtcp2_strm *strm, const ngtcp2_stream *fr);
  5036. static ngtcp2_ssize conn_recv_pkt(ngtcp2_conn *conn, const ngtcp2_path *path,
  5037. const ngtcp2_pkt_info *pi, const uint8_t *pkt,
  5038. size_t pktlen, size_t dgramlen,
  5039. ngtcp2_tstamp pkt_ts, ngtcp2_tstamp ts);
  5040. static int conn_process_buffered_protected_pkt(ngtcp2_conn *conn,
  5041. ngtcp2_pktns *pktns,
  5042. ngtcp2_tstamp ts);
  5043. /*
  5044. * conn_recv_handshake_pkt processes received packet |pkt| whose
  5045. * length is |pktlen| during handshake period. The buffer pointed by
  5046. * |pkt| might contain multiple packets. This function only processes
  5047. * one packet. |pkt_ts| is the timestamp when packet is received.
  5048. * |ts| should be the current time. Usually they are the same, but
  5049. * for buffered packets, |pkt_ts| would be earlier than |ts|.
  5050. *
  5051. * This function returns the number of bytes it reads if it succeeds,
  5052. * or one of the following negative error codes:
  5053. *
  5054. * NGTCP2_ERR_RECV_VERSION_NEGOTIATION
  5055. * Version Negotiation packet is received.
  5056. * NGTCP2_ERR_NOMEM
  5057. * Out of memory.
  5058. * NGTCP2_ERR_CALLBACK_FAILURE
  5059. * User-defined callback function failed.
  5060. * NGTCP2_ERR_DISCARD_PKT
  5061. * Packet was discarded because plain text header was malformed;
  5062. * or its payload could not be decrypted.
  5063. * NGTCP2_ERR_FRAME_FORMAT
  5064. * Frame is badly formatted
  5065. * NGTCP2_ERR_ACK_FRAME
  5066. * ACK frame is malformed.
  5067. * NGTCP2_ERR_CRYPTO
  5068. * TLS stack reported error.
  5069. * NGTCP2_ERR_PROTO
  5070. * Generic QUIC protocol error.
  5071. *
  5072. * In addition to the above error codes, error codes returned from
  5073. * conn_recv_pkt are also returned.
  5074. */
  5075. static ngtcp2_ssize
  5076. conn_recv_handshake_pkt(ngtcp2_conn *conn, const ngtcp2_path *path,
  5077. const ngtcp2_pkt_info *pi, const uint8_t *pkt,
  5078. size_t pktlen, size_t dgramlen, ngtcp2_tstamp pkt_ts,
  5079. ngtcp2_tstamp ts) {
  5080. ngtcp2_ssize nread;
  5081. ngtcp2_pkt_hd hd;
  5082. ngtcp2_max_frame mfr;
  5083. ngtcp2_frame *fr = &mfr.fr;
  5084. int rv;
  5085. int require_ack = 0;
  5086. size_t hdpktlen;
  5087. const uint8_t *payload;
  5088. size_t payloadlen;
  5089. ngtcp2_ssize nwrite;
  5090. ngtcp2_crypto_aead *aead;
  5091. ngtcp2_crypto_cipher *hp;
  5092. ngtcp2_crypto_km *ckm;
  5093. ngtcp2_crypto_cipher_ctx *hp_ctx;
  5094. ngtcp2_hp_mask hp_mask;
  5095. ngtcp2_decrypt decrypt;
  5096. ngtcp2_pktns *pktns;
  5097. ngtcp2_strm *crypto;
  5098. ngtcp2_encryption_level encryption_level;
  5099. int invalid_reserved_bits = 0;
  5100. size_t num_ack_processed = 0;
  5101. if (pktlen == 0) {
  5102. return 0;
  5103. }
  5104. if (!(pkt[0] & NGTCP2_HEADER_FORM_BIT)) {
  5105. if (conn->state == NGTCP2_CS_SERVER_INITIAL) {
  5106. /* Ignore 1RTT packet unless server's first Handshake packet has
  5107. been transmitted. */
  5108. return (ngtcp2_ssize)pktlen;
  5109. }
  5110. if (conn->pktns.crypto.rx.ckm) {
  5111. return 0;
  5112. }
  5113. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  5114. "buffering 1RTT packet len=%zu", pktlen);
  5115. rv =
  5116. conn_buffer_pkt(conn, &conn->pktns, path, pi, pkt, pktlen, dgramlen, ts);
  5117. if (rv != 0) {
  5118. assert(ngtcp2_err_is_fatal(rv));
  5119. return rv;
  5120. }
  5121. return (ngtcp2_ssize)pktlen;
  5122. }
  5123. nread = ngtcp2_pkt_decode_hd_long(&hd, pkt, pktlen);
  5124. if (nread < 0) {
  5125. return NGTCP2_ERR_DISCARD_PKT;
  5126. }
  5127. switch (hd.type) {
  5128. case NGTCP2_PKT_VERSION_NEGOTIATION:
  5129. hdpktlen = (size_t)nread;
  5130. ngtcp2_log_rx_pkt_hd(&conn->log, &hd);
  5131. if (conn->server) {
  5132. return NGTCP2_ERR_DISCARD_PKT;
  5133. }
  5134. /* Receiving Version Negotiation packet after getting Handshake
  5135. packet from server is invalid. */
  5136. if (conn->flags & NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED) {
  5137. return NGTCP2_ERR_DISCARD_PKT;
  5138. }
  5139. if (!ngtcp2_cid_eq(&conn->oscid, &hd.dcid)) {
  5140. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5141. "packet was ignored because of mismatched DCID");
  5142. return NGTCP2_ERR_DISCARD_PKT;
  5143. }
  5144. if (!ngtcp2_cid_eq(&conn->dcid.current.cid, &hd.scid)) {
  5145. /* Just discard invalid Version Negotiation packet */
  5146. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5147. "packet was ignored because of mismatched SCID");
  5148. return NGTCP2_ERR_DISCARD_PKT;
  5149. }
  5150. rv =
  5151. conn_on_version_negotiation(conn, &hd, pkt + hdpktlen, pktlen - hdpktlen);
  5152. if (rv != 0) {
  5153. if (ngtcp2_err_is_fatal(rv)) {
  5154. return rv;
  5155. }
  5156. return NGTCP2_ERR_DISCARD_PKT;
  5157. }
  5158. return NGTCP2_ERR_RECV_VERSION_NEGOTIATION;
  5159. case NGTCP2_PKT_RETRY:
  5160. hdpktlen = (size_t)nread;
  5161. ngtcp2_log_rx_pkt_hd(&conn->log, &hd);
  5162. if (conn->server) {
  5163. return NGTCP2_ERR_DISCARD_PKT;
  5164. }
  5165. if (conn_verify_fixed_bit(conn, &hd) != 0) {
  5166. return NGTCP2_ERR_DISCARD_PKT;
  5167. }
  5168. /* Receiving Retry packet after getting Initial packet from server
  5169. is invalid. */
  5170. if (conn->flags & NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED) {
  5171. return NGTCP2_ERR_DISCARD_PKT;
  5172. }
  5173. if (conn->client_chosen_version != hd.version) {
  5174. return NGTCP2_ERR_DISCARD_PKT;
  5175. }
  5176. rv = conn_on_retry(conn, &hd, hdpktlen, pkt, pktlen, ts);
  5177. if (rv != 0) {
  5178. if (ngtcp2_err_is_fatal(rv)) {
  5179. return rv;
  5180. }
  5181. return NGTCP2_ERR_DISCARD_PKT;
  5182. }
  5183. return (ngtcp2_ssize)pktlen;
  5184. }
  5185. if (pktlen < (size_t)nread + hd.len) {
  5186. return NGTCP2_ERR_DISCARD_PKT;
  5187. }
  5188. pktlen = (size_t)nread + hd.len;
  5189. if (!ngtcp2_is_supported_version(hd.version)) {
  5190. return NGTCP2_ERR_DISCARD_PKT;
  5191. }
  5192. if (conn->server) {
  5193. if (hd.version != conn->client_chosen_version &&
  5194. (!conn->negotiated_version || hd.version != conn->negotiated_version)) {
  5195. return NGTCP2_ERR_DISCARD_PKT;
  5196. }
  5197. } else if (hd.version != conn->client_chosen_version &&
  5198. conn->negotiated_version &&
  5199. hd.version != conn->negotiated_version) {
  5200. return NGTCP2_ERR_DISCARD_PKT;
  5201. }
  5202. if (conn_verify_fixed_bit(conn, &hd) != 0) {
  5203. return NGTCP2_ERR_DISCARD_PKT;
  5204. }
  5205. /* Quoted from spec: if subsequent packets of those types include a
  5206. different Source Connection ID, they MUST be discarded. */
  5207. if ((conn->flags & NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED) &&
  5208. !ngtcp2_cid_eq(&conn->dcid.current.cid, &hd.scid)) {
  5209. ngtcp2_log_rx_pkt_hd(&conn->log, &hd);
  5210. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5211. "packet was ignored because of mismatched SCID");
  5212. return NGTCP2_ERR_DISCARD_PKT;
  5213. }
  5214. switch (hd.type) {
  5215. case NGTCP2_PKT_0RTT:
  5216. if (!conn->server) {
  5217. return NGTCP2_ERR_DISCARD_PKT;
  5218. }
  5219. if (hd.version != conn->client_chosen_version) {
  5220. return NGTCP2_ERR_DISCARD_PKT;
  5221. }
  5222. if (conn->flags & NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED) {
  5223. if (conn->early.ckm) {
  5224. ngtcp2_ssize nread2;
  5225. /* TODO Avoid to parse header twice. */
  5226. nread2 =
  5227. conn_recv_pkt(conn, path, pi, pkt, pktlen, dgramlen, pkt_ts, ts);
  5228. if (nread2 < 0) {
  5229. return nread2;
  5230. }
  5231. }
  5232. /* Discard 0-RTT packet if we don't have a key to decrypt it. */
  5233. return (ngtcp2_ssize)pktlen;
  5234. }
  5235. /* Buffer re-ordered 0-RTT packet. */
  5236. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  5237. "buffering 0-RTT packet len=%zu", pktlen);
  5238. rv = conn_buffer_pkt(conn, conn->in_pktns, path, pi, pkt, pktlen, dgramlen,
  5239. ts);
  5240. if (rv != 0) {
  5241. assert(ngtcp2_err_is_fatal(rv));
  5242. return rv;
  5243. }
  5244. return (ngtcp2_ssize)pktlen;
  5245. case NGTCP2_PKT_INITIAL:
  5246. if (!conn->in_pktns) {
  5247. ngtcp2_log_info(
  5248. &conn->log, NGTCP2_LOG_EVENT_PKT,
  5249. "Initial packet is discarded because keys have been discarded");
  5250. return (ngtcp2_ssize)pktlen;
  5251. }
  5252. assert(conn->in_pktns);
  5253. if (conn->server) {
  5254. if (dgramlen < NGTCP2_MAX_UDP_PAYLOAD_SIZE) {
  5255. ngtcp2_log_info(
  5256. &conn->log, NGTCP2_LOG_EVENT_PKT,
  5257. "Initial packet was ignored because it is included in UDP datagram "
  5258. "less than %zu bytes: %zu bytes",
  5259. NGTCP2_MAX_UDP_PAYLOAD_SIZE, dgramlen);
  5260. return NGTCP2_ERR_DISCARD_PKT;
  5261. }
  5262. if (conn->local.settings.tokenlen) {
  5263. rv = verify_token(conn->local.settings.token,
  5264. conn->local.settings.tokenlen, &hd);
  5265. if (rv != 0) {
  5266. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5267. "packet was ignored because token is invalid");
  5268. return NGTCP2_ERR_DISCARD_PKT;
  5269. }
  5270. }
  5271. if ((conn->flags & NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED) == 0) {
  5272. /* Set rcid here so that it is available to callback. If this
  5273. packet is discarded later in this function and no packet is
  5274. processed in this connection attempt so far, connection
  5275. will be dropped. */
  5276. conn->rcid = hd.dcid;
  5277. rv = conn_call_recv_client_initial(conn, &hd.dcid);
  5278. if (rv != 0) {
  5279. return rv;
  5280. }
  5281. }
  5282. } else {
  5283. if (hd.tokenlen != 0) {
  5284. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5285. "packet was ignored because token is not empty");
  5286. return NGTCP2_ERR_DISCARD_PKT;
  5287. }
  5288. if (hd.version != conn->client_chosen_version &&
  5289. !conn->negotiated_version && conn->vneg.version != hd.version) {
  5290. if (!vneg_available_versions_includes(conn->vneg.available_versions,
  5291. conn->vneg.available_versionslen,
  5292. hd.version)) {
  5293. return NGTCP2_ERR_DISCARD_PKT;
  5294. }
  5295. /* Install new Initial keys using QUIC version = hd.version */
  5296. rv = conn_call_version_negotiation(
  5297. conn, hd.version,
  5298. (conn->flags & NGTCP2_CONN_FLAG_RECV_RETRY) ? &conn->dcid.current.cid
  5299. : &conn->rcid);
  5300. if (rv != 0) {
  5301. return rv;
  5302. }
  5303. assert(conn->vneg.version == hd.version);
  5304. }
  5305. }
  5306. pktns = conn->in_pktns;
  5307. crypto = &pktns->crypto.strm;
  5308. encryption_level = NGTCP2_ENCRYPTION_LEVEL_INITIAL;
  5309. if (hd.version == conn->client_chosen_version) {
  5310. ckm = pktns->crypto.rx.ckm;
  5311. hp_ctx = &pktns->crypto.rx.hp_ctx;
  5312. } else {
  5313. assert(conn->vneg.version == hd.version);
  5314. ckm = conn->vneg.rx.ckm;
  5315. hp_ctx = &conn->vneg.rx.hp_ctx;
  5316. }
  5317. break;
  5318. case NGTCP2_PKT_HANDSHAKE:
  5319. if (hd.version != conn->negotiated_version) {
  5320. return NGTCP2_ERR_DISCARD_PKT;
  5321. }
  5322. if (!conn->hs_pktns->crypto.rx.ckm) {
  5323. if (conn->server) {
  5324. ngtcp2_log_info(
  5325. &conn->log, NGTCP2_LOG_EVENT_PKT,
  5326. "Handshake packet at this point is unexpected and discarded");
  5327. return (ngtcp2_ssize)pktlen;
  5328. }
  5329. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  5330. "buffering Handshake packet len=%zu", pktlen);
  5331. rv = conn_buffer_pkt(conn, conn->hs_pktns, path, pi, pkt, pktlen,
  5332. dgramlen, ts);
  5333. if (rv != 0) {
  5334. assert(ngtcp2_err_is_fatal(rv));
  5335. return rv;
  5336. }
  5337. return (ngtcp2_ssize)pktlen;
  5338. }
  5339. pktns = conn->hs_pktns;
  5340. crypto = &pktns->crypto.strm;
  5341. encryption_level = NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE;
  5342. ckm = pktns->crypto.rx.ckm;
  5343. hp_ctx = &pktns->crypto.rx.hp_ctx;
  5344. break;
  5345. default:
  5346. ngtcp2_unreachable();
  5347. }
  5348. hp_mask = conn->callbacks.hp_mask;
  5349. decrypt = conn->callbacks.decrypt;
  5350. aead = &pktns->crypto.ctx.aead;
  5351. hp = &pktns->crypto.ctx.hp;
  5352. assert(ckm);
  5353. assert(hp_mask);
  5354. assert(decrypt);
  5355. rv = conn_ensure_decrypt_hp_buffer(conn, (size_t)nread + 4);
  5356. if (rv != 0) {
  5357. return rv;
  5358. }
  5359. nwrite = decrypt_hp(&hd, conn->crypto.decrypt_hp_buf.base, hp, pkt, pktlen,
  5360. (size_t)nread, hp_ctx, hp_mask);
  5361. if (nwrite < 0) {
  5362. if (ngtcp2_err_is_fatal((int)nwrite)) {
  5363. return nwrite;
  5364. }
  5365. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5366. "could not decrypt packet number");
  5367. return NGTCP2_ERR_DISCARD_PKT;
  5368. }
  5369. hdpktlen = (size_t)nwrite;
  5370. payload = pkt + hdpktlen;
  5371. payloadlen = hd.len - hd.pkt_numlen;
  5372. hd.pkt_num =
  5373. ngtcp2_pkt_adjust_pkt_num(pktns->rx.max_pkt_num, hd.pkt_num, hd.pkt_numlen);
  5374. if (hd.pkt_num > NGTCP2_MAX_PKT_NUM) {
  5375. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5376. "pkn=%" PRId64 " is greater than maximum pkn", hd.pkt_num);
  5377. return NGTCP2_ERR_DISCARD_PKT;
  5378. }
  5379. ngtcp2_log_rx_pkt_hd(&conn->log, &hd);
  5380. rv = ngtcp2_pkt_verify_reserved_bits(conn->crypto.decrypt_hp_buf.base[0]);
  5381. if (rv != 0) {
  5382. invalid_reserved_bits = 1;
  5383. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5384. "packet has incorrect reserved bits");
  5385. /* Will return error after decrypting payload */
  5386. }
  5387. if (pktns_pkt_num_is_duplicate(pktns, hd.pkt_num)) {
  5388. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5389. "packet was discarded because of duplicated packet number");
  5390. return NGTCP2_ERR_DISCARD_PKT;
  5391. }
  5392. rv = conn_ensure_decrypt_buffer(conn, payloadlen);
  5393. if (rv != 0) {
  5394. return rv;
  5395. }
  5396. nwrite = decrypt_pkt(conn->crypto.decrypt_buf.base, aead, payload, payloadlen,
  5397. conn->crypto.decrypt_hp_buf.base, hdpktlen, hd.pkt_num,
  5398. ckm, decrypt);
  5399. if (nwrite < 0) {
  5400. if (ngtcp2_err_is_fatal((int)nwrite)) {
  5401. return nwrite;
  5402. }
  5403. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5404. "could not decrypt packet payload");
  5405. return NGTCP2_ERR_DISCARD_PKT;
  5406. }
  5407. if (invalid_reserved_bits) {
  5408. return NGTCP2_ERR_PROTO;
  5409. }
  5410. if (!conn->server && hd.version != conn->client_chosen_version &&
  5411. !conn->negotiated_version) {
  5412. conn->negotiated_version = hd.version;
  5413. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  5414. "the negotiated version is 0x%08x",
  5415. conn->negotiated_version);
  5416. }
  5417. payload = conn->crypto.decrypt_buf.base;
  5418. payloadlen = (size_t)nwrite;
  5419. switch (hd.type) {
  5420. case NGTCP2_PKT_INITIAL:
  5421. if (!conn->server ||
  5422. ((conn->flags & NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED) &&
  5423. !ngtcp2_cid_eq(&conn->rcid, &hd.dcid))) {
  5424. rv = conn_verify_dcid(conn, NULL, &hd);
  5425. if (rv != 0) {
  5426. if (ngtcp2_err_is_fatal(rv)) {
  5427. return rv;
  5428. }
  5429. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5430. "packet was ignored because of mismatched DCID");
  5431. return NGTCP2_ERR_DISCARD_PKT;
  5432. }
  5433. }
  5434. break;
  5435. case NGTCP2_PKT_HANDSHAKE:
  5436. rv = conn_verify_dcid(conn, NULL, &hd);
  5437. if (rv != 0) {
  5438. if (ngtcp2_err_is_fatal(rv)) {
  5439. return rv;
  5440. }
  5441. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5442. "packet was ignored because of mismatched DCID");
  5443. return NGTCP2_ERR_DISCARD_PKT;
  5444. }
  5445. break;
  5446. default:
  5447. ngtcp2_unreachable();
  5448. }
  5449. if (payloadlen == 0) {
  5450. /* QUIC packet must contain at least one frame */
  5451. if (hd.type == NGTCP2_PKT_INITIAL) {
  5452. return NGTCP2_ERR_DISCARD_PKT;
  5453. }
  5454. return NGTCP2_ERR_PROTO;
  5455. }
  5456. if (hd.type == NGTCP2_PKT_INITIAL &&
  5457. !(conn->flags & NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED)) {
  5458. conn->flags |= NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED;
  5459. if (!conn->server) {
  5460. conn->dcid.current.cid = hd.scid;
  5461. }
  5462. }
  5463. ngtcp2_qlog_pkt_received_start(&conn->qlog);
  5464. for (; payloadlen;) {
  5465. nread = ngtcp2_pkt_decode_frame(fr, payload, payloadlen);
  5466. if (nread < 0) {
  5467. return nread;
  5468. }
  5469. payload += nread;
  5470. payloadlen -= (size_t)nread;
  5471. switch (fr->type) {
  5472. case NGTCP2_FRAME_ACK:
  5473. case NGTCP2_FRAME_ACK_ECN:
  5474. fr->ack.ack_delay = 0;
  5475. fr->ack.ack_delay_unscaled = 0;
  5476. rv =
  5477. ngtcp2_pkt_validate_ack(&fr->ack, conn->local.settings.initial_pkt_num);
  5478. if (rv != 0) {
  5479. return rv;
  5480. }
  5481. break;
  5482. }
  5483. ngtcp2_log_rx_fr(&conn->log, &hd, fr);
  5484. switch (fr->type) {
  5485. case NGTCP2_FRAME_ACK:
  5486. case NGTCP2_FRAME_ACK_ECN:
  5487. if (num_ack_processed >= NGTCP2_MAX_ACK_PER_PKT) {
  5488. break;
  5489. }
  5490. if (!conn->server && hd.type == NGTCP2_PKT_HANDSHAKE) {
  5491. conn->flags |= NGTCP2_CONN_FLAG_SERVER_ADDR_VERIFIED;
  5492. }
  5493. rv = conn_recv_ack(conn, pktns, &fr->ack, pkt_ts, ts);
  5494. if (rv != 0) {
  5495. return rv;
  5496. }
  5497. ++num_ack_processed;
  5498. break;
  5499. case NGTCP2_FRAME_PADDING:
  5500. break;
  5501. case NGTCP2_FRAME_CRYPTO:
  5502. if (!conn->server && !conn->negotiated_version &&
  5503. ngtcp2_vec_len(fr->stream.data, fr->stream.datacnt)) {
  5504. conn->negotiated_version = hd.version;
  5505. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  5506. "the negotiated version is 0x%08x",
  5507. conn->negotiated_version);
  5508. }
  5509. rv = conn_recv_crypto(conn, encryption_level, crypto, &fr->stream);
  5510. if (rv != 0) {
  5511. return rv;
  5512. }
  5513. require_ack = 1;
  5514. break;
  5515. case NGTCP2_FRAME_CONNECTION_CLOSE:
  5516. rv = conn_recv_connection_close(conn, &fr->connection_close);
  5517. if (rv != 0) {
  5518. return rv;
  5519. }
  5520. break;
  5521. case NGTCP2_FRAME_PING:
  5522. require_ack = 1;
  5523. break;
  5524. default:
  5525. return NGTCP2_ERR_PROTO;
  5526. }
  5527. ngtcp2_qlog_write_frame(&conn->qlog, fr);
  5528. }
  5529. if (hd.type == NGTCP2_PKT_HANDSHAKE) {
  5530. /* Successful processing of Handshake packet from a remote
  5531. endpoint validates its source address. */
  5532. conn->dcid.current.flags |= NGTCP2_DCID_FLAG_PATH_VALIDATED;
  5533. }
  5534. ngtcp2_qlog_pkt_received_end(&conn->qlog, &hd, pktlen);
  5535. rv = pktns_commit_recv_pkt_num(pktns, hd.pkt_num, require_ack, pkt_ts);
  5536. if (rv != 0) {
  5537. return rv;
  5538. }
  5539. pktns_increase_ecn_counts(pktns, pi);
  5540. /* Initial and Handshake are always acknowledged without delay. No
  5541. need to call ngtcp2_acktr_immediate_ack(). */
  5542. rv =
  5543. ngtcp2_conn_sched_ack(conn, &pktns->acktr, hd.pkt_num, require_ack, pkt_ts);
  5544. if (rv != 0) {
  5545. return rv;
  5546. }
  5547. conn_restart_timer_on_read(conn, ts);
  5548. ngtcp2_qlog_metrics_updated(&conn->qlog, &conn->cstat);
  5549. return conn->state == NGTCP2_CS_DRAINING ? NGTCP2_ERR_DRAINING
  5550. : (ngtcp2_ssize)pktlen;
  5551. }
  5552. static int is_unrecoverable_error(int liberr) {
  5553. switch (liberr) {
  5554. case NGTCP2_ERR_CRYPTO:
  5555. case NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM:
  5556. case NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM:
  5557. case NGTCP2_ERR_TRANSPORT_PARAM:
  5558. case NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE:
  5559. return 1;
  5560. }
  5561. return 0;
  5562. }
  5563. /*
  5564. * conn_recv_handshake_cpkt processes compound packet during
  5565. * handshake. The buffer pointed by |pkt| might contain multiple
  5566. * packets. The 1RTT packet must be the last one because it does not
  5567. * have payload length field.
  5568. *
  5569. * This function returns the same error code returned by
  5570. * conn_recv_handshake_pkt.
  5571. */
  5572. static ngtcp2_ssize conn_recv_handshake_cpkt(ngtcp2_conn *conn,
  5573. const ngtcp2_path *path,
  5574. const ngtcp2_pkt_info *pi,
  5575. const uint8_t *pkt, size_t pktlen,
  5576. ngtcp2_tstamp ts) {
  5577. ngtcp2_ssize nread;
  5578. size_t dgramlen = pktlen;
  5579. const uint8_t *origpkt = pkt;
  5580. uint32_t version;
  5581. if (ngtcp2_path_eq(&conn->dcid.current.ps.path, path)) {
  5582. conn->dcid.current.bytes_recv += dgramlen;
  5583. }
  5584. while (pktlen) {
  5585. nread =
  5586. conn_recv_handshake_pkt(conn, path, pi, pkt, pktlen, dgramlen, ts, ts);
  5587. if (nread < 0) {
  5588. if (ngtcp2_err_is_fatal((int)nread)) {
  5589. return nread;
  5590. }
  5591. if (nread == NGTCP2_ERR_DRAINING) {
  5592. return NGTCP2_ERR_DRAINING;
  5593. }
  5594. if ((pkt[0] & NGTCP2_HEADER_FORM_BIT) && pktlen > 4) {
  5595. /* Not a Version Negotiation packet */
  5596. ngtcp2_get_uint32be(&version, &pkt[1]);
  5597. if (ngtcp2_pkt_get_type_long(version, pkt[0]) == NGTCP2_PKT_INITIAL) {
  5598. if (conn->server) {
  5599. if (is_unrecoverable_error((int)nread)) {
  5600. /* If server gets crypto error from TLS stack, it is
  5601. unrecoverable, therefore drop connection. */
  5602. return nread;
  5603. }
  5604. /* If server discards first Initial, then drop connection
  5605. state. This is because SCID in packet might be corrupted
  5606. and the current connection state might wrongly discard
  5607. valid packet and prevent the handshake from
  5608. completing. */
  5609. if (conn->in_pktns && conn->in_pktns->rx.max_pkt_num == -1) {
  5610. return NGTCP2_ERR_DROP_CONN;
  5611. }
  5612. return (ngtcp2_ssize)dgramlen;
  5613. }
  5614. /* client */
  5615. if (is_unrecoverable_error((int)nread)) {
  5616. /* If client gets crypto error from TLS stack, it is
  5617. unrecoverable, therefore drop connection. */
  5618. return nread;
  5619. }
  5620. return (ngtcp2_ssize)dgramlen;
  5621. }
  5622. }
  5623. if (nread == NGTCP2_ERR_DISCARD_PKT) {
  5624. return (ngtcp2_ssize)dgramlen;
  5625. }
  5626. return nread;
  5627. }
  5628. if (nread == 0) {
  5629. assert(!(pkt[0] & NGTCP2_HEADER_FORM_BIT));
  5630. return pkt - origpkt;
  5631. }
  5632. assert(pktlen >= (size_t)nread);
  5633. pkt += nread;
  5634. pktlen -= (size_t)nread;
  5635. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  5636. "read packet %td left %zu", nread, pktlen);
  5637. }
  5638. return (ngtcp2_ssize)dgramlen;
  5639. }
  5640. int ngtcp2_conn_init_stream(ngtcp2_conn *conn, ngtcp2_strm *strm,
  5641. int64_t stream_id, void *stream_user_data) {
  5642. int rv;
  5643. uint64_t max_rx_offset;
  5644. uint64_t max_tx_offset;
  5645. int local_stream = conn_local_stream(conn, stream_id);
  5646. assert(conn->remote.transport_params);
  5647. if (bidi_stream(stream_id)) {
  5648. if (local_stream) {
  5649. max_rx_offset =
  5650. conn->local.transport_params.initial_max_stream_data_bidi_local;
  5651. max_tx_offset =
  5652. conn->remote.transport_params->initial_max_stream_data_bidi_remote;
  5653. } else {
  5654. max_rx_offset =
  5655. conn->local.transport_params.initial_max_stream_data_bidi_remote;
  5656. max_tx_offset =
  5657. conn->remote.transport_params->initial_max_stream_data_bidi_local;
  5658. }
  5659. } else if (local_stream) {
  5660. max_rx_offset = 0;
  5661. max_tx_offset = conn->remote.transport_params->initial_max_stream_data_uni;
  5662. } else {
  5663. max_rx_offset = conn->local.transport_params.initial_max_stream_data_uni;
  5664. max_tx_offset = 0;
  5665. }
  5666. ngtcp2_strm_init(strm, stream_id, NGTCP2_STRM_FLAG_NONE, max_rx_offset,
  5667. max_tx_offset, stream_user_data, &conn->frc_objalloc,
  5668. conn->mem);
  5669. rv =
  5670. ngtcp2_map_insert(&conn->strms, (ngtcp2_map_key_type)strm->stream_id, strm);
  5671. if (rv != 0) {
  5672. assert(rv != NGTCP2_ERR_INVALID_ARGUMENT);
  5673. goto fail;
  5674. }
  5675. return 0;
  5676. fail:
  5677. ngtcp2_strm_free(strm);
  5678. return rv;
  5679. }
  5680. /*
  5681. * conn_emit_pending_stream_data passes buffered ordered stream data
  5682. * to the application. |rx_offset| is the first offset to deliver to
  5683. * the application. This function assumes that the data up to
  5684. * |rx_offset| has been delivered already. This function only passes
  5685. * the ordered data without any gap. If there is a gap, it stops
  5686. * providing the data to the application, and returns.
  5687. *
  5688. * This function returns 0 if it succeeds, or one of the following
  5689. * negative error codes:
  5690. *
  5691. * NGTCP2_ERR_CALLBACK_FAILURE
  5692. * User callback failed.
  5693. * NGTCP2_ERR_NOMEM
  5694. * Out of memory.
  5695. */
  5696. static int conn_emit_pending_stream_data(ngtcp2_conn *conn, ngtcp2_strm *strm,
  5697. uint64_t rx_offset) {
  5698. size_t datalen;
  5699. const uint8_t *data;
  5700. int rv;
  5701. uint64_t offset;
  5702. uint32_t sdflags;
  5703. int handshake_completed = conn_is_tls_handshake_completed(conn);
  5704. if (!strm->rx.rob) {
  5705. return 0;
  5706. }
  5707. for (;;) {
  5708. /* Stop calling callback if application has called
  5709. ngtcp2_conn_shutdown_stream_read() inside the callback.
  5710. Because it doubly counts connection window. */
  5711. if (strm->flags & NGTCP2_STRM_FLAG_STOP_SENDING) {
  5712. return 0;
  5713. }
  5714. datalen = ngtcp2_rob_data_at(strm->rx.rob, &data, rx_offset);
  5715. if (datalen == 0) {
  5716. assert(rx_offset == ngtcp2_strm_rx_offset(strm));
  5717. return 0;
  5718. }
  5719. offset = rx_offset;
  5720. rx_offset += datalen;
  5721. sdflags = NGTCP2_STREAM_DATA_FLAG_NONE;
  5722. if ((strm->flags & NGTCP2_STRM_FLAG_SHUT_RD) &&
  5723. rx_offset == strm->rx.last_offset) {
  5724. sdflags |= NGTCP2_STREAM_DATA_FLAG_FIN;
  5725. }
  5726. if (!handshake_completed) {
  5727. sdflags |= NGTCP2_STREAM_DATA_FLAG_0RTT;
  5728. }
  5729. rv = conn_call_recv_stream_data(conn, strm, sdflags, offset, data, datalen);
  5730. if (rv != 0) {
  5731. return rv;
  5732. }
  5733. /* ngtcp2_conn_shutdown_stream_read from a callback will free
  5734. strm->rx.rob. */
  5735. if (!strm->rx.rob) {
  5736. return 0;
  5737. }
  5738. ngtcp2_rob_pop(strm->rx.rob, rx_offset - datalen, datalen);
  5739. }
  5740. }
  5741. /*
  5742. * conn_recv_crypto is called when CRYPTO frame |fr| is received.
  5743. * |rx_offset_base| is the offset in the entire TLS handshake stream.
  5744. * fr->offset specifies the offset in each encryption level.
  5745. * |max_rx_offset| is, if it is nonzero, the maximum offset in the
  5746. * entire TLS handshake stream that |fr| can carry.
  5747. * |encryption_level| is the encryption level where this data is
  5748. * received.
  5749. *
  5750. * This function returns 0 if it succeeds, or one of the following
  5751. * negative error codes:
  5752. *
  5753. * NGTCP2_ERR_PROTO
  5754. * CRYPTO frame has invalid offset.
  5755. * NGTCP2_ERR_NOMEM
  5756. * Out of memory.
  5757. * NGTCP2_ERR_CRYPTO
  5758. * TLS stack reported error.
  5759. * NGTCP2_ERR_FRAME_ENCODING
  5760. * The end offset exceeds the maximum value.
  5761. * NGTCP2_ERR_CALLBACK_FAILURE
  5762. * User-defined callback function failed.
  5763. */
  5764. static int conn_recv_crypto(ngtcp2_conn *conn,
  5765. ngtcp2_encryption_level encryption_level,
  5766. ngtcp2_strm *crypto, const ngtcp2_stream *fr) {
  5767. uint64_t fr_end_offset;
  5768. uint64_t rx_offset;
  5769. int rv;
  5770. if (fr->datacnt == 0) {
  5771. return 0;
  5772. }
  5773. fr_end_offset = fr->offset + fr->data[0].len;
  5774. if (NGTCP2_MAX_VARINT < fr_end_offset) {
  5775. return NGTCP2_ERR_FRAME_ENCODING;
  5776. }
  5777. rx_offset = ngtcp2_strm_rx_offset(crypto);
  5778. if (fr_end_offset <= rx_offset) {
  5779. if (conn->server &&
  5780. !(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_EARLY_RETRANSMIT) &&
  5781. encryption_level == NGTCP2_ENCRYPTION_LEVEL_INITIAL) {
  5782. /* https://datatracker.ietf.org/doc/html/rfc9002#section-6.2.3:
  5783. Speeding Up Handshake Completion
  5784. When a server receives an Initial packet containing duplicate
  5785. CRYPTO data, it can assume the client did not receive all of
  5786. the server's CRYPTO data sent in Initial packets, or the
  5787. client's estimated RTT is too small. ... To speed up
  5788. handshake completion under these conditions, an endpoint MAY
  5789. send a packet containing unacknowledged CRYPTO data earlier
  5790. than the PTO expiry, subject to address validation limits;
  5791. ... */
  5792. conn->flags |= NGTCP2_CONN_FLAG_HANDSHAKE_EARLY_RETRANSMIT;
  5793. conn->in_pktns->rtb.probe_pkt_left = 1;
  5794. conn->hs_pktns->rtb.probe_pkt_left = 1;
  5795. }
  5796. return 0;
  5797. }
  5798. crypto->rx.last_offset =
  5799. ngtcp2_max_uint64(crypto->rx.last_offset, fr_end_offset);
  5800. /* TODO Before dispatching incoming data to TLS stack, make sure
  5801. that previous data in previous encryption level has been
  5802. completely sent to TLS stack. Usually, if data is left, it is an
  5803. error because key is generated after consuming all data in the
  5804. previous encryption level. */
  5805. if (fr->offset <= rx_offset) {
  5806. size_t ncut = (size_t)(rx_offset - fr->offset);
  5807. const uint8_t *data = fr->data[0].base + ncut;
  5808. size_t datalen = fr->data[0].len - ncut;
  5809. uint64_t offset = rx_offset;
  5810. rx_offset += datalen;
  5811. ngtcp2_strm_update_rx_offset(crypto, rx_offset);
  5812. rv =
  5813. conn_call_recv_crypto_data(conn, encryption_level, offset, data, datalen);
  5814. if (rv != 0) {
  5815. return rv;
  5816. }
  5817. rv =
  5818. conn_emit_pending_crypto_data(conn, encryption_level, crypto, rx_offset);
  5819. if (rv != 0) {
  5820. return rv;
  5821. }
  5822. return 0;
  5823. }
  5824. if (fr_end_offset - rx_offset > NGTCP2_MAX_REORDERED_CRYPTO_DATA) {
  5825. return NGTCP2_ERR_CRYPTO_BUFFER_EXCEEDED;
  5826. }
  5827. return ngtcp2_strm_recv_reordering(crypto, fr->data[0].base, fr->data[0].len,
  5828. fr->offset);
  5829. }
  5830. /*
  5831. * conn_max_data_violated returns nonzero if receiving |datalen|
  5832. * violates connection flow control on local endpoint.
  5833. */
  5834. static int conn_max_data_violated(ngtcp2_conn *conn, uint64_t datalen) {
  5835. return conn->rx.max_offset - conn->rx.offset < datalen;
  5836. }
  5837. /*
  5838. * conn_recv_stream is called when STREAM frame |fr| is received.
  5839. *
  5840. * This function returns 0 if it succeeds, or one of the following
  5841. * negative error codes:
  5842. *
  5843. * NGTCP2_ERR_STREAM_STATE
  5844. * STREAM frame is received for a local stream which is not
  5845. * initiated; or STREAM frame is received for a local
  5846. * unidirectional stream
  5847. * NGTCP2_ERR_STREAM_LIMIT
  5848. * STREAM frame has remote stream ID which is strictly greater
  5849. * than the allowed limit.
  5850. * NGTCP2_ERR_NOMEM
  5851. * Out of memory.
  5852. * NGTCP2_ERR_CALLBACK_FAILURE
  5853. * User-defined callback function failed.
  5854. * NGTCP2_ERR_FLOW_CONTROL
  5855. * Flow control limit is violated; or the end offset of stream
  5856. * data is beyond the NGTCP2_MAX_VARINT.
  5857. * NGTCP2_ERR_FINAL_SIZE
  5858. * STREAM frame has strictly larger end offset than it is
  5859. * permitted.
  5860. */
  5861. static int conn_recv_stream(ngtcp2_conn *conn, const ngtcp2_stream *fr) {
  5862. int rv;
  5863. ngtcp2_strm *strm;
  5864. ngtcp2_idtr *idtr;
  5865. uint64_t rx_offset, fr_end_offset;
  5866. int local_stream;
  5867. int bidi;
  5868. uint64_t datalen = ngtcp2_vec_len(fr->data, fr->datacnt);
  5869. uint32_t sdflags = NGTCP2_STREAM_DATA_FLAG_NONE;
  5870. local_stream = conn_local_stream(conn, fr->stream_id);
  5871. bidi = bidi_stream(fr->stream_id);
  5872. if (bidi) {
  5873. if (local_stream) {
  5874. if (conn->local.bidi.next_stream_id <= fr->stream_id) {
  5875. return NGTCP2_ERR_STREAM_STATE;
  5876. }
  5877. } else if (conn->remote.bidi.max_streams <
  5878. ngtcp2_ord_stream_id(fr->stream_id)) {
  5879. return NGTCP2_ERR_STREAM_LIMIT;
  5880. }
  5881. idtr = &conn->remote.bidi.idtr;
  5882. } else {
  5883. if (local_stream) {
  5884. return NGTCP2_ERR_STREAM_STATE;
  5885. }
  5886. if (conn->remote.uni.max_streams < ngtcp2_ord_stream_id(fr->stream_id)) {
  5887. return NGTCP2_ERR_STREAM_LIMIT;
  5888. }
  5889. idtr = &conn->remote.uni.idtr;
  5890. }
  5891. if (NGTCP2_MAX_VARINT - datalen < fr->offset) {
  5892. return NGTCP2_ERR_FLOW_CONTROL;
  5893. }
  5894. strm = ngtcp2_conn_find_stream(conn, fr->stream_id);
  5895. if (strm == NULL) {
  5896. if (local_stream) {
  5897. /* TODO The stream has been closed. This should be responded
  5898. with RESET_STREAM, or simply ignored. */
  5899. return 0;
  5900. }
  5901. rv = ngtcp2_idtr_open(idtr, fr->stream_id);
  5902. if (rv != 0) {
  5903. if (ngtcp2_err_is_fatal(rv)) {
  5904. return rv;
  5905. }
  5906. assert(rv == NGTCP2_ERR_STREAM_IN_USE);
  5907. /* TODO The stream has been closed. This should be responded
  5908. with RESET_STREAM, or simply ignored. */
  5909. return 0;
  5910. }
  5911. strm = ngtcp2_objalloc_strm_get(&conn->strm_objalloc);
  5912. if (strm == NULL) {
  5913. return NGTCP2_ERR_NOMEM;
  5914. }
  5915. rv = ngtcp2_conn_init_stream(conn, strm, fr->stream_id, NULL);
  5916. if (rv != 0) {
  5917. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, strm);
  5918. return rv;
  5919. }
  5920. if (!bidi) {
  5921. ngtcp2_strm_shutdown(strm, NGTCP2_STRM_FLAG_SHUT_WR);
  5922. strm->flags |= NGTCP2_STRM_FLAG_FIN_ACKED;
  5923. }
  5924. rv = conn_call_stream_open(conn, strm);
  5925. if (rv != 0) {
  5926. return rv;
  5927. }
  5928. }
  5929. fr_end_offset = fr->offset + datalen;
  5930. if (strm->rx.max_offset < fr_end_offset) {
  5931. return NGTCP2_ERR_FLOW_CONTROL;
  5932. }
  5933. if (strm->rx.last_offset < fr_end_offset) {
  5934. uint64_t len = fr_end_offset - strm->rx.last_offset;
  5935. if (conn_max_data_violated(conn, len)) {
  5936. return NGTCP2_ERR_FLOW_CONTROL;
  5937. }
  5938. conn->rx.offset += len;
  5939. if (strm->flags & NGTCP2_STRM_FLAG_STOP_SENDING) {
  5940. ngtcp2_conn_extend_max_offset(conn, len);
  5941. }
  5942. }
  5943. rx_offset = ngtcp2_strm_rx_offset(strm);
  5944. if (fr->fin) {
  5945. if (strm->flags & NGTCP2_STRM_FLAG_SHUT_RD) {
  5946. if (strm->rx.last_offset != fr_end_offset) {
  5947. return NGTCP2_ERR_FINAL_SIZE;
  5948. }
  5949. if (strm->flags & NGTCP2_STRM_FLAG_RESET_STREAM_RECVED) {
  5950. return 0;
  5951. }
  5952. if (rx_offset == fr_end_offset) {
  5953. return 0;
  5954. }
  5955. } else if (strm->rx.last_offset > fr_end_offset) {
  5956. return NGTCP2_ERR_FINAL_SIZE;
  5957. } else {
  5958. strm->rx.last_offset = fr_end_offset;
  5959. ngtcp2_strm_shutdown(strm, NGTCP2_STRM_FLAG_SHUT_RD);
  5960. }
  5961. } else {
  5962. if ((strm->flags & NGTCP2_STRM_FLAG_SHUT_RD) &&
  5963. strm->rx.last_offset < fr_end_offset) {
  5964. return NGTCP2_ERR_FINAL_SIZE;
  5965. }
  5966. strm->rx.last_offset =
  5967. ngtcp2_max_uint64(strm->rx.last_offset, fr_end_offset);
  5968. if (fr_end_offset <= rx_offset) {
  5969. return 0;
  5970. }
  5971. if (strm->flags & NGTCP2_STRM_FLAG_RESET_STREAM_RECVED) {
  5972. return 0;
  5973. }
  5974. }
  5975. if (fr->offset <= rx_offset) {
  5976. size_t ncut = (size_t)(rx_offset - fr->offset);
  5977. uint64_t offset = rx_offset;
  5978. const uint8_t *data;
  5979. int fin;
  5980. if (fr->datacnt) {
  5981. data = fr->data[0].base + ncut;
  5982. datalen -= ncut;
  5983. rx_offset += datalen;
  5984. ngtcp2_strm_update_rx_offset(strm, rx_offset);
  5985. } else {
  5986. data = NULL;
  5987. datalen = 0;
  5988. }
  5989. if (strm->flags & NGTCP2_STRM_FLAG_STOP_SENDING) {
  5990. return ngtcp2_conn_close_stream_if_shut_rdwr(conn, strm);
  5991. }
  5992. fin = (strm->flags & NGTCP2_STRM_FLAG_SHUT_RD) &&
  5993. rx_offset == strm->rx.last_offset;
  5994. if (fin || datalen) {
  5995. if (fin) {
  5996. sdflags |= NGTCP2_STREAM_DATA_FLAG_FIN;
  5997. }
  5998. if (!conn_is_tls_handshake_completed(conn)) {
  5999. sdflags |= NGTCP2_STREAM_DATA_FLAG_0RTT;
  6000. }
  6001. rv = conn_call_recv_stream_data(conn, strm, sdflags, offset, data,
  6002. (size_t)datalen);
  6003. if (rv != 0) {
  6004. return rv;
  6005. }
  6006. rv = conn_emit_pending_stream_data(conn, strm, rx_offset);
  6007. if (rv != 0) {
  6008. return rv;
  6009. }
  6010. }
  6011. } else if (fr->datacnt && !(strm->flags & NGTCP2_STRM_FLAG_STOP_SENDING)) {
  6012. rv = ngtcp2_strm_recv_reordering(strm, fr->data[0].base, fr->data[0].len,
  6013. fr->offset);
  6014. if (rv != 0) {
  6015. return rv;
  6016. }
  6017. }
  6018. return ngtcp2_conn_close_stream_if_shut_rdwr(conn, strm);
  6019. }
  6020. /*
  6021. * conn_reset_stream adds RESET_STREAM frame to the transmission
  6022. * queue.
  6023. *
  6024. * This function returns 0 if it succeeds, or one of the following
  6025. * negative error codes:
  6026. *
  6027. * NGTCP2_ERR_NOMEM
  6028. * Out of memory.
  6029. */
  6030. static int conn_reset_stream(ngtcp2_conn *conn, ngtcp2_strm *strm,
  6031. uint64_t app_error_code) {
  6032. strm->flags |= NGTCP2_STRM_FLAG_SEND_RESET_STREAM;
  6033. strm->tx.reset_stream_app_error_code = app_error_code;
  6034. if (ngtcp2_strm_is_tx_queued(strm)) {
  6035. return 0;
  6036. }
  6037. strm->cycle = conn_tx_strmq_first_cycle(conn);
  6038. return ngtcp2_conn_tx_strmq_push(conn, strm);
  6039. }
  6040. /*
  6041. * conn_stop_sending adds STOP_SENDING frame to the transmission
  6042. * queue.
  6043. *
  6044. * This function returns 0 if it succeeds, or one of the following
  6045. * negative error codes:
  6046. *
  6047. * NGTCP2_ERR_NOMEM
  6048. * Out of memory.
  6049. */
  6050. static int conn_stop_sending(ngtcp2_conn *conn, ngtcp2_strm *strm,
  6051. uint64_t app_error_code) {
  6052. strm->flags |= NGTCP2_STRM_FLAG_SEND_STOP_SENDING;
  6053. strm->tx.stop_sending_app_error_code = app_error_code;
  6054. if (ngtcp2_strm_is_tx_queued(strm)) {
  6055. return 0;
  6056. }
  6057. strm->cycle = conn_tx_strmq_first_cycle(conn);
  6058. return ngtcp2_conn_tx_strmq_push(conn, strm);
  6059. }
  6060. /*
  6061. * handle_max_remote_streams_extension extends
  6062. * |*punsent_max_remote_streams| by |n| if a condition allows it.
  6063. */
  6064. static void
  6065. handle_max_remote_streams_extension(uint64_t *punsent_max_remote_streams,
  6066. size_t n) {
  6067. if (
  6068. #if SIZE_MAX > UINT32_MAX
  6069. NGTCP2_MAX_STREAMS < n ||
  6070. #endif /* SIZE_MAX > UINT32_MAX */
  6071. *punsent_max_remote_streams > (uint64_t)(NGTCP2_MAX_STREAMS - n)) {
  6072. *punsent_max_remote_streams = NGTCP2_MAX_STREAMS;
  6073. } else {
  6074. *punsent_max_remote_streams += n;
  6075. }
  6076. }
  6077. /*
  6078. * conn_recv_reset_stream is called when RESET_STREAM |fr| is
  6079. * received.
  6080. *
  6081. * This function returns 0 if it succeeds, or one of the following
  6082. * negative error codes:
  6083. *
  6084. * NGTCP2_ERR_STREAM_STATE
  6085. * RESET_STREAM frame is received to the local stream which is not
  6086. * initiated.
  6087. * NGTCP2_ERR_STREAM_LIMIT
  6088. * RESET_STREAM frame has remote stream ID which is strictly
  6089. * greater than the allowed limit.
  6090. * NGTCP2_ERR_PROTO
  6091. * RESET_STREAM frame is received to the local unidirectional
  6092. * stream
  6093. * NGTCP2_ERR_NOMEM
  6094. * Out of memory.
  6095. * NGTCP2_ERR_CALLBACK_FAILURE
  6096. * User-defined callback function failed.
  6097. * NGTCP2_ERR_FLOW_CONTROL
  6098. * Flow control limit is violated; or the final size is beyond the
  6099. * NGTCP2_MAX_VARINT.
  6100. * NGTCP2_ERR_FINAL_SIZE
  6101. * The final offset is strictly larger than it is permitted.
  6102. */
  6103. static int conn_recv_reset_stream(ngtcp2_conn *conn,
  6104. const ngtcp2_reset_stream *fr) {
  6105. ngtcp2_strm *strm;
  6106. int local_stream = conn_local_stream(conn, fr->stream_id);
  6107. int bidi = bidi_stream(fr->stream_id);
  6108. uint64_t datalen;
  6109. ngtcp2_idtr *idtr;
  6110. int rv;
  6111. /* TODO share this piece of code */
  6112. if (bidi) {
  6113. if (local_stream) {
  6114. if (conn->local.bidi.next_stream_id <= fr->stream_id) {
  6115. return NGTCP2_ERR_STREAM_STATE;
  6116. }
  6117. } else if (conn->remote.bidi.max_streams <
  6118. ngtcp2_ord_stream_id(fr->stream_id)) {
  6119. return NGTCP2_ERR_STREAM_LIMIT;
  6120. }
  6121. idtr = &conn->remote.bidi.idtr;
  6122. } else {
  6123. if (local_stream) {
  6124. return NGTCP2_ERR_PROTO;
  6125. }
  6126. if (conn->remote.uni.max_streams < ngtcp2_ord_stream_id(fr->stream_id)) {
  6127. return NGTCP2_ERR_STREAM_LIMIT;
  6128. }
  6129. idtr = &conn->remote.uni.idtr;
  6130. }
  6131. if (NGTCP2_MAX_VARINT < fr->final_size) {
  6132. return NGTCP2_ERR_FLOW_CONTROL;
  6133. }
  6134. strm = ngtcp2_conn_find_stream(conn, fr->stream_id);
  6135. if (strm == NULL) {
  6136. if (local_stream) {
  6137. return 0;
  6138. }
  6139. rv = ngtcp2_idtr_open(idtr, fr->stream_id);
  6140. if (rv != 0) {
  6141. if (ngtcp2_err_is_fatal(rv)) {
  6142. return rv;
  6143. }
  6144. assert(rv == NGTCP2_ERR_STREAM_IN_USE);
  6145. return 0;
  6146. }
  6147. if (conn_initial_stream_rx_offset(conn, fr->stream_id) < fr->final_size ||
  6148. conn_max_data_violated(conn, fr->final_size)) {
  6149. return NGTCP2_ERR_FLOW_CONTROL;
  6150. }
  6151. /* Stream is reset before we create ngtcp2_strm object. */
  6152. strm = ngtcp2_objalloc_strm_get(&conn->strm_objalloc);
  6153. if (strm == NULL) {
  6154. return NGTCP2_ERR_NOMEM;
  6155. }
  6156. rv = ngtcp2_conn_init_stream(conn, strm, fr->stream_id, NULL);
  6157. if (rv != 0) {
  6158. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, strm);
  6159. return rv;
  6160. }
  6161. rv = conn_call_stream_open(conn, strm);
  6162. if (rv != 0) {
  6163. return rv;
  6164. }
  6165. }
  6166. if ((strm->flags & NGTCP2_STRM_FLAG_SHUT_RD)) {
  6167. if (strm->rx.last_offset != fr->final_size) {
  6168. return NGTCP2_ERR_FINAL_SIZE;
  6169. }
  6170. } else if (strm->rx.last_offset > fr->final_size) {
  6171. return NGTCP2_ERR_FINAL_SIZE;
  6172. }
  6173. if (strm->flags & NGTCP2_STRM_FLAG_RESET_STREAM_RECVED) {
  6174. return 0;
  6175. }
  6176. if (strm->rx.max_offset < fr->final_size) {
  6177. return NGTCP2_ERR_FLOW_CONTROL;
  6178. }
  6179. datalen = fr->final_size - strm->rx.last_offset;
  6180. if (conn_max_data_violated(conn, datalen)) {
  6181. return NGTCP2_ERR_FLOW_CONTROL;
  6182. }
  6183. rv = conn_call_stream_reset(conn, fr->stream_id, fr->final_size,
  6184. fr->app_error_code, strm->stream_user_data);
  6185. if (rv != 0) {
  6186. return rv;
  6187. }
  6188. /* Extend connection flow control window for the amount of data
  6189. which are not passed to application. */
  6190. if (!(strm->flags & NGTCP2_STRM_FLAG_STOP_SENDING)) {
  6191. ngtcp2_conn_extend_max_offset(conn, strm->rx.last_offset -
  6192. ngtcp2_strm_rx_offset(strm));
  6193. }
  6194. conn->rx.offset += datalen;
  6195. ngtcp2_conn_extend_max_offset(conn, datalen);
  6196. strm->rx.last_offset = fr->final_size;
  6197. strm->flags |=
  6198. NGTCP2_STRM_FLAG_SHUT_RD | NGTCP2_STRM_FLAG_RESET_STREAM_RECVED;
  6199. ngtcp2_strm_set_app_error_code(strm, fr->app_error_code);
  6200. return ngtcp2_conn_close_stream_if_shut_rdwr(conn, strm);
  6201. }
  6202. /*
  6203. * conn_recv_stop_sending is called when STOP_SENDING |fr| is received.
  6204. *
  6205. * This function returns 0 if it succeeds, or one of the following
  6206. * negative error codes:
  6207. *
  6208. * NGTCP2_ERR_STREAM_STATE
  6209. * STOP_SENDING frame is received for a local stream which is not
  6210. * initiated; or STOP_SENDING frame is received for a local
  6211. * unidirectional stream.
  6212. * NGTCP2_ERR_STREAM_LIMIT
  6213. * STOP_SENDING frame has remote stream ID which is strictly
  6214. * greater than the allowed limit.
  6215. * NGTCP2_ERR_NOMEM
  6216. * Out of memory.
  6217. * NGTCP2_ERR_CALLBACK_FAILURE
  6218. * User-defined callback function failed.
  6219. */
  6220. static int conn_recv_stop_sending(ngtcp2_conn *conn,
  6221. const ngtcp2_stop_sending *fr) {
  6222. int rv;
  6223. ngtcp2_strm *strm;
  6224. ngtcp2_idtr *idtr;
  6225. int local_stream = conn_local_stream(conn, fr->stream_id);
  6226. int bidi = bidi_stream(fr->stream_id);
  6227. if (bidi) {
  6228. if (local_stream) {
  6229. if (conn->local.bidi.next_stream_id <= fr->stream_id) {
  6230. return NGTCP2_ERR_STREAM_STATE;
  6231. }
  6232. } else if (conn->remote.bidi.max_streams <
  6233. ngtcp2_ord_stream_id(fr->stream_id)) {
  6234. return NGTCP2_ERR_STREAM_LIMIT;
  6235. }
  6236. idtr = &conn->remote.bidi.idtr;
  6237. } else {
  6238. if (!local_stream || conn->local.uni.next_stream_id <= fr->stream_id) {
  6239. return NGTCP2_ERR_STREAM_STATE;
  6240. }
  6241. idtr = &conn->remote.uni.idtr;
  6242. }
  6243. strm = ngtcp2_conn_find_stream(conn, fr->stream_id);
  6244. if (strm == NULL) {
  6245. if (local_stream) {
  6246. return 0;
  6247. }
  6248. rv = ngtcp2_idtr_open(idtr, fr->stream_id);
  6249. if (rv != 0) {
  6250. if (ngtcp2_err_is_fatal(rv)) {
  6251. return rv;
  6252. }
  6253. assert(rv == NGTCP2_ERR_STREAM_IN_USE);
  6254. return 0;
  6255. }
  6256. /* STOP_SENDING frame is received before we create ngtcp2_strm
  6257. object. */
  6258. strm = ngtcp2_objalloc_strm_get(&conn->strm_objalloc);
  6259. if (strm == NULL) {
  6260. return NGTCP2_ERR_NOMEM;
  6261. }
  6262. rv = ngtcp2_conn_init_stream(conn, strm, fr->stream_id, NULL);
  6263. if (rv != 0) {
  6264. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, strm);
  6265. return rv;
  6266. }
  6267. rv = conn_call_stream_open(conn, strm);
  6268. if (rv != 0) {
  6269. return rv;
  6270. }
  6271. }
  6272. if (strm->flags & NGTCP2_STRM_FLAG_STOP_SENDING_RECVED) {
  6273. return 0;
  6274. }
  6275. ngtcp2_strm_set_app_error_code(strm, fr->app_error_code);
  6276. /* No RESET_STREAM is required if we have sent FIN and all data have
  6277. been acknowledged. */
  6278. if (!ngtcp2_strm_is_all_tx_data_fin_acked(strm) &&
  6279. !(strm->flags & NGTCP2_STRM_FLAG_RESET_STREAM)) {
  6280. strm->flags |= NGTCP2_STRM_FLAG_RESET_STREAM;
  6281. rv = conn_reset_stream(conn, strm, fr->app_error_code);
  6282. if (rv != 0) {
  6283. return rv;
  6284. }
  6285. }
  6286. strm->flags |=
  6287. NGTCP2_STRM_FLAG_SHUT_WR | NGTCP2_STRM_FLAG_STOP_SENDING_RECVED;
  6288. ngtcp2_strm_streamfrq_clear(strm);
  6289. return ngtcp2_conn_close_stream_if_shut_rdwr(conn, strm);
  6290. }
  6291. /*
  6292. * check_stateless_reset returns nonzero if Stateless Reset |sr|
  6293. * coming via |path| is valid against |dcid|.
  6294. */
  6295. static int check_stateless_reset(const ngtcp2_dcid *dcid,
  6296. const ngtcp2_path *path,
  6297. const ngtcp2_pkt_stateless_reset *sr) {
  6298. return ngtcp2_path_eq(&dcid->ps.path, path) &&
  6299. ngtcp2_dcid_verify_stateless_reset_token(
  6300. dcid, sr->stateless_reset_token) == 0;
  6301. }
  6302. /*
  6303. * conn_on_stateless_reset decodes Stateless Reset from the buffer
  6304. * pointed by |payload| whose length is |payloadlen|. |payload|
  6305. * should start after first byte of packet.
  6306. *
  6307. * If Stateless Reset is decoded, and the Stateless Reset Token is
  6308. * validated, the connection is closed.
  6309. *
  6310. * This function returns 0 if it succeeds, or one of the following
  6311. * negative error codes:
  6312. *
  6313. * NGTCP2_ERR_INVALID_ARGUMENT
  6314. * Could not decode Stateless Reset; or Stateless Reset Token does
  6315. * not match; or No stateless reset token is available.
  6316. * NGTCP2_ERR_CALLBACK_FAILURE
  6317. * User callback failed.
  6318. */
  6319. static int conn_on_stateless_reset(ngtcp2_conn *conn, const ngtcp2_path *path,
  6320. const uint8_t *payload, size_t payloadlen) {
  6321. int rv = 1;
  6322. ngtcp2_pv *pv = conn->pv;
  6323. ngtcp2_dcid *dcid;
  6324. ngtcp2_pkt_stateless_reset sr;
  6325. size_t len, i;
  6326. rv = ngtcp2_pkt_decode_stateless_reset(&sr, payload, payloadlen);
  6327. if (rv != 0) {
  6328. return rv;
  6329. }
  6330. if (!check_stateless_reset(&conn->dcid.current, path, &sr) &&
  6331. (!pv || (!check_stateless_reset(&pv->dcid, path, &sr) &&
  6332. (!(pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) ||
  6333. !check_stateless_reset(&pv->fallback_dcid, path, &sr))))) {
  6334. len = ngtcp2_ringbuf_len(&conn->dcid.retired.rb);
  6335. for (i = 0; i < len; ++i) {
  6336. dcid = ngtcp2_ringbuf_get(&conn->dcid.retired.rb, i);
  6337. if (check_stateless_reset(dcid, path, &sr)) {
  6338. break;
  6339. }
  6340. }
  6341. if (i == len) {
  6342. len = ngtcp2_ringbuf_len(&conn->dcid.bound.rb);
  6343. for (i = 0; i < len; ++i) {
  6344. dcid = ngtcp2_ringbuf_get(&conn->dcid.bound.rb, i);
  6345. if (check_stateless_reset(dcid, path, &sr)) {
  6346. break;
  6347. }
  6348. }
  6349. if (i == len) {
  6350. return NGTCP2_ERR_INVALID_ARGUMENT;
  6351. }
  6352. }
  6353. }
  6354. conn->state = NGTCP2_CS_DRAINING;
  6355. ngtcp2_log_rx_sr(&conn->log, &sr);
  6356. ngtcp2_qlog_stateless_reset_pkt_received(&conn->qlog, &sr);
  6357. return conn_call_recv_stateless_reset(conn, &sr);
  6358. }
  6359. /*
  6360. * conn_recv_max_streams processes the incoming MAX_STREAMS frame
  6361. * |fr|.
  6362. *
  6363. * This function returns 0 if it succeeds, or one of the following
  6364. * negative error codes:
  6365. *
  6366. * NGTCP2_ERR_CALLBACK_FAILURE
  6367. * User callback failed.
  6368. * NGTCP2_ERR_FRAME_ENCODING
  6369. * The maximum streams field exceeds the maximum value.
  6370. */
  6371. static int conn_recv_max_streams(ngtcp2_conn *conn,
  6372. const ngtcp2_max_streams *fr) {
  6373. uint64_t n;
  6374. if (fr->max_streams > NGTCP2_MAX_STREAMS) {
  6375. return NGTCP2_ERR_FRAME_ENCODING;
  6376. }
  6377. n = ngtcp2_min_uint64(fr->max_streams, NGTCP2_MAX_STREAMS);
  6378. if (fr->type == NGTCP2_FRAME_MAX_STREAMS_BIDI) {
  6379. if (conn->local.bidi.max_streams < n) {
  6380. conn->local.bidi.max_streams = n;
  6381. return conn_call_extend_max_local_streams_bidi(conn, n);
  6382. }
  6383. return 0;
  6384. }
  6385. if (conn->local.uni.max_streams < n) {
  6386. conn->local.uni.max_streams = n;
  6387. return conn_call_extend_max_local_streams_uni(conn, n);
  6388. }
  6389. return 0;
  6390. }
  6391. static int conn_retire_dcid_prior_to(ngtcp2_conn *conn, ngtcp2_ringbuf *rb,
  6392. uint64_t retire_prior_to) {
  6393. size_t i;
  6394. ngtcp2_dcid *dcid, *last;
  6395. int rv;
  6396. for (i = 0; i < ngtcp2_ringbuf_len(rb);) {
  6397. dcid = ngtcp2_ringbuf_get(rb, i);
  6398. if (dcid->seq >= retire_prior_to) {
  6399. ++i;
  6400. continue;
  6401. }
  6402. rv = conn_retire_dcid_seq(conn, dcid->seq);
  6403. if (rv != 0) {
  6404. return rv;
  6405. }
  6406. if (i == 0) {
  6407. ngtcp2_ringbuf_pop_front(rb);
  6408. continue;
  6409. }
  6410. if (i == ngtcp2_ringbuf_len(rb) - 1) {
  6411. ngtcp2_ringbuf_pop_back(rb);
  6412. break;
  6413. }
  6414. last = ngtcp2_ringbuf_get(rb, ngtcp2_ringbuf_len(rb) - 1);
  6415. ngtcp2_dcid_copy(dcid, last);
  6416. ngtcp2_ringbuf_pop_back(rb);
  6417. }
  6418. return 0;
  6419. }
  6420. /*
  6421. * conn_recv_new_connection_id processes the incoming
  6422. * NEW_CONNECTION_ID frame |fr|.
  6423. *
  6424. * This function returns 0 if it succeeds, or one of the following
  6425. * negative error codes:
  6426. *
  6427. * NGTCP2_ERR_PROTO
  6428. * |fr| has the duplicated sequence number with different CID or
  6429. * token; or DCID is zero-length.
  6430. */
  6431. static int conn_recv_new_connection_id(ngtcp2_conn *conn,
  6432. const ngtcp2_new_connection_id *fr) {
  6433. size_t i, len;
  6434. ngtcp2_dcid *dcid;
  6435. ngtcp2_pv *pv = conn->pv;
  6436. int rv;
  6437. int found = 0;
  6438. size_t extra_dcid = 0;
  6439. if (conn->dcid.current.cid.datalen == 0) {
  6440. return NGTCP2_ERR_PROTO;
  6441. }
  6442. if (fr->retire_prior_to > fr->seq) {
  6443. return NGTCP2_ERR_FRAME_ENCODING;
  6444. }
  6445. rv = ngtcp2_dcid_verify_uniqueness(&conn->dcid.current, fr->seq, &fr->cid,
  6446. fr->stateless_reset_token);
  6447. if (rv != 0) {
  6448. return rv;
  6449. }
  6450. if (ngtcp2_cid_eq(&conn->dcid.current.cid, &fr->cid)) {
  6451. found = 1;
  6452. }
  6453. if (pv) {
  6454. rv = ngtcp2_dcid_verify_uniqueness(&pv->dcid, fr->seq, &fr->cid,
  6455. fr->stateless_reset_token);
  6456. if (rv != 0) {
  6457. return rv;
  6458. }
  6459. if (ngtcp2_cid_eq(&pv->dcid.cid, &fr->cid)) {
  6460. found = 1;
  6461. }
  6462. }
  6463. len = ngtcp2_ringbuf_len(&conn->dcid.bound.rb);
  6464. for (i = 0; i < len; ++i) {
  6465. dcid = ngtcp2_ringbuf_get(&conn->dcid.bound.rb, i);
  6466. rv = ngtcp2_dcid_verify_uniqueness(dcid, fr->seq, &fr->cid,
  6467. fr->stateless_reset_token);
  6468. if (rv != 0) {
  6469. return NGTCP2_ERR_PROTO;
  6470. }
  6471. if (ngtcp2_cid_eq(&dcid->cid, &fr->cid)) {
  6472. found = 1;
  6473. }
  6474. }
  6475. len = ngtcp2_ringbuf_len(&conn->dcid.unused.rb);
  6476. for (i = 0; i < len; ++i) {
  6477. dcid = ngtcp2_ringbuf_get(&conn->dcid.unused.rb, i);
  6478. rv = ngtcp2_dcid_verify_uniqueness(dcid, fr->seq, &fr->cid,
  6479. fr->stateless_reset_token);
  6480. if (rv != 0) {
  6481. return NGTCP2_ERR_PROTO;
  6482. }
  6483. if (ngtcp2_cid_eq(&dcid->cid, &fr->cid)) {
  6484. found = 1;
  6485. }
  6486. }
  6487. if (conn->dcid.retire_prior_to < fr->retire_prior_to) {
  6488. conn->dcid.retire_prior_to = fr->retire_prior_to;
  6489. rv = conn_retire_dcid_prior_to(conn, &conn->dcid.bound.rb,
  6490. fr->retire_prior_to);
  6491. if (rv != 0) {
  6492. return rv;
  6493. }
  6494. rv = conn_retire_dcid_prior_to(conn, &conn->dcid.unused.rb,
  6495. conn->dcid.retire_prior_to);
  6496. if (rv != 0) {
  6497. return rv;
  6498. }
  6499. } else if (fr->seq < conn->dcid.retire_prior_to) {
  6500. /* If packets are reordered, we might have retire_prior_to which
  6501. is larger than fr->seq.
  6502. A malicious peer might send crafted NEW_CONNECTION_ID to force
  6503. local endpoint to create lots of RETIRE_CONNECTION_ID frames.
  6504. For example, a peer might send seq = 50000 and retire_prior_to
  6505. = 50000. Then send NEW_CONNECTION_ID frames with seq <
  6506. 50000. */
  6507. return conn_retire_dcid_seq(conn, fr->seq);
  6508. }
  6509. if (found) {
  6510. return 0;
  6511. }
  6512. if (ngtcp2_gaptr_is_pushed(&conn->dcid.seqgap, fr->seq, 1)) {
  6513. return 0;
  6514. }
  6515. rv = ngtcp2_gaptr_push(&conn->dcid.seqgap, fr->seq, 1);
  6516. if (rv != 0) {
  6517. return rv;
  6518. }
  6519. if (ngtcp2_ksl_len(&conn->dcid.seqgap.gap) > 32) {
  6520. ngtcp2_gaptr_drop_first_gap(&conn->dcid.seqgap);
  6521. }
  6522. len = ngtcp2_ringbuf_len(&conn->dcid.unused.rb);
  6523. if (conn->dcid.current.seq >= conn->dcid.retire_prior_to) {
  6524. ++extra_dcid;
  6525. }
  6526. if (pv) {
  6527. if (pv->dcid.seq != conn->dcid.current.seq &&
  6528. pv->dcid.seq >= conn->dcid.retire_prior_to) {
  6529. ++extra_dcid;
  6530. }
  6531. if ((pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  6532. pv->fallback_dcid.seq != conn->dcid.current.seq &&
  6533. pv->fallback_dcid.seq >= conn->dcid.retire_prior_to) {
  6534. ++extra_dcid;
  6535. }
  6536. }
  6537. if (conn->local.transport_params.active_connection_id_limit <=
  6538. len + extra_dcid) {
  6539. return NGTCP2_ERR_CONNECTION_ID_LIMIT;
  6540. }
  6541. if (len >= NGTCP2_MAX_DCID_POOL_SIZE) {
  6542. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON, "too many connection ID");
  6543. return 0;
  6544. }
  6545. dcid = ngtcp2_ringbuf_push_back(&conn->dcid.unused.rb);
  6546. ngtcp2_dcid_init(dcid, fr->seq, &fr->cid, fr->stateless_reset_token);
  6547. return 0;
  6548. }
  6549. /*
  6550. * conn_post_process_recv_new_connection_id handles retirement request
  6551. * of active DCIDs.
  6552. *
  6553. * This function returns 0 if it succeeds, or one of the following
  6554. * negative error codes:
  6555. *
  6556. * NGTCP2_ERR_NOMEM
  6557. * Out of memory.
  6558. * NGTCP2_ERR_CALLBACK_FAILURE
  6559. * User-defined callback function failed.
  6560. */
  6561. static int conn_post_process_recv_new_connection_id(ngtcp2_conn *conn,
  6562. ngtcp2_tstamp ts) {
  6563. ngtcp2_pv *pv = conn->pv;
  6564. ngtcp2_dcid *dcid;
  6565. int rv;
  6566. if (conn->dcid.current.seq < conn->dcid.retire_prior_to) {
  6567. if (ngtcp2_ringbuf_len(&conn->dcid.unused.rb) == 0) {
  6568. return 0;
  6569. }
  6570. rv = conn_retire_dcid(conn, &conn->dcid.current, ts);
  6571. if (rv != 0) {
  6572. return rv;
  6573. }
  6574. dcid = ngtcp2_ringbuf_get(&conn->dcid.unused.rb, 0);
  6575. if (pv) {
  6576. if (conn->dcid.current.seq == pv->dcid.seq) {
  6577. ngtcp2_dcid_copy_cid_token(&pv->dcid, dcid);
  6578. }
  6579. if ((pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  6580. conn->dcid.current.seq == pv->fallback_dcid.seq) {
  6581. ngtcp2_dcid_copy_cid_token(&pv->fallback_dcid, dcid);
  6582. }
  6583. }
  6584. ngtcp2_dcid_copy_cid_token(&conn->dcid.current, dcid);
  6585. ngtcp2_ringbuf_pop_front(&conn->dcid.unused.rb);
  6586. rv = conn_call_activate_dcid(conn, &conn->dcid.current);
  6587. if (rv != 0) {
  6588. return rv;
  6589. }
  6590. }
  6591. if (pv) {
  6592. if (pv->dcid.seq < conn->dcid.retire_prior_to) {
  6593. if (ngtcp2_ringbuf_len(&conn->dcid.unused.rb)) {
  6594. rv = conn_retire_dcid(conn, &pv->dcid, ts);
  6595. if (rv != 0) {
  6596. return rv;
  6597. }
  6598. dcid = ngtcp2_ringbuf_get(&conn->dcid.unused.rb, 0);
  6599. if ((pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  6600. pv->dcid.seq == pv->fallback_dcid.seq) {
  6601. ngtcp2_dcid_copy_cid_token(&pv->fallback_dcid, dcid);
  6602. }
  6603. ngtcp2_dcid_copy_cid_token(&pv->dcid, dcid);
  6604. ngtcp2_ringbuf_pop_front(&conn->dcid.unused.rb);
  6605. rv = conn_call_activate_dcid(conn, &pv->dcid);
  6606. if (rv != 0) {
  6607. return rv;
  6608. }
  6609. } else {
  6610. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PTV,
  6611. "path migration is aborted because connection ID is"
  6612. "retired and no unused connection ID is available");
  6613. return conn_abort_pv(conn, ts);
  6614. }
  6615. }
  6616. if ((pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  6617. pv->fallback_dcid.seq < conn->dcid.retire_prior_to) {
  6618. if (ngtcp2_ringbuf_len(&conn->dcid.unused.rb)) {
  6619. rv = conn_retire_dcid(conn, &pv->fallback_dcid, ts);
  6620. if (rv != 0) {
  6621. return rv;
  6622. }
  6623. dcid = ngtcp2_ringbuf_get(&conn->dcid.unused.rb, 0);
  6624. ngtcp2_dcid_copy_cid_token(&pv->fallback_dcid, dcid);
  6625. ngtcp2_ringbuf_pop_front(&conn->dcid.unused.rb);
  6626. rv = conn_call_activate_dcid(conn, &pv->fallback_dcid);
  6627. if (rv != 0) {
  6628. return rv;
  6629. }
  6630. } else {
  6631. /* Now we have no fallback dcid. */
  6632. return conn_abort_pv(conn, ts);
  6633. }
  6634. }
  6635. }
  6636. return 0;
  6637. }
  6638. /*
  6639. * conn_recv_retire_connection_id processes the incoming
  6640. * RETIRE_CONNECTION_ID frame |fr|. |hd| is a packet header which
  6641. * |fr| is included.
  6642. *
  6643. * This function returns 0 if it succeeds, or one of the following
  6644. * negative error codes:
  6645. *
  6646. * NGTCP2_ERR_NOMEM
  6647. * Out of memory.
  6648. * NGTCP2_ERR_PROTO
  6649. * SCID is zero-length.
  6650. * NGTCP2_ERR_FRAME_ENCODING
  6651. * Attempt to retire CID which is used as DCID to send this frame.
  6652. */
  6653. static int conn_recv_retire_connection_id(ngtcp2_conn *conn,
  6654. const ngtcp2_pkt_hd *hd,
  6655. const ngtcp2_retire_connection_id *fr,
  6656. ngtcp2_tstamp ts) {
  6657. ngtcp2_ksl_it it;
  6658. ngtcp2_scid *scid;
  6659. if (conn->oscid.datalen == 0 || conn->scid.last_seq < fr->seq) {
  6660. return NGTCP2_ERR_PROTO;
  6661. }
  6662. for (it = ngtcp2_ksl_begin(&conn->scid.set); !ngtcp2_ksl_it_end(&it);
  6663. ngtcp2_ksl_it_next(&it)) {
  6664. scid = ngtcp2_ksl_it_get(&it);
  6665. if (scid->seq == fr->seq) {
  6666. if (ngtcp2_cid_eq(&scid->cid, &hd->dcid)) {
  6667. return NGTCP2_ERR_PROTO;
  6668. }
  6669. if (!(scid->flags & NGTCP2_SCID_FLAG_RETIRED)) {
  6670. scid->flags |= NGTCP2_SCID_FLAG_RETIRED;
  6671. ++conn->scid.num_retired;
  6672. }
  6673. if (scid->pe.index != NGTCP2_PQ_BAD_INDEX) {
  6674. ngtcp2_pq_remove(&conn->scid.used, &scid->pe);
  6675. scid->pe.index = NGTCP2_PQ_BAD_INDEX;
  6676. }
  6677. scid->retired_ts = ts;
  6678. return ngtcp2_pq_push(&conn->scid.used, &scid->pe);
  6679. }
  6680. }
  6681. return 0;
  6682. }
  6683. /*
  6684. * conn_recv_new_token processes the incoming NEW_TOKEN frame |fr|.
  6685. *
  6686. * This function returns 0 if it succeeds, or one of the following
  6687. * negative error codes:
  6688. *
  6689. * NGTCP2_ERR_FRAME_ENCODING
  6690. * Token is empty
  6691. * NGTCP2_ERR_PROTO:
  6692. * Server received NEW_TOKEN.
  6693. */
  6694. static int conn_recv_new_token(ngtcp2_conn *conn, const ngtcp2_new_token *fr) {
  6695. if (conn->server) {
  6696. return NGTCP2_ERR_PROTO;
  6697. }
  6698. if (fr->tokenlen == 0) {
  6699. return NGTCP2_ERR_FRAME_ENCODING;
  6700. }
  6701. return conn_call_recv_new_token(conn, fr->token, fr->tokenlen);
  6702. }
  6703. /*
  6704. * conn_recv_streams_blocked_bidi processes the incoming
  6705. * STREAMS_BLOCKED (0x16).
  6706. *
  6707. * This function returns 0 if it succeeds, or one of the following
  6708. * negative error codes:
  6709. *
  6710. * NGTCP2_ERR_FRAME_ENCODING
  6711. * Maximum Streams is larger than advertised value.
  6712. */
  6713. static int conn_recv_streams_blocked_bidi(ngtcp2_conn *conn,
  6714. ngtcp2_streams_blocked *fr) {
  6715. if (fr->max_streams > conn->remote.bidi.max_streams) {
  6716. return NGTCP2_ERR_FRAME_ENCODING;
  6717. }
  6718. return 0;
  6719. }
  6720. /*
  6721. * conn_recv_streams_blocked_uni processes the incoming
  6722. * STREAMS_BLOCKED (0x17).
  6723. *
  6724. * This function returns 0 if it succeeds, or one of the following
  6725. * negative error codes:
  6726. *
  6727. * NGTCP2_ERR_FRAME_ENCODING
  6728. * Maximum Streams is larger than advertised value.
  6729. */
  6730. static int conn_recv_streams_blocked_uni(ngtcp2_conn *conn,
  6731. ngtcp2_streams_blocked *fr) {
  6732. if (fr->max_streams > conn->remote.uni.max_streams) {
  6733. return NGTCP2_ERR_FRAME_ENCODING;
  6734. }
  6735. return 0;
  6736. }
  6737. /*
  6738. * conn_recv_stream_data_blocked processes the incoming
  6739. * STREAM_DATA_BLOCKED frame |fr|.
  6740. *
  6741. * This function returns 0 if it succeeds, or one of the following
  6742. * negative error codes:
  6743. *
  6744. * NGTCP2_ERR_STREAM_STATE
  6745. * STREAM_DATA_BLOCKED is received for a local stream which is not
  6746. * initiated; or it is received for a local unidirectional stream.
  6747. * NGTCP2_ERR_STREAM_LIMIT
  6748. * STREAM_DATA_BLOCKED has remote stream ID which is strictly
  6749. * greater than the allowed limit.
  6750. * NGTCP2_ERR_FLOW_CONTROL
  6751. * STREAM_DATA_BLOCKED frame violates flow control limit.
  6752. * NGTCP2_ERR_FINAL_SIZE
  6753. * The offset is strictly larger than it is permitted.
  6754. * NGTCP2_ERR_NOMEM
  6755. * Out of memory.
  6756. * NGTCP2_ERR_CALLBACK_FAILURE
  6757. * User-defined callback function failed.
  6758. */
  6759. static int conn_recv_stream_data_blocked(ngtcp2_conn *conn,
  6760. ngtcp2_stream_data_blocked *fr) {
  6761. int rv;
  6762. ngtcp2_strm *strm;
  6763. ngtcp2_idtr *idtr;
  6764. int local_stream = conn_local_stream(conn, fr->stream_id);
  6765. int bidi = bidi_stream(fr->stream_id);
  6766. uint64_t datalen;
  6767. if (bidi) {
  6768. if (local_stream) {
  6769. if (conn->local.bidi.next_stream_id <= fr->stream_id) {
  6770. return NGTCP2_ERR_STREAM_STATE;
  6771. }
  6772. } else if (conn->remote.bidi.max_streams <
  6773. ngtcp2_ord_stream_id(fr->stream_id)) {
  6774. return NGTCP2_ERR_STREAM_LIMIT;
  6775. }
  6776. idtr = &conn->remote.bidi.idtr;
  6777. } else {
  6778. if (local_stream) {
  6779. return NGTCP2_ERR_STREAM_STATE;
  6780. }
  6781. if (conn->remote.uni.max_streams < ngtcp2_ord_stream_id(fr->stream_id)) {
  6782. return NGTCP2_ERR_STREAM_LIMIT;
  6783. }
  6784. idtr = &conn->remote.uni.idtr;
  6785. }
  6786. strm = ngtcp2_conn_find_stream(conn, fr->stream_id);
  6787. if (strm == NULL) {
  6788. if (local_stream) {
  6789. return 0;
  6790. }
  6791. rv = ngtcp2_idtr_open(idtr, fr->stream_id);
  6792. if (rv != 0) {
  6793. if (ngtcp2_err_is_fatal(rv)) {
  6794. return rv;
  6795. }
  6796. assert(rv == NGTCP2_ERR_STREAM_IN_USE);
  6797. return 0;
  6798. }
  6799. /* Frame is received before we create ngtcp2_strm object. */
  6800. strm = ngtcp2_objalloc_strm_get(&conn->strm_objalloc);
  6801. if (strm == NULL) {
  6802. return NGTCP2_ERR_NOMEM;
  6803. }
  6804. rv = ngtcp2_conn_init_stream(conn, strm, fr->stream_id, NULL);
  6805. if (rv != 0) {
  6806. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, strm);
  6807. return rv;
  6808. }
  6809. if (!bidi) {
  6810. ngtcp2_strm_shutdown(strm, NGTCP2_STRM_FLAG_SHUT_WR);
  6811. strm->flags |= NGTCP2_STRM_FLAG_FIN_ACKED;
  6812. }
  6813. rv = conn_call_stream_open(conn, strm);
  6814. if (rv != 0) {
  6815. return rv;
  6816. }
  6817. }
  6818. if (strm->rx.max_offset < fr->offset) {
  6819. return NGTCP2_ERR_FLOW_CONTROL;
  6820. }
  6821. if (fr->offset <= strm->rx.last_offset) {
  6822. return 0;
  6823. }
  6824. if (strm->flags & NGTCP2_STRM_FLAG_SHUT_RD) {
  6825. return NGTCP2_ERR_FINAL_SIZE;
  6826. }
  6827. datalen = fr->offset - strm->rx.last_offset;
  6828. if (datalen) {
  6829. if (conn_max_data_violated(conn, datalen)) {
  6830. return NGTCP2_ERR_FLOW_CONTROL;
  6831. }
  6832. conn->rx.offset += datalen;
  6833. if (strm->flags & NGTCP2_STRM_FLAG_STOP_SENDING) {
  6834. ngtcp2_conn_extend_max_offset(conn, datalen);
  6835. }
  6836. }
  6837. strm->rx.last_offset = fr->offset;
  6838. return 0;
  6839. }
  6840. /*
  6841. * conn_recv_data_blocked processes the incoming DATA_BLOCKED frame
  6842. * |fr|.
  6843. *
  6844. * This function returns 0 if it succeeds, or one of the following
  6845. * negative error codes:
  6846. *
  6847. * NGTCP2_ERR_FLOW_CONTROL
  6848. * It violates connection-level flow control limit.
  6849. */
  6850. static int conn_recv_data_blocked(ngtcp2_conn *conn, ngtcp2_data_blocked *fr) {
  6851. if (conn->rx.max_offset < fr->offset) {
  6852. return NGTCP2_ERR_FLOW_CONTROL;
  6853. }
  6854. return 0;
  6855. }
  6856. /*
  6857. * conn_select_preferred_addr asks a client application to select a
  6858. * server address from preferred addresses received from server. If a
  6859. * client chooses the address, path validation will start.
  6860. *
  6861. * This function returns 0 if it succeeds, or one of the following
  6862. * negative error codes:
  6863. *
  6864. * NGTCP2_ERR_NOMEM
  6865. * Out of memory.
  6866. * NGTCP2_ERR_CALLBACK_FAILURE
  6867. * User-defined callback function failed.
  6868. */
  6869. static int conn_select_preferred_addr(ngtcp2_conn *conn) {
  6870. ngtcp2_path_storage ps;
  6871. int rv;
  6872. ngtcp2_pv *pv;
  6873. ngtcp2_dcid *dcid;
  6874. if (ngtcp2_ringbuf_len(&conn->dcid.unused.rb) == 0) {
  6875. return 0;
  6876. }
  6877. ngtcp2_path_storage_zero(&ps);
  6878. ngtcp2_addr_copy(&ps.path.local, &conn->dcid.current.ps.path.local);
  6879. rv = conn_call_select_preferred_addr(conn, &ps.path);
  6880. if (rv != 0) {
  6881. return rv;
  6882. }
  6883. if (ps.path.remote.addrlen == 0 ||
  6884. ngtcp2_addr_eq(&conn->dcid.current.ps.path.remote, &ps.path.remote)) {
  6885. return 0;
  6886. }
  6887. assert(conn->pv == NULL);
  6888. dcid = ngtcp2_ringbuf_get(&conn->dcid.unused.rb, 0);
  6889. ngtcp2_dcid_set_path(dcid, &ps.path);
  6890. rv = ngtcp2_pv_new(&pv, dcid, conn_compute_pv_timeout(conn),
  6891. NGTCP2_PV_FLAG_PREFERRED_ADDR, &conn->log, conn->mem);
  6892. if (rv != 0) {
  6893. /* TODO Call ngtcp2_dcid_free here if it is introduced */
  6894. return rv;
  6895. }
  6896. ngtcp2_ringbuf_pop_front(&conn->dcid.unused.rb);
  6897. conn->pv = pv;
  6898. return conn_call_activate_dcid(conn, &pv->dcid);
  6899. }
  6900. /*
  6901. * conn_recv_handshake_done processes the incoming HANDSHAKE_DONE
  6902. * frame |fr|.
  6903. *
  6904. * This function returns 0 if it succeeds, or one of the following
  6905. * negative error codes:
  6906. *
  6907. * NGTCP2_ERR_PROTO
  6908. * Server received HANDSHAKE_DONE frame.
  6909. * NGTCP2_ERR_NOMEM
  6910. * Out of memory.
  6911. * NGTCP2_ERR_CALLBACK_FAILURE
  6912. * User-defined callback function failed.
  6913. */
  6914. static int conn_recv_handshake_done(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  6915. int rv;
  6916. if (conn->server) {
  6917. return NGTCP2_ERR_PROTO;
  6918. }
  6919. if (conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED) {
  6920. return 0;
  6921. }
  6922. conn->flags |= NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED |
  6923. NGTCP2_CONN_FLAG_SERVER_ADDR_VERIFIED;
  6924. conn->pktns.rtb.persistent_congestion_start_ts = ts;
  6925. ngtcp2_conn_discard_handshake_state(conn, ts);
  6926. assert(conn->remote.transport_params);
  6927. if (conn->remote.transport_params->preferred_addr_present) {
  6928. rv = conn_select_preferred_addr(conn);
  6929. if (rv != 0) {
  6930. return rv;
  6931. }
  6932. }
  6933. rv = conn_call_handshake_confirmed(conn);
  6934. if (rv != 0) {
  6935. return rv;
  6936. }
  6937. /* Re-arm loss detection timer after handshake has been
  6938. confirmed. */
  6939. ngtcp2_conn_set_loss_detection_timer(conn, ts);
  6940. return 0;
  6941. }
  6942. /*
  6943. * conn_recv_datagram processes the incoming DATAGRAM frame |fr|.
  6944. *
  6945. * This function returns 0 if it succeeds, or one of the following
  6946. * negative error codes:
  6947. *
  6948. * NGTCP2_ERR_CALLBACK_FAILURE
  6949. * User-defined callback function failed.
  6950. */
  6951. static int conn_recv_datagram(ngtcp2_conn *conn, ngtcp2_datagram *fr) {
  6952. assert(conn->local.transport_params.max_datagram_frame_size);
  6953. return conn_call_recv_datagram(conn, fr);
  6954. }
  6955. /*
  6956. * conn_key_phase_changed returns nonzero if |hd| indicates that the
  6957. * key phase has unexpected value.
  6958. */
  6959. static int conn_key_phase_changed(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd) {
  6960. ngtcp2_pktns *pktns = &conn->pktns;
  6961. return !(pktns->crypto.rx.ckm->flags & NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE) ^
  6962. !(hd->flags & NGTCP2_PKT_FLAG_KEY_PHASE);
  6963. }
  6964. static int conn_initiate_key_update(ngtcp2_conn *conn, ngtcp2_tstamp ts);
  6965. /*
  6966. * conn_prepare_key_update installs new updated keys.
  6967. */
  6968. static int conn_prepare_key_update(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  6969. int rv;
  6970. ngtcp2_tstamp confirmed_ts = conn->crypto.key_update.confirmed_ts;
  6971. ngtcp2_duration pto = conn_compute_pto(conn, &conn->pktns);
  6972. ngtcp2_pktns *pktns = &conn->pktns;
  6973. ngtcp2_crypto_km *rx_ckm = pktns->crypto.rx.ckm;
  6974. ngtcp2_crypto_km *tx_ckm = pktns->crypto.tx.ckm;
  6975. ngtcp2_crypto_km *new_rx_ckm, *new_tx_ckm;
  6976. ngtcp2_crypto_aead_ctx rx_aead_ctx = {0}, tx_aead_ctx = {0};
  6977. size_t secretlen, ivlen;
  6978. if ((conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED) &&
  6979. tx_ckm->use_count >= pktns->crypto.ctx.max_encryption &&
  6980. conn_initiate_key_update(conn, ts) != 0) {
  6981. return NGTCP2_ERR_AEAD_LIMIT_REACHED;
  6982. }
  6983. if ((conn->flags & NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED) ||
  6984. ngtcp2_tstamp_not_elapsed(confirmed_ts, pto, ts)) {
  6985. return 0;
  6986. }
  6987. if (conn->crypto.key_update.new_rx_ckm ||
  6988. conn->crypto.key_update.new_tx_ckm) {
  6989. assert(conn->crypto.key_update.new_rx_ckm);
  6990. assert(conn->crypto.key_update.new_tx_ckm);
  6991. return 0;
  6992. }
  6993. secretlen = rx_ckm->secret.len;
  6994. ivlen = rx_ckm->iv.len;
  6995. rv = ngtcp2_crypto_km_nocopy_new(&conn->crypto.key_update.new_rx_ckm,
  6996. secretlen, ivlen, conn->mem);
  6997. if (rv != 0) {
  6998. return rv;
  6999. }
  7000. rv = ngtcp2_crypto_km_nocopy_new(&conn->crypto.key_update.new_tx_ckm,
  7001. secretlen, ivlen, conn->mem);
  7002. if (rv != 0) {
  7003. return rv;
  7004. }
  7005. new_rx_ckm = conn->crypto.key_update.new_rx_ckm;
  7006. new_tx_ckm = conn->crypto.key_update.new_tx_ckm;
  7007. rv = conn_call_update_key(
  7008. conn, new_rx_ckm->secret.base, new_tx_ckm->secret.base, &rx_aead_ctx,
  7009. new_rx_ckm->iv.base, &tx_aead_ctx, new_tx_ckm->iv.base, rx_ckm->secret.base,
  7010. tx_ckm->secret.base, secretlen);
  7011. if (rv != 0) {
  7012. return rv;
  7013. }
  7014. new_rx_ckm->aead_ctx = rx_aead_ctx;
  7015. new_tx_ckm->aead_ctx = tx_aead_ctx;
  7016. if (!(rx_ckm->flags & NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE)) {
  7017. new_rx_ckm->flags |= NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE;
  7018. new_tx_ckm->flags |= NGTCP2_CRYPTO_KM_FLAG_KEY_PHASE_ONE;
  7019. }
  7020. if (conn->crypto.key_update.old_rx_ckm) {
  7021. conn_call_delete_crypto_aead_ctx(
  7022. conn, &conn->crypto.key_update.old_rx_ckm->aead_ctx);
  7023. ngtcp2_crypto_km_del(conn->crypto.key_update.old_rx_ckm, conn->mem);
  7024. conn->crypto.key_update.old_rx_ckm = NULL;
  7025. }
  7026. return 0;
  7027. }
  7028. /*
  7029. * conn_rotate_keys rotates keys. The current key moves to old key,
  7030. * and new key moves to the current key. If the local endpoint
  7031. * initiated this key update, pass nonzero as |initiator|.
  7032. */
  7033. static void conn_rotate_keys(ngtcp2_conn *conn, int64_t pkt_num,
  7034. int initiator) {
  7035. ngtcp2_pktns *pktns = &conn->pktns;
  7036. assert(conn->crypto.key_update.new_rx_ckm);
  7037. assert(conn->crypto.key_update.new_tx_ckm);
  7038. assert(!conn->crypto.key_update.old_rx_ckm);
  7039. assert(!(conn->flags & NGTCP2_CONN_FLAG_PPE_PENDING));
  7040. conn->crypto.key_update.old_rx_ckm = pktns->crypto.rx.ckm;
  7041. pktns->crypto.rx.ckm = conn->crypto.key_update.new_rx_ckm;
  7042. conn->crypto.key_update.new_rx_ckm = NULL;
  7043. pktns->crypto.rx.ckm->pkt_num = pkt_num;
  7044. assert(pktns->crypto.tx.ckm);
  7045. conn_call_delete_crypto_aead_ctx(conn, &pktns->crypto.tx.ckm->aead_ctx);
  7046. ngtcp2_crypto_km_del(pktns->crypto.tx.ckm, conn->mem);
  7047. pktns->crypto.tx.ckm = conn->crypto.key_update.new_tx_ckm;
  7048. conn->crypto.key_update.new_tx_ckm = NULL;
  7049. pktns->crypto.tx.ckm->pkt_num = pktns->tx.last_pkt_num + 1;
  7050. conn->flags |= NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED;
  7051. if (initiator) {
  7052. conn->flags |= NGTCP2_CONN_FLAG_KEY_UPDATE_INITIATOR;
  7053. }
  7054. }
  7055. /*
  7056. * conn_path_validation_in_progress returns nonzero if path validation
  7057. * against |path| is underway.
  7058. */
  7059. static int conn_path_validation_in_progress(ngtcp2_conn *conn,
  7060. const ngtcp2_path *path) {
  7061. ngtcp2_pv *pv = conn->pv;
  7062. return pv && ngtcp2_path_eq(&pv->dcid.ps.path, path);
  7063. }
  7064. /*
  7065. * conn_recv_non_probing_pkt_on_new_path is called when non-probing
  7066. * packet is received via new path. It starts path validation against
  7067. * the new path.
  7068. *
  7069. * This function returns 0 if it succeeds, or one of the following
  7070. * negative error codes:
  7071. *
  7072. * NGTCP2_ERR_CONN_ID_BLOCKED
  7073. * No DCID is available
  7074. * NGTCP2_ERR_NOMEM
  7075. * Out of memory
  7076. */
  7077. static int conn_recv_non_probing_pkt_on_new_path(ngtcp2_conn *conn,
  7078. const ngtcp2_path *path,
  7079. size_t dgramlen,
  7080. int new_cid_used,
  7081. ngtcp2_tstamp ts) {
  7082. ngtcp2_dcid dcid, *bound_dcid, *last;
  7083. ngtcp2_pv *pv;
  7084. int rv;
  7085. ngtcp2_duration pto;
  7086. int require_new_cid;
  7087. int local_addr_eq;
  7088. uint32_t remote_addr_cmp;
  7089. size_t len, i;
  7090. assert(conn->server);
  7091. if (conn->pv && (conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  7092. ngtcp2_path_eq(&conn->pv->fallback_dcid.ps.path, path)) {
  7093. /* If new path equals fallback path, that means connection
  7094. migrated back to the original path. Fallback path is
  7095. considered to be validated. */
  7096. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PTV,
  7097. "path is migrated back to the original path");
  7098. ngtcp2_dcid_copy(&conn->dcid.current, &conn->pv->fallback_dcid);
  7099. conn_reset_congestion_state(conn, ts);
  7100. conn->dcid.current.bytes_recv += dgramlen;
  7101. conn_reset_ecn_validation_state(conn);
  7102. rv = conn_abort_pv(conn, ts);
  7103. if (rv != 0) {
  7104. return rv;
  7105. }
  7106. /* Run PMTUD just in case if it is prematurely aborted */
  7107. assert(!conn->pmtud);
  7108. return conn_start_pmtud(conn);
  7109. }
  7110. remote_addr_cmp =
  7111. ngtcp2_addr_compare(&conn->dcid.current.ps.path.remote, &path->remote);
  7112. local_addr_eq =
  7113. ngtcp2_addr_eq(&conn->dcid.current.ps.path.local, &path->local);
  7114. /*
  7115. * When to change DCID? RFC 9002 section 9.5 says:
  7116. *
  7117. * An endpoint MUST NOT reuse a connection ID when sending from more
  7118. * than one local address -- for example, when initiating connection
  7119. * migration as described in Section 9.2 or when probing a new
  7120. * network path as described in Section 9.1.
  7121. *
  7122. * Similarly, an endpoint MUST NOT reuse a connection ID when
  7123. * sending to more than one destination address. Due to network
  7124. * changes outside the control of its peer, an endpoint might
  7125. * receive packets from a new source address with the same
  7126. * Destination Connection ID field value, in which case it MAY
  7127. * continue to use the current connection ID with the new remote
  7128. * address while still sending from the same local address.
  7129. */
  7130. require_new_cid = conn->dcid.current.cid.datalen &&
  7131. ((new_cid_used && remote_addr_cmp) || !local_addr_eq);
  7132. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  7133. "non-probing packet was received from new remote address");
  7134. len = ngtcp2_ringbuf_len(&conn->dcid.bound.rb);
  7135. for (i = 0; i < len; ++i) {
  7136. bound_dcid = ngtcp2_ringbuf_get(&conn->dcid.bound.rb, i);
  7137. if (ngtcp2_path_eq(&bound_dcid->ps.path, path)) {
  7138. ngtcp2_log_info(
  7139. &conn->log, NGTCP2_LOG_EVENT_CON,
  7140. "Found DCID which has already been bound to the new path");
  7141. ngtcp2_dcid_copy(&dcid, bound_dcid);
  7142. if (i == 0) {
  7143. ngtcp2_ringbuf_pop_front(&conn->dcid.bound.rb);
  7144. } else if (i == ngtcp2_ringbuf_len(&conn->dcid.bound.rb) - 1) {
  7145. ngtcp2_ringbuf_pop_back(&conn->dcid.bound.rb);
  7146. } else {
  7147. last = ngtcp2_ringbuf_get(&conn->dcid.bound.rb, len - 1);
  7148. ngtcp2_dcid_copy(bound_dcid, last);
  7149. ngtcp2_ringbuf_pop_back(&conn->dcid.bound.rb);
  7150. }
  7151. require_new_cid = 0;
  7152. if (dcid.cid.datalen) {
  7153. rv = conn_call_activate_dcid(conn, &dcid);
  7154. if (rv != 0) {
  7155. return rv;
  7156. }
  7157. }
  7158. break;
  7159. }
  7160. }
  7161. if (i == len) {
  7162. if (require_new_cid) {
  7163. if (ngtcp2_ringbuf_len(&conn->dcid.unused.rb) == 0) {
  7164. return NGTCP2_ERR_CONN_ID_BLOCKED;
  7165. }
  7166. ngtcp2_dcid_copy(&dcid, ngtcp2_ringbuf_get(&conn->dcid.unused.rb, 0));
  7167. ngtcp2_ringbuf_pop_front(&conn->dcid.unused.rb);
  7168. rv = conn_call_activate_dcid(conn, &dcid);
  7169. if (rv != 0) {
  7170. return rv;
  7171. }
  7172. } else {
  7173. /* Use the current DCID if a remote endpoint does not change
  7174. DCID. */
  7175. ngtcp2_dcid_copy(&dcid, &conn->dcid.current);
  7176. dcid.bytes_sent = 0;
  7177. dcid.bytes_recv = 0;
  7178. dcid.flags &= (uint8_t)~NGTCP2_DCID_FLAG_PATH_VALIDATED;
  7179. }
  7180. ngtcp2_dcid_set_path(&dcid, path);
  7181. }
  7182. dcid.bytes_recv += dgramlen;
  7183. pto = conn_compute_pto(conn, &conn->pktns);
  7184. rv = ngtcp2_pv_new(&pv, &dcid, conn_compute_pv_timeout_pto(conn, pto),
  7185. NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE, &conn->log, conn->mem);
  7186. if (rv != 0) {
  7187. return rv;
  7188. }
  7189. if (conn->pv && (conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE)) {
  7190. ngtcp2_dcid_copy(&pv->fallback_dcid, &conn->pv->fallback_dcid);
  7191. pv->fallback_pto = conn->pv->fallback_pto;
  7192. /* Unset the flag bit so that conn_stop_pv does not retire
  7193. DCID. */
  7194. conn->pv->flags &= (uint8_t)~NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE;
  7195. } else {
  7196. ngtcp2_dcid_copy(&pv->fallback_dcid, &conn->dcid.current);
  7197. pv->fallback_pto = pto;
  7198. }
  7199. ngtcp2_dcid_copy(&conn->dcid.current, &dcid);
  7200. if (!local_addr_eq || (remote_addr_cmp & (NGTCP2_ADDR_COMPARE_FLAG_ADDR |
  7201. NGTCP2_ADDR_COMPARE_FLAG_FAMILY))) {
  7202. conn_reset_congestion_state(conn, ts);
  7203. }
  7204. conn_reset_ecn_validation_state(conn);
  7205. ngtcp2_conn_stop_pmtud(conn);
  7206. if (conn->pv) {
  7207. ngtcp2_log_info(
  7208. &conn->log, NGTCP2_LOG_EVENT_PTV,
  7209. "path migration is aborted because new migration has started");
  7210. rv = conn_abort_pv(conn, ts);
  7211. if (rv != 0) {
  7212. return rv;
  7213. }
  7214. }
  7215. conn->pv = pv;
  7216. return 0;
  7217. }
  7218. /*
  7219. * conn_recv_pkt_from_new_path is called when a 1RTT packet is
  7220. * received from new path (not current path). This packet would be a
  7221. * packet which only contains probing frame, or reordered packet, or a
  7222. * path is being validated.
  7223. *
  7224. * This function returns 0 if it succeeds, or one of the following
  7225. * negative error codes:
  7226. *
  7227. * NGTCP2_ERR_CONN_ID_BLOCKED
  7228. * No unused DCID is available
  7229. * NGTCP2_ERR_NOMEM
  7230. * Out of memory
  7231. */
  7232. static int conn_recv_pkt_from_new_path(ngtcp2_conn *conn,
  7233. const ngtcp2_path *path, size_t dgramlen,
  7234. int path_challenge_recved,
  7235. ngtcp2_tstamp ts) {
  7236. ngtcp2_pv *pv = conn->pv;
  7237. ngtcp2_dcid *bound_dcid;
  7238. int rv;
  7239. if (pv) {
  7240. if (ngtcp2_path_eq(&pv->dcid.ps.path, path)) {
  7241. pv->dcid.bytes_recv += dgramlen;
  7242. return 0;
  7243. }
  7244. if ((pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  7245. ngtcp2_path_eq(&pv->fallback_dcid.ps.path, path)) {
  7246. pv->fallback_dcid.bytes_recv += dgramlen;
  7247. return 0;
  7248. }
  7249. }
  7250. if (!path_challenge_recved) {
  7251. return 0;
  7252. }
  7253. rv = conn_bind_dcid(conn, &bound_dcid, path, ts);
  7254. if (rv != 0) {
  7255. return rv;
  7256. }
  7257. ngtcp2_dcid_set_path(bound_dcid, path);
  7258. bound_dcid->bytes_recv += dgramlen;
  7259. return 0;
  7260. }
  7261. /*
  7262. * conn_recv_delayed_handshake_pkt processes the received Handshake
  7263. * packet which is received after handshake completed. This function
  7264. * does the minimal job, and its purpose is send acknowledgement of
  7265. * this packet to the peer. We assume that hd->type ==
  7266. * NGTCP2_PKT_HANDSHAKE.
  7267. *
  7268. * This function returns 0 if it succeeds, or one of the following
  7269. * negative error codes:
  7270. *
  7271. * NGTCP2_ERR_FRAME_ENCODING
  7272. * Frame is badly formatted; or frame type is unknown.
  7273. * NGTCP2_ERR_NOMEM
  7274. * Out of memory
  7275. * NGTCP2_ERR_DISCARD_PKT
  7276. * Packet was discarded.
  7277. * NGTCP2_ERR_ACK_FRAME
  7278. * ACK frame is malformed.
  7279. * NGTCP2_ERR_PROTO
  7280. * Frame that is not allowed in Handshake packet is received.
  7281. */
  7282. static int
  7283. conn_recv_delayed_handshake_pkt(ngtcp2_conn *conn, const ngtcp2_pkt_info *pi,
  7284. const ngtcp2_pkt_hd *hd, size_t pktlen,
  7285. const uint8_t *payload, size_t payloadlen,
  7286. ngtcp2_tstamp pkt_ts, ngtcp2_tstamp ts) {
  7287. ngtcp2_ssize nread;
  7288. ngtcp2_max_frame mfr;
  7289. ngtcp2_frame *fr = &mfr.fr;
  7290. int rv;
  7291. int require_ack = 0;
  7292. ngtcp2_pktns *pktns;
  7293. size_t num_ack_processed = 0;
  7294. assert(hd->type == NGTCP2_PKT_HANDSHAKE);
  7295. pktns = conn->hs_pktns;
  7296. if (payloadlen == 0) {
  7297. /* QUIC packet must contain at least one frame */
  7298. return NGTCP2_ERR_PROTO;
  7299. }
  7300. ngtcp2_qlog_pkt_received_start(&conn->qlog);
  7301. for (; payloadlen;) {
  7302. nread = ngtcp2_pkt_decode_frame(fr, payload, payloadlen);
  7303. if (nread < 0) {
  7304. return (int)nread;
  7305. }
  7306. payload += nread;
  7307. payloadlen -= (size_t)nread;
  7308. switch (fr->type) {
  7309. case NGTCP2_FRAME_ACK:
  7310. case NGTCP2_FRAME_ACK_ECN:
  7311. fr->ack.ack_delay = 0;
  7312. fr->ack.ack_delay_unscaled = 0;
  7313. rv =
  7314. ngtcp2_pkt_validate_ack(&fr->ack, conn->local.settings.initial_pkt_num);
  7315. if (rv != 0) {
  7316. return rv;
  7317. }
  7318. break;
  7319. }
  7320. ngtcp2_log_rx_fr(&conn->log, hd, fr);
  7321. switch (fr->type) {
  7322. case NGTCP2_FRAME_ACK:
  7323. case NGTCP2_FRAME_ACK_ECN:
  7324. if (num_ack_processed >= NGTCP2_MAX_ACK_PER_PKT) {
  7325. break;
  7326. }
  7327. if (!conn->server) {
  7328. conn->flags |= NGTCP2_CONN_FLAG_SERVER_ADDR_VERIFIED;
  7329. }
  7330. rv = conn_recv_ack(conn, pktns, &fr->ack, pkt_ts, ts);
  7331. if (rv != 0) {
  7332. return rv;
  7333. }
  7334. ++num_ack_processed;
  7335. break;
  7336. case NGTCP2_FRAME_PADDING:
  7337. break;
  7338. case NGTCP2_FRAME_CONNECTION_CLOSE:
  7339. rv = conn_recv_connection_close(conn, &fr->connection_close);
  7340. if (rv != 0) {
  7341. return rv;
  7342. }
  7343. break;
  7344. case NGTCP2_FRAME_CRYPTO:
  7345. case NGTCP2_FRAME_PING:
  7346. require_ack = 1;
  7347. break;
  7348. default:
  7349. return NGTCP2_ERR_PROTO;
  7350. }
  7351. ngtcp2_qlog_write_frame(&conn->qlog, fr);
  7352. }
  7353. ngtcp2_qlog_pkt_received_end(&conn->qlog, hd, pktlen);
  7354. rv = pktns_commit_recv_pkt_num(pktns, hd->pkt_num, require_ack, pkt_ts);
  7355. if (rv != 0) {
  7356. return rv;
  7357. }
  7358. pktns_increase_ecn_counts(pktns, pi);
  7359. /* Initial and Handshake are always acknowledged without delay. No
  7360. need to call ngtcp2_acktr_immediate_ack(). */
  7361. rv = ngtcp2_conn_sched_ack(conn, &pktns->acktr, hd->pkt_num, require_ack,
  7362. pkt_ts);
  7363. if (rv != 0) {
  7364. return rv;
  7365. }
  7366. conn_restart_timer_on_read(conn, ts);
  7367. ngtcp2_qlog_metrics_updated(&conn->qlog, &conn->cstat);
  7368. return 0;
  7369. }
  7370. /*
  7371. * conn_allow_path_change_under_disable_active_migration returns
  7372. * nonzero if a packet from |path| is acceptable under
  7373. * disable_active_migration is on.
  7374. */
  7375. static int
  7376. conn_allow_path_change_under_disable_active_migration(ngtcp2_conn *conn,
  7377. const ngtcp2_path *path) {
  7378. uint32_t remote_addr_cmp;
  7379. const ngtcp2_preferred_addr *paddr;
  7380. ngtcp2_addr addr;
  7381. assert(conn->server);
  7382. assert(conn->local.transport_params.disable_active_migration);
  7383. /* If local address does not change, it must be passive migration
  7384. (NAT rebinding). */
  7385. if (ngtcp2_addr_eq(&conn->dcid.current.ps.path.local, &path->local)) {
  7386. remote_addr_cmp =
  7387. ngtcp2_addr_compare(&conn->dcid.current.ps.path.remote, &path->remote);
  7388. return (remote_addr_cmp | NGTCP2_ADDR_COMPARE_FLAG_PORT) ==
  7389. NGTCP2_ADDR_COMPARE_FLAG_PORT;
  7390. }
  7391. /* If local address changes, it must be one of the preferred
  7392. addresses. */
  7393. if (!conn->local.transport_params.preferred_addr_present) {
  7394. return 0;
  7395. }
  7396. paddr = &conn->local.transport_params.preferred_addr;
  7397. if (paddr->ipv4_present) {
  7398. ngtcp2_addr_init(&addr, (const ngtcp2_sockaddr *)&paddr->ipv4,
  7399. sizeof(paddr->ipv4));
  7400. if (ngtcp2_addr_eq(&addr, &path->local)) {
  7401. return 1;
  7402. }
  7403. }
  7404. if (paddr->ipv6_present) {
  7405. ngtcp2_addr_init(&addr, (const ngtcp2_sockaddr *)&paddr->ipv6,
  7406. sizeof(paddr->ipv6));
  7407. if (ngtcp2_addr_eq(&addr, &path->local)) {
  7408. return 1;
  7409. }
  7410. }
  7411. return 0;
  7412. }
  7413. /*
  7414. * conn_recv_pkt processes a packet contained in the buffer pointed by
  7415. * |pkt| of length |pktlen|. |pkt| may contain multiple QUIC packets.
  7416. * This function only processes the first packet. |pkt_ts| is the
  7417. * timestamp when packet is received. |ts| should be the current
  7418. * time. Usually they are the same, but for buffered packets,
  7419. * |pkt_ts| would be earlier than |ts|.
  7420. *
  7421. * This function returns the number of bytes processed if it succeeds,
  7422. * or one of the following negative error codes:
  7423. *
  7424. * NGTCP2_ERR_DISCARD_PKT
  7425. * Packet was discarded because plain text header was malformed;
  7426. * or its payload could not be decrypted.
  7427. * NGTCP2_ERR_PROTO
  7428. * Packet is badly formatted; or 0RTT packet contains other than
  7429. * PADDING or STREAM frames; or other QUIC protocol violation is
  7430. * found.
  7431. * NGTCP2_ERR_CALLBACK_FAILURE
  7432. * User-defined callback function failed.
  7433. * NGTCP2_ERR_NOMEM
  7434. * Out of memory.
  7435. * NGTCP2_ERR_FRAME_ENCODING
  7436. * Frame is badly formatted; or frame type is unknown.
  7437. * NGTCP2_ERR_ACK_FRAME
  7438. * ACK frame is malformed.
  7439. * NGTCP2_ERR_STREAM_STATE
  7440. * Frame is received to the local stream which is not initiated.
  7441. * NGTCP2_ERR_STREAM_LIMIT
  7442. * Frame has remote stream ID which is strictly greater than the
  7443. * allowed limit.
  7444. * NGTCP2_ERR_FLOW_CONTROL
  7445. * Flow control limit is violated.
  7446. * NGTCP2_ERR_FINAL_SIZE
  7447. * Frame has strictly larger end offset than it is permitted.
  7448. */
  7449. static ngtcp2_ssize conn_recv_pkt(ngtcp2_conn *conn, const ngtcp2_path *path,
  7450. const ngtcp2_pkt_info *pi, const uint8_t *pkt,
  7451. size_t pktlen, size_t dgramlen,
  7452. ngtcp2_tstamp pkt_ts, ngtcp2_tstamp ts) {
  7453. ngtcp2_pkt_hd hd;
  7454. int rv = 0;
  7455. size_t hdpktlen;
  7456. const uint8_t *payload;
  7457. size_t payloadlen;
  7458. ngtcp2_ssize nread, nwrite;
  7459. ngtcp2_max_frame mfr;
  7460. ngtcp2_frame *fr = &mfr.fr;
  7461. int require_ack = 0;
  7462. ngtcp2_crypto_aead *aead;
  7463. ngtcp2_crypto_cipher *hp;
  7464. ngtcp2_crypto_km *ckm;
  7465. ngtcp2_crypto_cipher_ctx *hp_ctx;
  7466. ngtcp2_hp_mask hp_mask;
  7467. ngtcp2_decrypt decrypt;
  7468. ngtcp2_pktns *pktns;
  7469. int non_probing_pkt = 0;
  7470. int key_phase_bit_changed = 0;
  7471. int force_decrypt_failure = 0;
  7472. int recv_ncid = 0;
  7473. int new_cid_used = 0;
  7474. int path_challenge_recved = 0;
  7475. size_t num_ack_processed = 0;
  7476. if (conn->server && conn->local.transport_params.disable_active_migration &&
  7477. !ngtcp2_path_eq(&conn->dcid.current.ps.path, path) &&
  7478. !conn_allow_path_change_under_disable_active_migration(conn, path)) {
  7479. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7480. "packet is discarded because active migration is disabled");
  7481. return NGTCP2_ERR_DISCARD_PKT;
  7482. }
  7483. if (pkt[0] & NGTCP2_HEADER_FORM_BIT) {
  7484. nread = ngtcp2_pkt_decode_hd_long(&hd, pkt, pktlen);
  7485. if (nread < 0) {
  7486. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7487. "could not decode long header");
  7488. return NGTCP2_ERR_DISCARD_PKT;
  7489. }
  7490. if (pktlen < (size_t)nread + hd.len) {
  7491. return NGTCP2_ERR_DISCARD_PKT;
  7492. }
  7493. assert(conn->negotiated_version);
  7494. if (hd.version != conn->client_chosen_version &&
  7495. hd.version != conn->negotiated_version) {
  7496. return NGTCP2_ERR_DISCARD_PKT;
  7497. }
  7498. if (conn_verify_fixed_bit(conn, &hd) != 0) {
  7499. return NGTCP2_ERR_DISCARD_PKT;
  7500. }
  7501. pktlen = (size_t)nread + hd.len;
  7502. /* Quoted from spec: if subsequent packets of those types include
  7503. a different Source Connection ID, they MUST be discarded. */
  7504. if (!ngtcp2_cid_eq(&conn->dcid.current.cid, &hd.scid)) {
  7505. ngtcp2_log_rx_pkt_hd(&conn->log, &hd);
  7506. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7507. "packet was ignored because of mismatched SCID");
  7508. return NGTCP2_ERR_DISCARD_PKT;
  7509. }
  7510. switch (hd.type) {
  7511. case NGTCP2_PKT_INITIAL:
  7512. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7513. "delayed Initial packet was discarded");
  7514. return (ngtcp2_ssize)pktlen;
  7515. case NGTCP2_PKT_HANDSHAKE:
  7516. if (hd.version != conn->negotiated_version) {
  7517. return NGTCP2_ERR_DISCARD_PKT;
  7518. }
  7519. if (!conn->hs_pktns) {
  7520. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7521. "delayed Handshake packet was discarded");
  7522. return (ngtcp2_ssize)pktlen;
  7523. }
  7524. pktns = conn->hs_pktns;
  7525. aead = &pktns->crypto.ctx.aead;
  7526. hp = &pktns->crypto.ctx.hp;
  7527. ckm = pktns->crypto.rx.ckm;
  7528. hp_ctx = &pktns->crypto.rx.hp_ctx;
  7529. hp_mask = conn->callbacks.hp_mask;
  7530. decrypt = conn->callbacks.decrypt;
  7531. break;
  7532. case NGTCP2_PKT_0RTT:
  7533. if (!conn->server || hd.version != conn->client_chosen_version) {
  7534. return NGTCP2_ERR_DISCARD_PKT;
  7535. }
  7536. if (!conn->early.ckm) {
  7537. return (ngtcp2_ssize)pktlen;
  7538. }
  7539. pktns = &conn->pktns;
  7540. aead = &conn->early.ctx.aead;
  7541. hp = &conn->early.ctx.hp;
  7542. ckm = conn->early.ckm;
  7543. hp_ctx = &conn->early.hp_ctx;
  7544. hp_mask = conn->callbacks.hp_mask;
  7545. decrypt = conn->callbacks.decrypt;
  7546. break;
  7547. default:
  7548. ngtcp2_log_rx_pkt_hd(&conn->log, &hd);
  7549. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7550. "packet type 0x%02x was ignored", hd.type);
  7551. return (ngtcp2_ssize)pktlen;
  7552. }
  7553. } else {
  7554. nread = ngtcp2_pkt_decode_hd_short(&hd, pkt, pktlen, conn->oscid.datalen);
  7555. if (nread < 0) {
  7556. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7557. "could not decode short header");
  7558. return NGTCP2_ERR_DISCARD_PKT;
  7559. }
  7560. if (conn_verify_fixed_bit(conn, &hd) != 0) {
  7561. return NGTCP2_ERR_DISCARD_PKT;
  7562. }
  7563. pktns = &conn->pktns;
  7564. aead = &pktns->crypto.ctx.aead;
  7565. hp = &pktns->crypto.ctx.hp;
  7566. ckm = pktns->crypto.rx.ckm;
  7567. hp_ctx = &pktns->crypto.rx.hp_ctx;
  7568. hp_mask = conn->callbacks.hp_mask;
  7569. decrypt = conn->callbacks.decrypt;
  7570. }
  7571. rv = conn_ensure_decrypt_hp_buffer(conn, (size_t)nread + 4);
  7572. if (rv != 0) {
  7573. return rv;
  7574. }
  7575. nwrite = decrypt_hp(&hd, conn->crypto.decrypt_hp_buf.base, hp, pkt, pktlen,
  7576. (size_t)nread, hp_ctx, hp_mask);
  7577. if (nwrite < 0) {
  7578. if (ngtcp2_err_is_fatal((int)nwrite)) {
  7579. return nwrite;
  7580. }
  7581. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7582. "could not decrypt packet number");
  7583. return NGTCP2_ERR_DISCARD_PKT;
  7584. }
  7585. hdpktlen = (size_t)nwrite;
  7586. payload = pkt + hdpktlen;
  7587. payloadlen = pktlen - hdpktlen;
  7588. hd.pkt_num =
  7589. ngtcp2_pkt_adjust_pkt_num(pktns->rx.max_pkt_num, hd.pkt_num, hd.pkt_numlen);
  7590. if (hd.pkt_num > NGTCP2_MAX_PKT_NUM) {
  7591. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7592. "pkn=%" PRId64 " is greater than maximum pkn", hd.pkt_num);
  7593. return NGTCP2_ERR_DISCARD_PKT;
  7594. }
  7595. ngtcp2_log_rx_pkt_hd(&conn->log, &hd);
  7596. if (hd.type == NGTCP2_PKT_1RTT) {
  7597. key_phase_bit_changed = conn_key_phase_changed(conn, &hd);
  7598. }
  7599. rv = conn_ensure_decrypt_buffer(conn, payloadlen);
  7600. if (rv != 0) {
  7601. return rv;
  7602. }
  7603. if (key_phase_bit_changed) {
  7604. assert(hd.type == NGTCP2_PKT_1RTT);
  7605. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT, "unexpected KEY_PHASE");
  7606. if (ckm->pkt_num > hd.pkt_num) {
  7607. if (conn->crypto.key_update.old_rx_ckm) {
  7608. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7609. "decrypting with old key");
  7610. ckm = conn->crypto.key_update.old_rx_ckm;
  7611. } else {
  7612. force_decrypt_failure = 1;
  7613. }
  7614. } else if (pktns->rx.max_pkt_num < hd.pkt_num) {
  7615. assert(ckm->pkt_num < hd.pkt_num);
  7616. if (!conn->crypto.key_update.new_rx_ckm) {
  7617. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7618. "new key is not available");
  7619. force_decrypt_failure = 1;
  7620. } else {
  7621. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7622. "decrypting with new key");
  7623. ckm = conn->crypto.key_update.new_rx_ckm;
  7624. }
  7625. } else {
  7626. force_decrypt_failure = 1;
  7627. }
  7628. }
  7629. nwrite = decrypt_pkt(conn->crypto.decrypt_buf.base, aead, payload, payloadlen,
  7630. conn->crypto.decrypt_hp_buf.base, hdpktlen, hd.pkt_num,
  7631. ckm, decrypt);
  7632. if (force_decrypt_failure) {
  7633. nwrite = NGTCP2_ERR_DECRYPT;
  7634. }
  7635. if (nwrite < 0) {
  7636. if (ngtcp2_err_is_fatal((int)nwrite)) {
  7637. return nwrite;
  7638. }
  7639. assert(NGTCP2_ERR_DECRYPT == nwrite);
  7640. if (hd.type == NGTCP2_PKT_1RTT &&
  7641. ++conn->crypto.decryption_failure_count >=
  7642. pktns->crypto.ctx.max_decryption_failure) {
  7643. return NGTCP2_ERR_AEAD_LIMIT_REACHED;
  7644. }
  7645. if (hd.flags & NGTCP2_PKT_FLAG_LONG_FORM) {
  7646. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7647. "could not decrypt packet payload");
  7648. return NGTCP2_ERR_DISCARD_PKT;
  7649. }
  7650. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7651. "could not decrypt packet payload");
  7652. return NGTCP2_ERR_DISCARD_PKT;
  7653. }
  7654. rv = ngtcp2_pkt_verify_reserved_bits(conn->crypto.decrypt_hp_buf.base[0]);
  7655. if (rv != 0) {
  7656. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7657. "packet has incorrect reserved bits");
  7658. return NGTCP2_ERR_PROTO;
  7659. }
  7660. if (pktns_pkt_num_is_duplicate(pktns, hd.pkt_num)) {
  7661. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7662. "packet was discarded because of duplicated packet number");
  7663. return NGTCP2_ERR_DISCARD_PKT;
  7664. }
  7665. payload = conn->crypto.decrypt_buf.base;
  7666. payloadlen = (size_t)nwrite;
  7667. if (payloadlen == 0) {
  7668. /* QUIC packet must contain at least one frame */
  7669. return NGTCP2_ERR_PROTO;
  7670. }
  7671. if (hd.flags & NGTCP2_PKT_FLAG_LONG_FORM) {
  7672. switch (hd.type) {
  7673. case NGTCP2_PKT_HANDSHAKE:
  7674. rv = conn_verify_dcid(conn, NULL, &hd);
  7675. if (rv != 0) {
  7676. if (ngtcp2_err_is_fatal(rv)) {
  7677. return rv;
  7678. }
  7679. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7680. "packet was ignored because of mismatched DCID");
  7681. return NGTCP2_ERR_DISCARD_PKT;
  7682. }
  7683. rv = conn_recv_delayed_handshake_pkt(conn, pi, &hd, pktlen, payload,
  7684. payloadlen, pkt_ts, ts);
  7685. if (rv < 0) {
  7686. return (ngtcp2_ssize)rv;
  7687. }
  7688. return (ngtcp2_ssize)pktlen;
  7689. case NGTCP2_PKT_0RTT:
  7690. if (!ngtcp2_cid_eq(&conn->rcid, &hd.dcid)) {
  7691. rv = conn_verify_dcid(conn, NULL, &hd);
  7692. if (rv != 0) {
  7693. if (ngtcp2_err_is_fatal(rv)) {
  7694. return rv;
  7695. }
  7696. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7697. "packet was ignored because of mismatched DCID");
  7698. return NGTCP2_ERR_DISCARD_PKT;
  7699. }
  7700. }
  7701. break;
  7702. default:
  7703. /* Unreachable */
  7704. ngtcp2_unreachable();
  7705. }
  7706. } else {
  7707. rv = conn_verify_dcid(conn, &new_cid_used, &hd);
  7708. if (rv != 0) {
  7709. if (ngtcp2_err_is_fatal(rv)) {
  7710. return rv;
  7711. }
  7712. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  7713. "packet was ignored because of mismatched DCID");
  7714. return NGTCP2_ERR_DISCARD_PKT;
  7715. }
  7716. }
  7717. ngtcp2_qlog_pkt_received_start(&conn->qlog);
  7718. for (; payloadlen;) {
  7719. nread = ngtcp2_pkt_decode_frame(fr, payload, payloadlen);
  7720. if (nread < 0) {
  7721. return nread;
  7722. }
  7723. payload += nread;
  7724. payloadlen -= (size_t)nread;
  7725. switch (fr->type) {
  7726. case NGTCP2_FRAME_ACK:
  7727. case NGTCP2_FRAME_ACK_ECN:
  7728. if ((hd.flags & NGTCP2_PKT_FLAG_LONG_FORM) &&
  7729. hd.type == NGTCP2_PKT_0RTT) {
  7730. return NGTCP2_ERR_PROTO;
  7731. }
  7732. assert(conn->remote.transport_params);
  7733. assign_recved_ack_delay_unscaled(
  7734. &fr->ack, conn->remote.transport_params->ack_delay_exponent);
  7735. rv =
  7736. ngtcp2_pkt_validate_ack(&fr->ack, conn->local.settings.initial_pkt_num);
  7737. if (rv != 0) {
  7738. return rv;
  7739. }
  7740. break;
  7741. }
  7742. ngtcp2_log_rx_fr(&conn->log, &hd, fr);
  7743. if (hd.type == NGTCP2_PKT_0RTT) {
  7744. switch (fr->type) {
  7745. case NGTCP2_FRAME_PADDING:
  7746. case NGTCP2_FRAME_PING:
  7747. case NGTCP2_FRAME_RESET_STREAM:
  7748. case NGTCP2_FRAME_STOP_SENDING:
  7749. case NGTCP2_FRAME_STREAM:
  7750. case NGTCP2_FRAME_MAX_DATA:
  7751. case NGTCP2_FRAME_MAX_STREAM_DATA:
  7752. case NGTCP2_FRAME_MAX_STREAMS_BIDI:
  7753. case NGTCP2_FRAME_MAX_STREAMS_UNI:
  7754. case NGTCP2_FRAME_DATA_BLOCKED:
  7755. case NGTCP2_FRAME_STREAM_DATA_BLOCKED:
  7756. case NGTCP2_FRAME_STREAMS_BLOCKED_BIDI:
  7757. case NGTCP2_FRAME_STREAMS_BLOCKED_UNI:
  7758. case NGTCP2_FRAME_NEW_CONNECTION_ID:
  7759. case NGTCP2_FRAME_PATH_CHALLENGE:
  7760. case NGTCP2_FRAME_CONNECTION_CLOSE:
  7761. case NGTCP2_FRAME_CONNECTION_CLOSE_APP:
  7762. case NGTCP2_FRAME_DATAGRAM:
  7763. case NGTCP2_FRAME_DATAGRAM_LEN:
  7764. break;
  7765. default:
  7766. return NGTCP2_ERR_PROTO;
  7767. }
  7768. }
  7769. switch (fr->type) {
  7770. case NGTCP2_FRAME_ACK:
  7771. case NGTCP2_FRAME_ACK_ECN:
  7772. case NGTCP2_FRAME_PADDING:
  7773. case NGTCP2_FRAME_CONNECTION_CLOSE:
  7774. case NGTCP2_FRAME_CONNECTION_CLOSE_APP:
  7775. break;
  7776. default:
  7777. require_ack = 1;
  7778. }
  7779. switch (fr->type) {
  7780. case NGTCP2_FRAME_ACK:
  7781. case NGTCP2_FRAME_ACK_ECN:
  7782. if (num_ack_processed >= NGTCP2_MAX_ACK_PER_PKT) {
  7783. break;
  7784. }
  7785. if (!conn->server) {
  7786. conn->flags |= NGTCP2_CONN_FLAG_SERVER_ADDR_VERIFIED;
  7787. }
  7788. rv = conn_recv_ack(conn, pktns, &fr->ack, pkt_ts, ts);
  7789. if (rv != 0) {
  7790. return rv;
  7791. }
  7792. non_probing_pkt = 1;
  7793. ++num_ack_processed;
  7794. break;
  7795. case NGTCP2_FRAME_STREAM:
  7796. rv = conn_recv_stream(conn, &fr->stream);
  7797. if (rv != 0) {
  7798. return rv;
  7799. }
  7800. non_probing_pkt = 1;
  7801. break;
  7802. case NGTCP2_FRAME_CRYPTO:
  7803. rv = conn_recv_crypto(conn, NGTCP2_ENCRYPTION_LEVEL_1RTT,
  7804. &pktns->crypto.strm, &fr->stream);
  7805. if (rv != 0) {
  7806. return rv;
  7807. }
  7808. non_probing_pkt = 1;
  7809. break;
  7810. case NGTCP2_FRAME_RESET_STREAM:
  7811. rv = conn_recv_reset_stream(conn, &fr->reset_stream);
  7812. if (rv != 0) {
  7813. return rv;
  7814. }
  7815. non_probing_pkt = 1;
  7816. break;
  7817. case NGTCP2_FRAME_STOP_SENDING:
  7818. rv = conn_recv_stop_sending(conn, &fr->stop_sending);
  7819. if (rv != 0) {
  7820. return rv;
  7821. }
  7822. non_probing_pkt = 1;
  7823. break;
  7824. case NGTCP2_FRAME_MAX_STREAM_DATA:
  7825. rv = conn_recv_max_stream_data(conn, &fr->max_stream_data);
  7826. if (rv != 0) {
  7827. return rv;
  7828. }
  7829. non_probing_pkt = 1;
  7830. break;
  7831. case NGTCP2_FRAME_MAX_DATA:
  7832. conn_recv_max_data(conn, &fr->max_data);
  7833. non_probing_pkt = 1;
  7834. break;
  7835. case NGTCP2_FRAME_MAX_STREAMS_BIDI:
  7836. case NGTCP2_FRAME_MAX_STREAMS_UNI:
  7837. rv = conn_recv_max_streams(conn, &fr->max_streams);
  7838. if (rv != 0) {
  7839. return rv;
  7840. }
  7841. non_probing_pkt = 1;
  7842. break;
  7843. case NGTCP2_FRAME_CONNECTION_CLOSE:
  7844. case NGTCP2_FRAME_CONNECTION_CLOSE_APP:
  7845. rv = conn_recv_connection_close(conn, &fr->connection_close);
  7846. if (rv != 0) {
  7847. return rv;
  7848. }
  7849. break;
  7850. case NGTCP2_FRAME_PING:
  7851. non_probing_pkt = 1;
  7852. break;
  7853. case NGTCP2_FRAME_PATH_CHALLENGE:
  7854. conn_recv_path_challenge(conn, path, &fr->path_challenge);
  7855. path_challenge_recved = 1;
  7856. break;
  7857. case NGTCP2_FRAME_PATH_RESPONSE:
  7858. rv = conn_recv_path_response(conn, &fr->path_response, ts);
  7859. if (rv != 0) {
  7860. return rv;
  7861. }
  7862. break;
  7863. case NGTCP2_FRAME_NEW_CONNECTION_ID:
  7864. rv = conn_recv_new_connection_id(conn, &fr->new_connection_id);
  7865. if (rv != 0) {
  7866. return rv;
  7867. }
  7868. recv_ncid = 1;
  7869. break;
  7870. case NGTCP2_FRAME_RETIRE_CONNECTION_ID:
  7871. rv = conn_recv_retire_connection_id(conn, &hd, &fr->retire_connection_id,
  7872. ts);
  7873. if (rv != 0) {
  7874. return rv;
  7875. }
  7876. non_probing_pkt = 1;
  7877. break;
  7878. case NGTCP2_FRAME_NEW_TOKEN:
  7879. rv = conn_recv_new_token(conn, &fr->new_token);
  7880. if (rv != 0) {
  7881. return rv;
  7882. }
  7883. non_probing_pkt = 1;
  7884. break;
  7885. case NGTCP2_FRAME_HANDSHAKE_DONE:
  7886. rv = conn_recv_handshake_done(conn, ts);
  7887. if (rv != 0) {
  7888. return rv;
  7889. }
  7890. non_probing_pkt = 1;
  7891. break;
  7892. case NGTCP2_FRAME_STREAMS_BLOCKED_BIDI:
  7893. rv = conn_recv_streams_blocked_bidi(conn, &fr->streams_blocked);
  7894. if (rv != 0) {
  7895. return rv;
  7896. }
  7897. non_probing_pkt = 1;
  7898. break;
  7899. case NGTCP2_FRAME_STREAMS_BLOCKED_UNI:
  7900. rv = conn_recv_streams_blocked_uni(conn, &fr->streams_blocked);
  7901. if (rv != 0) {
  7902. return rv;
  7903. }
  7904. non_probing_pkt = 1;
  7905. break;
  7906. case NGTCP2_FRAME_STREAM_DATA_BLOCKED:
  7907. rv = conn_recv_stream_data_blocked(conn, &fr->stream_data_blocked);
  7908. if (rv != 0) {
  7909. return rv;
  7910. }
  7911. non_probing_pkt = 1;
  7912. break;
  7913. case NGTCP2_FRAME_DATA_BLOCKED:
  7914. rv = conn_recv_data_blocked(conn, &fr->data_blocked);
  7915. if (rv != 0) {
  7916. return rv;
  7917. }
  7918. non_probing_pkt = 1;
  7919. break;
  7920. case NGTCP2_FRAME_DATAGRAM:
  7921. case NGTCP2_FRAME_DATAGRAM_LEN:
  7922. if ((uint64_t)nread >
  7923. conn->local.transport_params.max_datagram_frame_size) {
  7924. return NGTCP2_ERR_PROTO;
  7925. }
  7926. rv = conn_recv_datagram(conn, &fr->datagram);
  7927. if (rv != 0) {
  7928. return rv;
  7929. }
  7930. non_probing_pkt = 1;
  7931. break;
  7932. }
  7933. ngtcp2_qlog_write_frame(&conn->qlog, fr);
  7934. }
  7935. ngtcp2_qlog_pkt_received_end(&conn->qlog, &hd, pktlen);
  7936. if (recv_ncid) {
  7937. rv = conn_post_process_recv_new_connection_id(conn, ts);
  7938. if (rv != 0) {
  7939. return rv;
  7940. }
  7941. }
  7942. if (conn->server && hd.type == NGTCP2_PKT_1RTT &&
  7943. !ngtcp2_path_eq(&conn->dcid.current.ps.path, path)) {
  7944. if (non_probing_pkt && pktns->rx.max_pkt_num < hd.pkt_num &&
  7945. !conn_path_validation_in_progress(conn, path)) {
  7946. rv = conn_recv_non_probing_pkt_on_new_path(conn, path, dgramlen,
  7947. new_cid_used, ts);
  7948. if (rv != 0) {
  7949. if (ngtcp2_err_is_fatal(rv)) {
  7950. return rv;
  7951. }
  7952. /* DCID is not available. Just continue. */
  7953. assert(NGTCP2_ERR_CONN_ID_BLOCKED == rv);
  7954. }
  7955. } else {
  7956. rv = conn_recv_pkt_from_new_path(conn, path, dgramlen,
  7957. path_challenge_recved, ts);
  7958. if (rv != 0) {
  7959. if (ngtcp2_err_is_fatal(rv)) {
  7960. return rv;
  7961. }
  7962. /* DCID is not available. Just continue. */
  7963. assert(NGTCP2_ERR_CONN_ID_BLOCKED == rv);
  7964. }
  7965. }
  7966. }
  7967. if (hd.type == NGTCP2_PKT_1RTT) {
  7968. if (ckm == conn->crypto.key_update.new_rx_ckm) {
  7969. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON, "rotate keys");
  7970. conn_rotate_keys(conn, hd.pkt_num, /* initiator = */ 0);
  7971. } else if (ckm->pkt_num > hd.pkt_num) {
  7972. ckm->pkt_num = hd.pkt_num;
  7973. }
  7974. if (conn->server && conn->early.ckm &&
  7975. conn->early.discard_started_ts == UINT64_MAX) {
  7976. conn->early.discard_started_ts = ts;
  7977. }
  7978. if (ngtcp2_path_eq(&conn->dcid.current.ps.path, path)) {
  7979. conn_update_keep_alive_last_ts(conn, ts);
  7980. }
  7981. }
  7982. rv = pktns_commit_recv_pkt_num(pktns, hd.pkt_num, require_ack, pkt_ts);
  7983. if (rv != 0) {
  7984. return rv;
  7985. }
  7986. pktns_increase_ecn_counts(pktns, pi);
  7987. if (require_ack &&
  7988. (++pktns->acktr.rx_npkt >= conn->local.settings.ack_thresh ||
  7989. (pi->ecn & NGTCP2_ECN_MASK) == NGTCP2_ECN_CE)) {
  7990. ngtcp2_acktr_immediate_ack(&pktns->acktr);
  7991. }
  7992. rv =
  7993. ngtcp2_conn_sched_ack(conn, &pktns->acktr, hd.pkt_num, require_ack, pkt_ts);
  7994. if (rv != 0) {
  7995. return rv;
  7996. }
  7997. conn_restart_timer_on_read(conn, ts);
  7998. ngtcp2_qlog_metrics_updated(&conn->qlog, &conn->cstat);
  7999. return conn->state == NGTCP2_CS_DRAINING ? NGTCP2_ERR_DRAINING
  8000. : (ngtcp2_ssize)pktlen;
  8001. }
  8002. /*
  8003. * conn_process_buffered_protected_pkt processes buffered 0RTT or 1RTT
  8004. * packets.
  8005. *
  8006. * This function returns 0 if it succeeds, or the same negative error
  8007. * codes from conn_recv_pkt.
  8008. */
  8009. static int conn_process_buffered_protected_pkt(ngtcp2_conn *conn,
  8010. ngtcp2_pktns *pktns,
  8011. ngtcp2_tstamp ts) {
  8012. ngtcp2_ssize nread;
  8013. ngtcp2_pkt_chain **ppc, *next;
  8014. int rv;
  8015. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  8016. "processing buffered protected packet");
  8017. for (ppc = &pktns->rx.buffed_pkts; *ppc;) {
  8018. next = (*ppc)->next;
  8019. nread = conn_recv_pkt(conn, &(*ppc)->path.path, &(*ppc)->pi, (*ppc)->pkt,
  8020. (*ppc)->pktlen, (*ppc)->dgramlen, (*ppc)->ts, ts);
  8021. if (nread < 0 && !ngtcp2_err_is_fatal((int)nread) &&
  8022. nread != NGTCP2_ERR_DRAINING) {
  8023. /* TODO We don't know this is the first QUIC packet in a
  8024. datagram. */
  8025. rv = conn_on_stateless_reset(conn, &(*ppc)->path.path, (*ppc)->pkt,
  8026. (*ppc)->pktlen);
  8027. if (rv == 0) {
  8028. ngtcp2_pkt_chain_del(*ppc, conn->mem);
  8029. *ppc = next;
  8030. return NGTCP2_ERR_DRAINING;
  8031. }
  8032. }
  8033. ngtcp2_pkt_chain_del(*ppc, conn->mem);
  8034. *ppc = next;
  8035. if (nread < 0) {
  8036. if (nread == NGTCP2_ERR_DISCARD_PKT) {
  8037. continue;
  8038. }
  8039. return (int)nread;
  8040. }
  8041. }
  8042. return 0;
  8043. }
  8044. /*
  8045. * conn_process_buffered_handshake_pkt processes buffered Handshake
  8046. * packets.
  8047. *
  8048. * This function returns 0 if it succeeds, or the same negative error
  8049. * codes from conn_recv_handshake_pkt.
  8050. */
  8051. static int conn_process_buffered_handshake_pkt(ngtcp2_conn *conn,
  8052. ngtcp2_tstamp ts) {
  8053. ngtcp2_pktns *pktns = conn->hs_pktns;
  8054. ngtcp2_ssize nread;
  8055. ngtcp2_pkt_chain **ppc, *next;
  8056. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  8057. "processing buffered handshake packet");
  8058. for (ppc = &pktns->rx.buffed_pkts; *ppc;) {
  8059. next = (*ppc)->next;
  8060. nread = conn_recv_handshake_pkt(conn, &(*ppc)->path.path, &(*ppc)->pi,
  8061. (*ppc)->pkt, (*ppc)->pktlen,
  8062. (*ppc)->dgramlen, (*ppc)->ts, ts);
  8063. ngtcp2_pkt_chain_del(*ppc, conn->mem);
  8064. *ppc = next;
  8065. if (nread < 0) {
  8066. if (nread == NGTCP2_ERR_DISCARD_PKT) {
  8067. continue;
  8068. }
  8069. return (int)nread;
  8070. }
  8071. }
  8072. return 0;
  8073. }
  8074. static void conn_sync_stream_id_limit(ngtcp2_conn *conn) {
  8075. ngtcp2_transport_params *params = conn->remote.transport_params;
  8076. assert(params);
  8077. conn->local.bidi.max_streams = params->initial_max_streams_bidi;
  8078. conn->local.uni.max_streams = params->initial_max_streams_uni;
  8079. }
  8080. static int strm_set_max_offset(void *data, void *ptr) {
  8081. ngtcp2_conn *conn = ptr;
  8082. ngtcp2_transport_params *params = conn->remote.transport_params;
  8083. ngtcp2_strm *strm = data;
  8084. uint64_t max_offset;
  8085. int rv;
  8086. assert(params);
  8087. if (!conn_local_stream(conn, strm->stream_id)) {
  8088. return 0;
  8089. }
  8090. if (bidi_stream(strm->stream_id)) {
  8091. max_offset = params->initial_max_stream_data_bidi_remote;
  8092. } else {
  8093. max_offset = params->initial_max_stream_data_uni;
  8094. }
  8095. if (strm->tx.max_offset < max_offset) {
  8096. strm->tx.max_offset = max_offset;
  8097. /* Don't call callback if stream is half-closed local */
  8098. if (strm->flags & NGTCP2_STRM_FLAG_SHUT_WR) {
  8099. return 0;
  8100. }
  8101. rv = conn_call_extend_max_stream_data(conn, strm, strm->stream_id,
  8102. strm->tx.max_offset);
  8103. if (rv != 0) {
  8104. return rv;
  8105. }
  8106. }
  8107. return 0;
  8108. }
  8109. static int conn_sync_stream_data_limit(ngtcp2_conn *conn) {
  8110. return ngtcp2_map_each(&conn->strms, strm_set_max_offset, conn);
  8111. }
  8112. /*
  8113. * conn_handshake_completed is called once cryptographic handshake has
  8114. * completed.
  8115. *
  8116. * This function returns 0 if it succeeds, or one of the following
  8117. * negative error codes:
  8118. *
  8119. * NGTCP2_ERR_CALLBACK_FAILURE
  8120. * User callback failed.
  8121. */
  8122. static int conn_handshake_completed(ngtcp2_conn *conn) {
  8123. int rv;
  8124. conn->flags |= NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED;
  8125. rv = conn_call_handshake_completed(conn);
  8126. if (rv != 0) {
  8127. return rv;
  8128. }
  8129. if (conn->local.bidi.max_streams > 0) {
  8130. rv = conn_call_extend_max_local_streams_bidi(conn,
  8131. conn->local.bidi.max_streams);
  8132. if (rv != 0) {
  8133. return rv;
  8134. }
  8135. }
  8136. if (conn->local.uni.max_streams > 0) {
  8137. rv =
  8138. conn_call_extend_max_local_streams_uni(conn, conn->local.uni.max_streams);
  8139. if (rv != 0) {
  8140. return rv;
  8141. }
  8142. }
  8143. return 0;
  8144. }
  8145. /*
  8146. * conn_recv_cpkt processes compound packet after handshake. The
  8147. * buffer pointed by |pkt| might contain multiple packets. The 1RTT
  8148. * packet must be the last one because it does not have payload length
  8149. * field.
  8150. *
  8151. * This function returns 0 if it succeeds, or the same negative error
  8152. * codes from conn_recv_pkt except for NGTCP2_ERR_DISCARD_PKT.
  8153. */
  8154. static int conn_recv_cpkt(ngtcp2_conn *conn, const ngtcp2_path *path,
  8155. const ngtcp2_pkt_info *pi, const uint8_t *pkt,
  8156. size_t pktlen, ngtcp2_tstamp ts) {
  8157. ngtcp2_ssize nread;
  8158. int rv;
  8159. const uint8_t *origpkt = pkt;
  8160. size_t dgramlen = pktlen;
  8161. if (ngtcp2_path_eq(&conn->dcid.current.ps.path, path)) {
  8162. conn->dcid.current.bytes_recv += dgramlen;
  8163. }
  8164. while (pktlen) {
  8165. nread = conn_recv_pkt(conn, path, pi, pkt, pktlen, dgramlen, ts, ts);
  8166. if (nread < 0) {
  8167. if (ngtcp2_err_is_fatal((int)nread)) {
  8168. return (int)nread;
  8169. }
  8170. if (nread == NGTCP2_ERR_DRAINING) {
  8171. return NGTCP2_ERR_DRAINING;
  8172. }
  8173. if (origpkt == pkt) {
  8174. rv = conn_on_stateless_reset(conn, path, origpkt, dgramlen);
  8175. if (rv == 0) {
  8176. return NGTCP2_ERR_DRAINING;
  8177. }
  8178. }
  8179. if (nread == NGTCP2_ERR_DISCARD_PKT) {
  8180. return 0;
  8181. }
  8182. return (int)nread;
  8183. }
  8184. assert(pktlen >= (size_t)nread);
  8185. pkt += nread;
  8186. pktlen -= (size_t)nread;
  8187. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_PKT,
  8188. "read packet %td left %zu", nread, pktlen);
  8189. }
  8190. return 0;
  8191. }
  8192. /*
  8193. * conn_is_retired_path returns nonzero if |path| is included in
  8194. * retired path list.
  8195. */
  8196. static int conn_is_retired_path(ngtcp2_conn *conn, const ngtcp2_path *path) {
  8197. size_t i, len = ngtcp2_ringbuf_len(&conn->dcid.retired.rb);
  8198. ngtcp2_dcid *dcid;
  8199. for (i = 0; i < len; ++i) {
  8200. dcid = ngtcp2_ringbuf_get(&conn->dcid.retired.rb, i);
  8201. if (ngtcp2_path_eq(&dcid->ps.path, path)) {
  8202. return 1;
  8203. }
  8204. }
  8205. return 0;
  8206. }
  8207. /*
  8208. * conn_enqueue_handshake_done enqueues HANDSHAKE_DONE frame for
  8209. * transmission.
  8210. */
  8211. static int conn_enqueue_handshake_done(ngtcp2_conn *conn) {
  8212. ngtcp2_pktns *pktns = &conn->pktns;
  8213. ngtcp2_frame_chain *nfrc;
  8214. int rv;
  8215. assert(conn->server);
  8216. rv = ngtcp2_frame_chain_objalloc_new(&nfrc, &conn->frc_objalloc);
  8217. if (rv != 0) {
  8218. return rv;
  8219. }
  8220. nfrc->fr.type = NGTCP2_FRAME_HANDSHAKE_DONE;
  8221. nfrc->next = pktns->tx.frq;
  8222. pktns->tx.frq = nfrc;
  8223. return 0;
  8224. }
  8225. /**
  8226. * @function
  8227. *
  8228. * `conn_read_handshake` performs QUIC cryptographic handshake by
  8229. * reading given data. |pkt| points to the buffer to read and
  8230. * |pktlen| is the length of the buffer. |path| is the network path.
  8231. *
  8232. * This function returns the number of bytes processed. Unless the
  8233. * last packet is 1RTT packet and an application decryption key has
  8234. * been installed, it returns |pktlen| if it succeeds. If it finds
  8235. * 1RTT packet and an application decryption key has been installed,
  8236. * it returns the number of bytes just before 1RTT packet begins.
  8237. *
  8238. * This function returns the number of bytes processed if it succeeds,
  8239. * or one of the following negative error codes: (TBD).
  8240. */
  8241. static ngtcp2_ssize conn_read_handshake(ngtcp2_conn *conn,
  8242. const ngtcp2_path *path,
  8243. const ngtcp2_pkt_info *pi,
  8244. const uint8_t *pkt, size_t pktlen,
  8245. ngtcp2_tstamp ts) {
  8246. int rv;
  8247. ngtcp2_ssize nread;
  8248. switch (conn->state) {
  8249. case NGTCP2_CS_CLIENT_INITIAL:
  8250. /* TODO Better to log something when we ignore input */
  8251. return (ngtcp2_ssize)pktlen;
  8252. case NGTCP2_CS_CLIENT_WAIT_HANDSHAKE:
  8253. nread = conn_recv_handshake_cpkt(conn, path, pi, pkt, pktlen, ts);
  8254. if (nread < 0) {
  8255. return nread;
  8256. }
  8257. if (conn->state == NGTCP2_CS_CLIENT_INITIAL) {
  8258. /* Retry packet was received */
  8259. return (ngtcp2_ssize)pktlen;
  8260. }
  8261. assert(conn->hs_pktns);
  8262. if (conn->hs_pktns->crypto.rx.ckm && conn->in_pktns) {
  8263. rv = conn_process_buffered_handshake_pkt(conn, ts);
  8264. if (rv != 0) {
  8265. return rv;
  8266. }
  8267. }
  8268. if (conn_is_tls_handshake_completed(conn) &&
  8269. !(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED)) {
  8270. rv = conn_handshake_completed(conn);
  8271. if (rv != 0) {
  8272. return rv;
  8273. }
  8274. rv = conn_process_buffered_protected_pkt(conn, &conn->pktns, ts);
  8275. if (rv != 0) {
  8276. return rv;
  8277. }
  8278. }
  8279. return nread;
  8280. case NGTCP2_CS_SERVER_INITIAL:
  8281. nread = conn_recv_handshake_cpkt(conn, path, pi, pkt, pktlen, ts);
  8282. if (nread < 0) {
  8283. return nread;
  8284. }
  8285. /*
  8286. * Client Hello might not fit into single Initial packet (e.g.,
  8287. * resuming session with client authentication). If we get Client
  8288. * Initial which does not increase offset or it is 0RTT packet
  8289. * buffered, perform address validation in order to buffer
  8290. * validated data only.
  8291. */
  8292. if (ngtcp2_strm_rx_offset(&conn->in_pktns->crypto.strm) == 0) {
  8293. if (conn->in_pktns->crypto.strm.rx.rob &&
  8294. ngtcp2_rob_data_buffered(conn->in_pktns->crypto.strm.rx.rob)) {
  8295. /* Address has been validated with token */
  8296. if (conn->local.settings.tokenlen) {
  8297. return nread;
  8298. }
  8299. return NGTCP2_ERR_RETRY;
  8300. }
  8301. /* If CRYPTO frame is not processed, just drop connection. */
  8302. return NGTCP2_ERR_DROP_CONN;
  8303. }
  8304. /* Process re-ordered 0-RTT packets which arrived before Initial
  8305. packet. */
  8306. if (conn->early.ckm) {
  8307. assert(conn->in_pktns);
  8308. rv = conn_process_buffered_protected_pkt(conn, conn->in_pktns, ts);
  8309. if (rv != 0) {
  8310. return rv;
  8311. }
  8312. }
  8313. return nread;
  8314. case NGTCP2_CS_SERVER_WAIT_HANDSHAKE:
  8315. nread = conn_recv_handshake_cpkt(conn, path, pi, pkt, pktlen, ts);
  8316. if (nread < 0) {
  8317. return nread;
  8318. }
  8319. if (conn->hs_pktns->crypto.rx.ckm) {
  8320. rv = conn_process_buffered_handshake_pkt(conn, ts);
  8321. if (rv != 0) {
  8322. return rv;
  8323. }
  8324. }
  8325. if (conn->hs_pktns->rx.max_pkt_num != -1) {
  8326. ngtcp2_conn_discard_initial_state(conn, ts);
  8327. }
  8328. if (!conn_is_tls_handshake_completed(conn)) {
  8329. /* If server hits amplification limit, it cancels loss detection
  8330. timer. If server receives a packet from client, the limit is
  8331. increased and server can send more. If server has
  8332. ack-eliciting Initial or Handshake packets, it should resend
  8333. it if timer fired but timer is not armed in this case. So
  8334. instead of resending Initial/Handshake packets, if server has
  8335. 1RTT data to send, it might send them and then might hit
  8336. amplification limit again until it hits stream data limit.
  8337. Initial/Handshake data is not resent. In order to avoid this
  8338. situation, try to arm loss detection and check the expiry
  8339. here so that on next write call, we can resend
  8340. Initial/Handshake first. */
  8341. if (conn->cstat.loss_detection_timer == UINT64_MAX) {
  8342. ngtcp2_conn_set_loss_detection_timer(conn, ts);
  8343. if (ngtcp2_conn_loss_detection_expiry(conn) <= ts) {
  8344. rv = ngtcp2_conn_on_loss_detection_timer(conn, ts);
  8345. if (rv != 0) {
  8346. return rv;
  8347. }
  8348. }
  8349. }
  8350. if ((size_t)nread < pktlen) {
  8351. /* We have 1RTT packet and application rx key, but the
  8352. handshake has not completed yet. */
  8353. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  8354. "buffering 1RTT packet len=%zu",
  8355. pktlen - (size_t)nread);
  8356. rv = conn_buffer_pkt(conn, &conn->pktns, path, pi, pkt + nread,
  8357. pktlen - (size_t)nread, pktlen, ts);
  8358. if (rv != 0) {
  8359. assert(ngtcp2_err_is_fatal(rv));
  8360. return rv;
  8361. }
  8362. return (ngtcp2_ssize)pktlen;
  8363. }
  8364. return nread;
  8365. }
  8366. if (!(conn->flags & NGTCP2_CONN_FLAG_TRANSPORT_PARAM_RECVED)) {
  8367. return NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM;
  8368. }
  8369. rv = conn_handshake_completed(conn);
  8370. if (rv != 0) {
  8371. return rv;
  8372. }
  8373. conn->state = NGTCP2_CS_POST_HANDSHAKE;
  8374. rv = conn_call_activate_dcid(conn, &conn->dcid.current);
  8375. if (rv != 0) {
  8376. return rv;
  8377. }
  8378. rv = conn_process_buffered_protected_pkt(conn, &conn->pktns, ts);
  8379. if (rv != 0) {
  8380. return rv;
  8381. }
  8382. ngtcp2_conn_discard_handshake_state(conn, ts);
  8383. rv = conn_enqueue_handshake_done(conn);
  8384. if (rv != 0) {
  8385. return rv;
  8386. }
  8387. if (!conn->local.settings.no_pmtud) {
  8388. rv = conn_start_pmtud(conn);
  8389. if (rv != 0) {
  8390. return rv;
  8391. }
  8392. }
  8393. conn->pktns.rtb.persistent_congestion_start_ts = ts;
  8394. /* Re-arm loss detection timer here after handshake has been
  8395. confirmed. */
  8396. ngtcp2_conn_set_loss_detection_timer(conn, ts);
  8397. return nread;
  8398. case NGTCP2_CS_CLOSING:
  8399. return NGTCP2_ERR_CLOSING;
  8400. case NGTCP2_CS_DRAINING:
  8401. return NGTCP2_ERR_DRAINING;
  8402. default:
  8403. return (ngtcp2_ssize)pktlen;
  8404. }
  8405. }
  8406. int ngtcp2_conn_read_pkt_versioned(ngtcp2_conn *conn, const ngtcp2_path *path,
  8407. int pkt_info_version,
  8408. const ngtcp2_pkt_info *pi,
  8409. const uint8_t *pkt, size_t pktlen,
  8410. ngtcp2_tstamp ts) {
  8411. int rv = 0;
  8412. ngtcp2_ssize nread = 0;
  8413. const ngtcp2_pkt_info zero_pi = {0};
  8414. (void)pkt_info_version;
  8415. assert(!(conn->flags & NGTCP2_CONN_FLAG_PPE_PENDING));
  8416. conn_update_timestamp(conn, ts);
  8417. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON, "recv packet len=%zu",
  8418. pktlen);
  8419. if (pktlen == 0) {
  8420. return 0;
  8421. }
  8422. /* client does not expect a packet from unknown path. */
  8423. if (!conn->server && !ngtcp2_path_eq(&conn->dcid.current.ps.path, path) &&
  8424. (!conn->pv || !ngtcp2_path_eq(&conn->pv->dcid.ps.path, path)) &&
  8425. !conn_is_retired_path(conn, path)) {
  8426. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  8427. "ignore packet from unknown path");
  8428. return 0;
  8429. }
  8430. if (!pi) {
  8431. pi = &zero_pi;
  8432. }
  8433. switch (conn->state) {
  8434. case NGTCP2_CS_CLIENT_INITIAL:
  8435. case NGTCP2_CS_CLIENT_WAIT_HANDSHAKE:
  8436. nread = conn_read_handshake(conn, path, pi, pkt, pktlen, ts);
  8437. if (nread < 0) {
  8438. return (int)nread;
  8439. }
  8440. if ((size_t)nread == pktlen) {
  8441. return 0;
  8442. }
  8443. assert(conn->pktns.crypto.rx.ckm);
  8444. pkt += nread;
  8445. pktlen -= (size_t)nread;
  8446. break;
  8447. case NGTCP2_CS_SERVER_INITIAL:
  8448. case NGTCP2_CS_SERVER_WAIT_HANDSHAKE:
  8449. if (!ngtcp2_path_eq(&conn->dcid.current.ps.path, path)) {
  8450. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  8451. "ignore packet from unknown path during handshake");
  8452. if (conn->state == NGTCP2_CS_SERVER_INITIAL &&
  8453. ngtcp2_strm_rx_offset(&conn->in_pktns->crypto.strm) == 0 &&
  8454. (!conn->in_pktns->crypto.strm.rx.rob ||
  8455. !ngtcp2_rob_data_buffered(conn->in_pktns->crypto.strm.rx.rob))) {
  8456. return NGTCP2_ERR_DROP_CONN;
  8457. }
  8458. return 0;
  8459. }
  8460. nread = conn_read_handshake(conn, path, pi, pkt, pktlen, ts);
  8461. if (nread < 0) {
  8462. return (int)nread;
  8463. }
  8464. if ((size_t)nread == pktlen) {
  8465. return 0;
  8466. }
  8467. assert(conn->pktns.crypto.rx.ckm);
  8468. pkt += nread;
  8469. pktlen -= (size_t)nread;
  8470. break;
  8471. case NGTCP2_CS_CLOSING:
  8472. return NGTCP2_ERR_CLOSING;
  8473. case NGTCP2_CS_DRAINING:
  8474. return NGTCP2_ERR_DRAINING;
  8475. case NGTCP2_CS_POST_HANDSHAKE:
  8476. rv = conn_prepare_key_update(conn, ts);
  8477. if (rv != 0) {
  8478. return rv;
  8479. }
  8480. break;
  8481. default:
  8482. ngtcp2_unreachable();
  8483. }
  8484. return conn_recv_cpkt(conn, path, pi, pkt, pktlen, ts);
  8485. }
  8486. /*
  8487. * conn_check_pkt_num_exhausted returns nonzero if packet number is
  8488. * exhausted in at least one of packet number space.
  8489. */
  8490. static int conn_check_pkt_num_exhausted(ngtcp2_conn *conn) {
  8491. ngtcp2_pktns *in_pktns = conn->in_pktns;
  8492. ngtcp2_pktns *hs_pktns = conn->hs_pktns;
  8493. return (in_pktns && in_pktns->tx.last_pkt_num == NGTCP2_MAX_PKT_NUM) ||
  8494. (hs_pktns && hs_pktns->tx.last_pkt_num == NGTCP2_MAX_PKT_NUM) ||
  8495. conn->pktns.tx.last_pkt_num == NGTCP2_MAX_PKT_NUM;
  8496. }
  8497. /*
  8498. * conn_retransmit_retry_early retransmits 0RTT packet after Retry is
  8499. * received from server.
  8500. */
  8501. static ngtcp2_ssize
  8502. conn_retransmit_retry_early(ngtcp2_conn *conn, ngtcp2_pkt_info *pi,
  8503. uint8_t *dest, size_t destlen, size_t dgram_offset,
  8504. uint8_t flags, ngtcp2_tstamp ts) {
  8505. return conn_write_pkt(conn, pi, dest, destlen, dgram_offset, NULL,
  8506. NGTCP2_PKT_0RTT, flags, ts);
  8507. }
  8508. /*
  8509. * conn_handshake_probe_left returns nonzero if there are probe
  8510. * packets to be sent for Initial or Handshake packet number space
  8511. * left.
  8512. */
  8513. static int conn_handshake_probe_left(ngtcp2_conn *conn) {
  8514. return (conn->in_pktns && conn->in_pktns->rtb.probe_pkt_left) ||
  8515. conn->hs_pktns->rtb.probe_pkt_left;
  8516. }
  8517. /*
  8518. * conn_validate_early_transport_params_limits validates that the
  8519. * limits in transport parameters remembered by client for early data
  8520. * are not reduced. This function is only used by client and should
  8521. * only be called when early data is accepted by server.
  8522. */
  8523. static int conn_validate_early_transport_params_limits(ngtcp2_conn *conn) {
  8524. const ngtcp2_transport_params *params = conn->remote.transport_params;
  8525. assert(!conn->server);
  8526. assert(params);
  8527. if (conn->early.transport_params.active_connection_id_limit >
  8528. params->active_connection_id_limit ||
  8529. conn->early.transport_params.initial_max_data >
  8530. params->initial_max_data ||
  8531. conn->early.transport_params.initial_max_stream_data_bidi_local >
  8532. params->initial_max_stream_data_bidi_local ||
  8533. conn->early.transport_params.initial_max_stream_data_bidi_remote >
  8534. params->initial_max_stream_data_bidi_remote ||
  8535. conn->early.transport_params.initial_max_stream_data_uni >
  8536. params->initial_max_stream_data_uni ||
  8537. conn->early.transport_params.initial_max_streams_bidi >
  8538. params->initial_max_streams_bidi ||
  8539. conn->early.transport_params.initial_max_streams_uni >
  8540. params->initial_max_streams_uni ||
  8541. conn->early.transport_params.max_datagram_frame_size >
  8542. params->max_datagram_frame_size) {
  8543. return NGTCP2_ERR_PROTO;
  8544. }
  8545. return 0;
  8546. }
  8547. /*
  8548. * conn_write_handshake writes QUIC handshake packets to the buffer
  8549. * pointed by |dest| of length |destlen|. |write_datalen| specifies
  8550. * the expected length of 0RTT packet payload. Specify 0 to
  8551. * |write_datalen| if there is no such data.
  8552. *
  8553. * This function returns the number of bytes written to the buffer, or
  8554. * one of the following negative error codes:
  8555. *
  8556. * NGTCP2_ERR_PKT_NUM_EXHAUSTED
  8557. * Packet number is exhausted.
  8558. * NGTCP2_ERR_NOMEM
  8559. * Out of memory
  8560. * NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM
  8561. * Required transport parameter is missing.
  8562. * NGTCP2_CS_CLOSING
  8563. * Connection is in closing state.
  8564. * NGTCP2_CS_DRAINING
  8565. * Connection is in draining state.
  8566. *
  8567. * In addition to the above negative error codes, the same error codes
  8568. * from conn_recv_pkt may also be returned.
  8569. */
  8570. static ngtcp2_ssize conn_write_handshake(ngtcp2_conn *conn, ngtcp2_pkt_info *pi,
  8571. uint8_t *dest, size_t destlen,
  8572. uint64_t write_datalen,
  8573. ngtcp2_tstamp ts) {
  8574. int rv;
  8575. ngtcp2_ssize res = 0, nwrite = 0, early_spktlen = 0;
  8576. size_t origlen = destlen;
  8577. uint64_t pending_early_datalen;
  8578. ngtcp2_dcid *dcid;
  8579. ngtcp2_preferred_addr *paddr;
  8580. switch (conn->state) {
  8581. case NGTCP2_CS_CLIENT_INITIAL:
  8582. pending_early_datalen = conn_retry_early_payloadlen(conn);
  8583. if (pending_early_datalen) {
  8584. write_datalen = pending_early_datalen;
  8585. }
  8586. if (!(conn->flags & NGTCP2_CONN_FLAG_RECV_RETRY)) {
  8587. nwrite =
  8588. conn_write_client_initial(conn, pi, dest, destlen, write_datalen, ts);
  8589. if (nwrite <= 0) {
  8590. return nwrite;
  8591. }
  8592. } else {
  8593. nwrite =
  8594. conn_write_handshake_pkt(conn, pi, dest, destlen, 0, NGTCP2_PKT_INITIAL,
  8595. NGTCP2_WRITE_PKT_FLAG_NONE, write_datalen, ts);
  8596. if (nwrite < 0) {
  8597. return nwrite;
  8598. }
  8599. }
  8600. if (pending_early_datalen) {
  8601. early_spktlen = conn_retransmit_retry_early(
  8602. conn, pi, dest + nwrite, destlen - (size_t)nwrite, (size_t)nwrite,
  8603. nwrite ? NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING
  8604. : NGTCP2_WRITE_PKT_FLAG_NONE,
  8605. ts);
  8606. if (early_spktlen < 0) {
  8607. assert(ngtcp2_err_is_fatal((int)early_spktlen));
  8608. return early_spktlen;
  8609. }
  8610. }
  8611. conn->state = NGTCP2_CS_CLIENT_WAIT_HANDSHAKE;
  8612. res = nwrite + early_spktlen;
  8613. return res;
  8614. case NGTCP2_CS_CLIENT_WAIT_HANDSHAKE:
  8615. pending_early_datalen = 0;
  8616. if (!conn_handshake_probe_left(conn) && conn_cwnd_is_zero(conn)) {
  8617. destlen = 0;
  8618. } else {
  8619. if (!(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED)) {
  8620. pending_early_datalen = conn_retry_early_payloadlen(conn);
  8621. if (pending_early_datalen) {
  8622. write_datalen = pending_early_datalen;
  8623. }
  8624. }
  8625. nwrite =
  8626. conn_write_handshake_pkts(conn, pi, dest, destlen, write_datalen, ts);
  8627. if (nwrite < 0) {
  8628. return nwrite;
  8629. }
  8630. res += nwrite;
  8631. dest += nwrite;
  8632. destlen -= (size_t)nwrite;
  8633. }
  8634. if (!conn_is_tls_handshake_completed(conn)) {
  8635. if (pending_early_datalen &&
  8636. !(conn->flags & NGTCP2_CONN_FLAG_EARLY_DATA_REJECTED)) {
  8637. nwrite = conn_retransmit_retry_early(
  8638. conn, pi, dest, destlen, (size_t)res, NGTCP2_WRITE_PKT_FLAG_NONE, ts);
  8639. if (nwrite < 0) {
  8640. return nwrite;
  8641. }
  8642. res += nwrite;
  8643. }
  8644. if (res == 0) {
  8645. nwrite = conn_write_handshake_ack_pkts(conn, pi, dest, origlen, ts);
  8646. if (nwrite < 0) {
  8647. return nwrite;
  8648. }
  8649. res = nwrite;
  8650. }
  8651. return res;
  8652. }
  8653. if (!(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED)) {
  8654. return res;
  8655. }
  8656. if (!(conn->flags & NGTCP2_CONN_FLAG_TRANSPORT_PARAM_RECVED)) {
  8657. return NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM;
  8658. }
  8659. if ((conn->flags & NGTCP2_CONN_FLAG_EARLY_KEY_INSTALLED) &&
  8660. !(conn->flags & NGTCP2_CONN_FLAG_EARLY_DATA_REJECTED)) {
  8661. rv = conn_validate_early_transport_params_limits(conn);
  8662. if (rv != 0) {
  8663. return rv;
  8664. }
  8665. }
  8666. /* Server might increase stream data limits. Extend it if we have
  8667. streams created for early data. */
  8668. rv = conn_sync_stream_data_limit(conn);
  8669. if (rv != 0) {
  8670. return rv;
  8671. }
  8672. conn->state = NGTCP2_CS_POST_HANDSHAKE;
  8673. assert(conn->remote.transport_params);
  8674. if (conn->remote.transport_params->preferred_addr_present) {
  8675. assert(!ngtcp2_ringbuf_full(&conn->dcid.unused.rb));
  8676. paddr = &conn->remote.transport_params->preferred_addr;
  8677. dcid = ngtcp2_ringbuf_push_back(&conn->dcid.unused.rb);
  8678. ngtcp2_dcid_init(dcid, 1, &paddr->cid, paddr->stateless_reset_token);
  8679. rv = ngtcp2_gaptr_push(&conn->dcid.seqgap, 1, 1);
  8680. if (rv != 0) {
  8681. return (ngtcp2_ssize)rv;
  8682. }
  8683. }
  8684. if (conn->remote.transport_params->stateless_reset_token_present) {
  8685. assert(conn->dcid.current.seq == 0);
  8686. assert(!(conn->dcid.current.flags & NGTCP2_DCID_FLAG_TOKEN_PRESENT));
  8687. ngtcp2_dcid_set_token(
  8688. &conn->dcid.current,
  8689. conn->remote.transport_params->stateless_reset_token);
  8690. }
  8691. rv = conn_call_activate_dcid(conn, &conn->dcid.current);
  8692. if (rv != 0) {
  8693. return rv;
  8694. }
  8695. conn_process_early_rtb(conn);
  8696. if (!conn->local.settings.no_pmtud) {
  8697. rv = conn_start_pmtud(conn);
  8698. if (rv != 0) {
  8699. return rv;
  8700. }
  8701. }
  8702. return res;
  8703. case NGTCP2_CS_SERVER_INITIAL:
  8704. nwrite =
  8705. conn_write_handshake_pkts(conn, pi, dest, destlen, write_datalen, ts);
  8706. if (nwrite < 0) {
  8707. return nwrite;
  8708. }
  8709. if (nwrite) {
  8710. conn->state = NGTCP2_CS_SERVER_WAIT_HANDSHAKE;
  8711. }
  8712. return nwrite;
  8713. case NGTCP2_CS_SERVER_WAIT_HANDSHAKE:
  8714. if (conn_handshake_probe_left(conn) || !conn_cwnd_is_zero(conn)) {
  8715. nwrite =
  8716. conn_write_handshake_pkts(conn, pi, dest, destlen, write_datalen, ts);
  8717. if (nwrite < 0) {
  8718. return nwrite;
  8719. }
  8720. res += nwrite;
  8721. dest += nwrite;
  8722. destlen -= (size_t)nwrite;
  8723. }
  8724. if (res == 0) {
  8725. nwrite = conn_write_handshake_ack_pkts(conn, pi, dest, origlen, ts);
  8726. if (nwrite < 0) {
  8727. return nwrite;
  8728. }
  8729. res += nwrite;
  8730. dest += nwrite;
  8731. origlen -= (size_t)nwrite;
  8732. }
  8733. return res;
  8734. case NGTCP2_CS_CLOSING:
  8735. return NGTCP2_ERR_CLOSING;
  8736. case NGTCP2_CS_DRAINING:
  8737. return NGTCP2_ERR_DRAINING;
  8738. default:
  8739. return 0;
  8740. }
  8741. }
  8742. /**
  8743. * @function
  8744. *
  8745. * `conn_client_write_handshake` writes client side handshake data and
  8746. * 0RTT packet.
  8747. *
  8748. * In order to send STREAM data in 0RTT packet, specify
  8749. * |vmsg|->stream. |vmsg|->stream.strm, |vmsg|->stream.fin,
  8750. * |vmsg|->stream.data, and |vmsg|->stream.datacnt are stream to which
  8751. * 0-RTT data is sent, whether it is a last data chunk in this stream,
  8752. * a vector of 0-RTT data, and its number of elements respectively.
  8753. * The amount of 0RTT data sent is assigned to
  8754. * *|vmsg|->stream.pdatalen. If no data is sent, -1 is assigned.
  8755. * Note that 0 length STREAM frame is allowed in QUIC, so 0 might be
  8756. * assigned to *|vmsg|->stream.pdatalen.
  8757. *
  8758. * This function returns 0 if it cannot write any frame because buffer
  8759. * is too small, or packet is congestion limited. Application should
  8760. * keep reading and wait for congestion window to grow.
  8761. *
  8762. * This function returns the number of bytes written to the buffer
  8763. * pointed by |dest| if it succeeds, or one of the following negative
  8764. * error codes: (TBD).
  8765. */
  8766. static ngtcp2_ssize conn_client_write_handshake(ngtcp2_conn *conn,
  8767. ngtcp2_pkt_info *pi,
  8768. uint8_t *dest, size_t destlen,
  8769. ngtcp2_vmsg *vmsg,
  8770. ngtcp2_tstamp ts) {
  8771. int send_stream = 0;
  8772. int send_datagram = 0;
  8773. ngtcp2_ssize spktlen, early_spktlen;
  8774. uint64_t datalen;
  8775. uint64_t write_datalen = 0;
  8776. uint8_t wflags = NGTCP2_WRITE_PKT_FLAG_NONE;
  8777. int ppe_pending = (conn->flags & NGTCP2_CONN_FLAG_PPE_PENDING) != 0;
  8778. uint32_t version;
  8779. assert(!conn->server);
  8780. /* conn->early.ckm might be created in the first call of
  8781. conn_handshake(). Check it later. */
  8782. if (vmsg) {
  8783. switch (vmsg->type) {
  8784. case NGTCP2_VMSG_TYPE_STREAM:
  8785. datalen = ngtcp2_vec_len(vmsg->stream.data, vmsg->stream.datacnt);
  8786. send_stream = conn_retry_early_payloadlen(conn) == 0;
  8787. if (send_stream) {
  8788. write_datalen = ngtcp2_min_uint64(datalen + NGTCP2_STREAM_OVERHEAD,
  8789. NGTCP2_MIN_COALESCED_PAYLOADLEN);
  8790. if (vmsg->stream.flags & NGTCP2_WRITE_STREAM_FLAG_MORE) {
  8791. wflags |= NGTCP2_WRITE_PKT_FLAG_MORE;
  8792. }
  8793. } else {
  8794. vmsg = NULL;
  8795. }
  8796. break;
  8797. case NGTCP2_VMSG_TYPE_DATAGRAM:
  8798. datalen = ngtcp2_vec_len(vmsg->datagram.data, vmsg->datagram.datacnt);
  8799. send_datagram = conn_retry_early_payloadlen(conn) == 0;
  8800. if (send_datagram) {
  8801. write_datalen = datalen + NGTCP2_DATAGRAM_OVERHEAD;
  8802. if (vmsg->datagram.flags & NGTCP2_WRITE_DATAGRAM_FLAG_MORE) {
  8803. wflags |= NGTCP2_WRITE_PKT_FLAG_MORE;
  8804. }
  8805. } else {
  8806. vmsg = NULL;
  8807. }
  8808. break;
  8809. }
  8810. }
  8811. if (!ppe_pending) {
  8812. spktlen = conn_write_handshake(conn, pi, dest, destlen, write_datalen, ts);
  8813. if (spktlen < 0) {
  8814. return spktlen;
  8815. }
  8816. if ((conn->flags & NGTCP2_CONN_FLAG_EARLY_DATA_REJECTED) ||
  8817. !conn->early.ckm || (!send_stream && !send_datagram)) {
  8818. return spktlen;
  8819. }
  8820. /* If spktlen > 0, we are making a compound packet. If Initial
  8821. packet is written, we have to pad bytes to 0-RTT packet. */
  8822. version = conn->negotiated_version ? conn->negotiated_version
  8823. : conn->client_chosen_version;
  8824. if (spktlen > 0 &&
  8825. ngtcp2_pkt_get_type_long(version, dest[0]) == NGTCP2_PKT_INITIAL) {
  8826. wflags |= NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING;
  8827. conn->pkt.require_padding = 1;
  8828. }
  8829. } else {
  8830. assert(!conn->pktns.crypto.rx.ckm);
  8831. assert(!conn->pktns.crypto.tx.ckm);
  8832. assert(conn->early.ckm);
  8833. if (conn->pkt.require_padding) {
  8834. wflags |= NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING;
  8835. }
  8836. spktlen = conn->pkt.hs_spktlen;
  8837. }
  8838. dest += spktlen;
  8839. destlen -= (size_t)spktlen;
  8840. if (conn_cwnd_is_zero(conn)) {
  8841. return spktlen;
  8842. }
  8843. early_spktlen = conn_write_pkt(conn, pi, dest, destlen, (size_t)spktlen, vmsg,
  8844. NGTCP2_PKT_0RTT, wflags, ts);
  8845. if (early_spktlen < 0) {
  8846. switch (early_spktlen) {
  8847. case NGTCP2_ERR_STREAM_DATA_BLOCKED:
  8848. if (!(wflags & NGTCP2_WRITE_PKT_FLAG_MORE)) {
  8849. if (spktlen) {
  8850. return spktlen;
  8851. }
  8852. break;
  8853. }
  8854. /* fall through */
  8855. case NGTCP2_ERR_WRITE_MORE:
  8856. conn->pkt.hs_spktlen = spktlen;
  8857. break;
  8858. }
  8859. return early_spktlen;
  8860. }
  8861. return spktlen + early_spktlen;
  8862. }
  8863. void ngtcp2_conn_tls_handshake_completed(ngtcp2_conn *conn) {
  8864. conn->flags |= NGTCP2_CONN_FLAG_TLS_HANDSHAKE_COMPLETED;
  8865. if (conn->server) {
  8866. conn->flags |= NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED;
  8867. }
  8868. }
  8869. int ngtcp2_conn_get_handshake_completed(ngtcp2_conn *conn) {
  8870. return conn_is_tls_handshake_completed(conn) &&
  8871. (conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED);
  8872. }
  8873. int ngtcp2_conn_sched_ack(ngtcp2_conn *conn, ngtcp2_acktr *acktr,
  8874. int64_t pkt_num, int active_ack, ngtcp2_tstamp ts) {
  8875. int rv;
  8876. (void)conn;
  8877. rv = ngtcp2_acktr_add(acktr, pkt_num, active_ack, ts);
  8878. if (rv != 0) {
  8879. assert(rv != NGTCP2_ERR_INVALID_ARGUMENT);
  8880. return rv;
  8881. }
  8882. return 0;
  8883. }
  8884. int ngtcp2_accept(ngtcp2_pkt_hd *dest, const uint8_t *pkt, size_t pktlen) {
  8885. ngtcp2_ssize nread;
  8886. ngtcp2_pkt_hd hd, *p;
  8887. if (dest) {
  8888. p = dest;
  8889. } else {
  8890. p = &hd;
  8891. }
  8892. if (pktlen == 0 || (pkt[0] & NGTCP2_HEADER_FORM_BIT) == 0) {
  8893. return NGTCP2_ERR_INVALID_ARGUMENT;
  8894. }
  8895. nread = ngtcp2_pkt_decode_hd_long(p, pkt, pktlen);
  8896. if (nread < 0) {
  8897. return (int)nread;
  8898. }
  8899. switch (p->type) {
  8900. case NGTCP2_PKT_INITIAL:
  8901. break;
  8902. case NGTCP2_PKT_0RTT:
  8903. /* 0-RTT packet may arrive before Initial packet due to
  8904. re-ordering. ngtcp2 does not buffer 0RTT packet unless the
  8905. very first Initial packet is received or token is received.
  8906. Previously, we returned NGTCP2_ERR_RETRY here, so that client
  8907. can resend 0RTT data. But it incurs 1RTT already and
  8908. diminishes the value of 0RTT. Therefore, we just discard the
  8909. packet here for now. */
  8910. default:
  8911. return NGTCP2_ERR_INVALID_ARGUMENT;
  8912. }
  8913. if (pktlen < NGTCP2_MAX_UDP_PAYLOAD_SIZE ||
  8914. (p->tokenlen == 0 && p->dcid.datalen < NGTCP2_MIN_INITIAL_DCIDLEN)) {
  8915. return NGTCP2_ERR_INVALID_ARGUMENT;
  8916. }
  8917. return 0;
  8918. }
  8919. int ngtcp2_conn_install_initial_key(
  8920. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *rx_aead_ctx,
  8921. const uint8_t *rx_iv, const ngtcp2_crypto_cipher_ctx *rx_hp_ctx,
  8922. const ngtcp2_crypto_aead_ctx *tx_aead_ctx, const uint8_t *tx_iv,
  8923. const ngtcp2_crypto_cipher_ctx *tx_hp_ctx, size_t ivlen) {
  8924. ngtcp2_pktns *pktns = conn->in_pktns;
  8925. int rv;
  8926. assert(ivlen >= 8);
  8927. assert(pktns);
  8928. conn_call_delete_crypto_cipher_ctx(conn, &pktns->crypto.rx.hp_ctx);
  8929. pktns->crypto.rx.hp_ctx.native_handle = NULL;
  8930. if (pktns->crypto.rx.ckm) {
  8931. conn_call_delete_crypto_aead_ctx(conn, &pktns->crypto.rx.ckm->aead_ctx);
  8932. ngtcp2_crypto_km_del(pktns->crypto.rx.ckm, conn->mem);
  8933. pktns->crypto.rx.ckm = NULL;
  8934. }
  8935. conn_call_delete_crypto_cipher_ctx(conn, &pktns->crypto.tx.hp_ctx);
  8936. pktns->crypto.tx.hp_ctx.native_handle = NULL;
  8937. if (pktns->crypto.tx.ckm) {
  8938. conn_call_delete_crypto_aead_ctx(conn, &pktns->crypto.tx.ckm->aead_ctx);
  8939. ngtcp2_crypto_km_del(pktns->crypto.tx.ckm, conn->mem);
  8940. pktns->crypto.tx.ckm = NULL;
  8941. }
  8942. rv = ngtcp2_crypto_km_new(&pktns->crypto.rx.ckm, NULL, 0, NULL, rx_iv, ivlen,
  8943. conn->mem);
  8944. if (rv != 0) {
  8945. return rv;
  8946. }
  8947. rv = ngtcp2_crypto_km_new(&pktns->crypto.tx.ckm, NULL, 0, NULL, tx_iv, ivlen,
  8948. conn->mem);
  8949. if (rv != 0) {
  8950. return rv;
  8951. }
  8952. /* Take owner ship after we are sure that no failure occurs, so that
  8953. caller can delete these contexts on failure. */
  8954. pktns->crypto.rx.ckm->aead_ctx = *rx_aead_ctx;
  8955. pktns->crypto.rx.hp_ctx = *rx_hp_ctx;
  8956. pktns->crypto.tx.ckm->aead_ctx = *tx_aead_ctx;
  8957. pktns->crypto.tx.hp_ctx = *tx_hp_ctx;
  8958. return 0;
  8959. }
  8960. int ngtcp2_conn_install_vneg_initial_key(
  8961. ngtcp2_conn *conn, uint32_t version,
  8962. const ngtcp2_crypto_aead_ctx *rx_aead_ctx, const uint8_t *rx_iv,
  8963. const ngtcp2_crypto_cipher_ctx *rx_hp_ctx,
  8964. const ngtcp2_crypto_aead_ctx *tx_aead_ctx, const uint8_t *tx_iv,
  8965. const ngtcp2_crypto_cipher_ctx *tx_hp_ctx, size_t ivlen) {
  8966. int rv;
  8967. assert(ivlen >= 8);
  8968. conn_call_delete_crypto_cipher_ctx(conn, &conn->vneg.rx.hp_ctx);
  8969. conn->vneg.rx.hp_ctx.native_handle = NULL;
  8970. if (conn->vneg.rx.ckm) {
  8971. conn_call_delete_crypto_aead_ctx(conn, &conn->vneg.rx.ckm->aead_ctx);
  8972. ngtcp2_crypto_km_del(conn->vneg.rx.ckm, conn->mem);
  8973. conn->vneg.rx.ckm = NULL;
  8974. }
  8975. conn_call_delete_crypto_cipher_ctx(conn, &conn->vneg.tx.hp_ctx);
  8976. conn->vneg.tx.hp_ctx.native_handle = NULL;
  8977. if (conn->vneg.tx.ckm) {
  8978. conn_call_delete_crypto_aead_ctx(conn, &conn->vneg.tx.ckm->aead_ctx);
  8979. ngtcp2_crypto_km_del(conn->vneg.tx.ckm, conn->mem);
  8980. conn->vneg.tx.ckm = NULL;
  8981. }
  8982. rv = ngtcp2_crypto_km_new(&conn->vneg.rx.ckm, NULL, 0, NULL, rx_iv, ivlen,
  8983. conn->mem);
  8984. if (rv != 0) {
  8985. return rv;
  8986. }
  8987. rv = ngtcp2_crypto_km_new(&conn->vneg.tx.ckm, NULL, 0, NULL, tx_iv, ivlen,
  8988. conn->mem);
  8989. if (rv != 0) {
  8990. return rv;
  8991. }
  8992. /* Take owner ship after we are sure that no failure occurs, so that
  8993. caller can delete these contexts on failure. */
  8994. conn->vneg.rx.ckm->aead_ctx = *rx_aead_ctx;
  8995. conn->vneg.rx.hp_ctx = *rx_hp_ctx;
  8996. conn->vneg.tx.ckm->aead_ctx = *tx_aead_ctx;
  8997. conn->vneg.tx.hp_ctx = *tx_hp_ctx;
  8998. conn->vneg.version = version;
  8999. return 0;
  9000. }
  9001. int ngtcp2_conn_install_rx_handshake_key(
  9002. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv,
  9003. size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx) {
  9004. ngtcp2_pktns *pktns = conn->hs_pktns;
  9005. int rv;
  9006. assert(ivlen >= 8);
  9007. assert(pktns);
  9008. assert(!pktns->crypto.rx.hp_ctx.native_handle);
  9009. assert(!pktns->crypto.rx.ckm);
  9010. rv = ngtcp2_crypto_km_new(&pktns->crypto.rx.ckm, NULL, 0, aead_ctx, iv, ivlen,
  9011. conn->mem);
  9012. if (rv != 0) {
  9013. return rv;
  9014. }
  9015. pktns->crypto.rx.hp_ctx = *hp_ctx;
  9016. rv = conn_call_recv_rx_key(conn, NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE);
  9017. if (rv != 0) {
  9018. ngtcp2_crypto_km_del(pktns->crypto.rx.ckm, conn->mem);
  9019. pktns->crypto.rx.ckm = NULL;
  9020. memset(&pktns->crypto.rx.hp_ctx, 0, sizeof(pktns->crypto.rx.hp_ctx));
  9021. return rv;
  9022. }
  9023. return 0;
  9024. }
  9025. int ngtcp2_conn_install_tx_handshake_key(
  9026. ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv,
  9027. size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx) {
  9028. ngtcp2_pktns *pktns = conn->hs_pktns;
  9029. int rv;
  9030. assert(ivlen >= 8);
  9031. assert(pktns);
  9032. assert(!pktns->crypto.tx.hp_ctx.native_handle);
  9033. assert(!pktns->crypto.tx.ckm);
  9034. rv = ngtcp2_crypto_km_new(&pktns->crypto.tx.ckm, NULL, 0, aead_ctx, iv, ivlen,
  9035. conn->mem);
  9036. if (rv != 0) {
  9037. return rv;
  9038. }
  9039. pktns->crypto.tx.hp_ctx = *hp_ctx;
  9040. if (conn->server) {
  9041. rv = ngtcp2_conn_commit_local_transport_params(conn);
  9042. if (rv != 0) {
  9043. return rv;
  9044. }
  9045. }
  9046. rv = conn_call_recv_tx_key(conn, NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE);
  9047. if (rv != 0) {
  9048. ngtcp2_crypto_km_del(pktns->crypto.tx.ckm, conn->mem);
  9049. pktns->crypto.tx.ckm = NULL;
  9050. memset(&pktns->crypto.tx.hp_ctx, 0, sizeof(pktns->crypto.tx.hp_ctx));
  9051. return rv;
  9052. }
  9053. return 0;
  9054. }
  9055. int ngtcp2_conn_install_0rtt_key(ngtcp2_conn *conn,
  9056. const ngtcp2_crypto_aead_ctx *aead_ctx,
  9057. const uint8_t *iv, size_t ivlen,
  9058. const ngtcp2_crypto_cipher_ctx *hp_ctx) {
  9059. int rv;
  9060. assert(ivlen >= 8);
  9061. assert(!conn->early.hp_ctx.native_handle);
  9062. assert(!conn->early.ckm);
  9063. rv = ngtcp2_crypto_km_new(&conn->early.ckm, NULL, 0, aead_ctx, iv, ivlen,
  9064. conn->mem);
  9065. if (rv != 0) {
  9066. return rv;
  9067. }
  9068. conn->early.hp_ctx = *hp_ctx;
  9069. conn->flags |= NGTCP2_CONN_FLAG_EARLY_KEY_INSTALLED;
  9070. if (conn->server) {
  9071. rv = conn_call_recv_rx_key(conn, NGTCP2_ENCRYPTION_LEVEL_0RTT);
  9072. } else {
  9073. rv = conn_call_recv_tx_key(conn, NGTCP2_ENCRYPTION_LEVEL_0RTT);
  9074. }
  9075. if (rv != 0) {
  9076. ngtcp2_crypto_km_del(conn->early.ckm, conn->mem);
  9077. conn->early.ckm = NULL;
  9078. memset(&conn->early.hp_ctx, 0, sizeof(conn->early.hp_ctx));
  9079. return rv;
  9080. }
  9081. return 0;
  9082. }
  9083. int ngtcp2_conn_install_rx_key(ngtcp2_conn *conn, const uint8_t *secret,
  9084. size_t secretlen,
  9085. const ngtcp2_crypto_aead_ctx *aead_ctx,
  9086. const uint8_t *iv, size_t ivlen,
  9087. const ngtcp2_crypto_cipher_ctx *hp_ctx) {
  9088. ngtcp2_pktns *pktns = &conn->pktns;
  9089. int rv;
  9090. assert(ivlen >= 8);
  9091. assert(!pktns->crypto.rx.hp_ctx.native_handle);
  9092. assert(!pktns->crypto.rx.ckm);
  9093. rv = ngtcp2_crypto_km_new(&pktns->crypto.rx.ckm, secret, secretlen, aead_ctx,
  9094. iv, ivlen, conn->mem);
  9095. if (rv != 0) {
  9096. return rv;
  9097. }
  9098. pktns->crypto.rx.hp_ctx = *hp_ctx;
  9099. if (!conn->server) {
  9100. if (conn->remote.pending_transport_params) {
  9101. ngtcp2_transport_params_del(conn->remote.transport_params, conn->mem);
  9102. conn->remote.transport_params = conn->remote.pending_transport_params;
  9103. conn->remote.pending_transport_params = NULL;
  9104. conn_sync_stream_id_limit(conn);
  9105. conn->tx.max_offset = conn->remote.transport_params->initial_max_data;
  9106. }
  9107. if (conn->early.ckm) {
  9108. conn_discard_early_key(conn);
  9109. }
  9110. }
  9111. rv = conn_call_recv_rx_key(conn, NGTCP2_ENCRYPTION_LEVEL_1RTT);
  9112. if (rv != 0) {
  9113. ngtcp2_crypto_km_del(pktns->crypto.rx.ckm, conn->mem);
  9114. pktns->crypto.rx.ckm = NULL;
  9115. memset(&pktns->crypto.rx.hp_ctx, 0, sizeof(pktns->crypto.rx.hp_ctx));
  9116. return rv;
  9117. }
  9118. return 0;
  9119. }
  9120. int ngtcp2_conn_install_tx_key(ngtcp2_conn *conn, const uint8_t *secret,
  9121. size_t secretlen,
  9122. const ngtcp2_crypto_aead_ctx *aead_ctx,
  9123. const uint8_t *iv, size_t ivlen,
  9124. const ngtcp2_crypto_cipher_ctx *hp_ctx) {
  9125. ngtcp2_pktns *pktns = &conn->pktns;
  9126. int rv;
  9127. assert(ivlen >= 8);
  9128. assert(!pktns->crypto.tx.hp_ctx.native_handle);
  9129. assert(!pktns->crypto.tx.ckm);
  9130. rv = ngtcp2_crypto_km_new(&pktns->crypto.tx.ckm, secret, secretlen, aead_ctx,
  9131. iv, ivlen, conn->mem);
  9132. if (rv != 0) {
  9133. return rv;
  9134. }
  9135. pktns->crypto.tx.hp_ctx = *hp_ctx;
  9136. if (conn->server) {
  9137. if (conn->remote.pending_transport_params) {
  9138. ngtcp2_transport_params_del(conn->remote.transport_params, conn->mem);
  9139. conn->remote.transport_params = conn->remote.pending_transport_params;
  9140. conn->remote.pending_transport_params = NULL;
  9141. conn_sync_stream_id_limit(conn);
  9142. conn->tx.max_offset = conn->remote.transport_params->initial_max_data;
  9143. }
  9144. } else if (conn->early.ckm) {
  9145. conn_discard_early_key(conn);
  9146. }
  9147. rv = conn_call_recv_tx_key(conn, NGTCP2_ENCRYPTION_LEVEL_1RTT);
  9148. if (rv != 0) {
  9149. ngtcp2_crypto_km_del(pktns->crypto.tx.ckm, conn->mem);
  9150. pktns->crypto.tx.ckm = NULL;
  9151. memset(&pktns->crypto.tx.hp_ctx, 0, sizeof(pktns->crypto.tx.hp_ctx));
  9152. return rv;
  9153. }
  9154. return 0;
  9155. }
  9156. static int conn_initiate_key_update(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  9157. ngtcp2_tstamp confirmed_ts = conn->crypto.key_update.confirmed_ts;
  9158. ngtcp2_duration pto = conn_compute_pto(conn, &conn->pktns);
  9159. assert(conn->state == NGTCP2_CS_POST_HANDSHAKE);
  9160. if (!(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED) ||
  9161. (conn->flags & NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED) ||
  9162. !conn->crypto.key_update.new_tx_ckm ||
  9163. !conn->crypto.key_update.new_rx_ckm ||
  9164. ngtcp2_tstamp_not_elapsed(confirmed_ts, 3 * pto, ts)) {
  9165. return NGTCP2_ERR_INVALID_STATE;
  9166. }
  9167. conn_rotate_keys(conn, NGTCP2_MAX_PKT_NUM, /* initiator = */ 1);
  9168. return 0;
  9169. }
  9170. int ngtcp2_conn_initiate_key_update(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  9171. conn_update_timestamp(conn, ts);
  9172. return conn_initiate_key_update(conn, ts);
  9173. }
  9174. /*
  9175. * conn_retire_stale_bound_dcid retires stale destination connection
  9176. * ID in conn->dcid.bound to keep some unused destination connection
  9177. * IDs available.
  9178. *
  9179. * This function returns 0 if it succeeds, or one of the following
  9180. * negative error codes:
  9181. *
  9182. * NGTCP2_ERR_NOMEM
  9183. * Out of memory.
  9184. */
  9185. static int conn_retire_stale_bound_dcid(ngtcp2_conn *conn,
  9186. ngtcp2_duration timeout,
  9187. ngtcp2_tstamp ts) {
  9188. size_t i;
  9189. ngtcp2_dcid *dcid, *last;
  9190. int rv;
  9191. for (i = 0; i < ngtcp2_ringbuf_len(&conn->dcid.bound.rb);) {
  9192. dcid = ngtcp2_ringbuf_get(&conn->dcid.bound.rb, i);
  9193. assert(dcid->cid.datalen);
  9194. if (ngtcp2_tstamp_not_elapsed(dcid->bound_ts, timeout, ts)) {
  9195. ++i;
  9196. continue;
  9197. }
  9198. rv = conn_retire_dcid_seq(conn, dcid->seq);
  9199. if (rv != 0) {
  9200. return rv;
  9201. }
  9202. if (i == 0) {
  9203. ngtcp2_ringbuf_pop_front(&conn->dcid.bound.rb);
  9204. continue;
  9205. }
  9206. if (i == ngtcp2_ringbuf_len(&conn->dcid.bound.rb) - 1) {
  9207. ngtcp2_ringbuf_pop_back(&conn->dcid.bound.rb);
  9208. break;
  9209. }
  9210. last = ngtcp2_ringbuf_get(&conn->dcid.bound.rb,
  9211. ngtcp2_ringbuf_len(&conn->dcid.bound.rb) - 1);
  9212. ngtcp2_dcid_copy(dcid, last);
  9213. ngtcp2_ringbuf_pop_back(&conn->dcid.bound.rb);
  9214. }
  9215. return 0;
  9216. }
  9217. ngtcp2_tstamp ngtcp2_conn_loss_detection_expiry(ngtcp2_conn *conn) {
  9218. return conn->cstat.loss_detection_timer;
  9219. }
  9220. ngtcp2_tstamp ngtcp2_conn_internal_expiry(ngtcp2_conn *conn) {
  9221. ngtcp2_tstamp res = UINT64_MAX;
  9222. ngtcp2_duration pto = conn_compute_pto(conn, &conn->pktns);
  9223. ngtcp2_scid *scid;
  9224. ngtcp2_dcid *dcid;
  9225. size_t i, len;
  9226. if (conn->pv) {
  9227. res = ngtcp2_pv_next_expiry(conn->pv);
  9228. }
  9229. if (conn->pmtud) {
  9230. res = ngtcp2_min_uint64(res, conn->pmtud->expiry);
  9231. }
  9232. if (!ngtcp2_pq_empty(&conn->scid.used)) {
  9233. scid = ngtcp2_struct_of(ngtcp2_pq_top(&conn->scid.used), ngtcp2_scid, pe);
  9234. if (scid->retired_ts != UINT64_MAX) {
  9235. res = ngtcp2_min_uint64(res, scid->retired_ts + pto);
  9236. }
  9237. }
  9238. if (ngtcp2_ringbuf_len(&conn->dcid.retired.rb)) {
  9239. dcid = ngtcp2_ringbuf_get(&conn->dcid.retired.rb, 0);
  9240. res = ngtcp2_min_uint64(res, dcid->retired_ts + pto);
  9241. }
  9242. if (conn->dcid.current.cid.datalen) {
  9243. len = ngtcp2_ringbuf_len(&conn->dcid.bound.rb);
  9244. for (i = 0; i < len; ++i) {
  9245. dcid = ngtcp2_ringbuf_get(&conn->dcid.bound.rb, i);
  9246. assert(dcid->cid.datalen);
  9247. assert(dcid->bound_ts != UINT64_MAX);
  9248. res = ngtcp2_min_uint64(res, dcid->bound_ts + 3 * pto);
  9249. }
  9250. }
  9251. if (conn->server && conn->early.ckm &&
  9252. conn->early.discard_started_ts != UINT64_MAX) {
  9253. res = ngtcp2_min_uint64(res, conn->early.discard_started_ts + 3 * pto);
  9254. }
  9255. return res;
  9256. }
  9257. ngtcp2_tstamp ngtcp2_conn_ack_delay_expiry(ngtcp2_conn *conn) {
  9258. ngtcp2_acktr *acktr = &conn->pktns.acktr;
  9259. if (!(acktr->flags & NGTCP2_ACKTR_FLAG_CANCEL_TIMER) &&
  9260. acktr->first_unacked_ts != UINT64_MAX) {
  9261. return acktr->first_unacked_ts + conn_compute_ack_delay(conn);
  9262. }
  9263. return UINT64_MAX;
  9264. }
  9265. static ngtcp2_tstamp conn_handshake_expiry(ngtcp2_conn *conn) {
  9266. if (conn_is_tls_handshake_completed(conn) ||
  9267. conn->local.settings.handshake_timeout == UINT64_MAX ||
  9268. conn->local.settings.initial_ts >=
  9269. UINT64_MAX - conn->local.settings.handshake_timeout) {
  9270. return UINT64_MAX;
  9271. }
  9272. return conn->local.settings.initial_ts +
  9273. conn->local.settings.handshake_timeout;
  9274. }
  9275. ngtcp2_tstamp ngtcp2_conn_get_expiry(ngtcp2_conn *conn) {
  9276. ngtcp2_tstamp res = ngtcp2_min_uint64(ngtcp2_conn_loss_detection_expiry(conn),
  9277. ngtcp2_conn_ack_delay_expiry(conn));
  9278. res = ngtcp2_min_uint64(res, ngtcp2_conn_internal_expiry(conn));
  9279. res = ngtcp2_min_uint64(res, ngtcp2_conn_lost_pkt_expiry(conn));
  9280. res = ngtcp2_min_uint64(res, conn_keep_alive_expiry(conn));
  9281. res = ngtcp2_min_uint64(res, conn_handshake_expiry(conn));
  9282. res = ngtcp2_min_uint64(res, ngtcp2_conn_get_idle_expiry(conn));
  9283. return ngtcp2_min_uint64(res, conn->tx.pacing.next_ts);
  9284. }
  9285. int ngtcp2_conn_handle_expiry(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  9286. int rv;
  9287. ngtcp2_duration pto;
  9288. conn_update_timestamp(conn, ts);
  9289. pto = conn_compute_pto(conn, &conn->pktns);
  9290. assert(!(conn->flags & NGTCP2_CONN_FLAG_PPE_PENDING));
  9291. if (ngtcp2_conn_get_idle_expiry(conn) <= ts) {
  9292. return NGTCP2_ERR_IDLE_CLOSE;
  9293. }
  9294. ngtcp2_conn_cancel_expired_ack_delay_timer(conn, ts);
  9295. conn_cancel_expired_keep_alive_timer(conn, ts);
  9296. conn_cancel_expired_pkt_tx_timer(conn, ts);
  9297. ngtcp2_conn_remove_lost_pkt(conn, ts);
  9298. if (conn->pv) {
  9299. ngtcp2_pv_cancel_expired_timer(conn->pv, ts);
  9300. }
  9301. if (conn->pmtud) {
  9302. ngtcp2_pmtud_handle_expiry(conn->pmtud, ts);
  9303. if (ngtcp2_pmtud_finished(conn->pmtud)) {
  9304. ngtcp2_conn_stop_pmtud(conn);
  9305. }
  9306. }
  9307. if (ngtcp2_conn_loss_detection_expiry(conn) <= ts) {
  9308. rv = ngtcp2_conn_on_loss_detection_timer(conn, ts);
  9309. if (rv != 0) {
  9310. return rv;
  9311. }
  9312. }
  9313. if (conn->dcid.current.cid.datalen) {
  9314. rv = conn_retire_stale_bound_dcid(conn, 3 * pto, ts);
  9315. if (rv != 0) {
  9316. return rv;
  9317. }
  9318. }
  9319. rv = conn_remove_retired_connection_id(conn, pto, ts);
  9320. if (rv != 0) {
  9321. return rv;
  9322. }
  9323. if (conn->server && conn->early.ckm &&
  9324. ngtcp2_tstamp_elapsed(conn->early.discard_started_ts, 3 * pto, ts)) {
  9325. conn_discard_early_key(conn);
  9326. }
  9327. if (!conn_is_tls_handshake_completed(conn) &&
  9328. ngtcp2_tstamp_elapsed(conn->local.settings.initial_ts,
  9329. conn->local.settings.handshake_timeout, ts)) {
  9330. return NGTCP2_ERR_HANDSHAKE_TIMEOUT;
  9331. }
  9332. return 0;
  9333. }
  9334. static void acktr_cancel_expired_ack_delay_timer(ngtcp2_acktr *acktr,
  9335. ngtcp2_duration max_ack_delay,
  9336. ngtcp2_tstamp ts) {
  9337. if (!(acktr->flags & NGTCP2_ACKTR_FLAG_CANCEL_TIMER) &&
  9338. ngtcp2_tstamp_elapsed(acktr->first_unacked_ts, max_ack_delay, ts)) {
  9339. acktr->flags |= NGTCP2_ACKTR_FLAG_CANCEL_TIMER;
  9340. }
  9341. }
  9342. void ngtcp2_conn_cancel_expired_ack_delay_timer(ngtcp2_conn *conn,
  9343. ngtcp2_tstamp ts) {
  9344. ngtcp2_duration ack_delay = conn_compute_ack_delay(conn);
  9345. if (conn->in_pktns) {
  9346. acktr_cancel_expired_ack_delay_timer(&conn->in_pktns->acktr, 0, ts);
  9347. }
  9348. if (conn->hs_pktns) {
  9349. acktr_cancel_expired_ack_delay_timer(&conn->hs_pktns->acktr, 0, ts);
  9350. }
  9351. acktr_cancel_expired_ack_delay_timer(&conn->pktns.acktr, ack_delay, ts);
  9352. }
  9353. ngtcp2_tstamp ngtcp2_conn_lost_pkt_expiry(ngtcp2_conn *conn) {
  9354. ngtcp2_tstamp res = UINT64_MAX, ts;
  9355. if (conn->in_pktns) {
  9356. ts = ngtcp2_rtb_lost_pkt_ts(&conn->in_pktns->rtb);
  9357. if (ts != UINT64_MAX) {
  9358. ts += conn_compute_pto(conn, conn->in_pktns);
  9359. res = ngtcp2_min_uint64(res, ts);
  9360. }
  9361. }
  9362. if (conn->hs_pktns) {
  9363. ts = ngtcp2_rtb_lost_pkt_ts(&conn->hs_pktns->rtb);
  9364. if (ts != UINT64_MAX) {
  9365. ts += conn_compute_pto(conn, conn->hs_pktns);
  9366. res = ngtcp2_min_uint64(res, ts);
  9367. }
  9368. }
  9369. ts = ngtcp2_rtb_lost_pkt_ts(&conn->pktns.rtb);
  9370. if (ts != UINT64_MAX) {
  9371. ts += conn_compute_pto(conn, &conn->pktns);
  9372. res = ngtcp2_min_uint64(res, ts);
  9373. }
  9374. return res;
  9375. }
  9376. void ngtcp2_conn_remove_lost_pkt(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  9377. ngtcp2_duration pto;
  9378. if (conn->in_pktns) {
  9379. pto = conn_compute_pto(conn, conn->in_pktns);
  9380. ngtcp2_rtb_remove_expired_lost_pkt(&conn->in_pktns->rtb, pto, ts);
  9381. }
  9382. if (conn->hs_pktns) {
  9383. pto = conn_compute_pto(conn, conn->hs_pktns);
  9384. ngtcp2_rtb_remove_expired_lost_pkt(&conn->hs_pktns->rtb, pto, ts);
  9385. }
  9386. pto = conn_compute_pto(conn, &conn->pktns);
  9387. ngtcp2_rtb_remove_expired_lost_pkt(&conn->pktns.rtb, pto, ts);
  9388. }
  9389. /*
  9390. * select_preferred_version selects the most preferred version.
  9391. * |fallback_version| is chosen if no preference is made, or
  9392. * |preferred_versions| does not include any of |chosen_version| or
  9393. * |available_versions|. |chosen_version| is treated as an extra
  9394. * other version.
  9395. */
  9396. static uint32_t select_preferred_version(const uint32_t *preferred_versions,
  9397. size_t preferred_versionslen,
  9398. uint32_t chosen_version,
  9399. const uint8_t *available_versions,
  9400. size_t available_versionslen,
  9401. uint32_t fallback_version) {
  9402. size_t i, j;
  9403. const uint8_t *p;
  9404. uint32_t v;
  9405. if (!preferred_versionslen ||
  9406. (!available_versionslen && chosen_version == fallback_version)) {
  9407. return fallback_version;
  9408. }
  9409. for (i = 0; i < preferred_versionslen; ++i) {
  9410. if (preferred_versions[i] == chosen_version) {
  9411. return chosen_version;
  9412. }
  9413. for (j = 0, p = available_versions; j < available_versionslen;
  9414. j += sizeof(uint32_t)) {
  9415. p = ngtcp2_get_uint32be(&v, p);
  9416. if (preferred_versions[i] == v) {
  9417. return v;
  9418. }
  9419. }
  9420. }
  9421. return fallback_version;
  9422. }
  9423. /*
  9424. * conn_client_validate_transport_params validates |params| as client.
  9425. * |params| must be sent with Encrypted Extensions.
  9426. *
  9427. * This function returns 0 if it succeeds, or one of the following
  9428. * negative error codes:
  9429. *
  9430. * NGTCP2_ERR_TRANSPORT_PARAM
  9431. * params contains preferred address but server chose zero-length
  9432. * connection ID.
  9433. * NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE
  9434. * Validation against version negotiation parameters failed.
  9435. */
  9436. static int
  9437. conn_client_validate_transport_params(ngtcp2_conn *conn,
  9438. const ngtcp2_transport_params *params) {
  9439. if (!params->original_dcid_present) {
  9440. return NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM;
  9441. }
  9442. if (!ngtcp2_cid_eq(&conn->rcid, &params->original_dcid)) {
  9443. return NGTCP2_ERR_TRANSPORT_PARAM;
  9444. }
  9445. if (conn->flags & NGTCP2_CONN_FLAG_RECV_RETRY) {
  9446. if (!params->retry_scid_present) {
  9447. return NGTCP2_ERR_TRANSPORT_PARAM;
  9448. }
  9449. if (!ngtcp2_cid_eq(&conn->retry_scid, &params->retry_scid)) {
  9450. return NGTCP2_ERR_TRANSPORT_PARAM;
  9451. }
  9452. } else if (params->retry_scid_present) {
  9453. return NGTCP2_ERR_TRANSPORT_PARAM;
  9454. }
  9455. if (params->preferred_addr_present && conn->dcid.current.cid.datalen == 0) {
  9456. return NGTCP2_ERR_TRANSPORT_PARAM;
  9457. }
  9458. if (params->version_info_present) {
  9459. if (conn->negotiated_version != params->version_info.chosen_version) {
  9460. return NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE;
  9461. }
  9462. assert(vneg_available_versions_includes(conn->vneg.available_versions,
  9463. conn->vneg.available_versionslen,
  9464. conn->negotiated_version));
  9465. } else if (conn->client_chosen_version != conn->negotiated_version) {
  9466. return NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE;
  9467. }
  9468. /* When client reacted upon Version Negotiation */
  9469. if (conn->local.settings.original_version != conn->client_chosen_version) {
  9470. if (!params->version_info_present) {
  9471. assert(conn->client_chosen_version == conn->negotiated_version);
  9472. /* QUIC v1 is treated specially. If version_info is missing, no
  9473. further validation is necessary. See
  9474. https://datatracker.ietf.org/doc/html/rfc9368#section-8
  9475. */
  9476. if (conn->client_chosen_version == NGTCP2_PROTO_VER_V1) {
  9477. return 0;
  9478. }
  9479. return NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE;
  9480. }
  9481. /* Server choose original version after Version Negotiation. RFC
  9482. 9368 does not say this particular case, but this smells like
  9483. misbehaved server because server should accept original_version
  9484. in the original connection. */
  9485. if (conn->local.settings.original_version ==
  9486. params->version_info.chosen_version) {
  9487. return NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE;
  9488. }
  9489. /* Check version downgrade on incompatible version negotiation. */
  9490. if (params->version_info.available_versionslen == 0) {
  9491. return NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE;
  9492. }
  9493. if (conn->client_chosen_version !=
  9494. select_preferred_version(conn->vneg.preferred_versions,
  9495. conn->vneg.preferred_versionslen,
  9496. params->version_info.chosen_version,
  9497. params->version_info.available_versions,
  9498. params->version_info.available_versionslen,
  9499. /* fallback_version = */ 0)) {
  9500. return NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE;
  9501. }
  9502. }
  9503. return 0;
  9504. }
  9505. uint32_t
  9506. ngtcp2_conn_server_negotiate_version(ngtcp2_conn *conn,
  9507. const ngtcp2_version_info *version_info) {
  9508. assert(conn->server);
  9509. assert(conn->client_chosen_version == version_info->chosen_version);
  9510. return select_preferred_version(
  9511. conn->vneg.preferred_versions, conn->vneg.preferred_versionslen,
  9512. version_info->chosen_version, version_info->available_versions,
  9513. version_info->available_versionslen, version_info->chosen_version);
  9514. }
  9515. int ngtcp2_conn_set_remote_transport_params(
  9516. ngtcp2_conn *conn, const ngtcp2_transport_params *params) {
  9517. int rv;
  9518. /* We expect this function is called once per QUIC connection, but
  9519. GnuTLS server seems to call TLS extension callback twice if it
  9520. sends HelloRetryRequest. In practice, same QUIC transport
  9521. parameters are sent in the 2nd client flight, just returning 0
  9522. would cause no harm. */
  9523. if (conn->flags & NGTCP2_CONN_FLAG_TRANSPORT_PARAM_RECVED) {
  9524. return 0;
  9525. }
  9526. if (!params->initial_scid_present) {
  9527. return NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM;
  9528. }
  9529. /* Assume that ngtcp2_transport_params_decode sets default value if
  9530. active_connection_id_limit is omitted. */
  9531. if (params->active_connection_id_limit <
  9532. NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT) {
  9533. return NGTCP2_ERR_TRANSPORT_PARAM;
  9534. }
  9535. /* We assume that conn->dcid.current.cid is still the initial one.
  9536. This requires that transport parameter must be fed into
  9537. ngtcp2_conn as early as possible. */
  9538. if (!ngtcp2_cid_eq(&conn->dcid.current.cid, &params->initial_scid)) {
  9539. return NGTCP2_ERR_TRANSPORT_PARAM;
  9540. }
  9541. if (params->max_udp_payload_size < NGTCP2_MAX_UDP_PAYLOAD_SIZE) {
  9542. return NGTCP2_ERR_TRANSPORT_PARAM;
  9543. }
  9544. if (conn->server) {
  9545. if (params->original_dcid_present ||
  9546. params->stateless_reset_token_present ||
  9547. params->preferred_addr_present || params->retry_scid_present) {
  9548. return NGTCP2_ERR_TRANSPORT_PARAM;
  9549. }
  9550. if (params->version_info_present) {
  9551. if (!vneg_available_versions_includes(
  9552. params->version_info.available_versions,
  9553. params->version_info.available_versionslen,
  9554. params->version_info.chosen_version)) {
  9555. return NGTCP2_ERR_TRANSPORT_PARAM;
  9556. }
  9557. if (params->version_info.chosen_version != conn->client_chosen_version) {
  9558. return NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE;
  9559. }
  9560. conn->negotiated_version =
  9561. ngtcp2_conn_server_negotiate_version(conn, &params->version_info);
  9562. if (conn->negotiated_version != conn->client_chosen_version) {
  9563. rv = conn_call_version_negotiation(conn, conn->negotiated_version,
  9564. &conn->rcid);
  9565. if (rv != 0) {
  9566. return rv;
  9567. }
  9568. }
  9569. } else {
  9570. conn->negotiated_version = conn->client_chosen_version;
  9571. }
  9572. conn->local.transport_params.version_info.chosen_version =
  9573. conn->negotiated_version;
  9574. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  9575. "the negotiated version is 0x%08x",
  9576. conn->negotiated_version);
  9577. } else {
  9578. rv = conn_client_validate_transport_params(conn, params);
  9579. if (rv != 0) {
  9580. return rv;
  9581. }
  9582. }
  9583. ngtcp2_log_remote_tp(&conn->log, params);
  9584. ngtcp2_qlog_parameters_set_transport_params(&conn->qlog, params, conn->server,
  9585. NGTCP2_QLOG_SIDE_REMOTE);
  9586. if ((conn->server && conn->pktns.crypto.tx.ckm) ||
  9587. (!conn->server && conn->pktns.crypto.rx.ckm)) {
  9588. ngtcp2_transport_params_del(conn->remote.transport_params, conn->mem);
  9589. conn->remote.transport_params = NULL;
  9590. rv = ngtcp2_transport_params_copy_new(&conn->remote.transport_params,
  9591. params, conn->mem);
  9592. if (rv != 0) {
  9593. return rv;
  9594. }
  9595. conn_sync_stream_id_limit(conn);
  9596. conn->tx.max_offset = conn->remote.transport_params->initial_max_data;
  9597. } else {
  9598. assert(!conn->remote.pending_transport_params);
  9599. rv = ngtcp2_transport_params_copy_new(
  9600. &conn->remote.pending_transport_params, params, conn->mem);
  9601. if (rv != 0) {
  9602. return rv;
  9603. }
  9604. }
  9605. conn->flags |= NGTCP2_CONN_FLAG_TRANSPORT_PARAM_RECVED;
  9606. return 0;
  9607. }
  9608. int ngtcp2_conn_decode_and_set_remote_transport_params(ngtcp2_conn *conn,
  9609. const uint8_t *data,
  9610. size_t datalen) {
  9611. ngtcp2_transport_params params;
  9612. int rv;
  9613. rv = ngtcp2_transport_params_decode(&params, data, datalen);
  9614. if (rv != 0) {
  9615. return rv;
  9616. }
  9617. return ngtcp2_conn_set_remote_transport_params(conn, &params);
  9618. }
  9619. const ngtcp2_transport_params *
  9620. ngtcp2_conn_get_remote_transport_params(ngtcp2_conn *conn) {
  9621. if (conn->remote.pending_transport_params) {
  9622. return conn->remote.pending_transport_params;
  9623. }
  9624. return conn->remote.transport_params;
  9625. }
  9626. ngtcp2_ssize ngtcp2_conn_encode_0rtt_transport_params(ngtcp2_conn *conn,
  9627. uint8_t *dest,
  9628. size_t destlen) {
  9629. ngtcp2_transport_params params, *src;
  9630. if (conn->server) {
  9631. src = &conn->local.transport_params;
  9632. } else {
  9633. assert(conn->remote.transport_params);
  9634. src = conn->remote.transport_params;
  9635. }
  9636. ngtcp2_transport_params_default(&params);
  9637. params.initial_max_streams_bidi = src->initial_max_streams_bidi;
  9638. params.initial_max_streams_uni = src->initial_max_streams_uni;
  9639. params.initial_max_stream_data_bidi_local =
  9640. src->initial_max_stream_data_bidi_local;
  9641. params.initial_max_stream_data_bidi_remote =
  9642. src->initial_max_stream_data_bidi_remote;
  9643. params.initial_max_stream_data_uni = src->initial_max_stream_data_uni;
  9644. params.initial_max_data = src->initial_max_data;
  9645. params.active_connection_id_limit = src->active_connection_id_limit;
  9646. params.max_datagram_frame_size = src->max_datagram_frame_size;
  9647. if (conn->server) {
  9648. params.max_idle_timeout = src->max_idle_timeout;
  9649. params.max_udp_payload_size = src->max_udp_payload_size;
  9650. params.disable_active_migration = src->disable_active_migration;
  9651. }
  9652. return ngtcp2_transport_params_encode(dest, destlen, &params);
  9653. }
  9654. int ngtcp2_conn_decode_and_set_0rtt_transport_params(ngtcp2_conn *conn,
  9655. const uint8_t *data,
  9656. size_t datalen) {
  9657. ngtcp2_transport_params params;
  9658. int rv;
  9659. rv = ngtcp2_transport_params_decode(&params, data, datalen);
  9660. if (rv != 0) {
  9661. return rv;
  9662. }
  9663. return ngtcp2_conn_set_0rtt_remote_transport_params(conn, &params);
  9664. }
  9665. int ngtcp2_conn_set_0rtt_remote_transport_params(
  9666. ngtcp2_conn *conn, const ngtcp2_transport_params *params) {
  9667. ngtcp2_transport_params *p;
  9668. assert(!conn->server);
  9669. assert(!conn->remote.transport_params);
  9670. /* Assume that all pointer fields in p are NULL */
  9671. p = ngtcp2_mem_calloc(conn->mem, 1, sizeof(*p));
  9672. if (p == NULL) {
  9673. return NGTCP2_ERR_NOMEM;
  9674. }
  9675. conn->remote.transport_params = p;
  9676. ngtcp2_transport_params_default(conn->remote.transport_params);
  9677. p->initial_max_streams_bidi = params->initial_max_streams_bidi;
  9678. p->initial_max_streams_uni = params->initial_max_streams_uni;
  9679. p->initial_max_stream_data_bidi_local =
  9680. params->initial_max_stream_data_bidi_local;
  9681. p->initial_max_stream_data_bidi_remote =
  9682. params->initial_max_stream_data_bidi_remote;
  9683. p->initial_max_stream_data_uni = params->initial_max_stream_data_uni;
  9684. p->initial_max_data = params->initial_max_data;
  9685. /* we might hit garbage, then set the sane default. */
  9686. p->active_connection_id_limit =
  9687. ngtcp2_max_uint64(NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT,
  9688. params->active_connection_id_limit);
  9689. p->max_datagram_frame_size = params->max_datagram_frame_size;
  9690. /* we might hit garbage, then set the sane default. */
  9691. if (params->max_udp_payload_size) {
  9692. p->max_udp_payload_size = ngtcp2_max_uint64(NGTCP2_MAX_UDP_PAYLOAD_SIZE,
  9693. params->max_udp_payload_size);
  9694. }
  9695. /* These parameters are treated specially. If server accepts early
  9696. data, it must not set values for these parameters that are
  9697. smaller than these remembered values. */
  9698. conn->early.transport_params.initial_max_streams_bidi =
  9699. params->initial_max_streams_bidi;
  9700. conn->early.transport_params.initial_max_streams_uni =
  9701. params->initial_max_streams_uni;
  9702. conn->early.transport_params.initial_max_stream_data_bidi_local =
  9703. params->initial_max_stream_data_bidi_local;
  9704. conn->early.transport_params.initial_max_stream_data_bidi_remote =
  9705. params->initial_max_stream_data_bidi_remote;
  9706. conn->early.transport_params.initial_max_stream_data_uni =
  9707. params->initial_max_stream_data_uni;
  9708. conn->early.transport_params.initial_max_data = params->initial_max_data;
  9709. conn->early.transport_params.active_connection_id_limit =
  9710. params->active_connection_id_limit;
  9711. conn->early.transport_params.max_datagram_frame_size =
  9712. params->max_datagram_frame_size;
  9713. conn_sync_stream_id_limit(conn);
  9714. conn->tx.max_offset = p->initial_max_data;
  9715. ngtcp2_qlog_parameters_set_transport_params(&conn->qlog, p, conn->server,
  9716. NGTCP2_QLOG_SIDE_REMOTE);
  9717. return 0;
  9718. }
  9719. int ngtcp2_conn_set_local_transport_params_versioned(
  9720. ngtcp2_conn *conn, int transport_params_version,
  9721. const ngtcp2_transport_params *params) {
  9722. ngtcp2_transport_params paramsbuf;
  9723. params = ngtcp2_transport_params_convert_to_latest(
  9724. &paramsbuf, transport_params_version, params);
  9725. assert(conn->server);
  9726. assert(params->active_connection_id_limit >=
  9727. NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT);
  9728. assert(params->active_connection_id_limit <= NGTCP2_MAX_DCID_POOL_SIZE);
  9729. if (conn->hs_pktns == NULL || conn->hs_pktns->crypto.tx.ckm) {
  9730. return NGTCP2_ERR_INVALID_STATE;
  9731. }
  9732. conn_set_local_transport_params(conn, params);
  9733. return 0;
  9734. }
  9735. int ngtcp2_conn_commit_local_transport_params(ngtcp2_conn *conn) {
  9736. const ngtcp2_mem *mem = conn->mem;
  9737. ngtcp2_transport_params *params = &conn->local.transport_params;
  9738. ngtcp2_scid *scident;
  9739. int rv;
  9740. assert(1 == ngtcp2_ksl_len(&conn->scid.set));
  9741. params->initial_scid = conn->oscid;
  9742. params->initial_scid_present = 1;
  9743. if (conn->oscid.datalen == 0) {
  9744. params->preferred_addr_present = 0;
  9745. }
  9746. if (conn->server && params->preferred_addr_present) {
  9747. scident = ngtcp2_mem_malloc(mem, sizeof(*scident));
  9748. if (scident == NULL) {
  9749. return NGTCP2_ERR_NOMEM;
  9750. }
  9751. ngtcp2_scid_init(scident, 1, &params->preferred_addr.cid);
  9752. rv = ngtcp2_ksl_insert(&conn->scid.set, NULL, &scident->cid, scident);
  9753. if (rv != 0) {
  9754. ngtcp2_mem_free(mem, scident);
  9755. return rv;
  9756. }
  9757. conn->scid.last_seq = 1;
  9758. }
  9759. conn->rx.window = conn->rx.unsent_max_offset = conn->rx.max_offset =
  9760. params->initial_max_data;
  9761. conn->remote.bidi.unsent_max_streams = params->initial_max_streams_bidi;
  9762. conn->remote.bidi.max_streams = params->initial_max_streams_bidi;
  9763. conn->remote.uni.unsent_max_streams = params->initial_max_streams_uni;
  9764. conn->remote.uni.max_streams = params->initial_max_streams_uni;
  9765. conn->flags |= NGTCP2_CONN_FLAG_LOCAL_TRANSPORT_PARAMS_COMMITTED;
  9766. ngtcp2_qlog_parameters_set_transport_params(&conn->qlog, params, conn->server,
  9767. NGTCP2_QLOG_SIDE_LOCAL);
  9768. return 0;
  9769. }
  9770. const ngtcp2_transport_params *
  9771. ngtcp2_conn_get_local_transport_params(ngtcp2_conn *conn) {
  9772. return &conn->local.transport_params;
  9773. }
  9774. ngtcp2_ssize ngtcp2_conn_encode_local_transport_params(ngtcp2_conn *conn,
  9775. uint8_t *dest,
  9776. size_t destlen) {
  9777. return ngtcp2_transport_params_encode(dest, destlen,
  9778. &conn->local.transport_params);
  9779. }
  9780. int ngtcp2_conn_open_bidi_stream(ngtcp2_conn *conn, int64_t *pstream_id,
  9781. void *stream_user_data) {
  9782. int rv;
  9783. ngtcp2_strm *strm;
  9784. if (ngtcp2_conn_get_streams_bidi_left(conn) == 0) {
  9785. return NGTCP2_ERR_STREAM_ID_BLOCKED;
  9786. }
  9787. strm = ngtcp2_objalloc_strm_get(&conn->strm_objalloc);
  9788. if (strm == NULL) {
  9789. return NGTCP2_ERR_NOMEM;
  9790. }
  9791. rv = ngtcp2_conn_init_stream(conn, strm, conn->local.bidi.next_stream_id,
  9792. stream_user_data);
  9793. if (rv != 0) {
  9794. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, strm);
  9795. return rv;
  9796. }
  9797. *pstream_id = conn->local.bidi.next_stream_id;
  9798. conn->local.bidi.next_stream_id += 4;
  9799. return 0;
  9800. }
  9801. int ngtcp2_conn_open_uni_stream(ngtcp2_conn *conn, int64_t *pstream_id,
  9802. void *stream_user_data) {
  9803. int rv;
  9804. ngtcp2_strm *strm;
  9805. if (ngtcp2_conn_get_streams_uni_left(conn) == 0) {
  9806. return NGTCP2_ERR_STREAM_ID_BLOCKED;
  9807. }
  9808. strm = ngtcp2_objalloc_strm_get(&conn->strm_objalloc);
  9809. if (strm == NULL) {
  9810. return NGTCP2_ERR_NOMEM;
  9811. }
  9812. rv = ngtcp2_conn_init_stream(conn, strm, conn->local.uni.next_stream_id,
  9813. stream_user_data);
  9814. if (rv != 0) {
  9815. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, strm);
  9816. return rv;
  9817. }
  9818. ngtcp2_strm_shutdown(strm, NGTCP2_STRM_FLAG_SHUT_RD);
  9819. *pstream_id = conn->local.uni.next_stream_id;
  9820. conn->local.uni.next_stream_id += 4;
  9821. return 0;
  9822. }
  9823. ngtcp2_strm *ngtcp2_conn_find_stream(ngtcp2_conn *conn, int64_t stream_id) {
  9824. return ngtcp2_map_find(&conn->strms, (uint64_t)stream_id);
  9825. }
  9826. ngtcp2_ssize ngtcp2_conn_write_stream_versioned(
  9827. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  9828. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_ssize *pdatalen,
  9829. uint32_t flags, int64_t stream_id, const uint8_t *data, size_t datalen,
  9830. ngtcp2_tstamp ts) {
  9831. ngtcp2_vec datav;
  9832. datav.len = datalen;
  9833. datav.base = (uint8_t *)data;
  9834. return ngtcp2_conn_writev_stream_versioned(conn, path, pkt_info_version, pi,
  9835. dest, destlen, pdatalen, flags,
  9836. stream_id, &datav, 1, ts);
  9837. }
  9838. static ngtcp2_ssize conn_write_vmsg_wrapper(ngtcp2_conn *conn,
  9839. ngtcp2_path *path,
  9840. int pkt_info_version,
  9841. ngtcp2_pkt_info *pi, uint8_t *dest,
  9842. size_t destlen, ngtcp2_vmsg *vmsg,
  9843. ngtcp2_tstamp ts) {
  9844. ngtcp2_conn_stat *cstat = &conn->cstat;
  9845. ngtcp2_ssize nwrite;
  9846. nwrite = ngtcp2_conn_write_vmsg(conn, path, pkt_info_version, pi, dest,
  9847. destlen, vmsg, ts);
  9848. if (nwrite < 0) {
  9849. return nwrite;
  9850. }
  9851. if (cstat->bytes_in_flight >= cstat->cwnd) {
  9852. conn->rst.is_cwnd_limited = 1;
  9853. } else if (conn->rst.app_limited == 0 &&
  9854. (cstat->cwnd >= cstat->ssthresh ||
  9855. cstat->bytes_in_flight * 2 < cstat->cwnd) &&
  9856. nwrite == 0 && conn_pacing_pkt_tx_allowed(conn, ts) &&
  9857. (conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED)) {
  9858. conn->rst.app_limited = conn->rst.delivered + cstat->bytes_in_flight;
  9859. if (conn->rst.app_limited == 0) {
  9860. conn->rst.app_limited = cstat->max_tx_udp_payload_size;
  9861. }
  9862. }
  9863. return nwrite;
  9864. }
  9865. ngtcp2_ssize ngtcp2_conn_writev_stream_versioned(
  9866. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  9867. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_ssize *pdatalen,
  9868. uint32_t flags, int64_t stream_id, const ngtcp2_vec *datav, size_t datavcnt,
  9869. ngtcp2_tstamp ts) {
  9870. ngtcp2_vmsg vmsg, *pvmsg;
  9871. ngtcp2_strm *strm;
  9872. int64_t datalen;
  9873. if (pdatalen) {
  9874. *pdatalen = -1;
  9875. }
  9876. if (stream_id != -1) {
  9877. strm = ngtcp2_conn_find_stream(conn, stream_id);
  9878. if (strm == NULL) {
  9879. return NGTCP2_ERR_STREAM_NOT_FOUND;
  9880. }
  9881. if (strm->flags & NGTCP2_STRM_FLAG_SHUT_WR) {
  9882. return NGTCP2_ERR_STREAM_SHUT_WR;
  9883. }
  9884. datalen = ngtcp2_vec_len_varint(datav, datavcnt);
  9885. if (datalen == -1) {
  9886. return NGTCP2_ERR_INVALID_ARGUMENT;
  9887. }
  9888. if (datalen == 0 && !(flags & NGTCP2_WRITE_STREAM_FLAG_FIN) &&
  9889. (strm->flags & NGTCP2_STRM_FLAG_ANY_SENT)) {
  9890. pvmsg = NULL;
  9891. } else {
  9892. if ((uint64_t)datalen > NGTCP2_MAX_VARINT - strm->tx.offset ||
  9893. (uint64_t)datalen > NGTCP2_MAX_VARINT - conn->tx.offset) {
  9894. return NGTCP2_ERR_INVALID_ARGUMENT;
  9895. }
  9896. vmsg.type = NGTCP2_VMSG_TYPE_STREAM;
  9897. vmsg.stream.strm = strm;
  9898. vmsg.stream.flags = flags;
  9899. vmsg.stream.data = datav;
  9900. vmsg.stream.datacnt = datavcnt;
  9901. vmsg.stream.pdatalen = pdatalen;
  9902. pvmsg = &vmsg;
  9903. }
  9904. } else {
  9905. pvmsg = NULL;
  9906. }
  9907. return conn_write_vmsg_wrapper(conn, path, pkt_info_version, pi, dest,
  9908. destlen, pvmsg, ts);
  9909. }
  9910. ngtcp2_ssize ngtcp2_conn_write_datagram_versioned(
  9911. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  9912. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, int *paccepted,
  9913. uint32_t flags, uint64_t dgram_id, const uint8_t *data, size_t datalen,
  9914. ngtcp2_tstamp ts) {
  9915. ngtcp2_vec datav;
  9916. datav.len = datalen;
  9917. datav.base = (uint8_t *)data;
  9918. return ngtcp2_conn_writev_datagram_versioned(conn, path, pkt_info_version, pi,
  9919. dest, destlen, paccepted, flags,
  9920. dgram_id, &datav, 1, ts);
  9921. }
  9922. ngtcp2_ssize ngtcp2_conn_writev_datagram_versioned(
  9923. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  9924. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, int *paccepted,
  9925. uint32_t flags, uint64_t dgram_id, const ngtcp2_vec *datav, size_t datavcnt,
  9926. ngtcp2_tstamp ts) {
  9927. ngtcp2_vmsg vmsg;
  9928. int64_t datalen;
  9929. if (paccepted) {
  9930. *paccepted = 0;
  9931. }
  9932. if (conn->remote.transport_params == NULL ||
  9933. conn->remote.transport_params->max_datagram_frame_size == 0) {
  9934. return NGTCP2_ERR_INVALID_STATE;
  9935. }
  9936. datalen = ngtcp2_vec_len_varint(datav, datavcnt);
  9937. if (datalen == -1
  9938. #if UINT64_MAX > SIZE_MAX
  9939. || (uint64_t)datalen > SIZE_MAX
  9940. #endif /* UINT64_MAX > SIZE_MAX */
  9941. ) {
  9942. return NGTCP2_ERR_INVALID_ARGUMENT;
  9943. }
  9944. if (conn->remote.transport_params->max_datagram_frame_size <
  9945. ngtcp2_pkt_datagram_framelen((size_t)datalen)) {
  9946. return NGTCP2_ERR_INVALID_ARGUMENT;
  9947. }
  9948. vmsg.type = NGTCP2_VMSG_TYPE_DATAGRAM;
  9949. vmsg.datagram.dgram_id = dgram_id;
  9950. vmsg.datagram.flags = flags;
  9951. vmsg.datagram.data = datav;
  9952. vmsg.datagram.datacnt = datavcnt;
  9953. vmsg.datagram.paccepted = paccepted;
  9954. return conn_write_vmsg_wrapper(conn, path, pkt_info_version, pi, dest,
  9955. destlen, &vmsg, ts);
  9956. }
  9957. ngtcp2_ssize ngtcp2_conn_write_vmsg(ngtcp2_conn *conn, ngtcp2_path *path,
  9958. int pkt_info_version, ngtcp2_pkt_info *pi,
  9959. uint8_t *dest, size_t destlen,
  9960. ngtcp2_vmsg *vmsg, ngtcp2_tstamp ts) {
  9961. ngtcp2_ssize nwrite;
  9962. size_t origlen;
  9963. size_t origdestlen = destlen;
  9964. int rv;
  9965. uint8_t wflags = NGTCP2_WRITE_PKT_FLAG_NONE;
  9966. int ppe_pending = (conn->flags & NGTCP2_CONN_FLAG_PPE_PENDING) != 0;
  9967. ngtcp2_conn_stat *cstat = &conn->cstat;
  9968. ngtcp2_ssize res = 0;
  9969. uint64_t server_tx_left;
  9970. int64_t prev_in_pkt_num = -1;
  9971. ngtcp2_ksl_it it;
  9972. ngtcp2_rtb_entry *rtbent;
  9973. (void)pkt_info_version;
  9974. conn_update_timestamp(conn, ts);
  9975. if (path) {
  9976. ngtcp2_path_copy(path, &conn->dcid.current.ps.path);
  9977. }
  9978. origlen = destlen =
  9979. conn_shape_udp_payload(conn, &conn->dcid.current, destlen);
  9980. if (!ppe_pending && pi) {
  9981. pi->ecn = NGTCP2_ECN_NOT_ECT;
  9982. }
  9983. switch (conn->state) {
  9984. case NGTCP2_CS_CLIENT_INITIAL:
  9985. case NGTCP2_CS_CLIENT_WAIT_HANDSHAKE:
  9986. if (!conn_pacing_pkt_tx_allowed(conn, ts)) {
  9987. assert(!ppe_pending);
  9988. return conn_write_handshake_ack_pkts(conn, pi, dest, origlen, ts);
  9989. }
  9990. nwrite = conn_client_write_handshake(conn, pi, dest, destlen, vmsg, ts);
  9991. /* We might be unable to write a packet because of depletion of
  9992. congestion window budget, perhaps due to packet loss that
  9993. shrinks the window drastically. */
  9994. if (nwrite <= 0) {
  9995. return nwrite;
  9996. }
  9997. if (conn->state != NGTCP2_CS_POST_HANDSHAKE) {
  9998. return nwrite;
  9999. }
  10000. assert(nwrite);
  10001. assert(dest[0] & NGTCP2_HEADER_FORM_BIT);
  10002. assert(conn->negotiated_version);
  10003. if (nwrite < NGTCP2_MAX_UDP_PAYLOAD_SIZE &&
  10004. ngtcp2_pkt_get_type_long(conn->negotiated_version, dest[0]) ==
  10005. NGTCP2_PKT_INITIAL) {
  10006. wflags |= NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING;
  10007. }
  10008. res = nwrite;
  10009. dest += nwrite;
  10010. destlen -= (size_t)nwrite;
  10011. /* Break here so that we can coalesces 1RTT packet. */
  10012. break;
  10013. case NGTCP2_CS_SERVER_INITIAL:
  10014. case NGTCP2_CS_SERVER_WAIT_HANDSHAKE:
  10015. if (!conn_pacing_pkt_tx_allowed(conn, ts)) {
  10016. assert(!ppe_pending);
  10017. if (!(conn->dcid.current.flags & NGTCP2_DCID_FLAG_PATH_VALIDATED)) {
  10018. server_tx_left = conn_server_tx_left(conn, &conn->dcid.current);
  10019. if (server_tx_left == 0) {
  10020. return 0;
  10021. }
  10022. origlen = (size_t)ngtcp2_min_uint64((uint64_t)origlen, server_tx_left);
  10023. }
  10024. return conn_write_handshake_ack_pkts(conn, pi, dest, origlen, ts);
  10025. }
  10026. if (!ppe_pending) {
  10027. if (!(conn->dcid.current.flags & NGTCP2_DCID_FLAG_PATH_VALIDATED)) {
  10028. server_tx_left = conn_server_tx_left(conn, &conn->dcid.current);
  10029. if (server_tx_left == 0) {
  10030. if (cstat->loss_detection_timer != UINT64_MAX) {
  10031. ngtcp2_log_info(
  10032. &conn->log, NGTCP2_LOG_EVENT_LDC,
  10033. "loss detection timer canceled due to amplification limit");
  10034. ngtcp2_conn_cancel_loss_detection_timer(conn);
  10035. }
  10036. return 0;
  10037. }
  10038. destlen = (size_t)ngtcp2_min_uint64((uint64_t)destlen, server_tx_left);
  10039. }
  10040. if (conn->in_pktns) {
  10041. it = ngtcp2_rtb_head(&conn->in_pktns->rtb);
  10042. if (!ngtcp2_ksl_it_end(&it)) {
  10043. rtbent = ngtcp2_ksl_it_get(&it);
  10044. prev_in_pkt_num = rtbent->hd.pkt_num;
  10045. }
  10046. }
  10047. nwrite = conn_write_handshake(conn, pi, dest, destlen,
  10048. /* write_datalen = */ 0, ts);
  10049. if (nwrite < 0) {
  10050. return nwrite;
  10051. }
  10052. res = nwrite;
  10053. dest += nwrite;
  10054. destlen -= (size_t)nwrite;
  10055. if (res < NGTCP2_MAX_UDP_PAYLOAD_SIZE && conn->in_pktns && nwrite > 0) {
  10056. it = ngtcp2_rtb_head(&conn->in_pktns->rtb);
  10057. if (!ngtcp2_ksl_it_end(&it)) {
  10058. rtbent = ngtcp2_ksl_it_get(&it);
  10059. if (rtbent->hd.pkt_num != prev_in_pkt_num &&
  10060. (rtbent->flags & NGTCP2_RTB_ENTRY_FLAG_ACK_ELICITING)) {
  10061. wflags |= NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING;
  10062. }
  10063. }
  10064. }
  10065. }
  10066. if (conn->pktns.crypto.tx.ckm == NULL) {
  10067. return res;
  10068. }
  10069. break;
  10070. case NGTCP2_CS_POST_HANDSHAKE:
  10071. if (!conn_pacing_pkt_tx_allowed(conn, ts)) {
  10072. assert(!ppe_pending);
  10073. if (conn->server &&
  10074. !(conn->dcid.current.flags & NGTCP2_DCID_FLAG_PATH_VALIDATED)) {
  10075. server_tx_left = conn_server_tx_left(conn, &conn->dcid.current);
  10076. if (server_tx_left == 0) {
  10077. return 0;
  10078. }
  10079. origlen = (size_t)ngtcp2_min_uint64((uint64_t)origlen, server_tx_left);
  10080. }
  10081. return conn_write_ack_pkt(conn, pi, dest, origlen, NGTCP2_PKT_1RTT, ts);
  10082. }
  10083. break;
  10084. case NGTCP2_CS_CLOSING:
  10085. return NGTCP2_ERR_CLOSING;
  10086. case NGTCP2_CS_DRAINING:
  10087. return NGTCP2_ERR_DRAINING;
  10088. default:
  10089. return 0;
  10090. }
  10091. assert(conn->pktns.crypto.tx.ckm);
  10092. if (conn_check_pkt_num_exhausted(conn)) {
  10093. return NGTCP2_ERR_PKT_NUM_EXHAUSTED;
  10094. }
  10095. if (vmsg) {
  10096. switch (vmsg->type) {
  10097. case NGTCP2_VMSG_TYPE_STREAM:
  10098. if (vmsg->stream.flags & NGTCP2_WRITE_STREAM_FLAG_MORE) {
  10099. wflags |= NGTCP2_WRITE_PKT_FLAG_MORE;
  10100. }
  10101. break;
  10102. case NGTCP2_VMSG_TYPE_DATAGRAM:
  10103. if (vmsg->datagram.flags & NGTCP2_WRITE_DATAGRAM_FLAG_MORE) {
  10104. wflags |= NGTCP2_WRITE_PKT_FLAG_MORE;
  10105. }
  10106. break;
  10107. default:
  10108. break;
  10109. }
  10110. }
  10111. if (ppe_pending) {
  10112. res = conn->pkt.hs_spktlen;
  10113. if (conn->pkt.require_padding) {
  10114. wflags |= NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING;
  10115. }
  10116. /* dest and destlen have already been adjusted in ppe in the first
  10117. run. They are adjusted for probe packet later. */
  10118. nwrite = conn_write_pkt(conn, pi, dest, destlen, (size_t)res, vmsg,
  10119. NGTCP2_PKT_1RTT, wflags, ts);
  10120. goto fin;
  10121. } else {
  10122. conn->pkt.require_padding =
  10123. (wflags & NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING);
  10124. if (conn->state == NGTCP2_CS_POST_HANDSHAKE) {
  10125. rv = conn_prepare_key_update(conn, ts);
  10126. if (rv != 0) {
  10127. return rv;
  10128. }
  10129. }
  10130. if (!conn->pktns.rtb.probe_pkt_left && conn_cwnd_is_zero(conn)) {
  10131. destlen = 0;
  10132. } else {
  10133. if (res == 0) {
  10134. nwrite =
  10135. conn_write_path_response(conn, path, pi, dest, origdestlen, ts);
  10136. if (nwrite) {
  10137. goto fin;
  10138. }
  10139. if (conn->pv) {
  10140. nwrite =
  10141. conn_write_path_challenge(conn, path, pi, dest, origdestlen, ts);
  10142. if (nwrite) {
  10143. goto fin;
  10144. }
  10145. }
  10146. if (conn->pmtud &&
  10147. (conn->dcid.current.flags & NGTCP2_DCID_FLAG_PATH_VALIDATED) &&
  10148. (!conn->hs_pktns ||
  10149. ngtcp2_strm_streamfrq_empty(&conn->hs_pktns->crypto.strm))) {
  10150. nwrite = conn_write_pmtud_probe(conn, pi, dest, origdestlen, ts);
  10151. if (nwrite) {
  10152. goto fin;
  10153. }
  10154. }
  10155. }
  10156. }
  10157. if (conn->server &&
  10158. !(conn->dcid.current.flags & NGTCP2_DCID_FLAG_PATH_VALIDATED)) {
  10159. server_tx_left = conn_server_tx_left(conn, &conn->dcid.current);
  10160. origlen = (size_t)ngtcp2_min_uint64((uint64_t)origlen, server_tx_left);
  10161. destlen = (size_t)ngtcp2_min_uint64((uint64_t)destlen, server_tx_left);
  10162. if (server_tx_left == 0 &&
  10163. conn->cstat.loss_detection_timer != UINT64_MAX) {
  10164. ngtcp2_log_info(
  10165. &conn->log, NGTCP2_LOG_EVENT_LDC,
  10166. "loss detection timer canceled due to amplification limit");
  10167. ngtcp2_conn_cancel_loss_detection_timer(conn);
  10168. }
  10169. }
  10170. }
  10171. if (res == 0) {
  10172. if (conn_handshake_remnants_left(conn)) {
  10173. if (conn_handshake_probe_left(conn) ||
  10174. /* Allow exceeding CWND if an Handshake packet needs to be
  10175. sent in order to avoid dead lock. In some situation,
  10176. typically for client, 1 RTT packets may occupy in-flight
  10177. bytes (e.g., some large requests and PMTUD), and
  10178. Handshake packet loss shrinks CWND, and we may get in the
  10179. situation that we are unable to send Handshake packet. */
  10180. (conn->hs_pktns->rtb.num_pto_eliciting == 0 &&
  10181. !ngtcp2_strm_streamfrq_empty(&conn->hs_pktns->crypto.strm))) {
  10182. destlen = origlen;
  10183. }
  10184. nwrite = conn_write_handshake_pkts(conn, pi, dest, destlen,
  10185. /* write_datalen = */ 0, ts);
  10186. if (nwrite < 0) {
  10187. return nwrite;
  10188. }
  10189. if (nwrite > 0) {
  10190. res = nwrite;
  10191. dest += nwrite;
  10192. destlen -= (size_t)nwrite;
  10193. } else if (destlen == 0) {
  10194. res = conn_write_handshake_ack_pkts(conn, pi, dest, origlen, ts);
  10195. if (res) {
  10196. return res;
  10197. }
  10198. }
  10199. }
  10200. }
  10201. if (conn->pktns.rtb.probe_pkt_left) {
  10202. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_CON,
  10203. "transmit probe pkt left=%zu",
  10204. conn->pktns.rtb.probe_pkt_left);
  10205. nwrite = conn_write_pkt(conn, pi, dest, destlen, (size_t)res, vmsg,
  10206. NGTCP2_PKT_1RTT, wflags, ts);
  10207. goto fin;
  10208. }
  10209. nwrite = conn_write_pkt(conn, pi, dest, destlen, (size_t)res, vmsg,
  10210. NGTCP2_PKT_1RTT, wflags, ts);
  10211. if (nwrite) {
  10212. assert(nwrite != NGTCP2_ERR_NOBUF);
  10213. goto fin;
  10214. }
  10215. if (res == 0) {
  10216. nwrite = conn_write_ack_pkt(conn, pi, dest, origlen, NGTCP2_PKT_1RTT, ts);
  10217. }
  10218. fin:
  10219. if (nwrite >= 0) {
  10220. res += nwrite;
  10221. return res;
  10222. }
  10223. switch (nwrite) {
  10224. case NGTCP2_ERR_STREAM_DATA_BLOCKED:
  10225. if (!(wflags & NGTCP2_WRITE_PKT_FLAG_MORE)) {
  10226. if (res) {
  10227. return res;
  10228. }
  10229. break;
  10230. }
  10231. /* fall through */
  10232. case NGTCP2_ERR_WRITE_MORE:
  10233. conn->pkt.hs_spktlen = res;
  10234. break;
  10235. }
  10236. return nwrite;
  10237. }
  10238. static ngtcp2_ssize
  10239. conn_write_connection_close(ngtcp2_conn *conn, ngtcp2_pkt_info *pi,
  10240. uint8_t *dest, size_t destlen, uint8_t pkt_type,
  10241. uint64_t error_code, const uint8_t *reason,
  10242. size_t reasonlen, ngtcp2_tstamp ts) {
  10243. ngtcp2_pktns *in_pktns = conn->in_pktns;
  10244. ngtcp2_pktns *hs_pktns = conn->hs_pktns;
  10245. ngtcp2_ssize res = 0, nwrite;
  10246. ngtcp2_frame fr;
  10247. uint8_t flags = NGTCP2_WRITE_PKT_FLAG_NONE;
  10248. fr.type = NGTCP2_FRAME_CONNECTION_CLOSE;
  10249. fr.connection_close.error_code = error_code;
  10250. fr.connection_close.frame_type = 0;
  10251. fr.connection_close.reasonlen = reasonlen;
  10252. fr.connection_close.reason = (uint8_t *)reason;
  10253. if (!(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED) &&
  10254. pkt_type != NGTCP2_PKT_INITIAL) {
  10255. if (in_pktns && conn->server) {
  10256. nwrite = ngtcp2_conn_write_single_frame_pkt(
  10257. conn, pi, dest, destlen, NGTCP2_PKT_INITIAL, NGTCP2_WRITE_PKT_FLAG_NONE,
  10258. &conn->dcid.current.cid, &fr, NGTCP2_RTB_ENTRY_FLAG_NONE, NULL, ts);
  10259. if (nwrite < 0) {
  10260. return nwrite;
  10261. }
  10262. dest += nwrite;
  10263. destlen -= (size_t)nwrite;
  10264. res += nwrite;
  10265. }
  10266. if (pkt_type != NGTCP2_PKT_HANDSHAKE && hs_pktns &&
  10267. hs_pktns->crypto.tx.ckm) {
  10268. nwrite = ngtcp2_conn_write_single_frame_pkt(
  10269. conn, pi, dest, destlen, NGTCP2_PKT_HANDSHAKE,
  10270. NGTCP2_WRITE_PKT_FLAG_NONE, &conn->dcid.current.cid, &fr,
  10271. NGTCP2_RTB_ENTRY_FLAG_NONE, NULL, ts);
  10272. if (nwrite < 0) {
  10273. return nwrite;
  10274. }
  10275. dest += nwrite;
  10276. destlen -= (size_t)nwrite;
  10277. res += nwrite;
  10278. }
  10279. }
  10280. if (!conn->server && pkt_type == NGTCP2_PKT_INITIAL) {
  10281. flags = NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING;
  10282. }
  10283. nwrite = ngtcp2_conn_write_single_frame_pkt(
  10284. conn, pi, dest, destlen, pkt_type, flags, &conn->dcid.current.cid, &fr,
  10285. NGTCP2_RTB_ENTRY_FLAG_NONE, NULL, ts);
  10286. if (nwrite < 0) {
  10287. return nwrite;
  10288. }
  10289. res += nwrite;
  10290. if (res == 0) {
  10291. return NGTCP2_ERR_NOBUF;
  10292. }
  10293. return res;
  10294. }
  10295. ngtcp2_ssize ngtcp2_conn_write_connection_close_pkt(
  10296. ngtcp2_conn *conn, ngtcp2_path *path, ngtcp2_pkt_info *pi, uint8_t *dest,
  10297. size_t destlen, uint64_t error_code, const uint8_t *reason, size_t reasonlen,
  10298. ngtcp2_tstamp ts) {
  10299. ngtcp2_pktns *in_pktns = conn->in_pktns;
  10300. ngtcp2_pktns *hs_pktns = conn->hs_pktns;
  10301. uint8_t pkt_type;
  10302. ngtcp2_ssize nwrite;
  10303. uint64_t server_tx_left;
  10304. if (conn_check_pkt_num_exhausted(conn)) {
  10305. return NGTCP2_ERR_PKT_NUM_EXHAUSTED;
  10306. }
  10307. switch (conn->state) {
  10308. case NGTCP2_CS_CLIENT_INITIAL:
  10309. return NGTCP2_ERR_INVALID_STATE;
  10310. case NGTCP2_CS_CLOSING:
  10311. case NGTCP2_CS_DRAINING:
  10312. return 0;
  10313. default:
  10314. break;
  10315. }
  10316. if (path) {
  10317. ngtcp2_path_copy(path, &conn->dcid.current.ps.path);
  10318. }
  10319. destlen = conn_shape_udp_payload(conn, &conn->dcid.current, destlen);
  10320. if (pi) {
  10321. pi->ecn = NGTCP2_ECN_NOT_ECT;
  10322. }
  10323. if (conn->server) {
  10324. server_tx_left = conn_server_tx_left(conn, &conn->dcid.current);
  10325. destlen = (size_t)ngtcp2_min_uint64((uint64_t)destlen, server_tx_left);
  10326. }
  10327. if (conn->state == NGTCP2_CS_POST_HANDSHAKE ||
  10328. (conn->server && conn->pktns.crypto.tx.ckm)) {
  10329. pkt_type = NGTCP2_PKT_1RTT;
  10330. } else if (hs_pktns && hs_pktns->crypto.tx.ckm) {
  10331. pkt_type = NGTCP2_PKT_HANDSHAKE;
  10332. } else if (in_pktns && in_pktns->crypto.tx.ckm) {
  10333. pkt_type = NGTCP2_PKT_INITIAL;
  10334. } else {
  10335. /* This branch is taken if server has not read any Initial packet
  10336. from client. */
  10337. return NGTCP2_ERR_INVALID_STATE;
  10338. }
  10339. nwrite = conn_write_connection_close(conn, pi, dest, destlen, pkt_type,
  10340. error_code, reason, reasonlen, ts);
  10341. if (nwrite < 0) {
  10342. return nwrite;
  10343. }
  10344. conn->state = NGTCP2_CS_CLOSING;
  10345. return nwrite;
  10346. }
  10347. ngtcp2_ssize ngtcp2_conn_write_application_close_pkt(
  10348. ngtcp2_conn *conn, ngtcp2_path *path, ngtcp2_pkt_info *pi, uint8_t *dest,
  10349. size_t destlen, uint64_t app_error_code, const uint8_t *reason,
  10350. size_t reasonlen, ngtcp2_tstamp ts) {
  10351. ngtcp2_ssize nwrite;
  10352. ngtcp2_ssize res = 0;
  10353. ngtcp2_frame fr;
  10354. uint64_t server_tx_left;
  10355. if (conn_check_pkt_num_exhausted(conn)) {
  10356. return NGTCP2_ERR_PKT_NUM_EXHAUSTED;
  10357. }
  10358. switch (conn->state) {
  10359. case NGTCP2_CS_CLIENT_INITIAL:
  10360. return NGTCP2_ERR_INVALID_STATE;
  10361. case NGTCP2_CS_CLOSING:
  10362. case NGTCP2_CS_DRAINING:
  10363. return 0;
  10364. default:
  10365. break;
  10366. }
  10367. if (path) {
  10368. ngtcp2_path_copy(path, &conn->dcid.current.ps.path);
  10369. }
  10370. destlen = conn_shape_udp_payload(conn, &conn->dcid.current, destlen);
  10371. if (pi) {
  10372. pi->ecn = NGTCP2_ECN_NOT_ECT;
  10373. }
  10374. if (conn->server) {
  10375. server_tx_left = conn_server_tx_left(conn, &conn->dcid.current);
  10376. destlen = (size_t)ngtcp2_min_uint64((uint64_t)destlen, server_tx_left);
  10377. }
  10378. if (!(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED)) {
  10379. nwrite = conn_write_connection_close(
  10380. conn, pi, dest, destlen,
  10381. conn->hs_pktns->crypto.tx.ckm ? NGTCP2_PKT_HANDSHAKE : NGTCP2_PKT_INITIAL,
  10382. NGTCP2_APPLICATION_ERROR, NULL, 0, ts);
  10383. if (nwrite < 0) {
  10384. return nwrite;
  10385. }
  10386. res = nwrite;
  10387. dest += nwrite;
  10388. destlen -= (size_t)nwrite;
  10389. }
  10390. if (conn->state != NGTCP2_CS_POST_HANDSHAKE &&
  10391. (!conn->server || !conn->pktns.crypto.tx.ckm)) {
  10392. return res;
  10393. }
  10394. assert(conn->pktns.crypto.tx.ckm);
  10395. fr.type = NGTCP2_FRAME_CONNECTION_CLOSE_APP;
  10396. fr.connection_close.error_code = app_error_code;
  10397. fr.connection_close.frame_type = 0;
  10398. fr.connection_close.reasonlen = reasonlen;
  10399. fr.connection_close.reason = (uint8_t *)reason;
  10400. nwrite = ngtcp2_conn_write_single_frame_pkt(
  10401. conn, pi, dest, destlen, NGTCP2_PKT_1RTT, NGTCP2_WRITE_PKT_FLAG_NONE,
  10402. &conn->dcid.current.cid, &fr, NGTCP2_RTB_ENTRY_FLAG_NONE, NULL, ts);
  10403. if (nwrite < 0) {
  10404. return nwrite;
  10405. }
  10406. res += nwrite;
  10407. if (res == 0) {
  10408. return NGTCP2_ERR_NOBUF;
  10409. }
  10410. conn->state = NGTCP2_CS_CLOSING;
  10411. return res;
  10412. }
  10413. static void ccerr_init(ngtcp2_ccerr *ccerr, ngtcp2_ccerr_type type,
  10414. uint64_t error_code, const uint8_t *reason,
  10415. size_t reasonlen) {
  10416. ccerr->type = type;
  10417. ccerr->error_code = error_code;
  10418. ccerr->frame_type = 0;
  10419. ccerr->reason = (uint8_t *)reason;
  10420. ccerr->reasonlen = reasonlen;
  10421. }
  10422. void ngtcp2_ccerr_default(ngtcp2_ccerr *ccerr) {
  10423. ccerr_init(ccerr, NGTCP2_CCERR_TYPE_TRANSPORT, NGTCP2_NO_ERROR, NULL, 0);
  10424. }
  10425. void ngtcp2_ccerr_set_transport_error(ngtcp2_ccerr *ccerr, uint64_t error_code,
  10426. const uint8_t *reason, size_t reasonlen) {
  10427. ccerr_init(ccerr, NGTCP2_CCERR_TYPE_TRANSPORT, error_code, reason, reasonlen);
  10428. }
  10429. void ngtcp2_ccerr_set_liberr(ngtcp2_ccerr *ccerr, int liberr,
  10430. const uint8_t *reason, size_t reasonlen) {
  10431. switch (liberr) {
  10432. case NGTCP2_ERR_RECV_VERSION_NEGOTIATION:
  10433. ccerr_init(ccerr, NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION, NGTCP2_NO_ERROR,
  10434. reason, reasonlen);
  10435. return;
  10436. case NGTCP2_ERR_IDLE_CLOSE:
  10437. ccerr_init(ccerr, NGTCP2_CCERR_TYPE_IDLE_CLOSE, NGTCP2_NO_ERROR, reason,
  10438. reasonlen);
  10439. return;
  10440. case NGTCP2_ERR_DROP_CONN:
  10441. ccerr_init(ccerr, NGTCP2_CCERR_TYPE_DROP_CONN, NGTCP2_NO_ERROR, reason,
  10442. reasonlen);
  10443. return;
  10444. case NGTCP2_ERR_RETRY:
  10445. ccerr_init(ccerr, NGTCP2_CCERR_TYPE_RETRY, NGTCP2_NO_ERROR, reason,
  10446. reasonlen);
  10447. return;
  10448. };
  10449. ngtcp2_ccerr_set_transport_error(
  10450. ccerr, ngtcp2_err_infer_quic_transport_error_code(liberr), reason,
  10451. reasonlen);
  10452. }
  10453. void ngtcp2_ccerr_set_tls_alert(ngtcp2_ccerr *ccerr, uint8_t tls_alert,
  10454. const uint8_t *reason, size_t reasonlen) {
  10455. ngtcp2_ccerr_set_transport_error(ccerr, NGTCP2_CRYPTO_ERROR | tls_alert,
  10456. reason, reasonlen);
  10457. }
  10458. void ngtcp2_ccerr_set_application_error(ngtcp2_ccerr *ccerr,
  10459. uint64_t error_code,
  10460. const uint8_t *reason,
  10461. size_t reasonlen) {
  10462. ccerr_init(ccerr, NGTCP2_CCERR_TYPE_APPLICATION, error_code, reason,
  10463. reasonlen);
  10464. }
  10465. ngtcp2_ssize ngtcp2_conn_write_connection_close_versioned(
  10466. ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
  10467. ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, const ngtcp2_ccerr *ccerr,
  10468. ngtcp2_tstamp ts) {
  10469. (void)pkt_info_version;
  10470. conn_update_timestamp(conn, ts);
  10471. switch (ccerr->type) {
  10472. case NGTCP2_CCERR_TYPE_TRANSPORT:
  10473. return ngtcp2_conn_write_connection_close_pkt(
  10474. conn, path, pi, dest, destlen, ccerr->error_code, ccerr->reason,
  10475. ccerr->reasonlen, ts);
  10476. case NGTCP2_CCERR_TYPE_APPLICATION:
  10477. return ngtcp2_conn_write_application_close_pkt(
  10478. conn, path, pi, dest, destlen, ccerr->error_code, ccerr->reason,
  10479. ccerr->reasonlen, ts);
  10480. default:
  10481. return 0;
  10482. }
  10483. }
  10484. int ngtcp2_conn_in_closing_period(ngtcp2_conn *conn) {
  10485. return conn->state == NGTCP2_CS_CLOSING;
  10486. }
  10487. int ngtcp2_conn_in_draining_period(ngtcp2_conn *conn) {
  10488. return conn->state == NGTCP2_CS_DRAINING;
  10489. }
  10490. int ngtcp2_conn_close_stream(ngtcp2_conn *conn, ngtcp2_strm *strm) {
  10491. int rv;
  10492. rv = conn_call_stream_close(conn, strm);
  10493. if (rv != 0) {
  10494. return rv;
  10495. }
  10496. rv = ngtcp2_map_remove(&conn->strms, (ngtcp2_map_key_type)strm->stream_id);
  10497. if (rv != 0) {
  10498. assert(rv != NGTCP2_ERR_INVALID_ARGUMENT);
  10499. return rv;
  10500. }
  10501. if (ngtcp2_strm_is_tx_queued(strm)) {
  10502. ngtcp2_pq_remove(&conn->tx.strmq, &strm->pe);
  10503. }
  10504. ngtcp2_strm_free(strm);
  10505. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, strm);
  10506. return 0;
  10507. }
  10508. int ngtcp2_conn_close_stream_if_shut_rdwr(ngtcp2_conn *conn,
  10509. ngtcp2_strm *strm) {
  10510. if ((strm->flags & NGTCP2_STRM_FLAG_SHUT_RDWR) ==
  10511. NGTCP2_STRM_FLAG_SHUT_RDWR &&
  10512. ((strm->flags & NGTCP2_STRM_FLAG_RESET_STREAM_RECVED) ||
  10513. ngtcp2_strm_rx_offset(strm) == strm->rx.last_offset) &&
  10514. (((strm->flags & NGTCP2_STRM_FLAG_RESET_STREAM) &&
  10515. (strm->flags & NGTCP2_STRM_FLAG_RESET_STREAM_ACKED)) ||
  10516. ngtcp2_strm_is_all_tx_data_fin_acked(strm))) {
  10517. return ngtcp2_conn_close_stream(conn, strm);
  10518. }
  10519. return 0;
  10520. }
  10521. /*
  10522. * conn_shutdown_stream_write closes send stream with error code
  10523. * |app_error_code|. RESET_STREAM frame is scheduled.
  10524. *
  10525. * This function returns 0 if it succeeds, or one of the following
  10526. * negative error codes:
  10527. *
  10528. * NGTCP2_ERR_NOMEM
  10529. * Out of memory.
  10530. */
  10531. static int conn_shutdown_stream_write(ngtcp2_conn *conn, ngtcp2_strm *strm,
  10532. uint64_t app_error_code) {
  10533. ngtcp2_strm_set_app_error_code(strm, app_error_code);
  10534. if ((strm->flags & NGTCP2_STRM_FLAG_RESET_STREAM) ||
  10535. ngtcp2_strm_is_all_tx_data_fin_acked(strm)) {
  10536. return 0;
  10537. }
  10538. /* Set this flag so that we don't accidentally send DATA to this
  10539. stream. */
  10540. strm->flags |= NGTCP2_STRM_FLAG_SHUT_WR | NGTCP2_STRM_FLAG_RESET_STREAM;
  10541. ngtcp2_strm_streamfrq_clear(strm);
  10542. return conn_reset_stream(conn, strm, app_error_code);
  10543. }
  10544. /*
  10545. * conn_shutdown_stream_read closes read stream with error code
  10546. * |app_error_code|. STOP_SENDING frame is scheduled.
  10547. *
  10548. * This function returns 0 if it succeeds, or one of the following
  10549. * negative error codes:
  10550. *
  10551. * NGTCP2_ERR_NOMEM
  10552. * Out of memory.
  10553. */
  10554. static int conn_shutdown_stream_read(ngtcp2_conn *conn, ngtcp2_strm *strm,
  10555. uint64_t app_error_code) {
  10556. ngtcp2_strm_set_app_error_code(strm, app_error_code);
  10557. if (strm->flags &
  10558. (NGTCP2_STRM_FLAG_STOP_SENDING | NGTCP2_STRM_FLAG_RESET_STREAM_RECVED)) {
  10559. return 0;
  10560. }
  10561. if ((strm->flags & NGTCP2_STRM_FLAG_SHUT_RD) &&
  10562. ngtcp2_strm_rx_offset(strm) == strm->rx.last_offset) {
  10563. return 0;
  10564. }
  10565. /* Extend connection flow control window for the amount of data
  10566. which are not passed to application. */
  10567. if (!(strm->flags & NGTCP2_STRM_FLAG_RESET_STREAM_RECVED)) {
  10568. ngtcp2_conn_extend_max_offset(conn, strm->rx.last_offset -
  10569. ngtcp2_strm_rx_offset(strm));
  10570. }
  10571. strm->flags |= NGTCP2_STRM_FLAG_STOP_SENDING;
  10572. ngtcp2_strm_discard_reordered_data(strm);
  10573. return conn_stop_sending(conn, strm, app_error_code);
  10574. }
  10575. int ngtcp2_conn_shutdown_stream(ngtcp2_conn *conn, uint32_t flags,
  10576. int64_t stream_id, uint64_t app_error_code) {
  10577. int rv;
  10578. ngtcp2_strm *strm;
  10579. (void)flags;
  10580. strm = ngtcp2_conn_find_stream(conn, stream_id);
  10581. if (strm == NULL) {
  10582. return 0;
  10583. }
  10584. if (bidi_stream(stream_id) || !conn_local_stream(conn, stream_id)) {
  10585. rv = conn_shutdown_stream_read(conn, strm, app_error_code);
  10586. if (rv != 0) {
  10587. return rv;
  10588. }
  10589. }
  10590. if (bidi_stream(stream_id) || conn_local_stream(conn, stream_id)) {
  10591. rv = conn_shutdown_stream_write(conn, strm, app_error_code);
  10592. if (rv != 0) {
  10593. return rv;
  10594. }
  10595. }
  10596. return 0;
  10597. }
  10598. int ngtcp2_conn_shutdown_stream_write(ngtcp2_conn *conn, uint32_t flags,
  10599. int64_t stream_id,
  10600. uint64_t app_error_code) {
  10601. ngtcp2_strm *strm;
  10602. (void)flags;
  10603. if (!bidi_stream(stream_id) && !conn_local_stream(conn, stream_id)) {
  10604. return NGTCP2_ERR_INVALID_ARGUMENT;
  10605. }
  10606. strm = ngtcp2_conn_find_stream(conn, stream_id);
  10607. if (strm == NULL) {
  10608. return 0;
  10609. }
  10610. return conn_shutdown_stream_write(conn, strm, app_error_code);
  10611. }
  10612. int ngtcp2_conn_shutdown_stream_read(ngtcp2_conn *conn, uint32_t flags,
  10613. int64_t stream_id,
  10614. uint64_t app_error_code) {
  10615. ngtcp2_strm *strm;
  10616. (void)flags;
  10617. if (!bidi_stream(stream_id) && conn_local_stream(conn, stream_id)) {
  10618. return NGTCP2_ERR_INVALID_ARGUMENT;
  10619. }
  10620. strm = ngtcp2_conn_find_stream(conn, stream_id);
  10621. if (strm == NULL) {
  10622. return 0;
  10623. }
  10624. return conn_shutdown_stream_read(conn, strm, app_error_code);
  10625. }
  10626. /*
  10627. * conn_extend_max_stream_offset extends stream level flow control
  10628. * window by |datalen| of the stream denoted by |strm|.
  10629. *
  10630. * This function returns 0 if it succeeds, or one of the following
  10631. * negative error codes:
  10632. *
  10633. * NGTCP2_ERR_NOMEM
  10634. * Out of memory.
  10635. */
  10636. static int conn_extend_max_stream_offset(ngtcp2_conn *conn, ngtcp2_strm *strm,
  10637. uint64_t datalen) {
  10638. ngtcp2_strm *top;
  10639. if (datalen > NGTCP2_MAX_VARINT ||
  10640. strm->rx.unsent_max_offset > NGTCP2_MAX_VARINT - datalen) {
  10641. strm->rx.unsent_max_offset = NGTCP2_MAX_VARINT;
  10642. } else {
  10643. strm->rx.unsent_max_offset += datalen;
  10644. }
  10645. if (!(strm->flags &
  10646. (NGTCP2_STRM_FLAG_SHUT_RD | NGTCP2_STRM_FLAG_STOP_SENDING)) &&
  10647. !ngtcp2_strm_is_tx_queued(strm) &&
  10648. conn_should_send_max_stream_data(conn, strm)) {
  10649. if (!ngtcp2_pq_empty(&conn->tx.strmq)) {
  10650. top = ngtcp2_conn_tx_strmq_top(conn);
  10651. strm->cycle = top->cycle;
  10652. }
  10653. strm->cycle = conn_tx_strmq_first_cycle(conn);
  10654. return ngtcp2_conn_tx_strmq_push(conn, strm);
  10655. }
  10656. return 0;
  10657. }
  10658. int ngtcp2_conn_extend_max_stream_offset(ngtcp2_conn *conn, int64_t stream_id,
  10659. uint64_t datalen) {
  10660. ngtcp2_strm *strm;
  10661. if (!bidi_stream(stream_id) && conn_local_stream(conn, stream_id)) {
  10662. return NGTCP2_ERR_INVALID_ARGUMENT;
  10663. }
  10664. strm = ngtcp2_conn_find_stream(conn, stream_id);
  10665. if (strm == NULL) {
  10666. return 0;
  10667. }
  10668. return conn_extend_max_stream_offset(conn, strm, datalen);
  10669. }
  10670. void ngtcp2_conn_extend_max_offset(ngtcp2_conn *conn, uint64_t datalen) {
  10671. if (NGTCP2_MAX_VARINT < datalen ||
  10672. conn->rx.unsent_max_offset > NGTCP2_MAX_VARINT - datalen) {
  10673. conn->rx.unsent_max_offset = NGTCP2_MAX_VARINT;
  10674. return;
  10675. }
  10676. conn->rx.unsent_max_offset += datalen;
  10677. }
  10678. void ngtcp2_conn_extend_max_streams_bidi(ngtcp2_conn *conn, size_t n) {
  10679. handle_max_remote_streams_extension(&conn->remote.bidi.unsent_max_streams, n);
  10680. }
  10681. void ngtcp2_conn_extend_max_streams_uni(ngtcp2_conn *conn, size_t n) {
  10682. handle_max_remote_streams_extension(&conn->remote.uni.unsent_max_streams, n);
  10683. }
  10684. const ngtcp2_cid *ngtcp2_conn_get_dcid(ngtcp2_conn *conn) {
  10685. return &conn->dcid.current.cid;
  10686. }
  10687. const ngtcp2_cid *ngtcp2_conn_get_client_initial_dcid(ngtcp2_conn *conn) {
  10688. return &conn->rcid;
  10689. }
  10690. uint32_t ngtcp2_conn_get_client_chosen_version(ngtcp2_conn *conn) {
  10691. return conn->client_chosen_version;
  10692. }
  10693. uint32_t ngtcp2_conn_get_negotiated_version(ngtcp2_conn *conn) {
  10694. return conn->negotiated_version;
  10695. }
  10696. static int delete_strms_pq_each(void *data, void *ptr) {
  10697. ngtcp2_conn *conn = ptr;
  10698. ngtcp2_strm *s = data;
  10699. if (ngtcp2_strm_is_tx_queued(s)) {
  10700. ngtcp2_pq_remove(&conn->tx.strmq, &s->pe);
  10701. }
  10702. ngtcp2_strm_free(s);
  10703. ngtcp2_objalloc_strm_release(&conn->strm_objalloc, s);
  10704. return 0;
  10705. }
  10706. /*
  10707. * conn_discard_early_data_state discards any connection states which
  10708. * are altered by any operations during early data transfer.
  10709. */
  10710. static void conn_discard_early_data_state(ngtcp2_conn *conn) {
  10711. ngtcp2_frame_chain **pfrc, *frc;
  10712. ngtcp2_rtb_remove_early_data(&conn->pktns.rtb, &conn->cstat);
  10713. ngtcp2_map_each(&conn->strms, delete_strms_pq_each, conn);
  10714. ngtcp2_map_clear(&conn->strms);
  10715. conn->tx.offset = 0;
  10716. conn->tx.last_blocked_offset = UINT64_MAX;
  10717. conn->rx.unsent_max_offset = conn->rx.max_offset =
  10718. conn->local.transport_params.initial_max_data;
  10719. conn->remote.bidi.unsent_max_streams = conn->remote.bidi.max_streams =
  10720. conn->local.transport_params.initial_max_streams_bidi;
  10721. conn->remote.uni.unsent_max_streams = conn->remote.uni.max_streams =
  10722. conn->local.transport_params.initial_max_streams_uni;
  10723. if (conn->server) {
  10724. conn->local.bidi.next_stream_id = 1;
  10725. conn->local.uni.next_stream_id = 3;
  10726. } else {
  10727. conn->local.bidi.next_stream_id = 0;
  10728. conn->local.uni.next_stream_id = 2;
  10729. }
  10730. for (pfrc = &conn->pktns.tx.frq; *pfrc;) {
  10731. frc = *pfrc;
  10732. *pfrc = (*pfrc)->next;
  10733. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  10734. }
  10735. }
  10736. int ngtcp2_conn_tls_early_data_rejected(ngtcp2_conn *conn) {
  10737. if (conn->flags & NGTCP2_CONN_FLAG_EARLY_DATA_REJECTED) {
  10738. return 0;
  10739. }
  10740. conn->flags |= NGTCP2_CONN_FLAG_EARLY_DATA_REJECTED;
  10741. conn_discard_early_data_state(conn);
  10742. if (conn->callbacks.tls_early_data_rejected) {
  10743. return conn->callbacks.tls_early_data_rejected(conn, conn->user_data);
  10744. }
  10745. if (conn->early.ckm) {
  10746. conn_discard_early_key(conn);
  10747. }
  10748. return 0;
  10749. }
  10750. int ngtcp2_conn_get_tls_early_data_rejected(ngtcp2_conn *conn) {
  10751. return (conn->flags & NGTCP2_CONN_FLAG_EARLY_DATA_REJECTED) != 0;
  10752. }
  10753. int ngtcp2_conn_update_rtt(ngtcp2_conn *conn, ngtcp2_duration rtt,
  10754. ngtcp2_duration ack_delay, ngtcp2_tstamp ts) {
  10755. ngtcp2_conn_stat *cstat = &conn->cstat;
  10756. assert(rtt > 0);
  10757. if (cstat->min_rtt == UINT64_MAX) {
  10758. cstat->latest_rtt = rtt;
  10759. cstat->min_rtt = rtt;
  10760. cstat->smoothed_rtt = rtt;
  10761. cstat->rttvar = rtt / 2;
  10762. cstat->first_rtt_sample_ts = ts;
  10763. } else {
  10764. if (conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED) {
  10765. assert(conn->remote.transport_params);
  10766. ack_delay = ngtcp2_min_uint64(
  10767. ack_delay, conn->remote.transport_params->max_ack_delay);
  10768. } else if (ack_delay > 0 && rtt >= cstat->min_rtt &&
  10769. rtt < cstat->min_rtt + ack_delay) {
  10770. /* Ignore RTT sample if adjusting ack_delay causes the sample
  10771. less than min_rtt before handshake confirmation. */
  10772. ngtcp2_log_info(
  10773. &conn->log, NGTCP2_LOG_EVENT_LDC,
  10774. "ignore rtt sample because ack_delay is too large latest_rtt=%" PRIu64
  10775. " min_rtt=%" PRIu64 " ack_delay=%" PRIu64,
  10776. rtt / NGTCP2_MILLISECONDS, cstat->min_rtt / NGTCP2_MILLISECONDS,
  10777. ack_delay / NGTCP2_MILLISECONDS);
  10778. return NGTCP2_ERR_INVALID_ARGUMENT;
  10779. }
  10780. cstat->latest_rtt = rtt;
  10781. cstat->min_rtt = ngtcp2_min_uint64(cstat->min_rtt, rtt);
  10782. if (rtt >= cstat->min_rtt + ack_delay) {
  10783. rtt -= ack_delay;
  10784. }
  10785. cstat->rttvar = (cstat->rttvar * 3 + (cstat->smoothed_rtt < rtt
  10786. ? rtt - cstat->smoothed_rtt
  10787. : cstat->smoothed_rtt - rtt)) /
  10788. 4;
  10789. cstat->smoothed_rtt = (cstat->smoothed_rtt * 7 + rtt) / 8;
  10790. }
  10791. ngtcp2_log_info(
  10792. &conn->log, NGTCP2_LOG_EVENT_LDC,
  10793. "latest_rtt=%" PRIu64 " min_rtt=%" PRIu64 " smoothed_rtt=%" PRIu64
  10794. " rttvar=%" PRIu64 " ack_delay=%" PRIu64,
  10795. cstat->latest_rtt / NGTCP2_MILLISECONDS,
  10796. cstat->min_rtt / NGTCP2_MILLISECONDS,
  10797. cstat->smoothed_rtt / NGTCP2_MILLISECONDS,
  10798. cstat->rttvar / NGTCP2_MILLISECONDS, ack_delay / NGTCP2_MILLISECONDS);
  10799. return 0;
  10800. }
  10801. void ngtcp2_conn_get_conn_info_versioned(ngtcp2_conn *conn,
  10802. int conn_info_version,
  10803. ngtcp2_conn_info *cinfo) {
  10804. const ngtcp2_conn_stat *cstat = &conn->cstat;
  10805. (void)conn_info_version;
  10806. cinfo->latest_rtt = cstat->latest_rtt;
  10807. cinfo->min_rtt = cstat->min_rtt;
  10808. cinfo->smoothed_rtt = cstat->smoothed_rtt;
  10809. cinfo->rttvar = cstat->rttvar;
  10810. cinfo->cwnd = cstat->cwnd;
  10811. cinfo->ssthresh = cstat->ssthresh;
  10812. cinfo->bytes_in_flight = cstat->bytes_in_flight;
  10813. }
  10814. static void conn_get_loss_time_and_pktns(ngtcp2_conn *conn,
  10815. ngtcp2_tstamp *ploss_time,
  10816. ngtcp2_pktns **ppktns) {
  10817. ngtcp2_pktns *const ns[] = {conn->hs_pktns, &conn->pktns};
  10818. ngtcp2_conn_stat *cstat = &conn->cstat;
  10819. ngtcp2_duration *loss_time = cstat->loss_time + 1;
  10820. ngtcp2_tstamp earliest_loss_time = cstat->loss_time[NGTCP2_PKTNS_ID_INITIAL];
  10821. ngtcp2_pktns *pktns = conn->in_pktns;
  10822. size_t i;
  10823. for (i = 0; i < ngtcp2_arraylen(ns); ++i) {
  10824. if (ns[i] == NULL || loss_time[i] >= earliest_loss_time) {
  10825. continue;
  10826. }
  10827. earliest_loss_time = loss_time[i];
  10828. pktns = ns[i];
  10829. }
  10830. if (ploss_time) {
  10831. *ploss_time = earliest_loss_time;
  10832. }
  10833. if (ppktns) {
  10834. *ppktns = pktns;
  10835. }
  10836. }
  10837. static ngtcp2_tstamp conn_get_earliest_pto_expiry(ngtcp2_conn *conn,
  10838. ngtcp2_tstamp ts) {
  10839. ngtcp2_pktns *ns[] = {conn->in_pktns, conn->hs_pktns, &conn->pktns};
  10840. size_t i;
  10841. ngtcp2_tstamp earliest_ts = UINT64_MAX, t;
  10842. ngtcp2_conn_stat *cstat = &conn->cstat;
  10843. ngtcp2_tstamp *times = cstat->last_tx_pkt_ts;
  10844. ngtcp2_duration duration =
  10845. compute_pto(cstat->smoothed_rtt, cstat->rttvar, /* max_ack_delay = */ 0) *
  10846. (1ULL << cstat->pto_count);
  10847. for (i = NGTCP2_PKTNS_ID_INITIAL; i < NGTCP2_PKTNS_ID_MAX; ++i) {
  10848. if (ns[i] == NULL || ns[i]->rtb.num_pto_eliciting == 0 ||
  10849. (times[i] == UINT64_MAX ||
  10850. (i == NGTCP2_PKTNS_ID_APPLICATION &&
  10851. !(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED)))) {
  10852. continue;
  10853. }
  10854. t = times[i] + duration;
  10855. if (i == NGTCP2_PKTNS_ID_APPLICATION) {
  10856. assert(conn->remote.transport_params);
  10857. t += conn->remote.transport_params->max_ack_delay *
  10858. (1ULL << cstat->pto_count);
  10859. }
  10860. if (t < earliest_ts) {
  10861. earliest_ts = t;
  10862. }
  10863. }
  10864. if (earliest_ts == UINT64_MAX) {
  10865. return ts + duration;
  10866. }
  10867. return earliest_ts;
  10868. }
  10869. void ngtcp2_conn_set_loss_detection_timer(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  10870. ngtcp2_conn_stat *cstat = &conn->cstat;
  10871. ngtcp2_duration timeout;
  10872. ngtcp2_pktns *in_pktns = conn->in_pktns;
  10873. ngtcp2_pktns *hs_pktns = conn->hs_pktns;
  10874. ngtcp2_pktns *pktns = &conn->pktns;
  10875. ngtcp2_tstamp earliest_loss_time;
  10876. conn_get_loss_time_and_pktns(conn, &earliest_loss_time, NULL);
  10877. if (earliest_loss_time != UINT64_MAX) {
  10878. cstat->loss_detection_timer = earliest_loss_time;
  10879. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_LDC,
  10880. "loss_detection_timer=%" PRIu64 " nonzero crypto loss time",
  10881. cstat->loss_detection_timer);
  10882. return;
  10883. }
  10884. if ((!in_pktns || in_pktns->rtb.num_pto_eliciting == 0) &&
  10885. (!hs_pktns || hs_pktns->rtb.num_pto_eliciting == 0) &&
  10886. (pktns->rtb.num_pto_eliciting == 0 ||
  10887. !(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED)) &&
  10888. (conn->server ||
  10889. (conn->flags & (NGTCP2_CONN_FLAG_SERVER_ADDR_VERIFIED |
  10890. NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED)))) {
  10891. if (cstat->loss_detection_timer != UINT64_MAX) {
  10892. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_LDC,
  10893. "loss detection timer canceled");
  10894. ngtcp2_conn_cancel_loss_detection_timer(conn);
  10895. }
  10896. return;
  10897. }
  10898. cstat->loss_detection_timer = conn_get_earliest_pto_expiry(conn, ts);
  10899. timeout =
  10900. cstat->loss_detection_timer > ts ? cstat->loss_detection_timer - ts : 0;
  10901. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_LDC,
  10902. "loss_detection_timer=%" PRIu64 " timeout=%" PRIu64,
  10903. cstat->loss_detection_timer, timeout / NGTCP2_MILLISECONDS);
  10904. }
  10905. void ngtcp2_conn_cancel_loss_detection_timer(ngtcp2_conn *conn) {
  10906. ngtcp2_conn_stat *cstat = &conn->cstat;
  10907. cstat->loss_detection_timer = UINT64_MAX;
  10908. cstat->pto_count = 0;
  10909. }
  10910. int ngtcp2_conn_on_loss_detection_timer(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  10911. ngtcp2_conn_stat *cstat = &conn->cstat;
  10912. int rv;
  10913. ngtcp2_pktns *in_pktns = conn->in_pktns;
  10914. ngtcp2_pktns *hs_pktns = conn->hs_pktns;
  10915. ngtcp2_tstamp earliest_loss_time;
  10916. ngtcp2_pktns *loss_pktns = NULL;
  10917. switch (conn->state) {
  10918. case NGTCP2_CS_CLOSING:
  10919. case NGTCP2_CS_DRAINING:
  10920. ngtcp2_conn_cancel_loss_detection_timer(conn);
  10921. return 0;
  10922. default:
  10923. break;
  10924. }
  10925. if (cstat->loss_detection_timer == UINT64_MAX) {
  10926. return 0;
  10927. }
  10928. conn_get_loss_time_and_pktns(conn, &earliest_loss_time, &loss_pktns);
  10929. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_LDC,
  10930. "loss detection timer fired");
  10931. if (earliest_loss_time != UINT64_MAX) {
  10932. assert(loss_pktns);
  10933. rv = ngtcp2_conn_detect_lost_pkt(conn, loss_pktns, cstat, ts);
  10934. if (rv != 0) {
  10935. return rv;
  10936. }
  10937. ngtcp2_conn_set_loss_detection_timer(conn, ts);
  10938. return 0;
  10939. }
  10940. if (!conn->server && !conn_is_tls_handshake_completed(conn)) {
  10941. if (hs_pktns->crypto.tx.ckm) {
  10942. hs_pktns->rtb.probe_pkt_left = 1;
  10943. } else {
  10944. in_pktns->rtb.probe_pkt_left = 1;
  10945. }
  10946. } else {
  10947. if (in_pktns && in_pktns->rtb.num_pto_eliciting) {
  10948. in_pktns->rtb.probe_pkt_left = 1;
  10949. assert(hs_pktns);
  10950. if (conn->server && hs_pktns->rtb.num_pto_eliciting) {
  10951. /* let server coalesce packets */
  10952. hs_pktns->rtb.probe_pkt_left = 1;
  10953. }
  10954. } else if (hs_pktns && hs_pktns->rtb.num_pto_eliciting) {
  10955. hs_pktns->rtb.probe_pkt_left = 2;
  10956. } else {
  10957. conn->pktns.rtb.probe_pkt_left = 2;
  10958. }
  10959. }
  10960. ++cstat->pto_count;
  10961. ngtcp2_log_info(&conn->log, NGTCP2_LOG_EVENT_LDC, "pto_count=%zu",
  10962. cstat->pto_count);
  10963. ngtcp2_conn_set_loss_detection_timer(conn, ts);
  10964. return 0;
  10965. }
  10966. static int conn_buffer_crypto_data(ngtcp2_conn *conn, const uint8_t **pdata,
  10967. ngtcp2_pktns *pktns, const uint8_t *data,
  10968. size_t datalen) {
  10969. int rv;
  10970. ngtcp2_buf_chain **pbufchain = &pktns->crypto.tx.data;
  10971. if (*pbufchain) {
  10972. for (; (*pbufchain)->next; pbufchain = &(*pbufchain)->next)
  10973. ;
  10974. if (ngtcp2_buf_left(&(*pbufchain)->buf) < datalen) {
  10975. pbufchain = &(*pbufchain)->next;
  10976. }
  10977. }
  10978. if (!*pbufchain) {
  10979. rv = ngtcp2_buf_chain_new(pbufchain, ngtcp2_max_size(1024, datalen),
  10980. conn->mem);
  10981. if (rv != 0) {
  10982. return rv;
  10983. }
  10984. }
  10985. *pdata = (*pbufchain)->buf.last;
  10986. (*pbufchain)->buf.last = ngtcp2_cpymem((*pbufchain)->buf.last, data, datalen);
  10987. return 0;
  10988. }
  10989. int ngtcp2_conn_submit_crypto_data(ngtcp2_conn *conn,
  10990. ngtcp2_encryption_level encryption_level,
  10991. const uint8_t *data, const size_t datalen) {
  10992. ngtcp2_pktns *pktns;
  10993. ngtcp2_frame_chain *frc;
  10994. ngtcp2_stream *fr;
  10995. int rv;
  10996. if (datalen == 0) {
  10997. return 0;
  10998. }
  10999. switch (encryption_level) {
  11000. case NGTCP2_ENCRYPTION_LEVEL_INITIAL:
  11001. assert(conn->in_pktns);
  11002. pktns = conn->in_pktns;
  11003. break;
  11004. case NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE:
  11005. assert(conn->hs_pktns);
  11006. pktns = conn->hs_pktns;
  11007. break;
  11008. case NGTCP2_ENCRYPTION_LEVEL_1RTT:
  11009. pktns = &conn->pktns;
  11010. break;
  11011. default:
  11012. return NGTCP2_ERR_INVALID_ARGUMENT;
  11013. }
  11014. rv = conn_buffer_crypto_data(conn, &data, pktns, data, datalen);
  11015. if (rv != 0) {
  11016. return rv;
  11017. }
  11018. rv = ngtcp2_frame_chain_objalloc_new(&frc, &conn->frc_objalloc);
  11019. if (rv != 0) {
  11020. return rv;
  11021. }
  11022. fr = &frc->fr.stream;
  11023. fr->type = NGTCP2_FRAME_CRYPTO;
  11024. fr->flags = 0;
  11025. fr->fin = 0;
  11026. fr->stream_id = 0;
  11027. fr->offset = pktns->crypto.tx.offset;
  11028. fr->datacnt = 1;
  11029. fr->data[0].len = datalen;
  11030. fr->data[0].base = (uint8_t *)data;
  11031. rv = ngtcp2_strm_streamfrq_push(&pktns->crypto.strm, frc);
  11032. if (rv != 0) {
  11033. ngtcp2_frame_chain_objalloc_del(frc, &conn->frc_objalloc, conn->mem);
  11034. return rv;
  11035. }
  11036. pktns->crypto.strm.tx.offset += datalen;
  11037. pktns->crypto.tx.offset += datalen;
  11038. return 0;
  11039. }
  11040. int ngtcp2_conn_submit_new_token(ngtcp2_conn *conn, const uint8_t *token,
  11041. size_t tokenlen) {
  11042. int rv;
  11043. ngtcp2_frame_chain *nfrc;
  11044. assert(conn->server);
  11045. assert(token);
  11046. assert(tokenlen);
  11047. rv = ngtcp2_frame_chain_new_token_objalloc_new(
  11048. &nfrc, token, tokenlen, &conn->frc_objalloc, conn->mem);
  11049. if (rv != 0) {
  11050. return rv;
  11051. }
  11052. nfrc->next = conn->pktns.tx.frq;
  11053. conn->pktns.tx.frq = nfrc;
  11054. return 0;
  11055. }
  11056. ngtcp2_strm *ngtcp2_conn_tx_strmq_top(ngtcp2_conn *conn) {
  11057. assert(!ngtcp2_pq_empty(&conn->tx.strmq));
  11058. return ngtcp2_struct_of(ngtcp2_pq_top(&conn->tx.strmq), ngtcp2_strm, pe);
  11059. }
  11060. void ngtcp2_conn_tx_strmq_pop(ngtcp2_conn *conn) {
  11061. ngtcp2_strm *strm = ngtcp2_conn_tx_strmq_top(conn);
  11062. assert(strm);
  11063. ngtcp2_pq_pop(&conn->tx.strmq);
  11064. strm->pe.index = NGTCP2_PQ_BAD_INDEX;
  11065. }
  11066. int ngtcp2_conn_tx_strmq_push(ngtcp2_conn *conn, ngtcp2_strm *strm) {
  11067. return ngtcp2_pq_push(&conn->tx.strmq, &strm->pe);
  11068. }
  11069. static int conn_has_uncommitted_preferred_addr_cid(ngtcp2_conn *conn) {
  11070. return conn->server &&
  11071. !(conn->flags & NGTCP2_CONN_FLAG_LOCAL_TRANSPORT_PARAMS_COMMITTED) &&
  11072. conn->oscid.datalen &&
  11073. conn->local.transport_params.preferred_addr_present;
  11074. }
  11075. size_t ngtcp2_conn_get_scid(ngtcp2_conn *conn, ngtcp2_cid *dest) {
  11076. ngtcp2_cid *origdest = dest;
  11077. ngtcp2_ksl_it it;
  11078. ngtcp2_scid *scid;
  11079. if (dest == NULL) {
  11080. return ngtcp2_ksl_len(&conn->scid.set) +
  11081. (size_t)conn_has_uncommitted_preferred_addr_cid(conn);
  11082. }
  11083. for (it = ngtcp2_ksl_begin(&conn->scid.set); !ngtcp2_ksl_it_end(&it);
  11084. ngtcp2_ksl_it_next(&it)) {
  11085. scid = ngtcp2_ksl_it_get(&it);
  11086. *dest++ = scid->cid;
  11087. }
  11088. if (conn_has_uncommitted_preferred_addr_cid(conn)) {
  11089. *dest++ = conn->local.transport_params.preferred_addr.cid;
  11090. }
  11091. return (size_t)(dest - origdest);
  11092. }
  11093. static size_t conn_get_num_active_dcid(ngtcp2_conn *conn) {
  11094. size_t n = 1; /* for conn->dcid.current */
  11095. ngtcp2_pv *pv = conn->pv;
  11096. if (pv) {
  11097. if (pv->dcid.seq != conn->dcid.current.seq) {
  11098. ++n;
  11099. }
  11100. if ((pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  11101. pv->fallback_dcid.seq != conn->dcid.current.seq &&
  11102. pv->fallback_dcid.seq != pv->dcid.seq) {
  11103. ++n;
  11104. }
  11105. }
  11106. n += ngtcp2_ringbuf_len(&conn->dcid.retired.rb);
  11107. return n;
  11108. }
  11109. static void copy_dcid_to_cid_token(ngtcp2_cid_token *dest,
  11110. const ngtcp2_dcid *src) {
  11111. dest->seq = src->seq;
  11112. dest->cid = src->cid;
  11113. ngtcp2_path_storage_init2(&dest->ps, &src->ps.path);
  11114. if ((dest->token_present =
  11115. (src->flags & NGTCP2_DCID_FLAG_TOKEN_PRESENT) != 0)) {
  11116. memcpy(dest->token, src->token, NGTCP2_STATELESS_RESET_TOKENLEN);
  11117. }
  11118. }
  11119. size_t ngtcp2_conn_get_active_dcid(ngtcp2_conn *conn, ngtcp2_cid_token *dest) {
  11120. ngtcp2_pv *pv = conn->pv;
  11121. ngtcp2_cid_token *orig = dest;
  11122. ngtcp2_dcid *dcid;
  11123. size_t len, i;
  11124. if (!(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED)) {
  11125. return 0;
  11126. }
  11127. if (dest == NULL) {
  11128. return conn_get_num_active_dcid(conn);
  11129. }
  11130. copy_dcid_to_cid_token(dest, &conn->dcid.current);
  11131. ++dest;
  11132. if (pv) {
  11133. if (pv->dcid.seq != conn->dcid.current.seq) {
  11134. copy_dcid_to_cid_token(dest, &pv->dcid);
  11135. ++dest;
  11136. }
  11137. if ((pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE) &&
  11138. pv->fallback_dcid.seq != conn->dcid.current.seq &&
  11139. pv->fallback_dcid.seq != pv->dcid.seq) {
  11140. copy_dcid_to_cid_token(dest, &pv->fallback_dcid);
  11141. ++dest;
  11142. }
  11143. }
  11144. len = ngtcp2_ringbuf_len(&conn->dcid.retired.rb);
  11145. for (i = 0; i < len; ++i) {
  11146. dcid = ngtcp2_ringbuf_get(&conn->dcid.retired.rb, i);
  11147. copy_dcid_to_cid_token(dest, dcid);
  11148. ++dest;
  11149. }
  11150. return (size_t)(dest - orig);
  11151. }
  11152. void ngtcp2_conn_set_local_addr(ngtcp2_conn *conn, const ngtcp2_addr *addr) {
  11153. ngtcp2_addr *dest = &conn->dcid.current.ps.path.local;
  11154. assert(addr->addrlen <=
  11155. (ngtcp2_socklen)sizeof(conn->dcid.current.ps.local_addrbuf));
  11156. ngtcp2_addr_copy(dest, addr);
  11157. }
  11158. void ngtcp2_conn_set_path_user_data(ngtcp2_conn *conn, void *path_user_data) {
  11159. conn->dcid.current.ps.path.user_data = path_user_data;
  11160. }
  11161. const ngtcp2_path *ngtcp2_conn_get_path(ngtcp2_conn *conn) {
  11162. return &conn->dcid.current.ps.path;
  11163. }
  11164. size_t ngtcp2_conn_get_max_tx_udp_payload_size(ngtcp2_conn *conn) {
  11165. return conn->local.settings.max_tx_udp_payload_size;
  11166. }
  11167. size_t ngtcp2_conn_get_path_max_tx_udp_payload_size(ngtcp2_conn *conn) {
  11168. if (conn->local.settings.no_tx_udp_payload_size_shaping) {
  11169. return ngtcp2_conn_get_max_tx_udp_payload_size(conn);
  11170. }
  11171. return conn->dcid.current.max_udp_payload_size;
  11172. }
  11173. static int conn_initiate_migration_precheck(ngtcp2_conn *conn,
  11174. const ngtcp2_addr *local_addr) {
  11175. if (!(conn->flags & NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED) ||
  11176. conn->remote.transport_params->disable_active_migration ||
  11177. conn->dcid.current.cid.datalen == 0 ||
  11178. (conn->pv && (conn->pv->flags & NGTCP2_PV_FLAG_PREFERRED_ADDR))) {
  11179. return NGTCP2_ERR_INVALID_STATE;
  11180. }
  11181. if (ngtcp2_ringbuf_len(&conn->dcid.unused.rb) == 0) {
  11182. return NGTCP2_ERR_CONN_ID_BLOCKED;
  11183. }
  11184. if (ngtcp2_addr_eq(&conn->dcid.current.ps.path.local, local_addr)) {
  11185. return NGTCP2_ERR_INVALID_ARGUMENT;
  11186. }
  11187. return 0;
  11188. }
  11189. int ngtcp2_conn_initiate_immediate_migration(ngtcp2_conn *conn,
  11190. const ngtcp2_path *path,
  11191. ngtcp2_tstamp ts) {
  11192. int rv;
  11193. ngtcp2_dcid *dcid;
  11194. ngtcp2_pv *pv;
  11195. assert(!conn->server);
  11196. conn_update_timestamp(conn, ts);
  11197. rv = conn_initiate_migration_precheck(conn, &path->local);
  11198. if (rv != 0) {
  11199. return rv;
  11200. }
  11201. ngtcp2_conn_stop_pmtud(conn);
  11202. if (conn->pv) {
  11203. rv = conn_abort_pv(conn, ts);
  11204. if (rv != 0) {
  11205. return rv;
  11206. }
  11207. }
  11208. rv = conn_retire_dcid(conn, &conn->dcid.current, ts);
  11209. if (rv != 0) {
  11210. return rv;
  11211. }
  11212. dcid = ngtcp2_ringbuf_get(&conn->dcid.unused.rb, 0);
  11213. ngtcp2_dcid_set_path(dcid, path);
  11214. ngtcp2_dcid_copy(&conn->dcid.current, dcid);
  11215. ngtcp2_ringbuf_pop_front(&conn->dcid.unused.rb);
  11216. conn_reset_congestion_state(conn, ts);
  11217. conn_reset_ecn_validation_state(conn);
  11218. /* TODO It might be better to add a new flag which indicates that a
  11219. connection should be closed if this path validation failed. The
  11220. current design allows an application to continue, by migrating
  11221. into yet another path. */
  11222. rv = ngtcp2_pv_new(&pv, dcid, conn_compute_pv_timeout(conn),
  11223. NGTCP2_PV_FLAG_NONE, &conn->log, conn->mem);
  11224. if (rv != 0) {
  11225. return rv;
  11226. }
  11227. conn->pv = pv;
  11228. return conn_call_activate_dcid(conn, &conn->dcid.current);
  11229. }
  11230. int ngtcp2_conn_initiate_migration(ngtcp2_conn *conn, const ngtcp2_path *path,
  11231. ngtcp2_tstamp ts) {
  11232. int rv;
  11233. ngtcp2_dcid *dcid;
  11234. ngtcp2_pv *pv;
  11235. assert(!conn->server);
  11236. conn_update_timestamp(conn, ts);
  11237. rv = conn_initiate_migration_precheck(conn, &path->local);
  11238. if (rv != 0) {
  11239. return rv;
  11240. }
  11241. if (conn->pv) {
  11242. rv = conn_abort_pv(conn, ts);
  11243. if (rv != 0) {
  11244. return rv;
  11245. }
  11246. }
  11247. dcid = ngtcp2_ringbuf_get(&conn->dcid.unused.rb, 0);
  11248. ngtcp2_dcid_set_path(dcid, path);
  11249. rv = ngtcp2_pv_new(&pv, dcid, conn_compute_pv_timeout(conn),
  11250. NGTCP2_PV_FLAG_NONE, &conn->log, conn->mem);
  11251. if (rv != 0) {
  11252. return rv;
  11253. }
  11254. ngtcp2_ringbuf_pop_front(&conn->dcid.unused.rb);
  11255. conn->pv = pv;
  11256. return conn_call_activate_dcid(conn, &pv->dcid);
  11257. }
  11258. uint64_t ngtcp2_conn_get_max_data_left(ngtcp2_conn *conn) {
  11259. return conn->tx.max_offset - conn->tx.offset;
  11260. }
  11261. uint64_t ngtcp2_conn_get_max_stream_data_left(ngtcp2_conn *conn,
  11262. int64_t stream_id) {
  11263. ngtcp2_strm *strm = ngtcp2_conn_find_stream(conn, stream_id);
  11264. if (strm == NULL) {
  11265. return 0;
  11266. }
  11267. return strm->tx.max_offset - strm->tx.offset;
  11268. }
  11269. uint64_t ngtcp2_conn_get_streams_bidi_left(ngtcp2_conn *conn) {
  11270. uint64_t n = ngtcp2_ord_stream_id(conn->local.bidi.next_stream_id);
  11271. return n > conn->local.bidi.max_streams
  11272. ? 0
  11273. : conn->local.bidi.max_streams - n + 1;
  11274. }
  11275. uint64_t ngtcp2_conn_get_streams_uni_left(ngtcp2_conn *conn) {
  11276. uint64_t n = ngtcp2_ord_stream_id(conn->local.uni.next_stream_id);
  11277. return n > conn->local.uni.max_streams ? 0
  11278. : conn->local.uni.max_streams - n + 1;
  11279. }
  11280. uint64_t ngtcp2_conn_get_cwnd_left(ngtcp2_conn *conn) {
  11281. uint64_t bytes_in_flight = conn->cstat.bytes_in_flight;
  11282. uint64_t cwnd = conn_get_cwnd(conn);
  11283. if (cwnd > bytes_in_flight) {
  11284. return cwnd - bytes_in_flight;
  11285. }
  11286. return 0;
  11287. }
  11288. ngtcp2_tstamp ngtcp2_conn_get_idle_expiry(ngtcp2_conn *conn) {
  11289. ngtcp2_duration trpto;
  11290. ngtcp2_duration idle_timeout;
  11291. /* TODO Remote max_idle_timeout becomes effective after handshake
  11292. completion. */
  11293. if (!conn_is_tls_handshake_completed(conn) ||
  11294. conn->remote.transport_params->max_idle_timeout == 0 ||
  11295. (conn->local.transport_params.max_idle_timeout &&
  11296. conn->local.transport_params.max_idle_timeout <
  11297. conn->remote.transport_params->max_idle_timeout)) {
  11298. idle_timeout = conn->local.transport_params.max_idle_timeout;
  11299. } else {
  11300. idle_timeout = conn->remote.transport_params->max_idle_timeout;
  11301. }
  11302. if (idle_timeout == 0) {
  11303. return UINT64_MAX;
  11304. }
  11305. trpto = 3 * conn_compute_pto(conn, conn_is_tls_handshake_completed(conn)
  11306. ? &conn->pktns
  11307. : conn->hs_pktns);
  11308. idle_timeout = ngtcp2_max_uint64(idle_timeout, trpto);
  11309. if (conn->idle_ts >= UINT64_MAX - idle_timeout) {
  11310. return UINT64_MAX;
  11311. }
  11312. return conn->idle_ts + idle_timeout;
  11313. }
  11314. ngtcp2_duration ngtcp2_conn_get_pto(ngtcp2_conn *conn) {
  11315. return conn_compute_pto(conn, conn_is_tls_handshake_completed(conn)
  11316. ? &conn->pktns
  11317. : conn->hs_pktns);
  11318. }
  11319. void ngtcp2_conn_set_initial_crypto_ctx(ngtcp2_conn *conn,
  11320. const ngtcp2_crypto_ctx *ctx) {
  11321. assert(conn->in_pktns);
  11322. conn->in_pktns->crypto.ctx = *ctx;
  11323. }
  11324. const ngtcp2_crypto_ctx *ngtcp2_conn_get_initial_crypto_ctx(ngtcp2_conn *conn) {
  11325. assert(conn->in_pktns);
  11326. return &conn->in_pktns->crypto.ctx;
  11327. }
  11328. void ngtcp2_conn_set_retry_aead(ngtcp2_conn *conn,
  11329. const ngtcp2_crypto_aead *aead,
  11330. const ngtcp2_crypto_aead_ctx *aead_ctx) {
  11331. assert(!conn->crypto.retry_aead_ctx.native_handle);
  11332. conn->crypto.retry_aead = *aead;
  11333. conn->crypto.retry_aead_ctx = *aead_ctx;
  11334. }
  11335. void ngtcp2_conn_set_crypto_ctx(ngtcp2_conn *conn,
  11336. const ngtcp2_crypto_ctx *ctx) {
  11337. assert(conn->hs_pktns);
  11338. conn->hs_pktns->crypto.ctx = *ctx;
  11339. conn->pktns.crypto.ctx = *ctx;
  11340. }
  11341. const ngtcp2_crypto_ctx *ngtcp2_conn_get_crypto_ctx(ngtcp2_conn *conn) {
  11342. return &conn->pktns.crypto.ctx;
  11343. }
  11344. void ngtcp2_conn_set_0rtt_crypto_ctx(ngtcp2_conn *conn,
  11345. const ngtcp2_crypto_ctx *ctx) {
  11346. conn->early.ctx = *ctx;
  11347. }
  11348. const ngtcp2_crypto_ctx *ngtcp2_conn_get_0rtt_crypto_ctx(ngtcp2_conn *conn) {
  11349. return &conn->early.ctx;
  11350. }
  11351. void *ngtcp2_conn_get_tls_native_handle(ngtcp2_conn *conn) {
  11352. return conn->crypto.tls_native_handle;
  11353. }
  11354. void ngtcp2_conn_set_tls_native_handle(ngtcp2_conn *conn,
  11355. void *tls_native_handle) {
  11356. conn->crypto.tls_native_handle = tls_native_handle;
  11357. }
  11358. const ngtcp2_ccerr *ngtcp2_conn_get_ccerr(ngtcp2_conn *conn) {
  11359. return &conn->rx.ccerr;
  11360. }
  11361. void ngtcp2_conn_set_tls_error(ngtcp2_conn *conn, int liberr) {
  11362. conn->crypto.tls_error = liberr;
  11363. }
  11364. int ngtcp2_conn_get_tls_error(ngtcp2_conn *conn) {
  11365. return conn->crypto.tls_error;
  11366. }
  11367. void ngtcp2_conn_set_tls_alert(ngtcp2_conn *conn, uint8_t alert) {
  11368. conn->crypto.tls_alert = alert;
  11369. }
  11370. uint8_t ngtcp2_conn_get_tls_alert(ngtcp2_conn *conn) {
  11371. return conn->crypto.tls_alert;
  11372. }
  11373. int ngtcp2_conn_is_local_stream(ngtcp2_conn *conn, int64_t stream_id) {
  11374. return conn_local_stream(conn, stream_id);
  11375. }
  11376. int ngtcp2_conn_is_server(ngtcp2_conn *conn) { return conn->server; }
  11377. int ngtcp2_conn_after_retry(ngtcp2_conn *conn) {
  11378. return (conn->flags & NGTCP2_CONN_FLAG_RECV_RETRY) != 0;
  11379. }
  11380. int ngtcp2_conn_set_stream_user_data(ngtcp2_conn *conn, int64_t stream_id,
  11381. void *stream_user_data) {
  11382. ngtcp2_strm *strm = ngtcp2_conn_find_stream(conn, stream_id);
  11383. if (strm == NULL) {
  11384. return NGTCP2_ERR_STREAM_NOT_FOUND;
  11385. }
  11386. strm->stream_user_data = stream_user_data;
  11387. return 0;
  11388. }
  11389. void ngtcp2_conn_update_pkt_tx_time(ngtcp2_conn *conn, ngtcp2_tstamp ts) {
  11390. ngtcp2_duration pacing_interval;
  11391. ngtcp2_duration wait;
  11392. conn_update_timestamp(conn, ts);
  11393. if (conn->tx.pacing.pktlen == 0) {
  11394. return;
  11395. }
  11396. if (conn->cstat.pacing_interval) {
  11397. pacing_interval = conn->cstat.pacing_interval;
  11398. } else {
  11399. /* 1.25 is the under-utilization avoidance factor described in
  11400. https://datatracker.ietf.org/doc/html/rfc9002#section-7.7 */
  11401. pacing_interval = (conn->cstat.first_rtt_sample_ts == UINT64_MAX
  11402. ? NGTCP2_MILLISECONDS
  11403. : conn->cstat.smoothed_rtt) *
  11404. 100 / 125 / conn->cstat.cwnd;
  11405. }
  11406. wait = (ngtcp2_duration)(conn->tx.pacing.pktlen * pacing_interval);
  11407. conn->tx.pacing.next_ts = ts + wait;
  11408. conn->tx.pacing.pktlen = 0;
  11409. }
  11410. size_t ngtcp2_conn_get_send_quantum(ngtcp2_conn *conn) {
  11411. return conn->cstat.send_quantum;
  11412. }
  11413. int ngtcp2_conn_track_retired_dcid_seq(ngtcp2_conn *conn, uint64_t seq) {
  11414. if (conn->dcid.retire_unacked.len >=
  11415. ngtcp2_arraylen(conn->dcid.retire_unacked.seqs)) {
  11416. return NGTCP2_ERR_CONNECTION_ID_LIMIT;
  11417. }
  11418. conn->dcid.retire_unacked.seqs[conn->dcid.retire_unacked.len++] = seq;
  11419. return 0;
  11420. }
  11421. void ngtcp2_conn_untrack_retired_dcid_seq(ngtcp2_conn *conn, uint64_t seq) {
  11422. size_t i;
  11423. for (i = 0; i < conn->dcid.retire_unacked.len; ++i) {
  11424. if (conn->dcid.retire_unacked.seqs[i] != seq) {
  11425. continue;
  11426. }
  11427. if (i != conn->dcid.retire_unacked.len - 1) {
  11428. conn->dcid.retire_unacked.seqs[i] =
  11429. conn->dcid.retire_unacked.seqs[conn->dcid.retire_unacked.len - 1];
  11430. }
  11431. --conn->dcid.retire_unacked.len;
  11432. return;
  11433. }
  11434. }
  11435. int ngtcp2_conn_check_retired_dcid_tracked(ngtcp2_conn *conn, uint64_t seq) {
  11436. size_t i;
  11437. for (i = 0; i < conn->dcid.retire_unacked.len; ++i) {
  11438. if (conn->dcid.retire_unacked.seqs[i] == seq) {
  11439. return 1;
  11440. }
  11441. }
  11442. return 0;
  11443. }
  11444. size_t ngtcp2_conn_get_stream_loss_count(ngtcp2_conn *conn, int64_t stream_id) {
  11445. ngtcp2_strm *strm = ngtcp2_conn_find_stream(conn, stream_id);
  11446. if (strm == NULL) {
  11447. return 0;
  11448. }
  11449. return strm->tx.loss_count;
  11450. }
  11451. void ngtcp2_path_challenge_entry_init(ngtcp2_path_challenge_entry *pcent,
  11452. const ngtcp2_path *path,
  11453. const uint8_t *data) {
  11454. ngtcp2_path_storage_init2(&pcent->ps, path);
  11455. memcpy(pcent->data, data, sizeof(pcent->data));
  11456. }
  11457. /* The functions prefixed with ngtcp2_pkt_ are usually put inside
  11458. ngtcp2_pkt.c. This function uses encryption construct and uses
  11459. test data defined only in ngtcp2_conn_test.c, so it is written
  11460. here. */
  11461. ngtcp2_ssize ngtcp2_pkt_write_connection_close(
  11462. uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid,
  11463. const ngtcp2_cid *scid, uint64_t error_code, const uint8_t *reason,
  11464. size_t reasonlen, ngtcp2_encrypt encrypt, const ngtcp2_crypto_aead *aead,
  11465. const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv,
  11466. ngtcp2_hp_mask hp_mask, const ngtcp2_crypto_cipher *hp,
  11467. const ngtcp2_crypto_cipher_ctx *hp_ctx) {
  11468. ngtcp2_pkt_hd hd;
  11469. ngtcp2_crypto_km ckm;
  11470. ngtcp2_crypto_cc cc;
  11471. ngtcp2_ppe ppe;
  11472. ngtcp2_frame fr = {0};
  11473. int rv;
  11474. ngtcp2_pkt_hd_init(&hd, NGTCP2_PKT_FLAG_LONG_FORM, NGTCP2_PKT_INITIAL, dcid,
  11475. scid, /* pkt_num = */ 0, /* pkt_numlen = */ 1, version,
  11476. /* len = */ 0);
  11477. ngtcp2_vec_init(&ckm.secret, NULL, 0);
  11478. ngtcp2_vec_init(&ckm.iv, iv, 12);
  11479. ckm.aead_ctx = *aead_ctx;
  11480. ckm.pkt_num = 0;
  11481. ckm.flags = NGTCP2_CRYPTO_KM_FLAG_NONE;
  11482. cc.aead = *aead;
  11483. cc.hp = *hp;
  11484. cc.ckm = &ckm;
  11485. cc.hp_ctx = *hp_ctx;
  11486. cc.encrypt = encrypt;
  11487. cc.hp_mask = hp_mask;
  11488. ngtcp2_ppe_init(&ppe, dest, destlen, 0, &cc);
  11489. rv = ngtcp2_ppe_encode_hd(&ppe, &hd);
  11490. if (rv != 0) {
  11491. assert(NGTCP2_ERR_NOBUF == rv);
  11492. return rv;
  11493. }
  11494. if (!ngtcp2_ppe_ensure_hp_sample(&ppe)) {
  11495. return NGTCP2_ERR_NOBUF;
  11496. }
  11497. fr.type = NGTCP2_FRAME_CONNECTION_CLOSE;
  11498. fr.connection_close.error_code = error_code;
  11499. fr.connection_close.reasonlen = reasonlen;
  11500. fr.connection_close.reason = (uint8_t *)reason;
  11501. rv = ngtcp2_ppe_encode_frame(&ppe, &fr);
  11502. if (rv != 0) {
  11503. assert(NGTCP2_ERR_NOBUF == rv);
  11504. return rv;
  11505. }
  11506. return ngtcp2_ppe_final(&ppe, NULL);
  11507. }
  11508. int ngtcp2_is_bidi_stream(int64_t stream_id) { return bidi_stream(stream_id); }
  11509. uint32_t ngtcp2_select_version(const uint32_t *preferred_versions,
  11510. size_t preferred_versionslen,
  11511. const uint32_t *offered_versions,
  11512. size_t offered_versionslen) {
  11513. size_t i, j;
  11514. if (!preferred_versionslen || !offered_versionslen) {
  11515. return 0;
  11516. }
  11517. for (i = 0; i < preferred_versionslen; ++i) {
  11518. assert(ngtcp2_is_supported_version(preferred_versions[i]));
  11519. for (j = 0; j < offered_versionslen; ++j) {
  11520. if (preferred_versions[i] == offered_versions[j]) {
  11521. return preferred_versions[i];
  11522. }
  11523. }
  11524. }
  11525. return 0;
  11526. }