quic_txp.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144
  1. /*
  2. * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/quic_txp.h"
  10. #include "internal/quic_fifd.h"
  11. #include "internal/quic_stream_map.h"
  12. #include "internal/quic_error.h"
  13. #include "internal/common.h"
  14. #include <openssl/err.h>
  15. #define MIN_CRYPTO_HDR_SIZE 3
  16. #define MIN_FRAME_SIZE_HANDSHAKE_DONE 1
  17. #define MIN_FRAME_SIZE_MAX_DATA 2
  18. #define MIN_FRAME_SIZE_ACK 5
  19. #define MIN_FRAME_SIZE_CRYPTO (MIN_CRYPTO_HDR_SIZE + 1)
  20. #define MIN_FRAME_SIZE_STREAM 3 /* minimum useful size (for non-FIN) */
  21. #define MIN_FRAME_SIZE_MAX_STREAMS_BIDI 2
  22. #define MIN_FRAME_SIZE_MAX_STREAMS_UNI 2
  23. /*
  24. * Packet Archetypes
  25. * =================
  26. */
  27. /* Generate normal packets containing most frame types, subject to EL. */
  28. #define TX_PACKETISER_ARCHETYPE_NORMAL 0
  29. /*
  30. * A probe packet is different in that:
  31. * - It bypasses CC, but *is* counted as in flight for purposes of CC;
  32. * - It must be ACK-eliciting.
  33. */
  34. #define TX_PACKETISER_ARCHETYPE_PROBE 1
  35. /*
  36. * An ACK-only packet is different in that:
  37. * - It bypasses CC, and is considered a 'non-inflight' packet;
  38. * - It may not contain anything other than an ACK frame, not even padding.
  39. */
  40. #define TX_PACKETISER_ARCHETYPE_ACK_ONLY 2
  41. #define TX_PACKETISER_ARCHETYPE_NUM 3
  42. struct ossl_quic_tx_packetiser_st {
  43. OSSL_QUIC_TX_PACKETISER_ARGS args;
  44. /*
  45. * Opaque initial token blob provided by caller. TXP frees using the
  46. * callback when it is no longer needed.
  47. */
  48. const unsigned char *initial_token;
  49. size_t initial_token_len;
  50. ossl_quic_initial_token_free_fn *initial_token_free_cb;
  51. void *initial_token_free_cb_arg;
  52. /* Subcomponents of the TXP that we own. */
  53. QUIC_FIFD fifd; /* QUIC Frame-in-Flight Dispatcher */
  54. /* Internal state. */
  55. uint64_t next_pn[QUIC_PN_SPACE_NUM]; /* Next PN to use in given PN space. */
  56. OSSL_TIME last_tx_time; /* Last time a packet was generated, or 0. */
  57. /* Internal state - frame (re)generation flags. */
  58. unsigned int want_handshake_done : 1;
  59. unsigned int want_max_data : 1;
  60. unsigned int want_max_streams_bidi : 1;
  61. unsigned int want_max_streams_uni : 1;
  62. /* Internal state - frame (re)generation flags - per PN space. */
  63. unsigned int want_ack : QUIC_PN_SPACE_NUM;
  64. unsigned int force_ack_eliciting : QUIC_PN_SPACE_NUM;
  65. /*
  66. * Internal state - connection close terminal state.
  67. * Once this is set, it is not unset unlike other want_ flags - we keep
  68. * sending it in every packet.
  69. */
  70. unsigned int want_conn_close : 1;
  71. /* Has the handshake been completed? */
  72. unsigned int handshake_complete : 1;
  73. OSSL_QUIC_FRAME_CONN_CLOSE conn_close_frame;
  74. /*
  75. * Counts of the number of bytes received and sent while in the closing
  76. * state.
  77. */
  78. uint64_t closing_bytes_recv;
  79. uint64_t closing_bytes_xmit;
  80. /* Internal state - packet assembly. */
  81. struct txp_el {
  82. unsigned char *scratch; /* scratch buffer for packet assembly */
  83. size_t scratch_len; /* number of bytes allocated for scratch */
  84. OSSL_QTX_IOVEC *iovec; /* scratch iovec array for use with QTX */
  85. size_t alloc_iovec; /* size of iovec array */
  86. } el[QUIC_ENC_LEVEL_NUM];
  87. /* Message callback related arguments */
  88. ossl_msg_cb msg_callback;
  89. void *msg_callback_arg;
  90. SSL *msg_callback_ssl;
  91. /* Callbacks. */
  92. void (*ack_tx_cb)(const OSSL_QUIC_FRAME_ACK *ack,
  93. uint32_t pn_space,
  94. void *arg);
  95. void *ack_tx_cb_arg;
  96. };
  97. /*
  98. * The TX helper records state used while generating frames into packets. It
  99. * enables serialization into the packet to be done "transactionally" where
  100. * serialization of a frame can be rolled back if it fails midway (e.g. if it
  101. * does not fit).
  102. */
  103. struct tx_helper {
  104. OSSL_QUIC_TX_PACKETISER *txp;
  105. /*
  106. * The Maximum Packet Payload Length in bytes. This is the amount of
  107. * space we have to generate frames into.
  108. */
  109. size_t max_ppl;
  110. /*
  111. * Number of bytes we have generated so far.
  112. */
  113. size_t bytes_appended;
  114. /*
  115. * Number of scratch bytes in txp->scratch we have used so far. Some iovecs
  116. * will reference this scratch buffer. When we need to use more of it (e.g.
  117. * when we need to put frame headers somewhere), we append to the scratch
  118. * buffer, resizing if necessary, and increase this accordingly.
  119. */
  120. size_t scratch_bytes;
  121. /*
  122. * Bytes reserved in the MaxPPL budget. We keep this number of bytes spare
  123. * until reserve_allowed is set to 1. Currently this is always at most 1, as
  124. * a PING frame takes up one byte and this mechanism is only used to ensure
  125. * we can encode a PING frame if we have been asked to ensure a packet is
  126. * ACK-eliciting and we are unusure if we are going to add any other
  127. * ACK-eliciting frames before we reach our MaxPPL budget.
  128. */
  129. size_t reserve;
  130. /*
  131. * Number of iovecs we have currently appended. This is the number of
  132. * entries valid in txp->iovec.
  133. */
  134. size_t num_iovec;
  135. /* The EL this TX helper is being used for. */
  136. uint32_t enc_level;
  137. /*
  138. * Whether we are allowed to make use of the reserve bytes in our MaxPPL
  139. * budget. This is used to ensure we have room to append a PING frame later
  140. * if we need to. Once we know we will not need to append a PING frame, this
  141. * is set to 1.
  142. */
  143. unsigned int reserve_allowed : 1;
  144. /*
  145. * Set to 1 if we have appended a STREAM frame with an implicit length. If
  146. * this happens we should never append another frame after that frame as it
  147. * cannot be validly encoded. This is just a safety check.
  148. */
  149. unsigned int done_implicit : 1;
  150. struct {
  151. /*
  152. * The fields in this structure are valid if active is set, which means
  153. * that a serialization transaction is currently in progress.
  154. */
  155. unsigned char *data;
  156. WPACKET wpkt;
  157. unsigned int active : 1;
  158. } txn;
  159. };
  160. static void tx_helper_rollback(struct tx_helper *h);
  161. static int txp_el_ensure_iovec(struct txp_el *el, size_t num);
  162. /* Initialises the TX helper. */
  163. static int tx_helper_init(struct tx_helper *h, OSSL_QUIC_TX_PACKETISER *txp,
  164. uint32_t enc_level, size_t max_ppl, size_t reserve)
  165. {
  166. if (reserve > max_ppl)
  167. return 0;
  168. h->txp = txp;
  169. h->enc_level = enc_level;
  170. h->max_ppl = max_ppl;
  171. h->reserve = reserve;
  172. h->num_iovec = 0;
  173. h->bytes_appended = 0;
  174. h->scratch_bytes = 0;
  175. h->reserve_allowed = 0;
  176. h->done_implicit = 0;
  177. h->txn.data = NULL;
  178. h->txn.active = 0;
  179. if (max_ppl > h->txp->el[enc_level].scratch_len) {
  180. unsigned char *scratch;
  181. scratch = OPENSSL_realloc(h->txp->el[enc_level].scratch, max_ppl);
  182. if (scratch == NULL)
  183. return 0;
  184. h->txp->el[enc_level].scratch = scratch;
  185. h->txp->el[enc_level].scratch_len = max_ppl;
  186. }
  187. return 1;
  188. }
  189. static void tx_helper_cleanup(struct tx_helper *h)
  190. {
  191. if (h->txn.active)
  192. tx_helper_rollback(h);
  193. h->txp = NULL;
  194. }
  195. static void tx_helper_unrestrict(struct tx_helper *h)
  196. {
  197. h->reserve_allowed = 1;
  198. }
  199. /*
  200. * Append an extent of memory to the iovec list. The memory must remain
  201. * allocated until we finish generating the packet and call the QTX.
  202. *
  203. * In general, the buffers passed to this function will be from one of two
  204. * ranges:
  205. *
  206. * - Application data contained in stream buffers managed elsewhere
  207. * in the QUIC stack; or
  208. *
  209. * - Control frame data appended into txp->scratch using tx_helper_begin and
  210. * tx_helper_commit.
  211. *
  212. */
  213. static int tx_helper_append_iovec(struct tx_helper *h,
  214. const unsigned char *buf,
  215. size_t buf_len)
  216. {
  217. struct txp_el *el = &h->txp->el[h->enc_level];
  218. if (buf_len == 0)
  219. return 1;
  220. if (!ossl_assert(!h->done_implicit))
  221. return 0;
  222. if (!txp_el_ensure_iovec(el, h->num_iovec + 1))
  223. return 0;
  224. el->iovec[h->num_iovec].buf = buf;
  225. el->iovec[h->num_iovec].buf_len = buf_len;
  226. ++h->num_iovec;
  227. h->bytes_appended += buf_len;
  228. return 1;
  229. }
  230. /*
  231. * How many more bytes of space do we have left in our plaintext packet payload?
  232. */
  233. static size_t tx_helper_get_space_left(struct tx_helper *h)
  234. {
  235. return h->max_ppl
  236. - (h->reserve_allowed ? 0 : h->reserve) - h->bytes_appended;
  237. }
  238. /*
  239. * Begin a control frame serialization transaction. This allows the
  240. * serialization of the control frame to be backed out if it turns out it won't
  241. * fit. Write the control frame to the returned WPACKET. Ensure you always
  242. * call tx_helper_rollback or tx_helper_commit (or tx_helper_cleanup). Returns
  243. * NULL on failure.
  244. */
  245. static WPACKET *tx_helper_begin(struct tx_helper *h)
  246. {
  247. size_t space_left, len;
  248. unsigned char *data;
  249. struct txp_el *el = &h->txp->el[h->enc_level];
  250. if (!ossl_assert(!h->txn.active))
  251. return NULL;
  252. if (!ossl_assert(!h->done_implicit))
  253. return NULL;
  254. data = (unsigned char *)el->scratch + h->scratch_bytes;
  255. len = el->scratch_len - h->scratch_bytes;
  256. space_left = tx_helper_get_space_left(h);
  257. if (!ossl_assert(space_left <= len))
  258. return NULL;
  259. if (!WPACKET_init_static_len(&h->txn.wpkt, data, len, 0))
  260. return NULL;
  261. if (!WPACKET_set_max_size(&h->txn.wpkt, space_left)) {
  262. WPACKET_cleanup(&h->txn.wpkt);
  263. return NULL;
  264. }
  265. h->txn.data = data;
  266. h->txn.active = 1;
  267. return &h->txn.wpkt;
  268. }
  269. static void tx_helper_end(struct tx_helper *h, int success)
  270. {
  271. if (success)
  272. WPACKET_finish(&h->txn.wpkt);
  273. else
  274. WPACKET_cleanup(&h->txn.wpkt);
  275. h->txn.active = 0;
  276. h->txn.data = NULL;
  277. }
  278. /* Abort a control frame serialization transaction. */
  279. static void tx_helper_rollback(struct tx_helper *h)
  280. {
  281. if (!h->txn.active)
  282. return;
  283. tx_helper_end(h, 0);
  284. }
  285. /* Commit a control frame. */
  286. static int tx_helper_commit(struct tx_helper *h)
  287. {
  288. size_t l = 0;
  289. if (!h->txn.active)
  290. return 0;
  291. if (!WPACKET_get_total_written(&h->txn.wpkt, &l)) {
  292. tx_helper_end(h, 0);
  293. return 0;
  294. }
  295. if (!tx_helper_append_iovec(h, h->txn.data, l)) {
  296. tx_helper_end(h, 0);
  297. return 0;
  298. }
  299. if (h->txp->msg_callback != NULL && l > 0) {
  300. uint64_t ftype;
  301. int ctype = SSL3_RT_QUIC_FRAME_FULL;
  302. PACKET pkt;
  303. if (!PACKET_buf_init(&pkt, h->txn.data, l)
  304. || !ossl_quic_wire_peek_frame_header(&pkt, &ftype, NULL)) {
  305. tx_helper_end(h, 0);
  306. return 0;
  307. }
  308. if (ftype == OSSL_QUIC_FRAME_TYPE_PADDING)
  309. ctype = SSL3_RT_QUIC_FRAME_PADDING;
  310. else if (OSSL_QUIC_FRAME_TYPE_IS_STREAM(ftype)
  311. || ftype == OSSL_QUIC_FRAME_TYPE_CRYPTO)
  312. ctype = SSL3_RT_QUIC_FRAME_HEADER;
  313. h->txp->msg_callback(1, OSSL_QUIC1_VERSION, ctype, h->txn.data, l,
  314. h->txp->msg_callback_ssl,
  315. h->txp->msg_callback_arg);
  316. }
  317. h->scratch_bytes += l;
  318. tx_helper_end(h, 1);
  319. return 1;
  320. }
  321. struct archetype_data {
  322. unsigned int allow_ack : 1;
  323. unsigned int allow_ping : 1;
  324. unsigned int allow_crypto : 1;
  325. unsigned int allow_handshake_done : 1;
  326. unsigned int allow_path_challenge : 1;
  327. unsigned int allow_path_response : 1;
  328. unsigned int allow_new_conn_id : 1;
  329. unsigned int allow_retire_conn_id : 1;
  330. unsigned int allow_stream_rel : 1;
  331. unsigned int allow_conn_fc : 1;
  332. unsigned int allow_conn_close : 1;
  333. unsigned int allow_cfq_other : 1;
  334. unsigned int allow_new_token : 1;
  335. unsigned int allow_force_ack_eliciting : 1;
  336. unsigned int allow_padding : 1;
  337. unsigned int require_ack_eliciting : 1;
  338. unsigned int bypass_cc : 1;
  339. };
  340. struct txp_pkt_geom {
  341. size_t cmpl, cmppl, hwm, pkt_overhead;
  342. uint32_t archetype;
  343. struct archetype_data adata;
  344. };
  345. struct txp_pkt {
  346. struct tx_helper h;
  347. int h_valid;
  348. QUIC_TXPIM_PKT *tpkt;
  349. QUIC_STREAM *stream_head;
  350. QUIC_PKT_HDR phdr;
  351. struct txp_pkt_geom geom;
  352. int force_pad;
  353. };
  354. static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
  355. void *arg);
  356. static void on_regen_notify(uint64_t frame_type, uint64_t stream_id,
  357. QUIC_TXPIM_PKT *pkt, void *arg);
  358. static void on_confirm_notify(uint64_t frame_type, uint64_t stream_id,
  359. QUIC_TXPIM_PKT *pkt, void *arg);
  360. static void on_sstream_updated(uint64_t stream_id, void *arg);
  361. static int sstream_is_pending(QUIC_SSTREAM *sstream);
  362. static int txp_should_try_staging(OSSL_QUIC_TX_PACKETISER *txp,
  363. uint32_t enc_level,
  364. uint32_t archetype,
  365. uint64_t cc_limit,
  366. uint32_t *conn_close_enc_level);
  367. static size_t txp_determine_pn_len(OSSL_QUIC_TX_PACKETISER *txp);
  368. static int txp_determine_ppl_from_pl(OSSL_QUIC_TX_PACKETISER *txp,
  369. size_t pl,
  370. uint32_t enc_level,
  371. size_t hdr_len,
  372. size_t *r);
  373. static size_t txp_get_mdpl(OSSL_QUIC_TX_PACKETISER *txp);
  374. static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp,
  375. struct txp_pkt *pkt,
  376. int chosen_for_conn_close);
  377. static int txp_pkt_init(struct txp_pkt *pkt, OSSL_QUIC_TX_PACKETISER *txp,
  378. uint32_t enc_level, uint32_t archetype,
  379. size_t running_total);
  380. static void txp_pkt_cleanup(struct txp_pkt *pkt, OSSL_QUIC_TX_PACKETISER *txp);
  381. static int txp_pkt_postgen_update_pkt_overhead(struct txp_pkt *pkt,
  382. OSSL_QUIC_TX_PACKETISER *txp);
  383. static int txp_pkt_append_padding(struct txp_pkt *pkt,
  384. OSSL_QUIC_TX_PACKETISER *txp, size_t num_bytes);
  385. static int txp_pkt_commit(OSSL_QUIC_TX_PACKETISER *txp, struct txp_pkt *pkt,
  386. uint32_t archetype, int *txpim_pkt_reffed);
  387. static uint32_t txp_determine_archetype(OSSL_QUIC_TX_PACKETISER *txp,
  388. uint64_t cc_limit);
  389. OSSL_QUIC_TX_PACKETISER *ossl_quic_tx_packetiser_new(const OSSL_QUIC_TX_PACKETISER_ARGS *args)
  390. {
  391. OSSL_QUIC_TX_PACKETISER *txp;
  392. if (args == NULL
  393. || args->qtx == NULL
  394. || args->txpim == NULL
  395. || args->cfq == NULL
  396. || args->ackm == NULL
  397. || args->qsm == NULL
  398. || args->conn_txfc == NULL
  399. || args->conn_rxfc == NULL
  400. || args->max_streams_bidi_rxfc == NULL
  401. || args->max_streams_uni_rxfc == NULL) {
  402. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  403. return NULL;
  404. }
  405. txp = OPENSSL_zalloc(sizeof(*txp));
  406. if (txp == NULL)
  407. return NULL;
  408. txp->args = *args;
  409. txp->last_tx_time = ossl_time_zero();
  410. if (!ossl_quic_fifd_init(&txp->fifd,
  411. txp->args.cfq, txp->args.ackm, txp->args.txpim,
  412. get_sstream_by_id, txp,
  413. on_regen_notify, txp,
  414. on_confirm_notify, txp,
  415. on_sstream_updated, txp)) {
  416. OPENSSL_free(txp);
  417. return NULL;
  418. }
  419. return txp;
  420. }
  421. void ossl_quic_tx_packetiser_free(OSSL_QUIC_TX_PACKETISER *txp)
  422. {
  423. uint32_t enc_level;
  424. if (txp == NULL)
  425. return;
  426. ossl_quic_tx_packetiser_set_initial_token(txp, NULL, 0, NULL, NULL);
  427. ossl_quic_fifd_cleanup(&txp->fifd);
  428. OPENSSL_free(txp->conn_close_frame.reason);
  429. for (enc_level = QUIC_ENC_LEVEL_INITIAL;
  430. enc_level < QUIC_ENC_LEVEL_NUM;
  431. ++enc_level) {
  432. OPENSSL_free(txp->el[enc_level].iovec);
  433. OPENSSL_free(txp->el[enc_level].scratch);
  434. }
  435. OPENSSL_free(txp);
  436. }
  437. /*
  438. * Determine if an Initial packet token length is reasonable based on the
  439. * current MDPL, returning 1 if it is OK.
  440. *
  441. * The real PMTU to the peer could differ from our (pessimistic) understanding
  442. * of the PMTU, therefore it is possible we could receive an Initial token from
  443. * a server in a Retry packet which is bigger than the MDPL. In this case it is
  444. * impossible for us ever to make forward progress and we need to error out
  445. * and fail the connection attempt.
  446. *
  447. * The specific boundary condition is complex: for example, after the size of
  448. * the Initial token, there are the Initial packet header overheads and then
  449. * encryption/AEAD tag overheads. After that, the minimum room for frame data in
  450. * order to guarantee forward progress must be guaranteed. For example, a crypto
  451. * stream needs to always be able to serialize at least one byte in a CRYPTO
  452. * frame in order to make forward progress. Because the offset field of a CRYPTO
  453. * frame uses a variable-length integer, the number of bytes needed to ensure
  454. * this also varies.
  455. *
  456. * Rather than trying to get this boundary condition check actually right,
  457. * require a reasonable amount of slack to avoid pathological behaviours. (After
  458. * all, transmitting a CRYPTO stream one byte at a time is probably not
  459. * desirable anyway.)
  460. *
  461. * We choose 160 bytes as the required margin, which is double the rough
  462. * estimation of the minimum we would require to guarantee forward progress
  463. * under worst case packet overheads.
  464. */
  465. #define TXP_REQUIRED_TOKEN_MARGIN 160
  466. static int txp_check_token_len(size_t token_len, size_t mdpl)
  467. {
  468. if (token_len == 0)
  469. return 1;
  470. if (token_len >= mdpl)
  471. return 0;
  472. if (TXP_REQUIRED_TOKEN_MARGIN >= mdpl)
  473. /* (should not be possible because MDPL must be at least 1200) */
  474. return 0;
  475. if (token_len > mdpl - TXP_REQUIRED_TOKEN_MARGIN)
  476. return 0;
  477. return 1;
  478. }
  479. int ossl_quic_tx_packetiser_set_initial_token(OSSL_QUIC_TX_PACKETISER *txp,
  480. const unsigned char *token,
  481. size_t token_len,
  482. ossl_quic_initial_token_free_fn *free_cb,
  483. void *free_cb_arg)
  484. {
  485. if (!txp_check_token_len(token_len, txp_get_mdpl(txp)))
  486. return 0;
  487. if (txp->initial_token != NULL && txp->initial_token_free_cb != NULL)
  488. txp->initial_token_free_cb(txp->initial_token, txp->initial_token_len,
  489. txp->initial_token_free_cb_arg);
  490. txp->initial_token = token;
  491. txp->initial_token_len = token_len;
  492. txp->initial_token_free_cb = free_cb;
  493. txp->initial_token_free_cb_arg = free_cb_arg;
  494. return 1;
  495. }
  496. int ossl_quic_tx_packetiser_set_cur_dcid(OSSL_QUIC_TX_PACKETISER *txp,
  497. const QUIC_CONN_ID *dcid)
  498. {
  499. if (dcid == NULL) {
  500. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  501. return 0;
  502. }
  503. txp->args.cur_dcid = *dcid;
  504. return 1;
  505. }
  506. int ossl_quic_tx_packetiser_set_cur_scid(OSSL_QUIC_TX_PACKETISER *txp,
  507. const QUIC_CONN_ID *scid)
  508. {
  509. if (scid == NULL) {
  510. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  511. return 0;
  512. }
  513. txp->args.cur_scid = *scid;
  514. return 1;
  515. }
  516. /* Change the destination L4 address the TXP uses to send datagrams. */
  517. int ossl_quic_tx_packetiser_set_peer(OSSL_QUIC_TX_PACKETISER *txp,
  518. const BIO_ADDR *peer)
  519. {
  520. if (peer == NULL) {
  521. BIO_ADDR_clear(&txp->args.peer);
  522. return 1;
  523. }
  524. txp->args.peer = *peer;
  525. return 1;
  526. }
  527. void ossl_quic_tx_packetiser_set_ack_tx_cb(OSSL_QUIC_TX_PACKETISER *txp,
  528. void (*cb)(const OSSL_QUIC_FRAME_ACK *ack,
  529. uint32_t pn_space,
  530. void *arg),
  531. void *cb_arg)
  532. {
  533. txp->ack_tx_cb = cb;
  534. txp->ack_tx_cb_arg = cb_arg;
  535. }
  536. int ossl_quic_tx_packetiser_discard_enc_level(OSSL_QUIC_TX_PACKETISER *txp,
  537. uint32_t enc_level)
  538. {
  539. if (enc_level >= QUIC_ENC_LEVEL_NUM) {
  540. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  541. return 0;
  542. }
  543. if (enc_level != QUIC_ENC_LEVEL_0RTT)
  544. txp->args.crypto[ossl_quic_enc_level_to_pn_space(enc_level)] = NULL;
  545. return 1;
  546. }
  547. void ossl_quic_tx_packetiser_notify_handshake_complete(OSSL_QUIC_TX_PACKETISER *txp)
  548. {
  549. txp->handshake_complete = 1;
  550. }
  551. void ossl_quic_tx_packetiser_schedule_handshake_done(OSSL_QUIC_TX_PACKETISER *txp)
  552. {
  553. txp->want_handshake_done = 1;
  554. }
  555. void ossl_quic_tx_packetiser_schedule_ack_eliciting(OSSL_QUIC_TX_PACKETISER *txp,
  556. uint32_t pn_space)
  557. {
  558. txp->force_ack_eliciting |= (1UL << pn_space);
  559. }
  560. void ossl_quic_tx_packetiser_schedule_ack(OSSL_QUIC_TX_PACKETISER *txp,
  561. uint32_t pn_space)
  562. {
  563. txp->want_ack |= (1UL << pn_space);
  564. }
  565. #define TXP_ERR_INTERNAL 0 /* Internal (e.g. alloc) error */
  566. #define TXP_ERR_SUCCESS 1 /* Success */
  567. #define TXP_ERR_SPACE 2 /* Not enough room for another packet */
  568. #define TXP_ERR_INPUT 3 /* Invalid/malformed input */
  569. /*
  570. * Generates a datagram by polling the various ELs to determine if they want to
  571. * generate any frames, and generating a datagram which coalesces packets for
  572. * any ELs which do.
  573. */
  574. int ossl_quic_tx_packetiser_generate(OSSL_QUIC_TX_PACKETISER *txp,
  575. QUIC_TXP_STATUS *status)
  576. {
  577. /*
  578. * Called to generate one or more datagrams, each containing one or more
  579. * packets.
  580. *
  581. * There are some tricky things to note here:
  582. *
  583. * - The TXP is only concerned with generating encrypted packets;
  584. * other packets use a different path.
  585. *
  586. * - Any datagram containing an Initial packet must have a payload length
  587. * (DPL) of at least 1200 bytes. This padding need not necessarily be
  588. * found in the Initial packet.
  589. *
  590. * - It is desirable to be able to coalesce an Initial packet
  591. * with a Handshake packet. Since, before generating the Handshake
  592. * packet, we do not know how long it will be, we cannot know the
  593. * correct amount of padding to ensure a DPL of at least 1200 bytes.
  594. * Thus this padding must added to the Handshake packet (or whatever
  595. * packet is the last in the datagram).
  596. *
  597. * - However, at the time that we generate the Initial packet,
  598. * we do not actually know for sure that we will be followed
  599. * in the datagram by another packet. For example, suppose we have
  600. * some queued data (e.g. crypto stream data for the HANDSHAKE EL)
  601. * it looks like we will want to send on the HANDSHAKE EL.
  602. * We could assume padding will be placed in the Handshake packet
  603. * subsequently and avoid adding any padding to the Initial packet
  604. * (which would leave no room for the Handshake packet in the
  605. * datagram).
  606. *
  607. * However, this is not actually a safe assumption. Suppose that we
  608. * are using a link with a MDPL of 1200 bytes, the minimum allowed by
  609. * QUIC. Suppose that the Initial packet consumes 1195 bytes in total.
  610. * Since it is not possible to fit a Handshake packet in just 5 bytes,
  611. * upon trying to add a Handshake packet after generating the Initial
  612. * packet, we will discover we have no room to fit it! This is not a
  613. * problem in itself as another datagram can be sent subsequently, but
  614. * it is a problem because we were counting to use that packet to hold
  615. * the essential padding. But if we have already finished encrypting
  616. * the Initial packet, we cannot go and add padding to it anymore.
  617. * This leaves us stuck.
  618. *
  619. * Because of this, we have to plan multiple packets simultaneously, such
  620. * that we can start generating a Handshake (or 0-RTT or 1-RTT, or so on)
  621. * packet while still having the option to go back and add padding to the
  622. * Initial packet if it turns out to be needed.
  623. *
  624. * Trying to predict ahead of time (e.g. during Initial packet generation)
  625. * whether we will successfully generate a subsequent packet is fraught with
  626. * error as it relies on a large number of variables:
  627. *
  628. * - Do we have room to fit a packet header? (Consider that due to
  629. * variable-length integer encoding this is highly variable and can even
  630. * depend on payload length due to a variable-length Length field.)
  631. *
  632. * - Can we fit even a single one of the frames we want to put in this
  633. * packet in the packet? (Each frame type has a bespoke encoding. While
  634. * our encodings of some frame types are adaptive based on the available
  635. * room - e.g. STREAM frames - ultimately all frame types have some
  636. * absolute minimum number of bytes to be successfully encoded. For
  637. * example, if after an Initial packet there is enough room to encode
  638. * only one byte of frame data, it is quite likely we can't send any of
  639. * the frames we wanted to send.) While this is not strictly a problem
  640. * because we could just fill the packet with padding frames, this is a
  641. * pointless packet and is wasteful.
  642. *
  643. * Thus we adopt a multi-phase architecture:
  644. *
  645. * 1. Archetype Selection: Determine desired packet archetype.
  646. *
  647. * 2. Packet Staging: Generation of packet information and packet payload
  648. * data (frame data) into staging areas.
  649. *
  650. * 3. Packet Adjustment: Adjustment of staged packets, adding padding to
  651. * the staged packets if needed.
  652. *
  653. * 4. Commit: The packets are sent to the QTX and recorded as having been
  654. * sent to the FIFM.
  655. *
  656. */
  657. int res = 0, rc;
  658. uint32_t archetype, enc_level;
  659. uint32_t conn_close_enc_level = QUIC_ENC_LEVEL_NUM;
  660. struct txp_pkt pkt[QUIC_ENC_LEVEL_NUM];
  661. size_t pkts_done = 0;
  662. uint64_t cc_limit = txp->args.cc_method->get_tx_allowance(txp->args.cc_data);
  663. int need_padding = 0, txpim_pkt_reffed;
  664. for (enc_level = QUIC_ENC_LEVEL_INITIAL;
  665. enc_level < QUIC_ENC_LEVEL_NUM;
  666. ++enc_level)
  667. pkt[enc_level].h_valid = 0;
  668. memset(status, 0, sizeof(*status));
  669. /*
  670. * Should not be needed, but a sanity check in case anyone else has been
  671. * using the QTX.
  672. */
  673. ossl_qtx_finish_dgram(txp->args.qtx);
  674. /* 1. Archetype Selection */
  675. archetype = txp_determine_archetype(txp, cc_limit);
  676. /* 2. Packet Staging */
  677. for (enc_level = QUIC_ENC_LEVEL_INITIAL;
  678. enc_level < QUIC_ENC_LEVEL_NUM;
  679. ++enc_level) {
  680. size_t running_total = (enc_level > QUIC_ENC_LEVEL_INITIAL)
  681. ? pkt[enc_level - 1].geom.hwm : 0;
  682. pkt[enc_level].geom.hwm = running_total;
  683. if (!txp_should_try_staging(txp, enc_level, archetype, cc_limit,
  684. &conn_close_enc_level))
  685. continue;
  686. if (!txp_pkt_init(&pkt[enc_level], txp, enc_level, archetype,
  687. running_total))
  688. /*
  689. * If this fails this is not a fatal error - it means the geometry
  690. * planning determined there was not enough space for another
  691. * packet. So just proceed with what we've already planned for.
  692. */
  693. break;
  694. rc = txp_generate_for_el(txp, &pkt[enc_level],
  695. conn_close_enc_level == enc_level);
  696. if (rc != TXP_ERR_SUCCESS)
  697. goto out;
  698. if (pkt[enc_level].force_pad)
  699. /*
  700. * txp_generate_for_el emitted a frame which forces packet padding.
  701. */
  702. need_padding = 1;
  703. pkt[enc_level].geom.hwm = running_total
  704. + pkt[enc_level].h.bytes_appended
  705. + pkt[enc_level].geom.pkt_overhead;
  706. }
  707. /* 3. Packet Adjustment */
  708. if (pkt[QUIC_ENC_LEVEL_INITIAL].h_valid
  709. && pkt[QUIC_ENC_LEVEL_INITIAL].h.bytes_appended > 0)
  710. /*
  711. * We have an Initial packet in this datagram, so we need to make sure
  712. * the total size of the datagram is adequate.
  713. */
  714. need_padding = 1;
  715. if (need_padding) {
  716. size_t total_dgram_size = 0;
  717. const size_t min_dpl = QUIC_MIN_INITIAL_DGRAM_LEN;
  718. uint32_t pad_el = QUIC_ENC_LEVEL_NUM;
  719. for (enc_level = QUIC_ENC_LEVEL_INITIAL;
  720. enc_level < QUIC_ENC_LEVEL_NUM;
  721. ++enc_level)
  722. if (pkt[enc_level].h_valid && pkt[enc_level].h.bytes_appended > 0) {
  723. if (pad_el == QUIC_ENC_LEVEL_NUM
  724. /*
  725. * We might not be able to add padding, for example if we
  726. * are using the ACK_ONLY archetype.
  727. */
  728. && pkt[enc_level].geom.adata.allow_padding
  729. && !pkt[enc_level].h.done_implicit)
  730. pad_el = enc_level;
  731. txp_pkt_postgen_update_pkt_overhead(&pkt[enc_level], txp);
  732. total_dgram_size += pkt[enc_level].geom.pkt_overhead
  733. + pkt[enc_level].h.bytes_appended;
  734. }
  735. if (pad_el != QUIC_ENC_LEVEL_NUM && total_dgram_size < min_dpl) {
  736. size_t deficit = min_dpl - total_dgram_size;
  737. if (!txp_pkt_append_padding(&pkt[pad_el], txp, deficit))
  738. goto out;
  739. total_dgram_size += deficit;
  740. /*
  741. * Padding frames make a packet ineligible for being a non-inflight
  742. * packet.
  743. */
  744. pkt[pad_el].tpkt->ackm_pkt.is_inflight = 1;
  745. }
  746. /*
  747. * If we have failed to make a datagram of adequate size, for example
  748. * because we have a padding requirement but are using the ACK_ONLY
  749. * archetype (because we are CC limited), which precludes us from
  750. * sending padding, give up on generating the datagram - there is
  751. * nothing we can do.
  752. */
  753. if (total_dgram_size < min_dpl) {
  754. res = 1;
  755. goto out;
  756. }
  757. }
  758. /* 4. Commit */
  759. for (enc_level = QUIC_ENC_LEVEL_INITIAL;
  760. enc_level < QUIC_ENC_LEVEL_NUM;
  761. ++enc_level) {
  762. if (!pkt[enc_level].h_valid)
  763. /* Did not attempt to generate a packet for this EL. */
  764. continue;
  765. if (pkt[enc_level].h.bytes_appended == 0)
  766. /* Nothing was generated for this EL, so skip. */
  767. continue;
  768. rc = txp_pkt_commit(txp, &pkt[enc_level], archetype,
  769. &txpim_pkt_reffed);
  770. if (rc) {
  771. status->sent_ack_eliciting
  772. = status->sent_ack_eliciting
  773. || pkt[enc_level].tpkt->ackm_pkt.is_ack_eliciting;
  774. if (enc_level == QUIC_ENC_LEVEL_HANDSHAKE)
  775. status->sent_handshake
  776. = (pkt[enc_level].h_valid
  777. && pkt[enc_level].h.bytes_appended > 0);
  778. }
  779. if (txpim_pkt_reffed)
  780. pkt[enc_level].tpkt = NULL; /* don't free */
  781. if (!rc)
  782. goto out;
  783. ++pkts_done;
  784. }
  785. /* Flush & Cleanup */
  786. res = 1;
  787. out:
  788. ossl_qtx_finish_dgram(txp->args.qtx);
  789. for (enc_level = QUIC_ENC_LEVEL_INITIAL;
  790. enc_level < QUIC_ENC_LEVEL_NUM;
  791. ++enc_level)
  792. txp_pkt_cleanup(&pkt[enc_level], txp);
  793. status->sent_pkt = pkts_done;
  794. return res;
  795. }
  796. static const struct archetype_data archetypes[QUIC_ENC_LEVEL_NUM][TX_PACKETISER_ARCHETYPE_NUM] = {
  797. /* EL 0(INITIAL) */
  798. {
  799. /* EL 0(INITIAL) - Archetype 0(NORMAL) */
  800. {
  801. /*allow_ack =*/ 1,
  802. /*allow_ping =*/ 1,
  803. /*allow_crypto =*/ 1,
  804. /*allow_handshake_done =*/ 0,
  805. /*allow_path_challenge =*/ 0,
  806. /*allow_path_response =*/ 0,
  807. /*allow_new_conn_id =*/ 0,
  808. /*allow_retire_conn_id =*/ 0,
  809. /*allow_stream_rel =*/ 0,
  810. /*allow_conn_fc =*/ 0,
  811. /*allow_conn_close =*/ 1,
  812. /*allow_cfq_other =*/ 0,
  813. /*allow_new_token =*/ 0,
  814. /*allow_force_ack_eliciting =*/ 1,
  815. /*allow_padding =*/ 1,
  816. /*require_ack_eliciting =*/ 0,
  817. /*bypass_cc =*/ 0,
  818. },
  819. /* EL 0(INITIAL) - Archetype 1(PROBE) */
  820. {
  821. /*allow_ack =*/ 1,
  822. /*allow_ping =*/ 1,
  823. /*allow_crypto =*/ 1,
  824. /*allow_handshake_done =*/ 0,
  825. /*allow_path_challenge =*/ 0,
  826. /*allow_path_response =*/ 0,
  827. /*allow_new_conn_id =*/ 0,
  828. /*allow_retire_conn_id =*/ 0,
  829. /*allow_stream_rel =*/ 0,
  830. /*allow_conn_fc =*/ 0,
  831. /*allow_conn_close =*/ 1,
  832. /*allow_cfq_other =*/ 0,
  833. /*allow_new_token =*/ 0,
  834. /*allow_force_ack_eliciting =*/ 1,
  835. /*allow_padding =*/ 1,
  836. /*require_ack_eliciting =*/ 1,
  837. /*bypass_cc =*/ 1,
  838. },
  839. /* EL 0(INITIAL) - Archetype 2(ACK_ONLY) */
  840. {
  841. /*allow_ack =*/ 1,
  842. /*allow_ping =*/ 0,
  843. /*allow_crypto =*/ 0,
  844. /*allow_handshake_done =*/ 0,
  845. /*allow_path_challenge =*/ 0,
  846. /*allow_path_response =*/ 0,
  847. /*allow_new_conn_id =*/ 0,
  848. /*allow_retire_conn_id =*/ 0,
  849. /*allow_stream_rel =*/ 0,
  850. /*allow_conn_fc =*/ 0,
  851. /*allow_conn_close =*/ 0,
  852. /*allow_cfq_other =*/ 0,
  853. /*allow_new_token =*/ 0,
  854. /*allow_force_ack_eliciting =*/ 1,
  855. /*allow_padding =*/ 0,
  856. /*require_ack_eliciting =*/ 0,
  857. /*bypass_cc =*/ 1,
  858. },
  859. },
  860. /* EL 1(HANDSHAKE) */
  861. {
  862. /* EL 1(HANDSHAKE) - Archetype 0(NORMAL) */
  863. {
  864. /*allow_ack =*/ 1,
  865. /*allow_ping =*/ 1,
  866. /*allow_crypto =*/ 1,
  867. /*allow_handshake_done =*/ 0,
  868. /*allow_path_challenge =*/ 0,
  869. /*allow_path_response =*/ 0,
  870. /*allow_new_conn_id =*/ 0,
  871. /*allow_retire_conn_id =*/ 0,
  872. /*allow_stream_rel =*/ 0,
  873. /*allow_conn_fc =*/ 0,
  874. /*allow_conn_close =*/ 1,
  875. /*allow_cfq_other =*/ 0,
  876. /*allow_new_token =*/ 0,
  877. /*allow_force_ack_eliciting =*/ 1,
  878. /*allow_padding =*/ 1,
  879. /*require_ack_eliciting =*/ 0,
  880. /*bypass_cc =*/ 0,
  881. },
  882. /* EL 1(HANDSHAKE) - Archetype 1(PROBE) */
  883. {
  884. /*allow_ack =*/ 1,
  885. /*allow_ping =*/ 1,
  886. /*allow_crypto =*/ 1,
  887. /*allow_handshake_done =*/ 0,
  888. /*allow_path_challenge =*/ 0,
  889. /*allow_path_response =*/ 0,
  890. /*allow_new_conn_id =*/ 0,
  891. /*allow_retire_conn_id =*/ 0,
  892. /*allow_stream_rel =*/ 0,
  893. /*allow_conn_fc =*/ 0,
  894. /*allow_conn_close =*/ 1,
  895. /*allow_cfq_other =*/ 0,
  896. /*allow_new_token =*/ 0,
  897. /*allow_force_ack_eliciting =*/ 1,
  898. /*allow_padding =*/ 1,
  899. /*require_ack_eliciting =*/ 1,
  900. /*bypass_cc =*/ 1,
  901. },
  902. /* EL 1(HANDSHAKE) - Archetype 2(ACK_ONLY) */
  903. {
  904. /*allow_ack =*/ 1,
  905. /*allow_ping =*/ 0,
  906. /*allow_crypto =*/ 0,
  907. /*allow_handshake_done =*/ 0,
  908. /*allow_path_challenge =*/ 0,
  909. /*allow_path_response =*/ 0,
  910. /*allow_new_conn_id =*/ 0,
  911. /*allow_retire_conn_id =*/ 0,
  912. /*allow_stream_rel =*/ 0,
  913. /*allow_conn_fc =*/ 0,
  914. /*allow_conn_close =*/ 0,
  915. /*allow_cfq_other =*/ 0,
  916. /*allow_new_token =*/ 0,
  917. /*allow_force_ack_eliciting =*/ 1,
  918. /*allow_padding =*/ 0,
  919. /*require_ack_eliciting =*/ 0,
  920. /*bypass_cc =*/ 1,
  921. },
  922. },
  923. /* EL 2(0RTT) */
  924. {
  925. /* EL 2(0RTT) - Archetype 0(NORMAL) */
  926. {
  927. /*allow_ack =*/ 0,
  928. /*allow_ping =*/ 1,
  929. /*allow_crypto =*/ 0,
  930. /*allow_handshake_done =*/ 0,
  931. /*allow_path_challenge =*/ 0,
  932. /*allow_path_response =*/ 0,
  933. /*allow_new_conn_id =*/ 1,
  934. /*allow_retire_conn_id =*/ 1,
  935. /*allow_stream_rel =*/ 1,
  936. /*allow_conn_fc =*/ 1,
  937. /*allow_conn_close =*/ 1,
  938. /*allow_cfq_other =*/ 0,
  939. /*allow_new_token =*/ 0,
  940. /*allow_force_ack_eliciting =*/ 0,
  941. /*allow_padding =*/ 1,
  942. /*require_ack_eliciting =*/ 0,
  943. /*bypass_cc =*/ 0,
  944. },
  945. /* EL 2(0RTT) - Archetype 1(PROBE) */
  946. {
  947. /*allow_ack =*/ 0,
  948. /*allow_ping =*/ 1,
  949. /*allow_crypto =*/ 0,
  950. /*allow_handshake_done =*/ 0,
  951. /*allow_path_challenge =*/ 0,
  952. /*allow_path_response =*/ 0,
  953. /*allow_new_conn_id =*/ 1,
  954. /*allow_retire_conn_id =*/ 1,
  955. /*allow_stream_rel =*/ 1,
  956. /*allow_conn_fc =*/ 1,
  957. /*allow_conn_close =*/ 1,
  958. /*allow_cfq_other =*/ 0,
  959. /*allow_new_token =*/ 0,
  960. /*allow_force_ack_eliciting =*/ 0,
  961. /*allow_padding =*/ 1,
  962. /*require_ack_eliciting =*/ 1,
  963. /*bypass_cc =*/ 1,
  964. },
  965. /* EL 2(0RTT) - Archetype 2(ACK_ONLY) */
  966. {
  967. /*allow_ack =*/ 0,
  968. /*allow_ping =*/ 0,
  969. /*allow_crypto =*/ 0,
  970. /*allow_handshake_done =*/ 0,
  971. /*allow_path_challenge =*/ 0,
  972. /*allow_path_response =*/ 0,
  973. /*allow_new_conn_id =*/ 0,
  974. /*allow_retire_conn_id =*/ 0,
  975. /*allow_stream_rel =*/ 0,
  976. /*allow_conn_fc =*/ 0,
  977. /*allow_conn_close =*/ 0,
  978. /*allow_cfq_other =*/ 0,
  979. /*allow_new_token =*/ 0,
  980. /*allow_force_ack_eliciting =*/ 0,
  981. /*allow_padding =*/ 0,
  982. /*require_ack_eliciting =*/ 0,
  983. /*bypass_cc =*/ 1,
  984. },
  985. },
  986. /* EL 3(1RTT) */
  987. {
  988. /* EL 3(1RTT) - Archetype 0(NORMAL) */
  989. {
  990. /*allow_ack =*/ 1,
  991. /*allow_ping =*/ 1,
  992. /*allow_crypto =*/ 1,
  993. /*allow_handshake_done =*/ 1,
  994. /*allow_path_challenge =*/ 0,
  995. /*allow_path_response =*/ 1,
  996. /*allow_new_conn_id =*/ 1,
  997. /*allow_retire_conn_id =*/ 1,
  998. /*allow_stream_rel =*/ 1,
  999. /*allow_conn_fc =*/ 1,
  1000. /*allow_conn_close =*/ 1,
  1001. /*allow_cfq_other =*/ 1,
  1002. /*allow_new_token =*/ 1,
  1003. /*allow_force_ack_eliciting =*/ 1,
  1004. /*allow_padding =*/ 1,
  1005. /*require_ack_eliciting =*/ 0,
  1006. /*bypass_cc =*/ 0,
  1007. },
  1008. /* EL 3(1RTT) - Archetype 1(PROBE) */
  1009. {
  1010. /*allow_ack =*/ 1,
  1011. /*allow_ping =*/ 1,
  1012. /*allow_crypto =*/ 1,
  1013. /*allow_handshake_done =*/ 1,
  1014. /*allow_path_challenge =*/ 0,
  1015. /*allow_path_response =*/ 1,
  1016. /*allow_new_conn_id =*/ 1,
  1017. /*allow_retire_conn_id =*/ 1,
  1018. /*allow_stream_rel =*/ 1,
  1019. /*allow_conn_fc =*/ 1,
  1020. /*allow_conn_close =*/ 1,
  1021. /*allow_cfq_other =*/ 1,
  1022. /*allow_new_token =*/ 1,
  1023. /*allow_force_ack_eliciting =*/ 1,
  1024. /*allow_padding =*/ 1,
  1025. /*require_ack_eliciting =*/ 1,
  1026. /*bypass_cc =*/ 1,
  1027. },
  1028. /* EL 3(1RTT) - Archetype 2(ACK_ONLY) */
  1029. {
  1030. /*allow_ack =*/ 1,
  1031. /*allow_ping =*/ 0,
  1032. /*allow_crypto =*/ 0,
  1033. /*allow_handshake_done =*/ 0,
  1034. /*allow_path_challenge =*/ 0,
  1035. /*allow_path_response =*/ 0,
  1036. /*allow_new_conn_id =*/ 0,
  1037. /*allow_retire_conn_id =*/ 0,
  1038. /*allow_stream_rel =*/ 0,
  1039. /*allow_conn_fc =*/ 0,
  1040. /*allow_conn_close =*/ 0,
  1041. /*allow_cfq_other =*/ 0,
  1042. /*allow_new_token =*/ 0,
  1043. /*allow_force_ack_eliciting =*/ 1,
  1044. /*allow_padding =*/ 0,
  1045. /*require_ack_eliciting =*/ 0,
  1046. /*bypass_cc =*/ 1,
  1047. }
  1048. }
  1049. };
  1050. static int txp_get_archetype_data(uint32_t enc_level,
  1051. uint32_t archetype,
  1052. struct archetype_data *a)
  1053. {
  1054. if (enc_level >= QUIC_ENC_LEVEL_NUM
  1055. || archetype >= TX_PACKETISER_ARCHETYPE_NUM)
  1056. return 0;
  1057. /* No need to avoid copying this as it should not exceed one int in size. */
  1058. *a = archetypes[enc_level][archetype];
  1059. return 1;
  1060. }
  1061. static int txp_determine_geometry(OSSL_QUIC_TX_PACKETISER *txp,
  1062. uint32_t archetype,
  1063. uint32_t enc_level,
  1064. size_t running_total,
  1065. QUIC_PKT_HDR *phdr,
  1066. struct txp_pkt_geom *geom)
  1067. {
  1068. size_t mdpl, cmpl, hdr_len;
  1069. /* Get information about packet archetype. */
  1070. if (!txp_get_archetype_data(enc_level, archetype, &geom->adata))
  1071. return 0;
  1072. /* Assemble packet header. */
  1073. phdr->type = ossl_quic_enc_level_to_pkt_type(enc_level);
  1074. phdr->spin_bit = 0;
  1075. phdr->pn_len = txp_determine_pn_len(txp);
  1076. phdr->partial = 0;
  1077. phdr->fixed = 1;
  1078. phdr->reserved = 0;
  1079. phdr->version = QUIC_VERSION_1;
  1080. phdr->dst_conn_id = txp->args.cur_dcid;
  1081. phdr->src_conn_id = txp->args.cur_scid;
  1082. /*
  1083. * We need to know the length of the payload to get an accurate header
  1084. * length for non-1RTT packets, because the Length field found in
  1085. * Initial/Handshake/0-RTT packets uses a variable-length encoding. However,
  1086. * we don't have a good idea of the length of our payload, because the
  1087. * length of the payload depends on the room in the datagram after fitting
  1088. * the header, which depends on the size of the header.
  1089. *
  1090. * In general, it does not matter if a packet is slightly shorter (because
  1091. * e.g. we predicted use of a 2-byte length field, but ended up only needing
  1092. * a 1-byte length field). However this does matter for Initial packets
  1093. * which must be at least 1200 bytes, which is also the assumed default MTU;
  1094. * therefore in many cases Initial packets will be padded to 1200 bytes,
  1095. * which means if we overestimated the header size, we will be short by a
  1096. * few bytes and the server will ignore the packet for being too short. In
  1097. * this case, however, such packets always *will* be padded to meet 1200
  1098. * bytes, which requires a 2-byte length field, so we don't actually need to
  1099. * worry about this. Thus we estimate the header length assuming a 2-byte
  1100. * length field here, which should in practice work well in all cases.
  1101. */
  1102. phdr->len = OSSL_QUIC_VLINT_2B_MAX - phdr->pn_len;
  1103. if (enc_level == QUIC_ENC_LEVEL_INITIAL) {
  1104. phdr->token = txp->initial_token;
  1105. phdr->token_len = txp->initial_token_len;
  1106. } else {
  1107. phdr->token = NULL;
  1108. phdr->token_len = 0;
  1109. }
  1110. hdr_len = ossl_quic_wire_get_encoded_pkt_hdr_len(phdr->dst_conn_id.id_len,
  1111. phdr);
  1112. if (hdr_len == 0)
  1113. return 0;
  1114. /* MDPL: Maximum datagram payload length. */
  1115. mdpl = txp_get_mdpl(txp);
  1116. /*
  1117. * CMPL: Maximum encoded packet size we can put into this datagram given any
  1118. * previous packets coalesced into it.
  1119. */
  1120. if (running_total > mdpl)
  1121. /* Should not be possible, but if it happens: */
  1122. cmpl = 0;
  1123. else
  1124. cmpl = mdpl - running_total;
  1125. /* CMPPL: Maximum amount we can put into the current packet payload */
  1126. if (!txp_determine_ppl_from_pl(txp, cmpl, enc_level, hdr_len, &geom->cmppl))
  1127. return 0;
  1128. geom->cmpl = cmpl;
  1129. geom->pkt_overhead = cmpl - geom->cmppl;
  1130. geom->archetype = archetype;
  1131. return 1;
  1132. }
  1133. static uint32_t txp_determine_archetype(OSSL_QUIC_TX_PACKETISER *txp,
  1134. uint64_t cc_limit)
  1135. {
  1136. OSSL_ACKM_PROBE_INFO *probe_info
  1137. = ossl_ackm_get0_probe_request(txp->args.ackm);
  1138. uint32_t pn_space;
  1139. /*
  1140. * If ACKM has requested probe generation (e.g. due to PTO), we generate a
  1141. * Probe-archetype packet. Actually, we determine archetype on a
  1142. * per-datagram basis, so if any EL wants a probe, do a pass in which
  1143. * we try and generate a probe (if needed) for all ELs.
  1144. */
  1145. if (probe_info->anti_deadlock_initial > 0
  1146. || probe_info->anti_deadlock_handshake > 0)
  1147. return TX_PACKETISER_ARCHETYPE_PROBE;
  1148. for (pn_space = QUIC_PN_SPACE_INITIAL;
  1149. pn_space < QUIC_PN_SPACE_NUM;
  1150. ++pn_space)
  1151. if (probe_info->pto[pn_space] > 0)
  1152. return TX_PACKETISER_ARCHETYPE_PROBE;
  1153. /*
  1154. * If we are out of CC budget, we cannot send a normal packet,
  1155. * but we can do an ACK-only packet (potentially, if we
  1156. * want to send an ACK).
  1157. */
  1158. if (cc_limit == 0)
  1159. return TX_PACKETISER_ARCHETYPE_ACK_ONLY;
  1160. /* All other packets. */
  1161. return TX_PACKETISER_ARCHETYPE_NORMAL;
  1162. }
  1163. static int txp_should_try_staging(OSSL_QUIC_TX_PACKETISER *txp,
  1164. uint32_t enc_level,
  1165. uint32_t archetype,
  1166. uint64_t cc_limit,
  1167. uint32_t *conn_close_enc_level)
  1168. {
  1169. struct archetype_data a;
  1170. uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
  1171. QUIC_CFQ_ITEM *cfq_item;
  1172. if (!ossl_qtx_is_enc_level_provisioned(txp->args.qtx, enc_level))
  1173. return 0;
  1174. if (!txp_get_archetype_data(enc_level, archetype, &a))
  1175. return 0;
  1176. if (!a.bypass_cc && cc_limit == 0)
  1177. /* CC not allowing us to send. */
  1178. return 0;
  1179. /*
  1180. * We can produce CONNECTION_CLOSE frames on any EL in principle, which
  1181. * means we need to choose which EL we would prefer to use. After a
  1182. * connection is fully established we have only one provisioned EL and this
  1183. * is a non-issue. Where multiple ELs are provisioned, it is possible the
  1184. * peer does not have the keys for the EL yet, which suggests in general it
  1185. * is preferable to use the lowest EL which is still provisioned.
  1186. *
  1187. * However (RFC 9000 s. 10.2.3 & 12.5) we are also required to not send
  1188. * application CONNECTION_CLOSE frames in non-1-RTT ELs, so as to not
  1189. * potentially leak application data on a connection which has yet to be
  1190. * authenticated. Thus when we have an application CONNECTION_CLOSE frame
  1191. * queued and need to send it on a non-1-RTT EL, we have to convert it
  1192. * into a transport CONNECTION_CLOSE frame which contains no application
  1193. * data. Since this loses information, it suggests we should use the 1-RTT
  1194. * EL to avoid this if possible, even if a lower EL is also available.
  1195. *
  1196. * At the same time, just because we have the 1-RTT EL provisioned locally
  1197. * does not necessarily mean the peer does, for example if a handshake
  1198. * CRYPTO frame has been lost. It is fairly important that CONNECTION_CLOSE
  1199. * is signalled in a way we know our peer can decrypt, as we stop processing
  1200. * connection retransmission logic for real after connection close and
  1201. * simply 'blindly' retransmit the same CONNECTION_CLOSE frame.
  1202. *
  1203. * This is not a major concern for clients, since if a client has a 1-RTT EL
  1204. * provisioned the server is guaranteed to also have a 1-RTT EL provisioned.
  1205. *
  1206. * TODO(QUIC SERVER): Revisit this when server support is added.
  1207. */
  1208. if (*conn_close_enc_level > enc_level
  1209. && *conn_close_enc_level != QUIC_ENC_LEVEL_1RTT)
  1210. *conn_close_enc_level = enc_level;
  1211. /* Do we need to send a PTO probe? */
  1212. if (a.allow_force_ack_eliciting) {
  1213. OSSL_ACKM_PROBE_INFO *probe_info
  1214. = ossl_ackm_get0_probe_request(txp->args.ackm);
  1215. if ((enc_level == QUIC_ENC_LEVEL_INITIAL
  1216. && probe_info->anti_deadlock_initial > 0)
  1217. || (enc_level == QUIC_ENC_LEVEL_HANDSHAKE
  1218. && probe_info->anti_deadlock_handshake > 0)
  1219. || probe_info->pto[pn_space] > 0)
  1220. return 1;
  1221. }
  1222. /* Does the crypto stream for this EL want to produce anything? */
  1223. if (a.allow_crypto && sstream_is_pending(txp->args.crypto[pn_space]))
  1224. return 1;
  1225. /* Does the ACKM for this PN space want to produce anything? */
  1226. if (a.allow_ack && (ossl_ackm_is_ack_desired(txp->args.ackm, pn_space)
  1227. || (txp->want_ack & (1UL << pn_space)) != 0))
  1228. return 1;
  1229. /* Do we need to force emission of an ACK-eliciting packet? */
  1230. if (a.allow_force_ack_eliciting
  1231. && (txp->force_ack_eliciting & (1UL << pn_space)) != 0)
  1232. return 1;
  1233. /* Does the connection-level RXFC want to produce a frame? */
  1234. if (a.allow_conn_fc && (txp->want_max_data
  1235. || ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 0)))
  1236. return 1;
  1237. /* Do we want to produce a MAX_STREAMS frame? */
  1238. if (a.allow_conn_fc
  1239. && (txp->want_max_streams_bidi
  1240. || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc,
  1241. 0)
  1242. || txp->want_max_streams_uni
  1243. || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc,
  1244. 0)))
  1245. return 1;
  1246. /* Do we want to produce a HANDSHAKE_DONE frame? */
  1247. if (a.allow_handshake_done && txp->want_handshake_done)
  1248. return 1;
  1249. /* Do we want to produce a CONNECTION_CLOSE frame? */
  1250. if (a.allow_conn_close && txp->want_conn_close &&
  1251. *conn_close_enc_level == enc_level)
  1252. /*
  1253. * This is a bit of a special case since CONNECTION_CLOSE can appear in
  1254. * most packet types, and when we decide we want to send it this status
  1255. * isn't tied to a specific EL. So if we want to send it, we send it
  1256. * only on the lowest non-dropped EL.
  1257. */
  1258. return 1;
  1259. /* Does the CFQ have any frames queued for this PN space? */
  1260. if (enc_level != QUIC_ENC_LEVEL_0RTT)
  1261. for (cfq_item = ossl_quic_cfq_get_priority_head(txp->args.cfq, pn_space);
  1262. cfq_item != NULL;
  1263. cfq_item = ossl_quic_cfq_item_get_priority_next(cfq_item, pn_space)) {
  1264. uint64_t frame_type = ossl_quic_cfq_item_get_frame_type(cfq_item);
  1265. switch (frame_type) {
  1266. case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
  1267. if (a.allow_new_conn_id)
  1268. return 1;
  1269. break;
  1270. case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
  1271. if (a.allow_retire_conn_id)
  1272. return 1;
  1273. break;
  1274. case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
  1275. if (a.allow_new_token)
  1276. return 1;
  1277. break;
  1278. case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
  1279. if (a.allow_path_response)
  1280. return 1;
  1281. break;
  1282. default:
  1283. if (a.allow_cfq_other)
  1284. return 1;
  1285. break;
  1286. }
  1287. }
  1288. if (a.allow_stream_rel && txp->handshake_complete) {
  1289. QUIC_STREAM_ITER it;
  1290. /* If there are any active streams, 0/1-RTT wants to produce a packet.
  1291. * Whether a stream is on the active list is required to be precise
  1292. * (i.e., a stream is never on the active list if we cannot produce a
  1293. * frame for it), and all stream-related frames are governed by
  1294. * a.allow_stream_rel (i.e., if we can send one type of stream-related
  1295. * frame, we can send any of them), so we don't need to inspect
  1296. * individual streams on the active list, just confirm that the active
  1297. * list is non-empty.
  1298. */
  1299. ossl_quic_stream_iter_init(&it, txp->args.qsm, 0);
  1300. if (it.stream != NULL)
  1301. return 1;
  1302. }
  1303. return 0;
  1304. }
  1305. static int sstream_is_pending(QUIC_SSTREAM *sstream)
  1306. {
  1307. OSSL_QUIC_FRAME_STREAM hdr;
  1308. OSSL_QTX_IOVEC iov[2];
  1309. size_t num_iov = OSSL_NELEM(iov);
  1310. return ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov);
  1311. }
  1312. /* Determine how many bytes we should use for the encoded PN. */
  1313. static size_t txp_determine_pn_len(OSSL_QUIC_TX_PACKETISER *txp)
  1314. {
  1315. return 4; /* TODO(QUIC FUTURE) */
  1316. }
  1317. /* Determine plaintext packet payload length from payload length. */
  1318. static int txp_determine_ppl_from_pl(OSSL_QUIC_TX_PACKETISER *txp,
  1319. size_t pl,
  1320. uint32_t enc_level,
  1321. size_t hdr_len,
  1322. size_t *r)
  1323. {
  1324. if (pl < hdr_len)
  1325. return 0;
  1326. pl -= hdr_len;
  1327. if (!ossl_qtx_calculate_plaintext_payload_len(txp->args.qtx, enc_level,
  1328. pl, &pl))
  1329. return 0;
  1330. *r = pl;
  1331. return 1;
  1332. }
  1333. static size_t txp_get_mdpl(OSSL_QUIC_TX_PACKETISER *txp)
  1334. {
  1335. return ossl_qtx_get_mdpl(txp->args.qtx);
  1336. }
  1337. static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
  1338. void *arg)
  1339. {
  1340. OSSL_QUIC_TX_PACKETISER *txp = arg;
  1341. QUIC_STREAM *s;
  1342. if (stream_id == UINT64_MAX)
  1343. return txp->args.crypto[pn_space];
  1344. s = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
  1345. if (s == NULL)
  1346. return NULL;
  1347. return s->sstream;
  1348. }
  1349. static void on_regen_notify(uint64_t frame_type, uint64_t stream_id,
  1350. QUIC_TXPIM_PKT *pkt, void *arg)
  1351. {
  1352. OSSL_QUIC_TX_PACKETISER *txp = arg;
  1353. switch (frame_type) {
  1354. case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
  1355. txp->want_handshake_done = 1;
  1356. break;
  1357. case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
  1358. txp->want_max_data = 1;
  1359. break;
  1360. case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
  1361. txp->want_max_streams_bidi = 1;
  1362. break;
  1363. case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
  1364. txp->want_max_streams_uni = 1;
  1365. break;
  1366. case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
  1367. txp->want_ack |= (1UL << pkt->ackm_pkt.pkt_space);
  1368. break;
  1369. case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
  1370. {
  1371. QUIC_STREAM *s
  1372. = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
  1373. if (s == NULL)
  1374. return;
  1375. s->want_max_stream_data = 1;
  1376. ossl_quic_stream_map_update_state(txp->args.qsm, s);
  1377. }
  1378. break;
  1379. case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
  1380. {
  1381. QUIC_STREAM *s
  1382. = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
  1383. if (s == NULL)
  1384. return;
  1385. ossl_quic_stream_map_schedule_stop_sending(txp->args.qsm, s);
  1386. }
  1387. break;
  1388. case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
  1389. {
  1390. QUIC_STREAM *s
  1391. = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
  1392. if (s == NULL)
  1393. return;
  1394. s->want_reset_stream = 1;
  1395. ossl_quic_stream_map_update_state(txp->args.qsm, s);
  1396. }
  1397. break;
  1398. default:
  1399. assert(0);
  1400. break;
  1401. }
  1402. }
  1403. static int txp_need_ping(OSSL_QUIC_TX_PACKETISER *txp,
  1404. uint32_t pn_space,
  1405. const struct archetype_data *adata)
  1406. {
  1407. return adata->allow_ping
  1408. && (adata->require_ack_eliciting
  1409. || (txp->force_ack_eliciting & (1UL << pn_space)) != 0);
  1410. }
  1411. static int txp_pkt_init(struct txp_pkt *pkt, OSSL_QUIC_TX_PACKETISER *txp,
  1412. uint32_t enc_level, uint32_t archetype,
  1413. size_t running_total)
  1414. {
  1415. uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
  1416. if (!txp_determine_geometry(txp, archetype, enc_level,
  1417. running_total, &pkt->phdr, &pkt->geom))
  1418. return 0;
  1419. /*
  1420. * Initialise TX helper. If we must be ACK eliciting, reserve 1 byte for
  1421. * PING.
  1422. */
  1423. if (!tx_helper_init(&pkt->h, txp, enc_level,
  1424. pkt->geom.cmppl,
  1425. txp_need_ping(txp, pn_space, &pkt->geom.adata) ? 1 : 0))
  1426. return 0;
  1427. pkt->h_valid = 1;
  1428. pkt->tpkt = NULL;
  1429. pkt->stream_head = NULL;
  1430. pkt->force_pad = 0;
  1431. return 1;
  1432. }
  1433. static void txp_pkt_cleanup(struct txp_pkt *pkt, OSSL_QUIC_TX_PACKETISER *txp)
  1434. {
  1435. if (!pkt->h_valid)
  1436. return;
  1437. tx_helper_cleanup(&pkt->h);
  1438. pkt->h_valid = 0;
  1439. if (pkt->tpkt != NULL) {
  1440. ossl_quic_txpim_pkt_release(txp->args.txpim, pkt->tpkt);
  1441. pkt->tpkt = NULL;
  1442. }
  1443. }
  1444. static int txp_pkt_postgen_update_pkt_overhead(struct txp_pkt *pkt,
  1445. OSSL_QUIC_TX_PACKETISER *txp)
  1446. {
  1447. /*
  1448. * After we have staged and generated our packets, but before we commit
  1449. * them, it is possible for the estimated packet overhead (packet header +
  1450. * AEAD tag size) to shrink slightly because we generated a short packet
  1451. * whose which can be represented in fewer bytes as a variable-length
  1452. * integer than we were (pessimistically) budgeting for. We need to account
  1453. * for this to ensure that we get our padding calculation exactly right.
  1454. *
  1455. * Update pkt_overhead to be accurate now that we know how much data is
  1456. * going in a packet.
  1457. */
  1458. size_t hdr_len, ciphertext_len;
  1459. if (pkt->h.enc_level == QUIC_ENC_LEVEL_INITIAL)
  1460. /*
  1461. * Don't update overheads for the INITIAL EL - we have not finished
  1462. * appending padding to it and would potentially miscalculate the
  1463. * correct padding if we now update the pkt_overhead field to switch to
  1464. * e.g. a 1-byte length field in the packet header. Since we are padding
  1465. * to QUIC_MIN_INITIAL_DGRAM_LEN which requires a 2-byte length field,
  1466. * this is guaranteed to be moot anyway. See comment in
  1467. * txp_determine_geometry for more information.
  1468. */
  1469. return 1;
  1470. if (!ossl_qtx_calculate_ciphertext_payload_len(txp->args.qtx, pkt->h.enc_level,
  1471. pkt->h.bytes_appended,
  1472. &ciphertext_len))
  1473. return 0;
  1474. pkt->phdr.len = ciphertext_len;
  1475. hdr_len = ossl_quic_wire_get_encoded_pkt_hdr_len(pkt->phdr.dst_conn_id.id_len,
  1476. &pkt->phdr);
  1477. pkt->geom.pkt_overhead = hdr_len + ciphertext_len - pkt->h.bytes_appended;
  1478. return 1;
  1479. }
  1480. static void on_confirm_notify(uint64_t frame_type, uint64_t stream_id,
  1481. QUIC_TXPIM_PKT *pkt, void *arg)
  1482. {
  1483. OSSL_QUIC_TX_PACKETISER *txp = arg;
  1484. switch (frame_type) {
  1485. case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
  1486. {
  1487. QUIC_STREAM *s
  1488. = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
  1489. if (s == NULL)
  1490. return;
  1491. s->acked_stop_sending = 1;
  1492. ossl_quic_stream_map_update_state(txp->args.qsm, s);
  1493. }
  1494. break;
  1495. case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
  1496. {
  1497. QUIC_STREAM *s
  1498. = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
  1499. if (s == NULL)
  1500. return;
  1501. /*
  1502. * We must already be in RESET_SENT or RESET_RECVD if we are
  1503. * here, so we don't need to check state here.
  1504. */
  1505. ossl_quic_stream_map_notify_reset_stream_acked(txp->args.qsm, s);
  1506. ossl_quic_stream_map_update_state(txp->args.qsm, s);
  1507. }
  1508. break;
  1509. default:
  1510. assert(0);
  1511. break;
  1512. }
  1513. }
  1514. static int txp_pkt_append_padding(struct txp_pkt *pkt,
  1515. OSSL_QUIC_TX_PACKETISER *txp, size_t num_bytes)
  1516. {
  1517. WPACKET *wpkt;
  1518. if (num_bytes == 0)
  1519. return 1;
  1520. if (!ossl_assert(pkt->h_valid))
  1521. return 0;
  1522. if (!ossl_assert(pkt->tpkt != NULL))
  1523. return 0;
  1524. wpkt = tx_helper_begin(&pkt->h);
  1525. if (wpkt == NULL)
  1526. return 0;
  1527. if (!ossl_quic_wire_encode_padding(wpkt, num_bytes)) {
  1528. tx_helper_rollback(&pkt->h);
  1529. return 0;
  1530. }
  1531. if (!tx_helper_commit(&pkt->h))
  1532. return 0;
  1533. pkt->tpkt->ackm_pkt.num_bytes += num_bytes;
  1534. /* Cannot be non-inflight if we have a PADDING frame */
  1535. pkt->tpkt->ackm_pkt.is_inflight = 1;
  1536. return 1;
  1537. }
  1538. static void on_sstream_updated(uint64_t stream_id, void *arg)
  1539. {
  1540. OSSL_QUIC_TX_PACKETISER *txp = arg;
  1541. QUIC_STREAM *s;
  1542. s = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
  1543. if (s == NULL)
  1544. return;
  1545. ossl_quic_stream_map_update_state(txp->args.qsm, s);
  1546. }
  1547. /*
  1548. * Returns 1 if we can send that many bytes in closing state, 0 otherwise.
  1549. * Also maintains the bytes sent state if it returns a success.
  1550. */
  1551. static int try_commit_conn_close(OSSL_QUIC_TX_PACKETISER *txp, size_t n)
  1552. {
  1553. int res;
  1554. /* We can always send the first connection close frame */
  1555. if (txp->closing_bytes_recv == 0)
  1556. return 1;
  1557. /*
  1558. * RFC 9000 s. 10.2.1 Closing Connection State:
  1559. * To avoid being used for an amplification attack, such
  1560. * endpoints MUST limit the cumulative size of packets it sends
  1561. * to three times the cumulative size of the packets that are
  1562. * received and attributed to the connection.
  1563. * and:
  1564. * An endpoint in the closing state MUST either discard packets
  1565. * received from an unvalidated address or limit the cumulative
  1566. * size of packets it sends to an unvalidated address to three
  1567. * times the size of packets it receives from that address.
  1568. */
  1569. res = txp->closing_bytes_xmit + n <= txp->closing_bytes_recv * 3;
  1570. /*
  1571. * Attribute the bytes to the connection, if we are allowed to send them
  1572. * and this isn't the first closing frame.
  1573. */
  1574. if (res && txp->closing_bytes_recv != 0)
  1575. txp->closing_bytes_xmit += n;
  1576. return res;
  1577. }
  1578. void ossl_quic_tx_packetiser_record_received_closing_bytes(
  1579. OSSL_QUIC_TX_PACKETISER *txp, size_t n)
  1580. {
  1581. txp->closing_bytes_recv += n;
  1582. }
  1583. static int txp_generate_pre_token(OSSL_QUIC_TX_PACKETISER *txp,
  1584. struct txp_pkt *pkt,
  1585. int chosen_for_conn_close,
  1586. int *can_be_non_inflight)
  1587. {
  1588. const uint32_t enc_level = pkt->h.enc_level;
  1589. const uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
  1590. const struct archetype_data *a = &pkt->geom.adata;
  1591. QUIC_TXPIM_PKT *tpkt = pkt->tpkt;
  1592. struct tx_helper *h = &pkt->h;
  1593. const OSSL_QUIC_FRAME_ACK *ack;
  1594. OSSL_QUIC_FRAME_ACK ack2;
  1595. tpkt->ackm_pkt.largest_acked = QUIC_PN_INVALID;
  1596. /* ACK Frames (Regenerate) */
  1597. if (a->allow_ack
  1598. && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_ACK
  1599. && (((txp->want_ack & (1UL << pn_space)) != 0)
  1600. || ossl_ackm_is_ack_desired(txp->args.ackm, pn_space))
  1601. && (ack = ossl_ackm_get_ack_frame(txp->args.ackm, pn_space)) != NULL) {
  1602. WPACKET *wpkt = tx_helper_begin(h);
  1603. if (wpkt == NULL)
  1604. return 0;
  1605. /* We do not currently support ECN */
  1606. ack2 = *ack;
  1607. ack2.ecn_present = 0;
  1608. if (ossl_quic_wire_encode_frame_ack(wpkt,
  1609. txp->args.ack_delay_exponent,
  1610. &ack2)) {
  1611. if (!tx_helper_commit(h))
  1612. return 0;
  1613. tpkt->had_ack_frame = 1;
  1614. if (ack->num_ack_ranges > 0)
  1615. tpkt->ackm_pkt.largest_acked = ack->ack_ranges[0].end;
  1616. if (txp->ack_tx_cb != NULL)
  1617. txp->ack_tx_cb(&ack2, pn_space, txp->ack_tx_cb_arg);
  1618. } else {
  1619. tx_helper_rollback(h);
  1620. }
  1621. }
  1622. /* CONNECTION_CLOSE Frames (Regenerate) */
  1623. if (a->allow_conn_close && txp->want_conn_close && chosen_for_conn_close) {
  1624. WPACKET *wpkt = tx_helper_begin(h);
  1625. OSSL_QUIC_FRAME_CONN_CLOSE f, *pf = &txp->conn_close_frame;
  1626. size_t l;
  1627. if (wpkt == NULL)
  1628. return 0;
  1629. /*
  1630. * Application CONNECTION_CLOSE frames may only be sent in the
  1631. * Application PN space, as otherwise they may be sent before a
  1632. * connection is authenticated and leak application data. Therefore, if
  1633. * we need to send a CONNECTION_CLOSE frame in another PN space and were
  1634. * given an application CONNECTION_CLOSE frame, convert it into a
  1635. * transport CONNECTION_CLOSE frame, removing any sensitive application
  1636. * data.
  1637. *
  1638. * RFC 9000 s. 10.2.3: "A CONNECTION_CLOSE of type 0x1d MUST be replaced
  1639. * by a CONNECTION_CLOSE of type 0x1c when sending the frame in Initial
  1640. * or Handshake packets. Otherwise, information about the application
  1641. * state might be revealed. Endpoints MUST clear the value of the Reason
  1642. * Phrase field and SHOULD use the APPLICATION_ERROR code when
  1643. * converting to a CONNECTION_CLOSE of type 0x1c."
  1644. */
  1645. if (pn_space != QUIC_PN_SPACE_APP && pf->is_app) {
  1646. pf = &f;
  1647. pf->is_app = 0;
  1648. pf->frame_type = 0;
  1649. pf->error_code = QUIC_ERR_APPLICATION_ERROR;
  1650. pf->reason = NULL;
  1651. pf->reason_len = 0;
  1652. }
  1653. if (ossl_quic_wire_encode_frame_conn_close(wpkt, pf)
  1654. && WPACKET_get_total_written(wpkt, &l)
  1655. && try_commit_conn_close(txp, l)) {
  1656. if (!tx_helper_commit(h))
  1657. return 0;
  1658. tpkt->had_conn_close = 1;
  1659. *can_be_non_inflight = 0;
  1660. } else {
  1661. tx_helper_rollback(h);
  1662. }
  1663. }
  1664. return 1;
  1665. }
  1666. static int try_len(size_t space_left, size_t orig_len,
  1667. size_t base_hdr_len, size_t lenbytes,
  1668. uint64_t maxn, size_t *hdr_len, size_t *payload_len)
  1669. {
  1670. size_t n;
  1671. size_t maxn_ = maxn > SIZE_MAX ? SIZE_MAX : (size_t)maxn;
  1672. *hdr_len = base_hdr_len + lenbytes;
  1673. if (orig_len == 0 && space_left >= *hdr_len) {
  1674. *payload_len = 0;
  1675. return 1;
  1676. }
  1677. n = orig_len;
  1678. if (n > maxn_)
  1679. n = maxn_;
  1680. if (n + *hdr_len > space_left)
  1681. n = (space_left >= *hdr_len) ? space_left - *hdr_len : 0;
  1682. *payload_len = n;
  1683. return n > 0;
  1684. }
  1685. static int determine_len(size_t space_left, size_t orig_len,
  1686. size_t base_hdr_len,
  1687. uint64_t *hlen, uint64_t *len)
  1688. {
  1689. int ok = 0;
  1690. size_t chosen_payload_len = 0;
  1691. size_t chosen_hdr_len = 0;
  1692. size_t payload_len[4], hdr_len[4];
  1693. int i, valid[4] = {0};
  1694. valid[0] = try_len(space_left, orig_len, base_hdr_len,
  1695. 1, OSSL_QUIC_VLINT_1B_MAX,
  1696. &hdr_len[0], &payload_len[0]);
  1697. valid[1] = try_len(space_left, orig_len, base_hdr_len,
  1698. 2, OSSL_QUIC_VLINT_2B_MAX,
  1699. &hdr_len[1], &payload_len[1]);
  1700. valid[2] = try_len(space_left, orig_len, base_hdr_len,
  1701. 4, OSSL_QUIC_VLINT_4B_MAX,
  1702. &hdr_len[2], &payload_len[2]);
  1703. valid[3] = try_len(space_left, orig_len, base_hdr_len,
  1704. 8, OSSL_QUIC_VLINT_8B_MAX,
  1705. &hdr_len[3], &payload_len[3]);
  1706. for (i = OSSL_NELEM(valid) - 1; i >= 0; --i)
  1707. if (valid[i] && payload_len[i] >= chosen_payload_len) {
  1708. chosen_payload_len = payload_len[i];
  1709. chosen_hdr_len = hdr_len[i];
  1710. ok = 1;
  1711. }
  1712. *hlen = chosen_hdr_len;
  1713. *len = chosen_payload_len;
  1714. return ok;
  1715. }
  1716. /*
  1717. * Given a CRYPTO frame header with accurate chdr->len and a budget
  1718. * (space_left), try to find the optimal value of chdr->len to fill as much of
  1719. * the budget as possible. This is slightly hairy because larger values of
  1720. * chdr->len cause larger encoded sizes of the length field of the frame, which
  1721. * in turn mean less space available for payload data. We check all possible
  1722. * encodings and choose the optimal encoding.
  1723. */
  1724. static int determine_crypto_len(struct tx_helper *h,
  1725. OSSL_QUIC_FRAME_CRYPTO *chdr,
  1726. size_t space_left,
  1727. uint64_t *hlen,
  1728. uint64_t *len)
  1729. {
  1730. size_t orig_len;
  1731. size_t base_hdr_len; /* CRYPTO header length without length field */
  1732. if (chdr->len > SIZE_MAX)
  1733. return 0;
  1734. orig_len = (size_t)chdr->len;
  1735. chdr->len = 0;
  1736. base_hdr_len = ossl_quic_wire_get_encoded_frame_len_crypto_hdr(chdr);
  1737. chdr->len = orig_len;
  1738. if (base_hdr_len == 0)
  1739. return 0;
  1740. --base_hdr_len;
  1741. return determine_len(space_left, orig_len, base_hdr_len, hlen, len);
  1742. }
  1743. static int determine_stream_len(struct tx_helper *h,
  1744. OSSL_QUIC_FRAME_STREAM *shdr,
  1745. size_t space_left,
  1746. uint64_t *hlen,
  1747. uint64_t *len)
  1748. {
  1749. size_t orig_len;
  1750. size_t base_hdr_len; /* STREAM header length without length field */
  1751. if (shdr->len > SIZE_MAX)
  1752. return 0;
  1753. orig_len = (size_t)shdr->len;
  1754. shdr->len = 0;
  1755. base_hdr_len = ossl_quic_wire_get_encoded_frame_len_stream_hdr(shdr);
  1756. shdr->len = orig_len;
  1757. if (base_hdr_len == 0)
  1758. return 0;
  1759. if (shdr->has_explicit_len)
  1760. --base_hdr_len;
  1761. return determine_len(space_left, orig_len, base_hdr_len, hlen, len);
  1762. }
  1763. static int txp_generate_crypto_frames(OSSL_QUIC_TX_PACKETISER *txp,
  1764. struct txp_pkt *pkt,
  1765. int *have_ack_eliciting)
  1766. {
  1767. const uint32_t enc_level = pkt->h.enc_level;
  1768. const uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
  1769. QUIC_TXPIM_PKT *tpkt = pkt->tpkt;
  1770. struct tx_helper *h = &pkt->h;
  1771. size_t num_stream_iovec;
  1772. OSSL_QUIC_FRAME_STREAM shdr = {0};
  1773. OSSL_QUIC_FRAME_CRYPTO chdr = {0};
  1774. OSSL_QTX_IOVEC iov[2];
  1775. uint64_t hdr_bytes;
  1776. WPACKET *wpkt;
  1777. QUIC_TXPIM_CHUNK chunk = {0};
  1778. size_t i, space_left;
  1779. for (i = 0;; ++i) {
  1780. space_left = tx_helper_get_space_left(h);
  1781. if (space_left < MIN_FRAME_SIZE_CRYPTO)
  1782. return 1; /* no point trying */
  1783. /* Do we have any CRYPTO data waiting? */
  1784. num_stream_iovec = OSSL_NELEM(iov);
  1785. if (!ossl_quic_sstream_get_stream_frame(txp->args.crypto[pn_space],
  1786. i, &shdr, iov,
  1787. &num_stream_iovec))
  1788. return 1; /* nothing to do */
  1789. /* Convert STREAM frame header to CRYPTO frame header */
  1790. chdr.offset = shdr.offset;
  1791. chdr.len = shdr.len;
  1792. if (chdr.len == 0)
  1793. return 1; /* nothing to do */
  1794. /* Find best fit (header length, payload length) combination. */
  1795. if (!determine_crypto_len(h, &chdr, space_left, &hdr_bytes,
  1796. &chdr.len))
  1797. return 1; /* can't fit anything */
  1798. /*
  1799. * Truncate IOVs to match our chosen length.
  1800. *
  1801. * The length cannot be more than SIZE_MAX because this length comes
  1802. * from our send stream buffer.
  1803. */
  1804. ossl_quic_sstream_adjust_iov((size_t)chdr.len, iov, num_stream_iovec);
  1805. /*
  1806. * Ensure we have enough iovecs allocated (1 for the header, up to 2 for
  1807. * the stream data.)
  1808. */
  1809. if (!txp_el_ensure_iovec(&txp->el[enc_level], h->num_iovec + 3))
  1810. return 0; /* alloc error */
  1811. /* Encode the header. */
  1812. wpkt = tx_helper_begin(h);
  1813. if (wpkt == NULL)
  1814. return 0; /* alloc error */
  1815. if (!ossl_quic_wire_encode_frame_crypto_hdr(wpkt, &chdr)) {
  1816. tx_helper_rollback(h);
  1817. return 1; /* can't fit */
  1818. }
  1819. if (!tx_helper_commit(h))
  1820. return 0; /* alloc error */
  1821. /* Add payload iovecs to the helper (infallible). */
  1822. for (i = 0; i < num_stream_iovec; ++i)
  1823. tx_helper_append_iovec(h, iov[i].buf, iov[i].buf_len);
  1824. *have_ack_eliciting = 1;
  1825. tx_helper_unrestrict(h); /* no longer need PING */
  1826. /* Log chunk to TXPIM. */
  1827. chunk.stream_id = UINT64_MAX; /* crypto stream */
  1828. chunk.start = chdr.offset;
  1829. chunk.end = chdr.offset + chdr.len - 1;
  1830. chunk.has_fin = 0; /* Crypto stream never ends */
  1831. if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
  1832. return 0; /* alloc error */
  1833. }
  1834. }
  1835. struct chunk_info {
  1836. OSSL_QUIC_FRAME_STREAM shdr;
  1837. uint64_t orig_len;
  1838. OSSL_QTX_IOVEC iov[2];
  1839. size_t num_stream_iovec;
  1840. int valid;
  1841. };
  1842. static int txp_plan_stream_chunk(OSSL_QUIC_TX_PACKETISER *txp,
  1843. struct tx_helper *h,
  1844. QUIC_SSTREAM *sstream,
  1845. QUIC_TXFC *stream_txfc,
  1846. size_t skip,
  1847. struct chunk_info *chunk,
  1848. uint64_t consumed)
  1849. {
  1850. uint64_t fc_credit, fc_swm, fc_limit;
  1851. chunk->num_stream_iovec = OSSL_NELEM(chunk->iov);
  1852. chunk->valid = ossl_quic_sstream_get_stream_frame(sstream, skip,
  1853. &chunk->shdr,
  1854. chunk->iov,
  1855. &chunk->num_stream_iovec);
  1856. if (!chunk->valid)
  1857. return 1;
  1858. if (!ossl_assert(chunk->shdr.len > 0 || chunk->shdr.is_fin))
  1859. /* Should only have 0-length chunk if FIN */
  1860. return 0;
  1861. chunk->orig_len = chunk->shdr.len;
  1862. /* Clamp according to connection and stream-level TXFC. */
  1863. fc_credit = ossl_quic_txfc_get_credit(stream_txfc, consumed);
  1864. fc_swm = ossl_quic_txfc_get_swm(stream_txfc);
  1865. fc_limit = fc_swm + fc_credit;
  1866. if (chunk->shdr.len > 0 && chunk->shdr.offset + chunk->shdr.len > fc_limit) {
  1867. chunk->shdr.len = (fc_limit <= chunk->shdr.offset)
  1868. ? 0 : fc_limit - chunk->shdr.offset;
  1869. chunk->shdr.is_fin = 0;
  1870. }
  1871. if (chunk->shdr.len == 0 && !chunk->shdr.is_fin) {
  1872. /*
  1873. * Nothing to do due to TXFC. Since SSTREAM returns chunks in ascending
  1874. * order of offset we don't need to check any later chunks, so stop
  1875. * iterating here.
  1876. */
  1877. chunk->valid = 0;
  1878. return 1;
  1879. }
  1880. return 1;
  1881. }
  1882. /*
  1883. * Returns 0 on fatal error (e.g. allocation failure), 1 on success.
  1884. * *packet_full is set to 1 if there is no longer enough room for another STREAM
  1885. * frame.
  1886. */
  1887. static int txp_generate_stream_frames(OSSL_QUIC_TX_PACKETISER *txp,
  1888. struct txp_pkt *pkt,
  1889. uint64_t id,
  1890. QUIC_SSTREAM *sstream,
  1891. QUIC_TXFC *stream_txfc,
  1892. QUIC_STREAM *next_stream,
  1893. int *have_ack_eliciting,
  1894. int *packet_full,
  1895. uint64_t *new_credit_consumed,
  1896. uint64_t conn_consumed)
  1897. {
  1898. int rc = 0;
  1899. struct chunk_info chunks[2] = {0};
  1900. const uint32_t enc_level = pkt->h.enc_level;
  1901. QUIC_TXPIM_PKT *tpkt = pkt->tpkt;
  1902. struct tx_helper *h = &pkt->h;
  1903. OSSL_QUIC_FRAME_STREAM *shdr;
  1904. WPACKET *wpkt;
  1905. QUIC_TXPIM_CHUNK chunk;
  1906. size_t i, j, space_left;
  1907. int can_fill_payload, use_explicit_len;
  1908. int could_have_following_chunk;
  1909. uint64_t orig_len;
  1910. uint64_t hdr_len_implicit, payload_len_implicit;
  1911. uint64_t hdr_len_explicit, payload_len_explicit;
  1912. uint64_t fc_swm, fc_new_hwm;
  1913. fc_swm = ossl_quic_txfc_get_swm(stream_txfc);
  1914. fc_new_hwm = fc_swm;
  1915. /*
  1916. * Load the first two chunks if any offered by the send stream. We retrieve
  1917. * the next chunk in advance so we can determine if we need to send any more
  1918. * chunks from the same stream after this one, which is needed when
  1919. * determining when we can use an implicit length in a STREAM frame.
  1920. */
  1921. for (i = 0; i < 2; ++i) {
  1922. if (!txp_plan_stream_chunk(txp, h, sstream, stream_txfc, i, &chunks[i],
  1923. conn_consumed))
  1924. goto err;
  1925. if (i == 0 && !chunks[i].valid) {
  1926. /* No chunks, nothing to do. */
  1927. rc = 1;
  1928. goto err;
  1929. }
  1930. chunks[i].shdr.stream_id = id;
  1931. }
  1932. for (i = 0;; ++i) {
  1933. space_left = tx_helper_get_space_left(h);
  1934. if (!chunks[i % 2].valid) {
  1935. /* Out of chunks; we're done. */
  1936. rc = 1;
  1937. goto err;
  1938. }
  1939. if (space_left < MIN_FRAME_SIZE_STREAM) {
  1940. *packet_full = 1;
  1941. rc = 1;
  1942. goto err;
  1943. }
  1944. if (!ossl_assert(!h->done_implicit))
  1945. /*
  1946. * Logic below should have ensured we didn't append an
  1947. * implicit-length unless we filled the packet or didn't have
  1948. * another stream to handle, so this should not be possible.
  1949. */
  1950. goto err;
  1951. shdr = &chunks[i % 2].shdr;
  1952. orig_len = chunks[i % 2].orig_len;
  1953. if (i > 0)
  1954. /* Load next chunk for lookahead. */
  1955. if (!txp_plan_stream_chunk(txp, h, sstream, stream_txfc, i + 1,
  1956. &chunks[(i + 1) % 2], conn_consumed))
  1957. goto err;
  1958. /*
  1959. * Find best fit (header length, payload length) combination for if we
  1960. * use an implicit length.
  1961. */
  1962. shdr->has_explicit_len = 0;
  1963. hdr_len_implicit = payload_len_implicit = 0;
  1964. if (!determine_stream_len(h, shdr, space_left,
  1965. &hdr_len_implicit, &payload_len_implicit)) {
  1966. *packet_full = 1;
  1967. rc = 1;
  1968. goto err; /* can't fit anything */
  1969. }
  1970. /*
  1971. * If there is a next stream, we don't use the implicit length so we can
  1972. * add more STREAM frames after this one, unless there is enough data
  1973. * for this STREAM frame to fill the packet.
  1974. */
  1975. can_fill_payload = (hdr_len_implicit + payload_len_implicit
  1976. >= space_left);
  1977. /*
  1978. * Is there is a stream after this one, or another chunk pending
  1979. * transmission in this stream?
  1980. */
  1981. could_have_following_chunk
  1982. = (next_stream != NULL || chunks[(i + 1) % 2].valid);
  1983. /* Choose between explicit or implicit length representations. */
  1984. use_explicit_len = !((can_fill_payload || !could_have_following_chunk)
  1985. && !pkt->force_pad);
  1986. if (use_explicit_len) {
  1987. /*
  1988. * Find best fit (header length, payload length) combination for if
  1989. * we use an explicit length.
  1990. */
  1991. shdr->has_explicit_len = 1;
  1992. hdr_len_explicit = payload_len_explicit = 0;
  1993. if (!determine_stream_len(h, shdr, space_left,
  1994. &hdr_len_explicit, &payload_len_explicit)) {
  1995. *packet_full = 1;
  1996. rc = 1;
  1997. goto err; /* can't fit anything */
  1998. }
  1999. shdr->len = payload_len_explicit;
  2000. } else {
  2001. *packet_full = 1;
  2002. shdr->has_explicit_len = 0;
  2003. shdr->len = payload_len_implicit;
  2004. }
  2005. /* If this is a FIN, don't keep filling the packet with more FINs. */
  2006. if (shdr->is_fin)
  2007. chunks[(i + 1) % 2].valid = 0;
  2008. /*
  2009. * We are now committed to our length (shdr->len can't change).
  2010. * If we truncated the chunk, clear the FIN bit.
  2011. */
  2012. if (shdr->len < orig_len)
  2013. shdr->is_fin = 0;
  2014. /* Truncate IOVs to match our chosen length. */
  2015. ossl_quic_sstream_adjust_iov((size_t)shdr->len, chunks[i % 2].iov,
  2016. chunks[i % 2].num_stream_iovec);
  2017. /*
  2018. * Ensure we have enough iovecs allocated (1 for the header, up to 2 for
  2019. * the stream data.)
  2020. */
  2021. if (!txp_el_ensure_iovec(&txp->el[enc_level], h->num_iovec + 3))
  2022. goto err; /* alloc error */
  2023. /* Encode the header. */
  2024. wpkt = tx_helper_begin(h);
  2025. if (wpkt == NULL)
  2026. goto err; /* alloc error */
  2027. if (!ossl_assert(ossl_quic_wire_encode_frame_stream_hdr(wpkt, shdr))) {
  2028. /* (Should not be possible.) */
  2029. tx_helper_rollback(h);
  2030. *packet_full = 1;
  2031. rc = 1;
  2032. goto err; /* can't fit */
  2033. }
  2034. if (!tx_helper_commit(h))
  2035. goto err; /* alloc error */
  2036. /* Add payload iovecs to the helper (infallible). */
  2037. for (j = 0; j < chunks[i % 2].num_stream_iovec; ++j)
  2038. tx_helper_append_iovec(h, chunks[i % 2].iov[j].buf,
  2039. chunks[i % 2].iov[j].buf_len);
  2040. *have_ack_eliciting = 1;
  2041. tx_helper_unrestrict(h); /* no longer need PING */
  2042. if (!shdr->has_explicit_len)
  2043. h->done_implicit = 1;
  2044. /* Log new TXFC credit which was consumed. */
  2045. if (shdr->len > 0 && shdr->offset + shdr->len > fc_new_hwm)
  2046. fc_new_hwm = shdr->offset + shdr->len;
  2047. /* Log chunk to TXPIM. */
  2048. chunk.stream_id = shdr->stream_id;
  2049. chunk.start = shdr->offset;
  2050. chunk.end = shdr->offset + shdr->len - 1;
  2051. chunk.has_fin = shdr->is_fin;
  2052. chunk.has_stop_sending = 0;
  2053. chunk.has_reset_stream = 0;
  2054. if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
  2055. goto err; /* alloc error */
  2056. if (shdr->len < orig_len) {
  2057. /*
  2058. * If we did not serialize all of this chunk we definitely do not
  2059. * want to try the next chunk
  2060. */
  2061. rc = 1;
  2062. goto err;
  2063. }
  2064. }
  2065. err:
  2066. *new_credit_consumed = fc_new_hwm - fc_swm;
  2067. return rc;
  2068. }
  2069. static void txp_enlink_tmp(QUIC_STREAM **tmp_head, QUIC_STREAM *stream)
  2070. {
  2071. stream->txp_next = *tmp_head;
  2072. *tmp_head = stream;
  2073. }
  2074. static int txp_generate_stream_related(OSSL_QUIC_TX_PACKETISER *txp,
  2075. struct txp_pkt *pkt,
  2076. int *have_ack_eliciting,
  2077. QUIC_STREAM **tmp_head)
  2078. {
  2079. QUIC_STREAM_ITER it;
  2080. WPACKET *wpkt;
  2081. uint64_t cwm;
  2082. QUIC_STREAM *stream, *snext;
  2083. struct tx_helper *h = &pkt->h;
  2084. uint64_t conn_consumed = 0;
  2085. for (ossl_quic_stream_iter_init(&it, txp->args.qsm, 1);
  2086. it.stream != NULL;) {
  2087. stream = it.stream;
  2088. ossl_quic_stream_iter_next(&it);
  2089. snext = it.stream;
  2090. stream->txp_sent_fc = 0;
  2091. stream->txp_sent_stop_sending = 0;
  2092. stream->txp_sent_reset_stream = 0;
  2093. stream->txp_blocked = 0;
  2094. stream->txp_txfc_new_credit_consumed = 0;
  2095. /* Stream Abort Frames (STOP_SENDING, RESET_STREAM) */
  2096. if (stream->want_stop_sending) {
  2097. OSSL_QUIC_FRAME_STOP_SENDING f;
  2098. wpkt = tx_helper_begin(h);
  2099. if (wpkt == NULL)
  2100. return 0; /* alloc error */
  2101. f.stream_id = stream->id;
  2102. f.app_error_code = stream->stop_sending_aec;
  2103. if (!ossl_quic_wire_encode_frame_stop_sending(wpkt, &f)) {
  2104. tx_helper_rollback(h); /* can't fit */
  2105. txp_enlink_tmp(tmp_head, stream);
  2106. break;
  2107. }
  2108. if (!tx_helper_commit(h))
  2109. return 0; /* alloc error */
  2110. *have_ack_eliciting = 1;
  2111. tx_helper_unrestrict(h); /* no longer need PING */
  2112. stream->txp_sent_stop_sending = 1;
  2113. }
  2114. if (stream->want_reset_stream) {
  2115. OSSL_QUIC_FRAME_RESET_STREAM f;
  2116. if (!ossl_assert(stream->send_state == QUIC_SSTREAM_STATE_RESET_SENT))
  2117. return 0;
  2118. wpkt = tx_helper_begin(h);
  2119. if (wpkt == NULL)
  2120. return 0; /* alloc error */
  2121. f.stream_id = stream->id;
  2122. f.app_error_code = stream->reset_stream_aec;
  2123. if (!ossl_quic_stream_send_get_final_size(stream, &f.final_size))
  2124. return 0; /* should not be possible */
  2125. if (!ossl_quic_wire_encode_frame_reset_stream(wpkt, &f)) {
  2126. tx_helper_rollback(h); /* can't fit */
  2127. txp_enlink_tmp(tmp_head, stream);
  2128. break;
  2129. }
  2130. if (!tx_helper_commit(h))
  2131. return 0; /* alloc error */
  2132. *have_ack_eliciting = 1;
  2133. tx_helper_unrestrict(h); /* no longer need PING */
  2134. stream->txp_sent_reset_stream = 1;
  2135. /*
  2136. * The final size of the stream as indicated by RESET_STREAM is used
  2137. * to ensure a consistent view of flow control state by both
  2138. * parties; if we happen to send a RESET_STREAM that consumes more
  2139. * flow control credit, make sure we account for that.
  2140. */
  2141. if (!ossl_assert(f.final_size <= ossl_quic_txfc_get_swm(&stream->txfc)))
  2142. return 0;
  2143. stream->txp_txfc_new_credit_consumed
  2144. = f.final_size - ossl_quic_txfc_get_swm(&stream->txfc);
  2145. }
  2146. /*
  2147. * Stream Flow Control Frames (MAX_STREAM_DATA)
  2148. *
  2149. * RFC 9000 s. 13.3: "An endpoint SHOULD stop sending MAX_STREAM_DATA
  2150. * frames when the receiving part of the stream enters a "Size Known" or
  2151. * "Reset Recvd" state." -- In practice, RECV is the only state
  2152. * in which it makes sense to generate more MAX_STREAM_DATA frames.
  2153. */
  2154. if (stream->recv_state == QUIC_RSTREAM_STATE_RECV
  2155. && (stream->want_max_stream_data
  2156. || ossl_quic_rxfc_has_cwm_changed(&stream->rxfc, 0))) {
  2157. wpkt = tx_helper_begin(h);
  2158. if (wpkt == NULL)
  2159. return 0; /* alloc error */
  2160. cwm = ossl_quic_rxfc_get_cwm(&stream->rxfc);
  2161. if (!ossl_quic_wire_encode_frame_max_stream_data(wpkt, stream->id,
  2162. cwm)) {
  2163. tx_helper_rollback(h); /* can't fit */
  2164. txp_enlink_tmp(tmp_head, stream);
  2165. break;
  2166. }
  2167. if (!tx_helper_commit(h))
  2168. return 0; /* alloc error */
  2169. *have_ack_eliciting = 1;
  2170. tx_helper_unrestrict(h); /* no longer need PING */
  2171. stream->txp_sent_fc = 1;
  2172. }
  2173. /*
  2174. * Stream Data Frames (STREAM)
  2175. *
  2176. * RFC 9000 s. 3.3: A sender MUST NOT send a STREAM [...] frame for a
  2177. * stream in the "Reset Sent" state [or any terminal state]. We don't
  2178. * send any more STREAM frames if we are sending, have sent, or are
  2179. * planning to send, RESET_STREAM. The other terminal state is Data
  2180. * Recvd, but txp_generate_stream_frames() is guaranteed to generate
  2181. * nothing in this case.
  2182. */
  2183. if (ossl_quic_stream_has_send_buffer(stream)
  2184. && !ossl_quic_stream_send_is_reset(stream)) {
  2185. int packet_full = 0;
  2186. if (!ossl_assert(!stream->want_reset_stream))
  2187. return 0;
  2188. if (!txp_generate_stream_frames(txp, pkt,
  2189. stream->id, stream->sstream,
  2190. &stream->txfc,
  2191. snext,
  2192. have_ack_eliciting,
  2193. &packet_full,
  2194. &stream->txp_txfc_new_credit_consumed,
  2195. conn_consumed)) {
  2196. /* Fatal error (allocation, etc.) */
  2197. txp_enlink_tmp(tmp_head, stream);
  2198. return 0;
  2199. }
  2200. conn_consumed += stream->txp_txfc_new_credit_consumed;
  2201. if (packet_full) {
  2202. txp_enlink_tmp(tmp_head, stream);
  2203. break;
  2204. }
  2205. }
  2206. txp_enlink_tmp(tmp_head, stream);
  2207. }
  2208. return 1;
  2209. }
  2210. static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp,
  2211. struct txp_pkt *pkt,
  2212. int chosen_for_conn_close)
  2213. {
  2214. int rc = TXP_ERR_SUCCESS;
  2215. const uint32_t enc_level = pkt->h.enc_level;
  2216. const uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
  2217. int have_ack_eliciting = 0, done_pre_token = 0;
  2218. const struct archetype_data a = pkt->geom.adata;
  2219. /*
  2220. * Cleared if we encode any non-ACK-eliciting frame type which rules out the
  2221. * packet being a non-inflight frame. This means any non-ACK ACK-eliciting
  2222. * frame, even PADDING frames. ACK eliciting frames always cause a packet to
  2223. * become ineligible for non-inflight treatment so it is not necessary to
  2224. * clear this in cases where have_ack_eliciting is set, as it is ignored in
  2225. * that case.
  2226. */
  2227. int can_be_non_inflight = 1;
  2228. QUIC_CFQ_ITEM *cfq_item;
  2229. QUIC_TXPIM_PKT *tpkt = NULL;
  2230. struct tx_helper *h = &pkt->h;
  2231. /* Maximum PN reached? */
  2232. if (!ossl_quic_pn_valid(txp->next_pn[pn_space]))
  2233. goto fatal_err;
  2234. if (!ossl_assert(pkt->tpkt == NULL))
  2235. goto fatal_err;
  2236. if ((pkt->tpkt = tpkt = ossl_quic_txpim_pkt_alloc(txp->args.txpim)) == NULL)
  2237. goto fatal_err;
  2238. /*
  2239. * Frame Serialization
  2240. * ===================
  2241. *
  2242. * We now serialize frames into the packet in descending order of priority.
  2243. */
  2244. /* HANDSHAKE_DONE (Regenerate) */
  2245. if (a.allow_handshake_done && txp->want_handshake_done
  2246. && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_HANDSHAKE_DONE) {
  2247. WPACKET *wpkt = tx_helper_begin(h);
  2248. if (wpkt == NULL)
  2249. goto fatal_err;
  2250. if (ossl_quic_wire_encode_frame_handshake_done(wpkt)) {
  2251. tpkt->had_handshake_done_frame = 1;
  2252. have_ack_eliciting = 1;
  2253. if (!tx_helper_commit(h))
  2254. goto fatal_err;
  2255. tx_helper_unrestrict(h); /* no longer need PING */
  2256. } else {
  2257. tx_helper_rollback(h);
  2258. }
  2259. }
  2260. /* MAX_DATA (Regenerate) */
  2261. if (a.allow_conn_fc
  2262. && (txp->want_max_data
  2263. || ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 0))
  2264. && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_MAX_DATA) {
  2265. WPACKET *wpkt = tx_helper_begin(h);
  2266. uint64_t cwm = ossl_quic_rxfc_get_cwm(txp->args.conn_rxfc);
  2267. if (wpkt == NULL)
  2268. goto fatal_err;
  2269. if (ossl_quic_wire_encode_frame_max_data(wpkt, cwm)) {
  2270. tpkt->had_max_data_frame = 1;
  2271. have_ack_eliciting = 1;
  2272. if (!tx_helper_commit(h))
  2273. goto fatal_err;
  2274. tx_helper_unrestrict(h); /* no longer need PING */
  2275. } else {
  2276. tx_helper_rollback(h);
  2277. }
  2278. }
  2279. /* MAX_STREAMS_BIDI (Regenerate) */
  2280. if (a.allow_conn_fc
  2281. && (txp->want_max_streams_bidi
  2282. || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc, 0))
  2283. && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_MAX_STREAMS_BIDI) {
  2284. WPACKET *wpkt = tx_helper_begin(h);
  2285. uint64_t max_streams
  2286. = ossl_quic_rxfc_get_cwm(txp->args.max_streams_bidi_rxfc);
  2287. if (wpkt == NULL)
  2288. goto fatal_err;
  2289. if (ossl_quic_wire_encode_frame_max_streams(wpkt, /*is_uni=*/0,
  2290. max_streams)) {
  2291. tpkt->had_max_streams_bidi_frame = 1;
  2292. have_ack_eliciting = 1;
  2293. if (!tx_helper_commit(h))
  2294. goto fatal_err;
  2295. tx_helper_unrestrict(h); /* no longer need PING */
  2296. } else {
  2297. tx_helper_rollback(h);
  2298. }
  2299. }
  2300. /* MAX_STREAMS_UNI (Regenerate) */
  2301. if (a.allow_conn_fc
  2302. && (txp->want_max_streams_uni
  2303. || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc, 0))
  2304. && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_MAX_STREAMS_UNI) {
  2305. WPACKET *wpkt = tx_helper_begin(h);
  2306. uint64_t max_streams
  2307. = ossl_quic_rxfc_get_cwm(txp->args.max_streams_uni_rxfc);
  2308. if (wpkt == NULL)
  2309. goto fatal_err;
  2310. if (ossl_quic_wire_encode_frame_max_streams(wpkt, /*is_uni=*/1,
  2311. max_streams)) {
  2312. tpkt->had_max_streams_uni_frame = 1;
  2313. have_ack_eliciting = 1;
  2314. if (!tx_helper_commit(h))
  2315. goto fatal_err;
  2316. tx_helper_unrestrict(h); /* no longer need PING */
  2317. } else {
  2318. tx_helper_rollback(h);
  2319. }
  2320. }
  2321. /* GCR Frames */
  2322. for (cfq_item = ossl_quic_cfq_get_priority_head(txp->args.cfq, pn_space);
  2323. cfq_item != NULL;
  2324. cfq_item = ossl_quic_cfq_item_get_priority_next(cfq_item, pn_space)) {
  2325. uint64_t frame_type = ossl_quic_cfq_item_get_frame_type(cfq_item);
  2326. const unsigned char *encoded = ossl_quic_cfq_item_get_encoded(cfq_item);
  2327. size_t encoded_len = ossl_quic_cfq_item_get_encoded_len(cfq_item);
  2328. switch (frame_type) {
  2329. case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
  2330. if (!a.allow_new_conn_id)
  2331. continue;
  2332. break;
  2333. case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
  2334. if (!a.allow_retire_conn_id)
  2335. continue;
  2336. break;
  2337. case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
  2338. if (!a.allow_new_token)
  2339. continue;
  2340. /*
  2341. * NEW_TOKEN frames are handled via GCR, but some
  2342. * Regenerate-strategy frames should come before them (namely
  2343. * ACK, CONNECTION_CLOSE, PATH_CHALLENGE and PATH_RESPONSE). If
  2344. * we find a NEW_TOKEN frame, do these now. If there are no
  2345. * NEW_TOKEN frames in the GCR queue we will handle these below.
  2346. */
  2347. if (!done_pre_token)
  2348. if (txp_generate_pre_token(txp, pkt,
  2349. chosen_for_conn_close,
  2350. &can_be_non_inflight))
  2351. done_pre_token = 1;
  2352. break;
  2353. case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
  2354. if (!a.allow_path_response)
  2355. continue;
  2356. /*
  2357. * RFC 9000 s. 8.2.2: An endpoint MUST expand datagrams that
  2358. * contain a PATH_RESPONSE frame to at least the smallest
  2359. * allowed maximum datagram size of 1200 bytes.
  2360. */
  2361. pkt->force_pad = 1;
  2362. break;
  2363. default:
  2364. if (!a.allow_cfq_other)
  2365. continue;
  2366. break;
  2367. }
  2368. /*
  2369. * If the frame is too big, don't try to schedule any more GCR frames in
  2370. * this packet rather than sending subsequent ones out of order.
  2371. */
  2372. if (encoded_len > tx_helper_get_space_left(h))
  2373. break;
  2374. if (!tx_helper_append_iovec(h, encoded, encoded_len))
  2375. goto fatal_err;
  2376. ossl_quic_txpim_pkt_add_cfq_item(tpkt, cfq_item);
  2377. if (ossl_quic_frame_type_is_ack_eliciting(frame_type)) {
  2378. have_ack_eliciting = 1;
  2379. tx_helper_unrestrict(h); /* no longer need PING */
  2380. }
  2381. }
  2382. /*
  2383. * If we didn't generate ACK, CONNECTION_CLOSE, PATH_CHALLENGE or
  2384. * PATH_RESPONSE (as desired) before, do so now.
  2385. */
  2386. if (!done_pre_token)
  2387. if (txp_generate_pre_token(txp, pkt,
  2388. chosen_for_conn_close,
  2389. &can_be_non_inflight))
  2390. done_pre_token = 1;
  2391. /* CRYPTO Frames */
  2392. if (a.allow_crypto)
  2393. if (!txp_generate_crypto_frames(txp, pkt, &have_ack_eliciting))
  2394. goto fatal_err;
  2395. /* Stream-specific frames */
  2396. if (a.allow_stream_rel && txp->handshake_complete)
  2397. if (!txp_generate_stream_related(txp, pkt,
  2398. &have_ack_eliciting,
  2399. &pkt->stream_head))
  2400. goto fatal_err;
  2401. /* PING */
  2402. tx_helper_unrestrict(h);
  2403. if (!have_ack_eliciting && txp_need_ping(txp, pn_space, &a)) {
  2404. WPACKET *wpkt;
  2405. assert(h->reserve > 0);
  2406. wpkt = tx_helper_begin(h);
  2407. if (wpkt == NULL)
  2408. goto fatal_err;
  2409. if (!ossl_quic_wire_encode_frame_ping(wpkt)
  2410. || !tx_helper_commit(h))
  2411. /*
  2412. * We treat a request to be ACK-eliciting as a requirement, so this
  2413. * is an error.
  2414. */
  2415. goto fatal_err;
  2416. have_ack_eliciting = 1;
  2417. }
  2418. /* PADDING is added by ossl_quic_tx_packetiser_generate(). */
  2419. /*
  2420. * ACKM Data
  2421. * =========
  2422. */
  2423. if (have_ack_eliciting)
  2424. can_be_non_inflight = 0;
  2425. /* ACKM Data */
  2426. tpkt->ackm_pkt.num_bytes = h->bytes_appended + pkt->geom.pkt_overhead;
  2427. tpkt->ackm_pkt.pkt_num = txp->next_pn[pn_space];
  2428. /* largest_acked is set in txp_generate_pre_token */
  2429. tpkt->ackm_pkt.pkt_space = pn_space;
  2430. tpkt->ackm_pkt.is_inflight = !can_be_non_inflight;
  2431. tpkt->ackm_pkt.is_ack_eliciting = have_ack_eliciting;
  2432. tpkt->ackm_pkt.is_pto_probe = 0;
  2433. tpkt->ackm_pkt.is_mtu_probe = 0;
  2434. tpkt->ackm_pkt.time = txp->args.now(txp->args.now_arg);
  2435. /* Done. */
  2436. return rc;
  2437. fatal_err:
  2438. /*
  2439. * Handler for fatal errors, i.e. errors causing us to abort the entire
  2440. * packet rather than just one frame. Examples of such errors include
  2441. * allocation errors.
  2442. */
  2443. if (tpkt != NULL) {
  2444. ossl_quic_txpim_pkt_release(txp->args.txpim, tpkt);
  2445. pkt->tpkt = NULL;
  2446. }
  2447. return TXP_ERR_INTERNAL;
  2448. }
  2449. /*
  2450. * Commits and queues a packet for transmission. There is no backing out after
  2451. * this.
  2452. *
  2453. * This:
  2454. *
  2455. * - Sends the packet to the QTX for encryption and transmission;
  2456. *
  2457. * - Records the packet as having been transmitted in FIFM. ACKM is informed,
  2458. * etc. and the TXPIM record is filed.
  2459. *
  2460. * - Informs various subsystems of frames that were sent and clears frame
  2461. * wanted flags so that we do not generate the same frames again.
  2462. *
  2463. * Assumptions:
  2464. *
  2465. * - pkt is a txp_pkt for the correct EL;
  2466. *
  2467. * - pkt->tpkt is valid;
  2468. *
  2469. * - pkt->tpkt->ackm_pkt has been fully filled in;
  2470. *
  2471. * - Stream chunk records have been appended to pkt->tpkt for STREAM and
  2472. * CRYPTO frames, but not for RESET_STREAM or STOP_SENDING frames;
  2473. *
  2474. * - The chosen stream list for the packet can be fully walked from
  2475. * pkt->stream_head using stream->txp_next;
  2476. *
  2477. * - pkt->has_ack_eliciting is set correctly.
  2478. *
  2479. */
  2480. static int txp_pkt_commit(OSSL_QUIC_TX_PACKETISER *txp,
  2481. struct txp_pkt *pkt,
  2482. uint32_t archetype,
  2483. int *txpim_pkt_reffed)
  2484. {
  2485. int rc = 1;
  2486. uint32_t enc_level = pkt->h.enc_level;
  2487. uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
  2488. QUIC_TXPIM_PKT *tpkt = pkt->tpkt;
  2489. QUIC_STREAM *stream;
  2490. OSSL_QTX_PKT txpkt;
  2491. struct archetype_data a;
  2492. *txpim_pkt_reffed = 0;
  2493. /* Cannot send a packet with an empty payload. */
  2494. if (pkt->h.bytes_appended == 0)
  2495. return 0;
  2496. if (!txp_get_archetype_data(enc_level, archetype, &a))
  2497. return 0;
  2498. /* Packet Information for QTX */
  2499. txpkt.hdr = &pkt->phdr;
  2500. txpkt.iovec = txp->el[enc_level].iovec;
  2501. txpkt.num_iovec = pkt->h.num_iovec;
  2502. txpkt.local = NULL;
  2503. txpkt.peer = BIO_ADDR_family(&txp->args.peer) == AF_UNSPEC
  2504. ? NULL : &txp->args.peer;
  2505. txpkt.pn = txp->next_pn[pn_space];
  2506. txpkt.flags = OSSL_QTX_PKT_FLAG_COALESCE; /* always try to coalesce */
  2507. /* Generate TXPIM chunks representing STOP_SENDING and RESET_STREAM frames. */
  2508. for (stream = pkt->stream_head; stream != NULL; stream = stream->txp_next)
  2509. if (stream->txp_sent_stop_sending || stream->txp_sent_reset_stream) {
  2510. /* Log STOP_SENDING/RESET_STREAM chunk to TXPIM. */
  2511. QUIC_TXPIM_CHUNK chunk;
  2512. chunk.stream_id = stream->id;
  2513. chunk.start = UINT64_MAX;
  2514. chunk.end = 0;
  2515. chunk.has_fin = 0;
  2516. chunk.has_stop_sending = stream->txp_sent_stop_sending;
  2517. chunk.has_reset_stream = stream->txp_sent_reset_stream;
  2518. if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
  2519. return 0; /* alloc error */
  2520. }
  2521. /* Dispatch to FIFD. */
  2522. if (!ossl_quic_fifd_pkt_commit(&txp->fifd, tpkt))
  2523. return 0;
  2524. /*
  2525. * Transmission and Post-Packet Generation Bookkeeping
  2526. * ===================================================
  2527. *
  2528. * No backing out anymore - at this point the ACKM has recorded the packet
  2529. * as having been sent, so we need to increment our next PN counter, or
  2530. * the ACKM will complain when we try to record a duplicate packet with
  2531. * the same PN later. At this point actually sending the packet may still
  2532. * fail. In this unlikely event it will simply be handled as though it
  2533. * were a lost packet.
  2534. */
  2535. ++txp->next_pn[pn_space];
  2536. *txpim_pkt_reffed = 1;
  2537. /* Send the packet. */
  2538. if (!ossl_qtx_write_pkt(txp->args.qtx, &txpkt))
  2539. return 0;
  2540. /*
  2541. * Record FC and stream abort frames as sent; deactivate streams which no
  2542. * longer have anything to do.
  2543. */
  2544. for (stream = pkt->stream_head; stream != NULL; stream = stream->txp_next) {
  2545. if (stream->txp_sent_fc) {
  2546. stream->want_max_stream_data = 0;
  2547. ossl_quic_rxfc_has_cwm_changed(&stream->rxfc, 1);
  2548. }
  2549. if (stream->txp_sent_stop_sending)
  2550. stream->want_stop_sending = 0;
  2551. if (stream->txp_sent_reset_stream)
  2552. stream->want_reset_stream = 0;
  2553. if (stream->txp_txfc_new_credit_consumed > 0) {
  2554. if (!ossl_assert(ossl_quic_txfc_consume_credit(&stream->txfc,
  2555. stream->txp_txfc_new_credit_consumed)))
  2556. /*
  2557. * Should not be possible, but we should continue with our
  2558. * bookkeeping as we have already committed the packet to the
  2559. * FIFD. Just change the value we return.
  2560. */
  2561. rc = 0;
  2562. stream->txp_txfc_new_credit_consumed = 0;
  2563. }
  2564. /*
  2565. * If we no longer need to generate any flow control (MAX_STREAM_DATA),
  2566. * STOP_SENDING or RESET_STREAM frames, nor any STREAM frames (because
  2567. * the stream is drained of data or TXFC-blocked), we can mark the
  2568. * stream as inactive.
  2569. */
  2570. ossl_quic_stream_map_update_state(txp->args.qsm, stream);
  2571. if (ossl_quic_stream_has_send_buffer(stream)
  2572. && !ossl_quic_sstream_has_pending(stream->sstream)
  2573. && ossl_quic_sstream_get_final_size(stream->sstream, NULL))
  2574. /*
  2575. * Transition to DATA_SENT if stream has a final size and we have
  2576. * sent all data.
  2577. */
  2578. ossl_quic_stream_map_notify_all_data_sent(txp->args.qsm, stream);
  2579. }
  2580. /* We have now sent the packet, so update state accordingly. */
  2581. if (tpkt->ackm_pkt.is_ack_eliciting)
  2582. txp->force_ack_eliciting &= ~(1UL << pn_space);
  2583. if (tpkt->had_handshake_done_frame)
  2584. txp->want_handshake_done = 0;
  2585. if (tpkt->had_max_data_frame) {
  2586. txp->want_max_data = 0;
  2587. ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 1);
  2588. }
  2589. if (tpkt->had_max_streams_bidi_frame) {
  2590. txp->want_max_streams_bidi = 0;
  2591. ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc, 1);
  2592. }
  2593. if (tpkt->had_max_streams_uni_frame) {
  2594. txp->want_max_streams_uni = 0;
  2595. ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc, 1);
  2596. }
  2597. if (tpkt->had_ack_frame)
  2598. txp->want_ack &= ~(1UL << pn_space);
  2599. if (tpkt->had_conn_close)
  2600. txp->want_conn_close = 0;
  2601. /*
  2602. * Decrement probe request counts if we have sent a packet that meets
  2603. * the requirement of a probe, namely being ACK-eliciting.
  2604. */
  2605. if (tpkt->ackm_pkt.is_ack_eliciting) {
  2606. OSSL_ACKM_PROBE_INFO *probe_info
  2607. = ossl_ackm_get0_probe_request(txp->args.ackm);
  2608. if (enc_level == QUIC_ENC_LEVEL_INITIAL
  2609. && probe_info->anti_deadlock_initial > 0)
  2610. --probe_info->anti_deadlock_initial;
  2611. if (enc_level == QUIC_ENC_LEVEL_HANDSHAKE
  2612. && probe_info->anti_deadlock_handshake > 0)
  2613. --probe_info->anti_deadlock_handshake;
  2614. if (a.allow_force_ack_eliciting /* (i.e., not for 0-RTT) */
  2615. && probe_info->pto[pn_space] > 0)
  2616. --probe_info->pto[pn_space];
  2617. }
  2618. return rc;
  2619. }
  2620. /* Ensure the iovec array is at least num elements long. */
  2621. static int txp_el_ensure_iovec(struct txp_el *el, size_t num)
  2622. {
  2623. OSSL_QTX_IOVEC *iovec;
  2624. if (el->alloc_iovec >= num)
  2625. return 1;
  2626. num = el->alloc_iovec != 0 ? el->alloc_iovec * 2 : 8;
  2627. iovec = OPENSSL_realloc(el->iovec, sizeof(OSSL_QTX_IOVEC) * num);
  2628. if (iovec == NULL)
  2629. return 0;
  2630. el->iovec = iovec;
  2631. el->alloc_iovec = num;
  2632. return 1;
  2633. }
  2634. int ossl_quic_tx_packetiser_schedule_conn_close(OSSL_QUIC_TX_PACKETISER *txp,
  2635. const OSSL_QUIC_FRAME_CONN_CLOSE *f)
  2636. {
  2637. char *reason = NULL;
  2638. size_t reason_len = f->reason_len;
  2639. size_t max_reason_len = txp_get_mdpl(txp) / 2;
  2640. if (txp->want_conn_close)
  2641. return 0;
  2642. /*
  2643. * Arbitrarily limit the length of the reason length string to half of the
  2644. * MDPL.
  2645. */
  2646. if (reason_len > max_reason_len)
  2647. reason_len = max_reason_len;
  2648. if (reason_len > 0) {
  2649. reason = OPENSSL_memdup(f->reason, reason_len);
  2650. if (reason == NULL)
  2651. return 0;
  2652. }
  2653. txp->conn_close_frame = *f;
  2654. txp->conn_close_frame.reason = reason;
  2655. txp->conn_close_frame.reason_len = reason_len;
  2656. txp->want_conn_close = 1;
  2657. return 1;
  2658. }
  2659. void ossl_quic_tx_packetiser_set_msg_callback(OSSL_QUIC_TX_PACKETISER *txp,
  2660. ossl_msg_cb msg_callback,
  2661. SSL *msg_callback_ssl)
  2662. {
  2663. txp->msg_callback = msg_callback;
  2664. txp->msg_callback_ssl = msg_callback_ssl;
  2665. }
  2666. void ossl_quic_tx_packetiser_set_msg_callback_arg(OSSL_QUIC_TX_PACKETISER *txp,
  2667. void *msg_callback_arg)
  2668. {
  2669. txp->msg_callback_arg = msg_callback_arg;
  2670. }
  2671. QUIC_PN ossl_quic_tx_packetiser_get_next_pn(OSSL_QUIC_TX_PACKETISER *txp,
  2672. uint32_t pn_space)
  2673. {
  2674. if (pn_space >= QUIC_PN_SPACE_NUM)
  2675. return UINT64_MAX;
  2676. return txp->next_pn[pn_space];
  2677. }
  2678. OSSL_TIME ossl_quic_tx_packetiser_get_deadline(OSSL_QUIC_TX_PACKETISER *txp)
  2679. {
  2680. /*
  2681. * TXP-specific deadline computations which rely on TXP innards. This is in
  2682. * turn relied on by the QUIC_CHANNEL code to determine the channel event
  2683. * handling deadline.
  2684. */
  2685. OSSL_TIME deadline = ossl_time_infinite();
  2686. uint32_t enc_level, pn_space;
  2687. /*
  2688. * ACK generation is not CC-gated - packets containing only ACKs are allowed
  2689. * to bypass CC. We want to generate ACK frames even if we are currently
  2690. * restricted by CC so the peer knows we have received data. The generate
  2691. * call will take care of selecting the correct packet archetype.
  2692. */
  2693. for (enc_level = QUIC_ENC_LEVEL_INITIAL;
  2694. enc_level < QUIC_ENC_LEVEL_NUM;
  2695. ++enc_level)
  2696. if (ossl_qtx_is_enc_level_provisioned(txp->args.qtx, enc_level)) {
  2697. pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
  2698. deadline = ossl_time_min(deadline,
  2699. ossl_ackm_get_ack_deadline(txp->args.ackm, pn_space));
  2700. }
  2701. /* When will CC let us send more? */
  2702. if (txp->args.cc_method->get_tx_allowance(txp->args.cc_data) == 0)
  2703. deadline = ossl_time_min(deadline,
  2704. txp->args.cc_method->get_wakeup_deadline(txp->args.cc_data));
  2705. return deadline;
  2706. }