blob: 1670da40f47193f8aa999e47278c6435d34e2823 [file] [log] [blame]
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001/*
2 * QUIC protocol implementation. Lower layer with internal features implemented
3 * here such as QUIC encryption, idle timeout, acknowledgement and
4 * retransmission.
5 *
6 * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 */
14
15#include <haproxy/quic_conn.h>
16
17#define _GNU_SOURCE
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020018#include <stdio.h>
19#include <stdlib.h>
20
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include <netinet/tcp.h>
26
27#include <import/ebmbtree.h>
28
29#include <haproxy/buf-t.h>
30#include <haproxy/compat.h>
31#include <haproxy/api.h>
32#include <haproxy/debug.h>
33#include <haproxy/tools.h>
34#include <haproxy/ticks.h>
Amaury Denoyelle162baaf2023-04-03 18:49:39 +020035#include <haproxy/xxhash.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020036
Amaury Denoyelle15c74702023-02-01 10:18:26 +010037#include <haproxy/applet-t.h>
38#include <haproxy/cli.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020039#include <haproxy/connection.h>
40#include <haproxy/fd.h>
41#include <haproxy/freq_ctr.h>
42#include <haproxy/global.h>
43#include <haproxy/h3.h>
44#include <haproxy/hq_interop.h>
45#include <haproxy/log.h>
46#include <haproxy/mux_quic.h>
47#include <haproxy/ncbuf.h>
48#include <haproxy/pipe.h>
49#include <haproxy/proxy.h>
50#include <haproxy/quic_cc.h>
51#include <haproxy/quic_frame.h>
52#include <haproxy/quic_enc.h>
53#include <haproxy/quic_loss.h>
54#include <haproxy/quic_sock.h>
55#include <haproxy/quic_stats.h>
56#include <haproxy/quic_stream.h>
57#include <haproxy/quic_tp.h>
58#include <haproxy/cbuf.h>
59#include <haproxy/proto_quic.h>
60#include <haproxy/quic_tls.h>
61#include <haproxy/ssl_sock.h>
62#include <haproxy/task.h>
Amaury Denoyelle15c74702023-02-01 10:18:26 +010063#include <haproxy/thread.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020064#include <haproxy/trace.h>
65
Amaury Denoyelle15c74702023-02-01 10:18:26 +010066/* incremented by each "show quic". */
67static unsigned int qc_epoch = 0;
68
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020069/* list of supported QUIC versions by this implementation */
70const struct quic_version quic_versions[] = {
71 {
72 .num = QUIC_PROTOCOL_VERSION_DRAFT_29,
73 .initial_salt = initial_salt_draft_29,
74 .initial_salt_len = sizeof initial_salt_draft_29,
75 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
76 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
77 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
78 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
79 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
80 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
81 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
82 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
83 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT,
84 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT,
85 },
86 {
87 .num = QUIC_PROTOCOL_VERSION_1,
88 .initial_salt = initial_salt_v1,
89 .initial_salt_len = sizeof initial_salt_v1,
90 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
91 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
92 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
93 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
94 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
95 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
96 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
97 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
98 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1,
99 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1,
100 },
101 {
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +0100102 .num = QUIC_PROTOCOL_VERSION_2,
103 .initial_salt = initial_salt_v2,
104 .initial_salt_len = sizeof initial_salt_v2,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200105 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2,
106 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1,
107 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2,
108 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1,
109 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2,
110 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1,
111 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2,
112 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1,
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +0100113 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2,
114 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200115 },
116};
117
118/* The total number of supported versions */
119const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions;
120/* Listener only preferred version */
121const struct quic_version *preferred_version;
122
123/* trace source and events */
124static void quic_trace(enum trace_level level, uint64_t mask, \
125 const struct trace_source *src,
126 const struct ist where, const struct ist func,
127 const void *a1, const void *a2, const void *a3, const void *a4);
128
129static const struct trace_event quic_trace_events[] = {
130 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
131 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
132 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
133 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
134 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
135 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
136 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
137 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
138 { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" },
139 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
140 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
141 { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" },
142 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
143 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
144 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
145 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
146 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
147 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
148 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
149 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
150 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
151 { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" },
152 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
153 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
154 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
155 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
156 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
157 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
158 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
159 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
160 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
161 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
162 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
163 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
164 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
165 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
166 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
167 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
168 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
169 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
170 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
171 { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"},
172 { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"},
173 { .mask = QUIC_EV_CONN_SUB, .name = "xprt_sub", .desc = "RX/TX subcription or unsubscription to QUIC xprt"},
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200174 { .mask = QUIC_EV_CONN_RCV, .name = "conn_recv", .desc = "RX on connection" },
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200175 { /* end */ }
176};
177
178static const struct name_desc quic_trace_lockon_args[4] = {
179 /* arg1 */ { /* already used by the connection */ },
180 /* arg2 */ { .name="quic", .desc="QUIC transport" },
181 /* arg3 */ { },
182 /* arg4 */ { }
183};
184
185static const struct name_desc quic_trace_decoding[] = {
186#define QUIC_VERB_CLEAN 1
187 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
188 { /* end */ }
189};
190
191
192struct trace_source trace_quic = {
193 .name = IST("quic"),
194 .desc = "QUIC xprt",
195 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
196 .default_cb = quic_trace,
197 .known_events = quic_trace_events,
198 .lockon_args = quic_trace_lockon_args,
199 .decoding = quic_trace_decoding,
200 .report_events = ~0, /* report everything by default */
201};
202
203#define TRACE_SOURCE &trace_quic
204INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
205
206static BIO_METHOD *ha_quic_meth;
207
208DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ);
209DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
210DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
211 "quic_conn_ctx", sizeof(struct ssl_sock_ctx));
212DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
213DECLARE_POOL(pool_head_quic_connection_id,
214 "quic_connnection_id", sizeof(struct quic_connection_id));
215DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
216DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet));
217DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet));
218DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm));
219DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf", sizeof(struct quic_crypto_buf));
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +0200220DECLARE_STATIC_POOL(pool_head_quic_cstream, "quic_cstream", sizeof(struct quic_cstream));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200221DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame));
222DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node));
223
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +0100224static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +0200225 struct quic_conn *qc,
226 const struct quic_cid *odcid,
227 const struct sockaddr_storage *saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200228static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
229 struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
230 struct list *frms, struct quic_conn *qc,
231 const struct quic_version *ver, size_t dglen, int pkt_type,
232 int force_ack, int padding, int probe, int cc, int *err);
233struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécailled7215712023-03-24 18:13:37 +0100234static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack);
235static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200236static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
237static int quic_conn_init_timer(struct quic_conn *qc);
238static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
239
240/* Only for debug purpose */
241struct enc_debug_info {
242 unsigned char *payload;
243 size_t payload_len;
244 unsigned char *aad;
245 size_t aad_len;
246 uint64_t pn;
247};
248
249/* Initializes a enc_debug_info struct (only for debug purpose) */
250static inline void enc_debug_info_init(struct enc_debug_info *edi,
251 unsigned char *payload, size_t payload_len,
252 unsigned char *aad, size_t aad_len, uint64_t pn)
253{
254 edi->payload = payload;
255 edi->payload_len = payload_len;
256 edi->aad = aad;
257 edi->aad_len = aad_len;
258 edi->pn = pn;
259}
260
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100261/* Used only for QUIC TLS key phase traces */
262struct quic_kp_trace {
263 const unsigned char *rx_sec;
264 size_t rx_seclen;
265 const struct quic_tls_kp *rx;
266 const unsigned char *tx_sec;
267 size_t tx_seclen;
268 const struct quic_tls_kp *tx;
269};
270
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200271/* Trace callback for QUIC.
272 * These traces always expect that arg1, if non-null, is of type connection.
273 */
274static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
275 const struct ist where, const struct ist func,
276 const void *a1, const void *a2, const void *a3, const void *a4)
277{
278 const struct quic_conn *qc = a1;
279
280 if (qc) {
281 const struct quic_tls_ctx *tls_ctx;
282
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +0200283 chunk_appendf(&trace_buf, " : qc@%p flags=0x%x", qc, qc->flags);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200284 if (mask & QUIC_EV_CONN_INIT) {
285 chunk_appendf(&trace_buf, "\n odcid");
286 quic_cid_dump(&trace_buf, &qc->odcid);
287 chunk_appendf(&trace_buf, "\n dcid");
288 quic_cid_dump(&trace_buf, &qc->dcid);
289 chunk_appendf(&trace_buf, "\n scid");
290 quic_cid_dump(&trace_buf, &qc->scid);
291 }
292
293 if (mask & QUIC_EV_TRANSP_PARAMS) {
294 const struct quic_transport_params *p = a2;
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100295
296 if (p)
297 quic_transport_params_dump(&trace_buf, qc, p);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200298 }
299
300 if (mask & QUIC_EV_CONN_ADDDATA) {
301 const enum ssl_encryption_level_t *level = a2;
302 const size_t *len = a3;
303
304 if (level) {
305 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
306
307 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
308 }
309 if (len)
310 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
311 }
312 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
313 /* Initial read & write secrets. */
314 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
315 const unsigned char *rx_sec = a2;
316 const unsigned char *tx_sec = a3;
317
318 tls_ctx = &qc->els[level].tls_ctx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200319 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
320 if (rx_sec)
321 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
322 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
323 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
324 if (tx_sec)
325 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
326 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200327 }
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100328
329 if ((mask & QUIC_EV_CONN_KP) && qc) {
330 /* Initial read & write secrets. */
331 const struct quic_kp_trace *kp = a2;
332
333 if (kp) {
334 if (kp->rx) {
335 chunk_appendf(&trace_buf, "\n RX kp");
336 if (kp->rx_sec)
337 quic_tls_secret_hexdump(&trace_buf, kp->rx_sec, kp->rx_seclen);
338 quic_tls_kp_keys_hexdump(&trace_buf, kp->rx);
339 }
340 if (kp->tx) {
341 chunk_appendf(&trace_buf, "\n TX kp");
342 if (kp->tx_sec)
343 quic_tls_secret_hexdump(&trace_buf, kp->tx_sec, kp->tx_seclen);
344 quic_tls_kp_keys_hexdump(&trace_buf, kp->tx);
345 }
346 }
347 }
348
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200349 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
350 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200351
352 if (level) {
353 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
354
355 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200356 if (quic_tls_has_rx_sec(&qc->els[lvl])) {
357 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200358 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200359 }
360 else
361 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200362 }
363 }
364
365 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
366 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200367
368 if (level) {
369 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
370
371 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200372 if (quic_tls_has_tx_sec(&qc->els[lvl])) {
373 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200374 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200375 }
376 else
377 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200378 }
379
380 }
381
382 if (mask & QUIC_EV_CONN_FRMLIST) {
383 const struct list *l = a2;
384
385 if (l) {
386 const struct quic_frame *frm;
387 list_for_each_entry(frm, l, list) {
388 chunk_appendf(&trace_buf, " frm@%p", frm);
389 chunk_frm_appendf(&trace_buf, frm);
390 }
391 }
392 }
393
394 if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) {
395 const struct quic_tx_packet *pkt = a2;
396 const struct quic_enc_level *qel = a3;
397 const ssize_t *room = a4;
398
399 if (qel) {
400 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaille45400532023-02-13 18:39:19 +0100401 chunk_appendf(&trace_buf, " qel=%c pto_count=%d cwnd=%llu ppif=%lld pif=%llu "
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200402 "if=%llu pp=%u",
403 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille45400532023-02-13 18:39:19 +0100404 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200405 (unsigned long long)qc->path->cwnd,
406 (unsigned long long)qc->path->prep_in_flight,
407 (unsigned long long)qc->path->in_flight,
408 (unsigned long long)pktns->tx.in_flight,
409 pktns->tx.pto_probe);
410 }
411 if (pkt) {
412 const struct quic_frame *frm;
413 if (pkt->pn_node.key != (uint64_t)-1)
414 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
415 list_for_each_entry(frm, &pkt->frms, list) {
416 chunk_appendf(&trace_buf, " frm@%p", frm);
417 chunk_frm_appendf(&trace_buf, frm);
418 }
419 }
420
421 if (room) {
422 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
423 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
424 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
425 }
426 }
427
428 if (mask & QUIC_EV_CONN_IO_CB) {
429 const enum quic_handshake_state *state = a2;
430 const int *err = a3;
431
432 if (state)
433 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
434 if (err)
435 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
436 }
437
438 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
439 const struct quic_rx_packet *pkt = a2;
440 const unsigned long *pktlen = a3;
441 const SSL *ssl = a4;
442
443 if (pkt) {
444 chunk_appendf(&trace_buf, " pkt@%p", pkt);
445 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
446 chunk_appendf(&trace_buf, " kp=%d",
447 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
448 chunk_appendf(&trace_buf, " el=%c",
449 quic_packet_type_enc_level_char(pkt->type));
450 if (pkt->pnl)
451 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
452 (unsigned long long)pkt->pn);
453 if (pkt->token_len)
454 chunk_appendf(&trace_buf, " toklen=%llu",
455 (unsigned long long)pkt->token_len);
456 if (pkt->aad_len)
457 chunk_appendf(&trace_buf, " aadlen=%llu",
458 (unsigned long long)pkt->aad_len);
459 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
460 pkt->flags, (unsigned long long)pkt->len);
461 }
462 if (pktlen)
463 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
464 if (ssl) {
465 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
466 chunk_appendf(&trace_buf, " el=%c",
467 quic_enc_level_char(ssl_to_quic_enc_level(level)));
468 }
469 }
470
471 if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
472 const struct quic_rx_packet *pkt = a2;
473 const struct quic_rx_crypto_frm *cf = a3;
474 const SSL *ssl = a4;
475
476 if (pkt)
477 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
478 quic_packet_type_enc_level_char(pkt->type),
479 (unsigned long long)pkt->pn);
480 if (cf)
481 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
482 (unsigned long long)cf->offset_node.key,
483 (unsigned long long)cf->len);
484 if (ssl) {
485 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
486 chunk_appendf(&trace_buf, " rel=%c",
487 quic_enc_level_char(ssl_to_quic_enc_level(level)));
488 }
489
490 if (qc->err.code)
491 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code);
492 }
493
494 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
495 const struct quic_frame *frm = a2;
496
497 if (frm)
498 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
499 }
500
501 if (mask & QUIC_EV_CONN_PHPKTS) {
502 const struct quic_enc_level *qel = a2;
503
504 if (qel) {
505 const struct quic_pktns *pktns = qel->pktns;
506 chunk_appendf(&trace_buf,
Frédéric Lécaille45400532023-02-13 18:39:19 +0100507 " qel=%c state=%s ack?%d pto_count=%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u off=%llu",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200508 quic_enc_level_char_from_qel(qel, qc),
509 quic_hdshk_state_str(qc->state),
510 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille45400532023-02-13 18:39:19 +0100511 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200512 (unsigned long long)qc->path->cwnd,
513 (unsigned long long)qc->path->prep_in_flight,
514 (unsigned long long)qc->path->in_flight,
515 (unsigned long long)pktns->tx.in_flight,
Amaury Denoyelle2f668f02022-11-18 15:24:08 +0100516 pktns->tx.pto_probe,
517 qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200518 }
519 }
520
521 if (mask & QUIC_EV_CONN_ENCPKT) {
522 const struct enc_debug_info *edi = a2;
523
524 if (edi)
525 chunk_appendf(&trace_buf,
526 " payload=@%p payload_len=%llu"
527 " aad=@%p aad_len=%llu pn=%llu",
528 edi->payload, (unsigned long long)edi->payload_len,
529 edi->aad, (unsigned long long)edi->aad_len,
530 (unsigned long long)edi->pn);
531 }
532
533 if (mask & QUIC_EV_CONN_RMHP) {
534 const struct quic_rx_packet *pkt = a2;
535
536 if (pkt) {
537 const int *ret = a3;
538
539 chunk_appendf(&trace_buf, " pkt@%p", pkt);
540 if (ret && *ret)
541 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
542 pkt->pnl, (unsigned long long)pkt->pn);
543 }
544 }
545
546 if (mask & QUIC_EV_CONN_PRSAFRM) {
547 const struct quic_frame *frm = a2;
548 const unsigned long *val1 = a3;
549 const unsigned long *val2 = a4;
550
551 if (frm) {
552 chunk_appendf(&trace_buf, " frm@%p", frm);
553 chunk_frm_appendf(&trace_buf, frm);
554 }
555 if (val1)
556 chunk_appendf(&trace_buf, " %lu", *val1);
557 if (val2)
558 chunk_appendf(&trace_buf, "..%lu", *val2);
559 }
560
561 if (mask & QUIC_EV_CONN_ACKSTRM) {
562 const struct quic_stream *s = a2;
563 const struct qc_stream_desc *stream = a3;
564
565 if (s)
566 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
567 if (stream)
568 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
569 }
570
571 if (mask & QUIC_EV_CONN_RTTUPDT) {
572 const unsigned int *rtt_sample = a2;
573 const unsigned int *ack_delay = a3;
574 const struct quic_loss *ql = a4;
575
576 if (rtt_sample)
577 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
578 if (ack_delay)
579 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
580 if (ql)
581 chunk_appendf(&trace_buf,
582 " srtt=%ums rttvar=%ums min_rtt=%ums",
583 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
584 }
585 if (mask & QUIC_EV_CONN_CC) {
586 const struct quic_cc_event *ev = a2;
587 const struct quic_cc *cc = a3;
588
589 if (a2)
590 quic_cc_event_trace(&trace_buf, ev);
591 if (a3)
592 quic_cc_state_trace(&trace_buf, cc);
593 }
594
595 if (mask & QUIC_EV_CONN_PKTLOSS) {
596 const struct quic_pktns *pktns = a2;
597 const struct list *lost_pkts = a3;
598
599 if (pktns) {
600 chunk_appendf(&trace_buf, " pktns=%s",
601 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
602 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
603 if (pktns->tx.loss_time)
604 chunk_appendf(&trace_buf, " loss_time=%dms",
605 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
606 }
607 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
608 struct quic_tx_packet *pkt;
609
610 chunk_appendf(&trace_buf, " lost_pkts:");
611 list_for_each_entry(pkt, lost_pkts, list)
612 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
613 }
614 }
615
616 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
617 const struct quic_pktns *pktns = a2;
618 const int *duration = a3;
619 const uint64_t *ifae_pkts = a4;
620
621 if (ifae_pkts)
622 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
623 (unsigned long long)*ifae_pkts);
624 if (pktns) {
625 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
626 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
627 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
628 pktns->tx.pto_probe);
629 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
630 if (pktns->tx.in_flight)
631 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
632 if (pktns->tx.loss_time)
633 chunk_appendf(&trace_buf, " loss_time=%dms",
634 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
635 }
636 if (mask & QUIC_EV_CONN_SPTO) {
637 if (pktns->tx.time_of_last_eliciting)
638 chunk_appendf(&trace_buf, " tole=%dms",
639 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
640 if (duration)
641 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
642 }
643 }
644
645 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
646 chunk_appendf(&trace_buf,
647 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
648 }
649 }
650
651 if (mask & QUIC_EV_CONN_SPPKTS) {
652 const struct quic_tx_packet *pkt = a2;
653
Frédéric Lécaille45400532023-02-13 18:39:19 +0100654 chunk_appendf(&trace_buf, " pto_count=%d cwnd=%llu ppif=%llu pif=%llu",
655 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200656 (unsigned long long)qc->path->cwnd,
657 (unsigned long long)qc->path->prep_in_flight,
658 (unsigned long long)qc->path->in_flight);
659 if (pkt) {
660 const struct quic_frame *frm;
Frédéric Lécaille6fd25762023-04-07 19:01:33 +0200661 if (pkt->flags & QUIC_FL_TX_PACKET_ACK)
662 chunk_appendf(&trace_buf, " ack");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200663 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
664 (unsigned long)pkt->pn_node.key,
665 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
666 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
667 (unsigned long long)pkt->in_flight_len);
668 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
669 (unsigned long long)qc->rx.bytes,
670 (unsigned long long)qc->tx.bytes);
671 list_for_each_entry(frm, &pkt->frms, list) {
672 chunk_appendf(&trace_buf, " frm@%p", frm);
673 chunk_frm_appendf(&trace_buf, frm);
674 }
Frédéric Lécaillebc09f742023-02-13 17:45:36 +0100675
676 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
677 chunk_appendf(&trace_buf, " with scid");
678 quic_cid_dump(&trace_buf, &qc->scid);
679 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200680 }
681 }
682
683 if (mask & QUIC_EV_CONN_SSLALERT) {
684 const uint8_t *alert = a2;
685 const enum ssl_encryption_level_t *level = a3;
686
687 if (alert)
688 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
689 if (level)
690 chunk_appendf(&trace_buf, " el=%c",
691 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
692 }
693
694 if (mask & QUIC_EV_CONN_BCFRMS) {
695 const size_t *sz1 = a2;
696 const size_t *sz2 = a3;
697 const size_t *sz3 = a4;
698
699 if (sz1)
700 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
701 if (sz2)
702 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
703 if (sz3)
704 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
705 }
706
707 if (mask & QUIC_EV_CONN_PSTRM) {
708 const struct quic_frame *frm = a2;
709
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100710 if (frm)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200711 chunk_frm_appendf(&trace_buf, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200712 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +0200713
714 if (mask & QUIC_EV_CONN_ELEVELSEL) {
715 const enum quic_handshake_state *state = a2;
716 const enum quic_tls_enc_level *level = a3;
717 const enum quic_tls_enc_level *next_level = a4;
718
719 if (state)
720 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state));
721 if (level)
722 chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level));
723 if (next_level)
724 chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level));
725
726 }
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200727
728 if (mask & QUIC_EV_CONN_RCV) {
729 const struct quic_dgram *dgram = a2;
730
731 if (dgram)
732 chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len);
733 }
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200734
735 if (mask & QUIC_EV_CONN_IDLE_TIMER) {
736 if (tick_isset(qc->ack_expire))
737 chunk_appendf(&trace_buf, " ack_expire=%ums",
738 TICKS_TO_MS(tick_remain(now_ms, qc->ack_expire)));
739 if (tick_isset(qc->idle_expire))
740 chunk_appendf(&trace_buf, " idle_expire=%ums",
741 TICKS_TO_MS(tick_remain(now_ms, qc->idle_expire)));
Frédéric Lécaillece5c1452023-04-05 09:44:21 +0200742 if (qc->idle_timer_task && tick_isset(qc->idle_timer_task->expire))
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200743 chunk_appendf(&trace_buf, " expire=%ums",
744 TICKS_TO_MS(tick_remain(now_ms, qc->idle_timer_task->expire)));
745 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200746 }
747 if (mask & QUIC_EV_CONN_LPKT) {
748 const struct quic_rx_packet *pkt = a2;
749 const uint64_t *len = a3;
750 const struct quic_version *ver = a4;
751
752 if (pkt) {
753 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
754 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
755 if (pkt->pn_node.key != (uint64_t)-1)
756 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
757 }
758
759 if (len)
760 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
761
762 if (ver)
763 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
764 }
765
766 if (mask & QUIC_EV_STATELESS_RST) {
767 const struct quic_cid *cid = a2;
768
769 if (cid)
770 quic_cid_dump(&trace_buf, cid);
771 }
772
773}
774
775/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
776static inline int quic_peer_validated_addr(struct quic_conn *qc)
777{
778 struct quic_pktns *hdshk_pktns, *app_pktns;
779
780 if (!qc_is_listener(qc))
781 return 1;
782
783 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
784 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
785 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
786 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
787 qc->state >= QUIC_HS_ST_COMPLETE)
788 return 1;
789
790 return 0;
791}
792
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100793/* To be called to kill a connection as soon as possible (without sending any packet). */
794void qc_kill_conn(struct quic_conn *qc)
795{
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100796 TRACE_ENTER(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200797 TRACE_PROTO("killing the connection", QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100798 qc->flags |= QUIC_FL_CONN_TO_KILL;
799 task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER);
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100800 TRACE_LEAVE(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100801}
802
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200803/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
804 * both loss detection and PTO and schedule the task assiated to this timer if needed.
805 */
806static inline void qc_set_timer(struct quic_conn *qc)
807{
808 struct quic_pktns *pktns;
809 unsigned int pto;
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100810 int handshake_confirmed;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200811
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100812 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc);
813 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200814
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100815 pktns = NULL;
816 if (!qc->timer_task) {
817 TRACE_PROTO("already released timer task", QUIC_EV_CONN_STIMER, qc);
818 goto leave;
819 }
820
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200821 pktns = quic_loss_pktns(qc);
822 if (tick_isset(pktns->tx.loss_time)) {
823 qc->timer = pktns->tx.loss_time;
824 goto out;
825 }
826
827 /* anti-amplification: the timer must be
828 * cancelled for a server which reached the anti-amplification limit.
829 */
830 if (!quic_peer_validated_addr(qc) &&
831 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
832 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
833 qc->timer = TICK_ETERNITY;
834 goto out;
835 }
836
837 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
838 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
839 /* Timer cancellation. */
840 qc->timer = TICK_ETERNITY;
841 goto out;
842 }
843
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100844 handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED;
845 pktns = quic_pto_pktns(qc, handshake_confirmed, &pto);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200846 if (tick_isset(pto))
847 qc->timer = pto;
848 out:
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100849 if (qc->timer == TICK_ETERNITY) {
850 qc->timer_task->expire = TICK_ETERNITY;
851 }
852 else if (tick_is_expired(qc->timer, now_ms)) {
853 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
854 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
855 }
856 else {
857 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
858 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200859 }
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100860 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100861 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, pktns);
862 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200863}
864
865/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
866 * connection.
867 * Return 1 if succeeded, 0 if not.
868 */
869static int quic_tls_key_update(struct quic_conn *qc)
870{
871 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100872 struct quic_tls_secrets *rx = &tls_ctx->rx;
873 struct quic_tls_secrets *tx = &tls_ctx->tx;
874 /* Used only for the traces */
875 struct quic_kp_trace kp_trace = {
876 .rx_sec = rx->secret,
877 .rx_seclen = rx->secretlen,
878 .tx_sec = tx->secret,
879 .tx_seclen = tx->secretlen,
880 };
881 /* The next key phase secrets to be derived */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200882 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
883 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
884 const struct quic_version *ver =
885 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
886 int ret = 0;
887
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100888 TRACE_ENTER(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200889
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200890 nxt_rx = &qc->ku.nxt_rx;
891 nxt_tx = &qc->ku.nxt_tx;
892
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100893 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
894 "nxt_rx->secretlen=%llu rx->secretlen=%llu",
895 (ull)nxt_rx->secretlen, (ull)rx->secretlen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200896 /* Prepare new RX secrets */
897 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
898 rx->secret, rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100899 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200900 goto leave;
901 }
902
903 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
904 nxt_rx->key, nxt_rx->keylen,
905 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
906 nxt_rx->secret, nxt_rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100907 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200908 goto leave;
909 }
910
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100911 kp_trace.rx = nxt_rx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200912 /* Prepare new TX secrets */
913 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
914 tx->secret, tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100915 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200916 goto leave;
917 }
918
919 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
920 nxt_tx->key, nxt_tx->keylen,
921 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
922 nxt_tx->secret, nxt_tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100923 TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200924 goto leave;
925 }
926
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100927 kp_trace.tx = nxt_tx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200928 if (nxt_rx->ctx) {
929 EVP_CIPHER_CTX_free(nxt_rx->ctx);
930 nxt_rx->ctx = NULL;
931 }
932
933 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100934 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200935 goto leave;
936 }
937
938 if (nxt_tx->ctx) {
939 EVP_CIPHER_CTX_free(nxt_tx->ctx);
940 nxt_tx->ctx = NULL;
941 }
942
943 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100944 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200945 goto leave;
946 }
947
948 ret = 1;
949 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100950 TRACE_PROTO("key update", QUIC_EV_CONN_KP, qc, &kp_trace);
951 TRACE_LEAVE(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200952 return ret;
953}
954
955/* Rotate the Key Update information for <qc> QUIC connection.
956 * Must be used after having updated them.
957 * Always succeeds.
958 */
959static void quic_tls_rotate_keys(struct quic_conn *qc)
960{
961 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
962 unsigned char *curr_secret, *curr_iv, *curr_key;
963 EVP_CIPHER_CTX *curr_ctx;
964
965 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
966
967 /* Rotate the RX secrets */
968 curr_ctx = tls_ctx->rx.ctx;
969 curr_secret = tls_ctx->rx.secret;
970 curr_iv = tls_ctx->rx.iv;
971 curr_key = tls_ctx->rx.key;
972
973 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
974 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
975 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
976 tls_ctx->rx.key = qc->ku.nxt_rx.key;
977
978 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
979 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
980 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
981 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
982
983 qc->ku.prv_rx.ctx = curr_ctx;
984 qc->ku.prv_rx.secret = curr_secret;
985 qc->ku.prv_rx.iv = curr_iv;
986 qc->ku.prv_rx.key = curr_key;
987 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
988
989 /* Update the TX secrets */
990 curr_ctx = tls_ctx->tx.ctx;
991 curr_secret = tls_ctx->tx.secret;
992 curr_iv = tls_ctx->tx.iv;
993 curr_key = tls_ctx->tx.key;
994
995 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
996 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
997 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
998 tls_ctx->tx.key = qc->ku.nxt_tx.key;
999
1000 qc->ku.nxt_tx.ctx = curr_ctx;
1001 qc->ku.nxt_tx.secret = curr_secret;
1002 qc->ku.nxt_tx.iv = curr_iv;
1003 qc->ku.nxt_tx.key = curr_key;
1004
1005 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1006}
1007
1008/* returns 0 on error, 1 on success */
1009int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
1010 const uint8_t *read_secret,
1011 const uint8_t *write_secret, size_t secret_len)
1012{
1013 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1014 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
1015 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001016 struct quic_tls_secrets *rx = NULL, *tx = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001017 const struct quic_version *ver =
1018 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
1019 int ret = 0;
1020
1021 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
1022 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001023
1024 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1025 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1026 goto out;
1027 }
1028
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001029 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1030 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001031 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001032 }
1033
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001034 if (!read_secret)
1035 goto write;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001036
1037 rx = &tls_ctx->rx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001038 if (!quic_tls_secrets_keys_alloc(rx)) {
1039 TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1040 goto leave;
1041 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001042
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001043 rx->aead = tls_aead(cipher);
1044 rx->md = tls_md(cipher);
1045 rx->hp = tls_hp(cipher);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001046
1047 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen,
1048 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
1049 read_secret, secret_len)) {
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001050 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001051 goto leave;
1052 }
1053
1054 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
1055 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1056 goto leave;
1057 }
1058
1059 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
1060 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1061 goto leave;
1062 }
1063
1064 /* Enqueue this connection asap if we could derive O-RTT secrets as
1065 * listener. Note that a listener derives only RX secrets for this
1066 * level.
1067 */
1068 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
1069 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
1070 quic_accept_push_qc(qc);
1071 }
1072
1073write:
1074
1075 if (!write_secret)
1076 goto out;
1077
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001078 tx = &tls_ctx->tx;
1079 if (!quic_tls_secrets_keys_alloc(tx)) {
1080 TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1081 goto leave;
1082 }
1083
1084 tx->aead = tls_aead(cipher);
1085 tx->md = tls_md(cipher);
1086 tx->hp = tls_hp(cipher);
1087
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001088 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen,
1089 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
1090 write_secret, secret_len)) {
1091 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
1092 goto leave;
1093 }
1094
1095 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
1096 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1097 goto leave;
1098 }
1099
1100 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
1101 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1102 goto leave;
1103 }
1104
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01001105 if (level == ssl_encryption_handshake && qc_is_listener(qc)) {
1106 qc->enc_params_len =
1107 quic_transport_params_encode(qc->enc_params,
1108 qc->enc_params + sizeof qc->enc_params,
1109 &qc->rx.params, ver, 1);
1110 if (!qc->enc_params_len) {
1111 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC);
1112 goto leave;
1113 }
1114
1115 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
1116 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC);
1117 goto leave;
1118 }
1119 }
1120
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001121 if (level == ssl_encryption_application) {
1122 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
1123 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
1124 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
1125
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001126 if (rx) {
1127 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1128 TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1129 goto leave;
1130 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001131
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001132 memcpy(rx->secret, read_secret, secret_len);
1133 rx->secretlen = secret_len;
1134 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001135
1136 if (tx) {
1137 if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1138 TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1139 goto leave;
1140 }
1141
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001142 memcpy(tx->secret, write_secret, secret_len);
1143 tx->secretlen = secret_len;
1144 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001145
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001146 /* Initialize all the secret keys lengths */
1147 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001148 }
1149
1150 out:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001151 ret = 1;
1152 leave:
1153 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
1154 return ret;
1155}
1156
1157/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1158 * with <len> as size in CRYPTO buffers dedicated to store the information about
1159 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1160 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
1161 * buffers to store all the data.
1162 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1163 */
1164static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
1165 const unsigned char *data, size_t len)
1166{
1167 struct quic_crypto_buf **qcb;
1168 /* The remaining byte to store in CRYPTO buffers. */
1169 size_t cf_offset, cf_len, *nb_buf;
1170 unsigned char *pos;
1171 int ret = 0;
1172
1173 nb_buf = &qel->tx.crypto.nb_buf;
1174 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1175 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1176 cf_len = len;
1177
1178 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1179
1180 while (len) {
1181 size_t to_copy, room;
1182
1183 pos = (*qcb)->data + (*qcb)->sz;
1184 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1185 to_copy = len > room ? room : len;
1186 if (to_copy) {
1187 memcpy(pos, data, to_copy);
1188 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1189 qel->tx.crypto.sz += to_copy;
1190 (*qcb)->sz += to_copy;
1191 len -= to_copy;
1192 data += to_copy;
1193 }
1194 else {
1195 struct quic_crypto_buf **tmp;
1196
1197 // FIXME: realloc!
1198 tmp = realloc(qel->tx.crypto.bufs,
1199 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1200 if (tmp) {
1201 qel->tx.crypto.bufs = tmp;
1202 qcb = &qel->tx.crypto.bufs[*nb_buf];
1203 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1204 if (!*qcb) {
1205 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1206 goto leave;
1207 }
1208
1209 (*qcb)->sz = 0;
1210 ++*nb_buf;
1211 }
1212 else {
1213 break;
1214 }
1215 }
1216 }
1217
1218 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1219 * have been buffered.
1220 */
1221 if (!len) {
1222 struct quic_frame *frm;
1223 struct quic_frame *found = NULL;
1224
1225 /* There is at most one CRYPTO frame in this packet number
1226 * space. Let's look for it.
1227 */
1228 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
1229 if (frm->type != QUIC_FT_CRYPTO)
1230 continue;
1231
1232 /* Found */
1233 found = frm;
1234 break;
1235 }
1236
1237 if (found) {
1238 found->crypto.len += cf_len;
1239 }
1240 else {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001241 frm = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001242 if (!frm) {
1243 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1244 goto leave;
1245 }
1246
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001247 frm->crypto.offset = cf_offset;
1248 frm->crypto.len = cf_len;
1249 frm->crypto.qel = qel;
1250 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
1251 }
1252 }
1253 ret = len == 0;
1254 leave:
1255 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1256 return ret;
1257}
1258
1259/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1260 * activity for <qc> will be interrupted.
1261 */
1262void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
1263{
1264 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1265 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
1266 goto leave;
1267
1268 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
1269 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
1270 qc->err.code = err.code;
1271 qc->err.app = err.app;
1272 leave:
1273 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1274}
1275
1276/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1277void quic_set_tls_alert(struct quic_conn *qc, int alert)
1278{
1279 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1280
1281 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1282 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
1283 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
1284 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1285 }
1286 quic_set_connection_close(qc, quic_err_tls(alert));
1287 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
1288 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1289
1290 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1291}
1292
1293/* Set the application for <qc> QUIC connection.
1294 * Return 1 if succeeded, 0 if not.
1295 */
1296int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1297{
1298 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1299 qc->app_ops = &h3_ops;
1300 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1301 qc->app_ops = &hq_interop_ops;
1302 else
1303 return 0;
1304
1305 return 1;
1306}
1307
1308/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1309 * wants to provide the QUIC layer with CRYPTO data.
1310 * Returns 1 if succeeded, 0 if not.
1311 */
1312int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1313 const uint8_t *data, size_t len)
1314{
1315 struct quic_conn *qc;
1316 enum quic_tls_enc_level tel;
1317 struct quic_enc_level *qel;
1318 int ret = 0;
1319
1320 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1321 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1322
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001323 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1324 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1325 goto out;
1326 }
1327
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001328 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1329 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
1330 goto out;
1331 }
1332
1333 tel = ssl_to_quic_enc_level(level);
1334 if (tel == -1) {
1335 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1336 goto leave;
1337 }
1338
1339 qel = &qc->els[tel];
1340 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1341 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1342 goto leave;
1343 }
1344
1345 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
1346 qc, &level, &len);
1347 out:
1348 ret = 1;
1349 leave:
1350 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1351 return ret;
1352}
1353
1354int ha_quic_flush_flight(SSL *ssl)
1355{
1356 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1357
1358 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1359 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
1360
1361 return 1;
1362}
1363
1364int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1365{
1366 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1367
1368 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1369
1370 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1371
1372 quic_set_tls_alert(qc, alert);
1373 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1374 return 1;
1375}
1376
1377/* QUIC TLS methods */
1378static SSL_QUIC_METHOD ha_quic_method = {
1379 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1380 .add_handshake_data = ha_quic_add_handshake_data,
1381 .flush_flight = ha_quic_flush_flight,
1382 .send_alert = ha_quic_send_alert,
1383};
1384
1385/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1386 * Returns an error count.
1387 */
1388int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1389{
1390 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1391 int cfgerr = 0;
1392
1393 long options =
1394 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1395 SSL_OP_SINGLE_ECDH_USE |
1396 SSL_OP_CIPHER_SERVER_PREFERENCE;
1397 SSL_CTX *ctx;
1398
1399 ctx = SSL_CTX_new(TLS_server_method());
1400 bind_conf->initial_ctx = ctx;
1401
1402 SSL_CTX_set_options(ctx, options);
1403 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1404 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1405 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1406
1407#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1408# if defined(HAVE_SSL_CLIENT_HELLO_CB)
1409# if defined(SSL_OP_NO_ANTI_REPLAY)
1410 if (bind_conf->ssl_conf.early_data) {
1411 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
1412 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
1413 }
1414# endif /* !SSL_OP_NO_ANTI_REPLAY */
1415 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1416 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1417# else /* ! HAVE_SSL_CLIENT_HELLO_CB */
1418 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1419# endif
1420 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1421#endif
1422 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1423
1424 return cfgerr;
1425}
1426
1427/* Decode an expected packet number from <truncated_on> its truncated value,
1428 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1429 * the number of bits used to encode this packet number (its length in bytes * 8).
1430 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1431 */
1432static uint64_t decode_packet_number(uint64_t largest_pn,
1433 uint32_t truncated_pn, unsigned int pn_nbits)
1434{
1435 uint64_t expected_pn = largest_pn + 1;
1436 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1437 uint64_t pn_hwin = pn_win / 2;
1438 uint64_t pn_mask = pn_win - 1;
1439 uint64_t candidate_pn;
1440
1441
1442 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1443 /* Note that <pn_win> > <pn_hwin>. */
1444 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1445 candidate_pn + pn_hwin <= expected_pn)
1446 return candidate_pn + pn_win;
1447
1448 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1449 return candidate_pn - pn_win;
1450
1451 return candidate_pn;
1452}
1453
1454/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1455 * cryptographic context.
1456 * <largest_pn> is the largest received packet number and <pn> the address of
1457 * the packet number field for this packet with <byte0> address of its first byte.
1458 * <end> points to one byte past the end of this packet.
1459 * Returns 1 if succeeded, 0 if not.
1460 */
1461static int qc_do_rm_hp(struct quic_conn *qc,
1462 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1463 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
1464{
1465 int ret, i, pnlen;
1466 uint64_t packet_number;
1467 uint32_t truncated_pn = 0;
1468 unsigned char mask[5] = {0};
1469 unsigned char *sample;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001470
1471 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1472
1473 ret = 0;
1474
1475 /* Check there is enough data in this packet. */
1476 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1477 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1478 goto leave;
1479 }
1480
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001481 sample = pn + QUIC_PACKET_PN_MAXLEN;
1482
1483 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1484 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
1485 goto leave;
1486 }
1487
1488 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1489 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1490 for (i = 0; i < pnlen; i++) {
1491 pn[i] ^= mask[i + 1];
1492 truncated_pn = (truncated_pn << 8) | pn[i];
1493 }
1494
1495 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1496 /* Store remaining information for this unprotected header */
1497 pkt->pn = packet_number;
1498 pkt->pnl = pnlen;
1499
1500 ret = 1;
1501 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001502 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
1503 return ret;
1504}
1505
1506/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1507 * address, with <payload_len> as payload length, <aad> as address of
1508 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1509 * context.
1510 * Returns 1 if succeeded, 0 if not.
1511 */
1512static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1513 unsigned char *aad, size_t aad_len, uint64_t pn,
1514 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
1515{
1516 int ret = 0;
1517 unsigned char iv[QUIC_TLS_IV_LEN];
1518 unsigned char *tx_iv = tls_ctx->tx.iv;
1519 size_t tx_iv_sz = tls_ctx->tx.ivlen;
1520 struct enc_debug_info edi;
1521
1522 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1523
1524 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1525 TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc);
1526 goto err;
1527 }
1528
1529 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1530 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1531 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
1532 goto err;
1533 }
1534
1535 ret = 1;
1536 leave:
1537 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1538 return ret;
1539
1540 err:
1541 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1542 goto leave;
1543}
1544
Frédéric Lécaille72027782023-02-22 16:20:09 +01001545/* Select the correct TLS cipher context to used to decipher <pkt> packet
1546 * attached to <qc> connection from <qel> encryption level.
1547 */
1548static inline struct quic_tls_ctx *qc_select_tls_ctx(struct quic_conn *qc,
1549 struct quic_enc_level *qel,
1550 struct quic_rx_packet *pkt)
1551{
1552 return pkt->type != QUIC_PACKET_TYPE_INITIAL ? &qel->tls_ctx :
1553 pkt->version == qc->negotiated_version ? &qc->negotiated_ictx : &qel->tls_ctx;
1554}
1555
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001556/* Decrypt <pkt> packet using encryption level <qel> for <qc> connection.
1557 * Decryption is done in place in packet buffer.
1558 *
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +05001559 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001560 */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001561static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel,
1562 struct quic_rx_packet *pkt)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001563{
1564 int ret, kp_changed;
1565 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaille72027782023-02-22 16:20:09 +01001566 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001567 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
1568 unsigned char *rx_iv = tls_ctx->rx.iv;
1569 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1570 unsigned char *rx_key = tls_ctx->rx.key;
1571
1572 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1573
1574 ret = 0;
1575 kp_changed = 0;
1576
1577 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1578 /* The two tested bits are not at the same position,
1579 * this is why they are first both inversed.
1580 */
1581 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1582 if (pkt->pn < tls_ctx->rx.pn) {
1583 /* The lowest packet number of a previous key phase
1584 * cannot be null if it really stores previous key phase
1585 * secrets.
1586 */
1587 // TODO: check if BUG_ON() more suitable
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001588 if (!qc->ku.prv_rx.pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001589 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1590 goto leave;
1591 }
1592
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001593 rx_ctx = qc->ku.prv_rx.ctx;
1594 rx_iv = qc->ku.prv_rx.iv;
1595 rx_key = qc->ku.prv_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001596 }
1597 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1598 /* Next key phase */
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +01001599 TRACE_PROTO("Key phase changed", QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001600 kp_changed = 1;
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001601 rx_ctx = qc->ku.nxt_rx.ctx;
1602 rx_iv = qc->ku.nxt_rx.iv;
1603 rx_key = qc->ku.nxt_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001604 }
1605 }
1606 }
1607
1608 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1609 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1610 goto leave;
1611 }
1612
1613 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1614 pkt->data, pkt->aad_len,
1615 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
1616 if (!ret) {
1617 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1618 goto leave;
1619 }
1620
1621 /* Update the keys only if the packet decryption succeeded. */
1622 if (kp_changed) {
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001623 quic_tls_rotate_keys(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001624 /* Toggle the Key Phase bit */
1625 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1626 /* Store the lowest packet number received for the current key phase */
1627 tls_ctx->rx.pn = pkt->pn;
1628 /* Prepare the next key update */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001629 if (!quic_tls_key_update(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001630 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1631 goto leave;
1632 }
1633 }
1634
1635 /* Update the packet length (required to parse the frames). */
1636 pkt->len -= QUIC_TLS_TAG_LEN;
1637 ret = 1;
1638 leave:
1639 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1640 return ret;
1641}
1642
1643
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001644/* Release <frm> frame and mark its copies as acknowledged */
1645void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
1646{
1647 uint64_t pn;
1648 struct quic_frame *origin, *f, *tmp;
1649
1650 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1651
1652 /* Identify this frame: a frame copy or one of its copies */
1653 origin = frm->origin ? frm->origin : frm;
1654 /* Ensure the source of the copies is flagged as acked, <frm> being
1655 * possibly a copy of <origin>
1656 */
1657 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1658 /* Mark all the copy of <origin> as acknowledged. We must
1659 * not release the packets (releasing the frames) at this time as
1660 * they are possibly also to be acknowledged alongside the
1661 * the current one.
1662 */
1663 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1664 if (f->pkt) {
1665 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1666 f->origin = NULL;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001667 LIST_DEL_INIT(&f->ref);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001668 pn = f->pkt->pn_node.key;
1669 TRACE_DEVEL("mark frame as acked from packet",
1670 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1671 }
1672 else {
1673 TRACE_DEVEL("freeing unsent frame",
1674 QUIC_EV_CONN_PRSAFRM, qc, f);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001675 LIST_DEL_INIT(&f->ref);
1676 qc_frm_free(&f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001677 }
1678 }
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001679 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001680 pn = frm->pkt->pn_node.key;
1681 quic_tx_packet_refdec(frm->pkt);
1682 TRACE_DEVEL("freeing frame from packet",
1683 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001684 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001685
1686 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1687}
1688
1689/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1690 * and all STREAM data are acknowledged. The MUX is responsible to have set
1691 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1692 *
1693 * TODO this should also be called on lost packet detection
1694 */
1695void qc_check_close_on_released_mux(struct quic_conn *qc)
1696{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001697 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1698
1699 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1700 /* Reuse errcode which should have been previously set by the MUX on release. */
1701 quic_set_connection_close(qc, qc->err);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02001702 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001703 }
1704
1705 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1706}
1707
1708/* Remove from <stream> the acknowledged frames.
1709 *
1710 * Returns 1 if at least one frame was removed else 0.
1711 */
1712static int quic_stream_try_to_consume(struct quic_conn *qc,
1713 struct qc_stream_desc *stream)
1714{
1715 int ret;
1716 struct eb64_node *frm_node;
1717
1718 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1719
1720 ret = 0;
1721 frm_node = eb64_first(&stream->acked_frms);
1722 while (frm_node) {
1723 struct quic_stream *strm;
1724 struct quic_frame *frm;
1725 size_t offset, len;
1726
1727 strm = eb64_entry(frm_node, struct quic_stream, offset);
1728 offset = strm->offset.key;
1729 len = strm->len;
1730
1731 if (offset > stream->ack_offset)
1732 break;
1733
1734 if (qc_stream_desc_ack(&stream, offset, len)) {
1735 /* cf. next comment : frame may be freed at this stage. */
1736 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1737 qc, stream ? strm : NULL, stream);
1738 ret = 1;
1739 }
1740
1741 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1742 * has been freed. with the stream frames tree. Nothing to do
1743 * anymore in here.
1744 */
1745 if (!stream) {
1746 qc_check_close_on_released_mux(qc);
1747 ret = 1;
1748 goto leave;
1749 }
1750
1751 frm_node = eb64_next(frm_node);
1752 eb64_delete(&strm->offset);
1753
1754 frm = container_of(strm, struct quic_frame, stream);
1755 qc_release_frm(qc, frm);
1756 }
1757
1758 leave:
1759 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
1760 return ret;
1761}
1762
1763/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1764static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1765 struct quic_frame *frm)
1766{
Frédéric Lécaille8f991942023-03-24 15:14:45 +01001767 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1768 TRACE_PROTO("RX ack TX frm", QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001769
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001770 switch (frm->type) {
1771 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1772 {
1773 struct quic_stream *strm_frm = &frm->stream;
1774 struct eb64_node *node = NULL;
1775 struct qc_stream_desc *stream = NULL;
1776 const size_t offset = strm_frm->offset.key;
1777 const size_t len = strm_frm->len;
1778
1779 /* do not use strm_frm->stream as the qc_stream_desc instance
1780 * might be freed at this stage. Use the id to do a proper
1781 * lookup.
1782 *
1783 * TODO if lookup operation impact on the perf is noticeable,
1784 * implement a refcount on qc_stream_desc instances.
1785 */
1786 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1787 if (!node) {
1788 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
1789 qc_release_frm(qc, frm);
1790 /* early return */
1791 goto leave;
1792 }
1793 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1794
1795 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
1796 if (offset <= stream->ack_offset) {
1797 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001798 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1799 qc, strm_frm, stream);
1800 }
1801
1802 if (!stream) {
1803 /* no need to continue if stream freed. */
1804 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
1805 qc_release_frm(qc, frm);
1806 qc_check_close_on_released_mux(qc);
1807 break;
1808 }
1809
1810 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1811 qc, strm_frm, stream);
1812 qc_release_frm(qc, frm);
1813 }
1814 else {
1815 eb64_insert(&stream->acked_frms, &strm_frm->offset);
1816 }
1817
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01001818 quic_stream_try_to_consume(qc, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001819 }
1820 break;
1821 default:
1822 qc_release_frm(qc, frm);
1823 }
1824
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001825 leave:
1826 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1827}
1828
1829/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1830 * deallocating them, and their TX frames.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001831 * May be NULL if <largest> node could not be found.
1832 */
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001833static inline void qc_ackrng_pkts(struct quic_conn *qc,
1834 struct eb_root *pkts,
1835 unsigned int *pkt_flags,
1836 struct list *newly_acked_pkts,
1837 struct eb64_node *largest_node,
1838 uint64_t largest, uint64_t smallest)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001839{
1840 struct eb64_node *node;
1841 struct quic_tx_packet *pkt;
1842
1843 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1844
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001845 node = eb64_lookup_ge(pkts, smallest);
1846 if (!node)
1847 goto leave;
1848
1849 largest_node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
1850 if (!largest_node)
1851 goto leave;
1852
1853 while (node && node->key <= largest_node->key) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001854 struct quic_frame *frm, *frmbak;
1855
1856 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
1857 *pkt_flags |= pkt->flags;
1858 LIST_INSERT(newly_acked_pkts, &pkt->list);
1859 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
1860 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1861 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaille814645f2022-11-18 18:15:28 +01001862 /* If there are others packet in the same datagram <pkt> is attached to,
1863 * detach the previous one and the next one from <pkt>.
1864 */
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01001865 quic_tx_packet_dgram_detach(pkt);
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001866 node = eb64_next(node);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001867 eb64_delete(&pkt->pn_node);
1868 }
1869
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001870 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001871 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001872}
1873
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001874/* Remove all frames from <pkt_frm_list> and reinsert them in the same order
1875 * they have been sent into <pktns_frm_list>. The loss counter of each frame is
1876 * incremented and checked if it does not exceed retransmission limit.
1877 *
1878 * Returns 1 on success, 0 if a frame loss limit is exceeded. A
1879 * CONNECTION_CLOSE is scheduled in this case.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001880 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001881static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1882 struct quic_tx_packet *pkt,
1883 struct list *pktns_frm_list)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001884{
1885 struct quic_frame *frm, *frmbak;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001886 struct list *pkt_frm_list = &pkt->frms;
1887 uint64_t pn = pkt->pn_node.key;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001888 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001889
1890 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1891
1892 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1893 /* First remove this frame from the packet it was attached to */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001894 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001895 quic_tx_packet_refdec(pkt);
1896 /* At this time, this frame is not freed but removed from its packet */
1897 frm->pkt = NULL;
1898 /* Remove any reference to this frame */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001899 qc_frm_unref(frm, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001900 switch (frm->type) {
1901 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1902 {
1903 struct quic_stream *strm_frm = &frm->stream;
1904 struct eb64_node *node = NULL;
1905 struct qc_stream_desc *stream_desc;
1906
1907 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1908 if (!node) {
1909 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1910 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1911 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001912 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001913 continue;
1914 }
1915
1916 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1917 /* Do not resend this frame if in the "already acked range" */
1918 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1919 TRACE_DEVEL("ignored frame in already acked range",
1920 QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001921 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001922 continue;
1923 }
1924 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01001925 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
1926
Frédéric Lécaillec425e032023-03-20 14:32:59 +01001927 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001928 TRACE_DEVEL("updated partially acked frame",
1929 QUIC_EV_CONN_PRSAFRM, qc, frm);
1930 }
1931 break;
1932 }
1933
1934 default:
1935 break;
1936 }
1937
1938 /* Do not resend probing packet with old data */
1939 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1940 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1941 qc, frm, &pn);
1942 if (frm->origin)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001943 LIST_DEL_INIT(&frm->ref);
1944 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001945 continue;
1946 }
1947
1948 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1949 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1950 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1951 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001952 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001953 }
1954 else {
Amaury Denoyelle24d5b722023-01-31 11:44:50 +01001955 if (++frm->loss_count >= global.tune.quic_max_frame_loss) {
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001956 TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc);
1957 quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR));
1958 close = 1;
1959 }
1960
Frédéric Lécaillebe795ce2023-03-08 18:23:13 +01001961 LIST_APPEND(pktns_frm_list, &frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001962 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
1963 }
1964 }
1965
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001966 end:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001967 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001968 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001969}
1970
1971/* Free <pkt> TX packet and its attached frames.
1972 * This is the responsibility of the caller to remove this packet of
1973 * any data structure it was possibly attached to.
1974 */
1975static inline void free_quic_tx_packet(struct quic_conn *qc,
1976 struct quic_tx_packet *pkt)
1977{
1978 struct quic_frame *frm, *frmbak;
1979
1980 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1981
1982 if (!pkt)
1983 goto leave;
1984
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001985 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1986 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001987 pool_free(pool_head_quic_tx_packet, pkt);
1988
1989 leave:
1990 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
1991}
1992
1993/* Free the TX packets of <pkts> list */
1994static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
1995{
1996 struct quic_tx_packet *pkt, *tmp;
1997
1998 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1999
2000 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2001 LIST_DELETE(&pkt->list);
2002 eb64_delete(&pkt->pn_node);
2003 free_quic_tx_packet(qc, pkt);
2004 }
2005
2006 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
2007}
2008
2009/* Remove already sent ranges of acknowledged packet numbers from
2010 * <pktns> packet number space tree below <largest_acked_pn> possibly
2011 * updating the range which contains <largest_acked_pn>.
2012 * Never fails.
2013 */
2014static void qc_treat_ack_of_ack(struct quic_conn *qc,
2015 struct quic_pktns *pktns,
2016 int64_t largest_acked_pn)
2017{
2018 struct eb64_node *ar, *next_ar;
2019 struct quic_arngs *arngs = &pktns->rx.arngs;
2020
2021 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2022
2023 ar = eb64_first(&arngs->root);
2024 while (ar) {
2025 struct quic_arng_node *ar_node;
2026
2027 next_ar = eb64_next(ar);
2028 ar_node = eb64_entry(ar, struct quic_arng_node, first);
2029
2030 if ((int64_t)ar_node->first.key > largest_acked_pn) {
2031 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
2032 break;
2033 }
2034
2035 if (largest_acked_pn < ar_node->last) {
2036 eb64_delete(ar);
2037 ar_node->first.key = largest_acked_pn + 1;
2038 eb64_insert(&arngs->root, ar);
2039 break;
2040 }
2041
2042 eb64_delete(ar);
2043 pool_free(pool_head_quic_arng, ar_node);
2044 arngs->sz--;
2045 ar = next_ar;
2046 }
2047
2048 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2049}
2050
2051/* Send a packet ack event nofication for each newly acked packet of
2052 * <newly_acked_pkts> list and free them.
2053 * Always succeeds.
2054 */
2055static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
2056 struct list *newly_acked_pkts)
2057{
2058 struct quic_tx_packet *pkt, *tmp;
2059 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
2060
2061 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2062
2063 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
2064 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2065 qc->path->prep_in_flight -= pkt->in_flight_len;
2066 qc->path->in_flight -= pkt->in_flight_len;
2067 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2068 qc->path->ifae_pkts--;
2069 /* If this packet contained an ACK frame, proceed to the
2070 * acknowledging of range of acks from the largest acknowledged
2071 * packet number which was sent in an ACK frame by this packet.
2072 */
2073 if (pkt->largest_acked_pn != -1)
2074 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
2075 ev.ack.acked = pkt->in_flight_len;
2076 ev.ack.time_sent = pkt->time_sent;
2077 quic_cc_event(&qc->path->cc, &ev);
2078 LIST_DELETE(&pkt->list);
2079 eb64_delete(&pkt->pn_node);
2080 quic_tx_packet_refdec(pkt);
2081 }
2082
2083 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2084
2085}
2086
2087/* Release all the frames attached to <pktns> packet number space */
2088static inline void qc_release_pktns_frms(struct quic_conn *qc,
2089 struct quic_pktns *pktns)
2090{
2091 struct quic_frame *frm, *frmbak;
2092
2093 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2094
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002095 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list)
2096 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002097
2098 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2099}
2100
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002101/* Handle <pkts> list of lost packets detected at <now_us> handling their TX
2102 * frames. Send a packet loss event to the congestion controller if in flight
2103 * packet have been lost. Also frees the packet in <pkts> list.
2104 *
2105 * Returns 1 on success else 0 if loss limit has been exceeded. A
2106 * CONNECTION_CLOSE was prepared to close the connection ASAP.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002107 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002108static inline int qc_release_lost_pkts(struct quic_conn *qc,
2109 struct quic_pktns *pktns,
2110 struct list *pkts,
2111 uint64_t now_us)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002112{
2113 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002114 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002115
2116 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2117
2118 if (LIST_ISEMPTY(pkts))
2119 goto leave;
2120
2121 oldest_lost = newest_lost = NULL;
2122 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2123 struct list tmp = LIST_HEAD_INIT(tmp);
2124
2125 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2126 qc->path->prep_in_flight -= pkt->in_flight_len;
2127 qc->path->in_flight -= pkt->in_flight_len;
2128 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2129 qc->path->ifae_pkts--;
2130 /* Treat the frames of this lost packet. */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002131 if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms))
2132 close = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002133 LIST_DELETE(&pkt->list);
2134 if (!oldest_lost) {
2135 oldest_lost = newest_lost = pkt;
2136 }
2137 else {
2138 if (newest_lost != oldest_lost)
2139 quic_tx_packet_refdec(newest_lost);
2140 newest_lost = pkt;
2141 }
2142 }
2143
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002144 if (!close) {
2145 if (newest_lost) {
2146 /* Sent a congestion event to the controller */
2147 struct quic_cc_event ev = { };
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002148
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002149 ev.type = QUIC_CC_EVT_LOSS;
2150 ev.loss.time_sent = newest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002151
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002152 quic_cc_event(&qc->path->cc, &ev);
2153 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002154
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002155 /* If an RTT have been already sampled, <rtt_min> has been set.
2156 * We must check if we are experiencing a persistent congestion.
2157 * If this is the case, the congestion controller must re-enter
2158 * slow start state.
2159 */
2160 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2161 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002162
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002163 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2164 now_ms, qc->max_ack_delay))
2165 qc->path->cc.algo->slow_start(&qc->path->cc);
2166 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002167 }
2168
Amaury Denoyelle3a72ba22022-11-14 11:41:34 +01002169 /* <oldest_lost> cannot be NULL at this stage because we have ensured
2170 * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a
2171 * possible overflow on a 0 byte region with O2 optimization.
2172 */
2173 ALREADY_CHECKED(oldest_lost);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002174 quic_tx_packet_refdec(oldest_lost);
2175 if (newest_lost != oldest_lost)
2176 quic_tx_packet_refdec(newest_lost);
2177
2178 leave:
2179 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002180 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002181}
2182
2183/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2184 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
2185 * if the largest acked packet was newly acked and if there was at least one newly
2186 * acked ack-eliciting packet.
2187 * Return 1, if succeeded, 0 if not.
2188 */
2189static inline int qc_parse_ack_frm(struct quic_conn *qc,
2190 struct quic_frame *frm,
2191 struct quic_enc_level *qel,
2192 unsigned int *rtt_sample,
2193 const unsigned char **pos, const unsigned char *end)
2194{
2195 struct quic_ack *ack = &frm->ack;
2196 uint64_t smallest, largest;
2197 struct eb_root *pkts;
2198 struct eb64_node *largest_node;
2199 unsigned int time_sent, pkt_flags;
2200 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2201 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2202 int ret = 0;
2203
2204 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2205
2206 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2207 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
2208 qc, NULL, &ack->largest_ack);
2209 goto err;
2210 }
2211
2212 if (ack->first_ack_range > ack->largest_ack) {
2213 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
2214 qc, NULL, &ack->first_ack_range);
2215 goto err;
2216 }
2217
2218 largest = ack->largest_ack;
2219 smallest = largest - ack->first_ack_range;
2220 pkts = &qel->pktns->tx.pkts;
2221 pkt_flags = 0;
2222 largest_node = NULL;
2223 time_sent = 0;
2224
2225 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
2226 largest_node = eb64_lookup(pkts, largest);
2227 if (!largest_node) {
2228 TRACE_DEVEL("Largest acked packet not found",
2229 QUIC_EV_CONN_PRSAFRM, qc);
2230 }
2231 else {
2232 time_sent = eb64_entry(largest_node,
2233 struct quic_tx_packet, pn_node)->time_sent;
2234 }
2235 }
2236
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002237 TRACE_PROTO("RX ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002238 qc, NULL, &largest, &smallest);
2239 do {
2240 uint64_t gap, ack_range;
2241
2242 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2243 largest_node, largest, smallest);
2244 if (!ack->ack_range_num--)
2245 break;
2246
2247 if (!quic_dec_int(&gap, pos, end)) {
2248 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
2249 goto err;
2250 }
2251
2252 if (smallest < gap + 2) {
2253 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
2254 qc, NULL, &gap, &smallest);
2255 goto err;
2256 }
2257
2258 largest = smallest - gap - 2;
2259 if (!quic_dec_int(&ack_range, pos, end)) {
2260 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
2261 goto err;
2262 }
2263
2264 if (largest < ack_range) {
2265 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
2266 qc, NULL, &largest, &ack_range);
2267 goto err;
2268 }
2269
2270 /* Do not use this node anymore. */
2271 largest_node = NULL;
2272 /* Next range */
2273 smallest = largest - ack_range;
2274
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002275 TRACE_PROTO("RX next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002276 qc, NULL, &largest, &smallest);
2277 } while (1);
2278
2279 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2280 *rtt_sample = tick_remain(time_sent, now_ms);
2281 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
2282 }
2283
2284 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2285 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
2286 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002287 if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms))
2288 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002289 }
2290 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2291 if (quic_peer_validated_addr(qc))
2292 qc->path->loss.pto_count = 0;
2293 qc_set_timer(qc);
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01002294 qc_notify_send(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002295 }
2296
2297 ret = 1;
2298 leave:
2299 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2300 return ret;
2301
2302 err:
2303 free_quic_tx_pkts(qc, &newly_acked_pkts);
2304 goto leave;
2305}
2306
2307/* This function gives the detail of the SSL error. It is used only
2308 * if the debug mode and the verbose mode are activated. It dump all
2309 * the SSL error until the stack was empty.
2310 */
2311static forceinline void qc_ssl_dump_errors(struct connection *conn)
2312{
2313 if (unlikely(global.mode & MODE_DEBUG)) {
2314 while (1) {
2315 const char *func = NULL;
2316 unsigned long ret;
2317
2318 ERR_peek_error_func(&func);
2319 ret = ERR_get_error();
2320 if (!ret)
2321 return;
2322
2323 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
2324 func, ERR_reason_error_string(ret));
2325 }
2326 }
2327}
2328
2329int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2330 const char **str, int *len);
2331
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002332/* Finalize <qc> QUIC connection:
2333 * - initialize the Initial QUIC TLS context for negotiated version,
2334 * - derive the secrets for this context,
2335 * - set them into the TLS stack,
2336 *
2337 * MUST be called after having received the remote transport parameters which
2338 * are parsed when the TLS callback for the ClientHello message is called upon
2339 * SSL_do_handshake() calls, not necessarily at the first time as this TLS
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002340 * message may be split between packets
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002341 * Return 1 if succeeded, 0 if not.
2342 */
2343static int qc_conn_finalize(struct quic_conn *qc, int server)
2344{
2345 int ret = 0;
2346
2347 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
2348
2349 if (qc->flags & QUIC_FL_CONN_FINALIZED)
2350 goto finalized;
2351
2352 if (qc->negotiated_version &&
2353 !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
2354 qc->odcid.data, qc->odcid.len, server))
2355 goto out;
2356
2357 /* This connection is functional (ready to send/receive) */
2358 qc->flags |= QUIC_FL_CONN_FINALIZED;
2359
2360 finalized:
2361 ret = 1;
2362 out:
2363 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
2364 return ret;
2365}
2366
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002367/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2368 * from <qel> encryption level with <ctx> as QUIC connection context.
2369 * Remaining parameter are there for debugging purposes.
2370 * Return 1 if succeeded, 0 if not.
2371 */
2372static inline int qc_provide_cdata(struct quic_enc_level *el,
2373 struct ssl_sock_ctx *ctx,
2374 const unsigned char *data, size_t len,
2375 struct quic_rx_packet *pkt,
2376 struct quic_rx_crypto_frm *cf)
2377{
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002378#ifdef DEBUG_STRICT
2379 enum ncb_ret ncb_ret;
2380#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002381 int ssl_err, state;
2382 struct quic_conn *qc;
2383 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002384 struct ncbuf *ncbuf = &el->cstream->rx.ncbuf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002385
2386 ssl_err = SSL_ERROR_NONE;
2387 qc = ctx->qc;
2388
2389 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2390
2391 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2392 TRACE_ERROR("SSL_provide_quic_data() error",
2393 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
2394 goto leave;
2395 }
2396
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002397 TRACE_PROTO("in order CRYPTO data",
2398 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
2399
2400 state = qc->state;
2401 if (state < QUIC_HS_ST_COMPLETE) {
2402 ssl_err = SSL_do_handshake(ctx->ssl);
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002403
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01002404 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
2405 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
2406 goto leave;
2407 }
2408
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002409 /* Finalize the connection as soon as possible if the peer transport parameters
2410 * have been received. This may be useful to send packets even if this
2411 * handshake fails.
2412 */
2413 if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) {
2414 TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state);
2415 goto leave;
2416 }
2417
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002418 if (ssl_err != 1) {
2419 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2420 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2421 TRACE_PROTO("SSL handshake in progress",
2422 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2423 goto out;
2424 }
2425
2426 /* TODO: Should close the connection asap */
2427 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2428 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2429 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2430 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2431 }
2432 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2433 qc_ssl_dump_errors(ctx->conn);
2434 ERR_clear_error();
2435 goto leave;
2436 }
2437
2438 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
2439
2440 /* Check the alpn could be negotiated */
2441 if (!qc->app_ops) {
2442 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2443 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2444 goto leave;
2445 }
2446
2447 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2448 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
2449 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2450 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2451 }
2452 /* I/O callback switch */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02002453 qc->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002454 if (qc_is_listener(ctx->qc)) {
2455 qc->state = QUIC_HS_ST_CONFIRMED;
2456 /* The connection is ready to be accepted. */
2457 quic_accept_push_qc(qc);
2458 }
2459 else {
2460 qc->state = QUIC_HS_ST_COMPLETE;
2461 }
2462
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02002463 /* Prepare the next key update */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002464 if (!quic_tls_key_update(qc)) {
2465 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc);
2466 goto leave;
2467 }
2468 } else {
2469 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2470 if (ssl_err != 1) {
2471 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2472 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2473 TRACE_PROTO("SSL post handshake in progress",
2474 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2475 goto out;
2476 }
2477
2478 TRACE_ERROR("SSL post handshake error",
2479 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2480 goto leave;
2481 }
2482
2483 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
2484 }
2485
2486 out:
2487 ret = 1;
2488 leave:
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002489 /* The CRYPTO data are consumed even in case of an error to release
2490 * the memory asap.
2491 */
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002492 if (!ncb_is_null(ncbuf)) {
2493#ifdef DEBUG_STRICT
2494 ncb_ret = ncb_advance(ncbuf, len);
2495 /* ncb_advance() must always succeed. This is guaranteed as
2496 * this is only done inside a data block. If false, this will
2497 * lead to handshake failure with quic_enc_level offset shifted
2498 * from buffer data.
2499 */
2500 BUG_ON(ncb_ret != NCB_RET_OK);
2501#else
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002502 ncb_advance(ncbuf, len);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002503#endif
2504 }
2505
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002506 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
2507 return ret;
2508}
2509
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002510/* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc>
2511 * connection. <fin> is true if FIN bit is set on frame type.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002512 *
2513 * Return 1 on success. On error, 0 is returned. In this case, the packet
2514 * containing the frame must not be acknowledged.
2515 */
2516static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2517 struct quic_stream *strm_frm,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002518 struct quic_conn *qc, char fin)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002519{
2520 int ret;
2521
2522 /* RFC9000 13.1. Packet Processing
2523 *
2524 * A packet MUST NOT be acknowledged until packet protection has been
2525 * successfully removed and all frames contained in the packet have
2526 * been processed. For STREAM frames, this means the data has been
2527 * enqueued in preparation to be received by the application protocol,
2528 * but it does not require that data be delivered and consumed.
2529 */
2530 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2531
2532 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002533 strm_frm->offset.key, fin, (char *)strm_frm->data);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002534
2535 /* frame rejected - packet must not be acknowledeged */
2536 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2537 return !ret;
2538}
2539
2540/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2541 * for <qc> QUIC connection.
2542 * This is a best effort function which never fails even if no memory could be
2543 * allocated to duplicate these frames.
2544 */
2545static void qc_dup_pkt_frms(struct quic_conn *qc,
2546 struct list *pkt_frm_list, struct list *out_frm_list)
2547{
2548 struct quic_frame *frm, *frmbak;
2549 struct list tmp = LIST_HEAD_INIT(tmp);
2550
2551 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2552
2553 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2554 struct quic_frame *dup_frm, *origin;
2555
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002556 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
2557 TRACE_DEVEL("already acknowledged frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2558 continue;
2559 }
2560
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002561 switch (frm->type) {
2562 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2563 {
2564 struct quic_stream *strm_frm = &frm->stream;
2565 struct eb64_node *node = NULL;
2566 struct qc_stream_desc *stream_desc;
2567
2568 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2569 if (!node) {
2570 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
2571 continue;
2572 }
2573
2574 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2575 /* Do not resend this frame if in the "already acked range" */
2576 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2577 TRACE_DEVEL("ignored frame in already acked range",
2578 QUIC_EV_CONN_PRSAFRM, qc, frm);
2579 continue;
2580 }
2581 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01002582 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
2583
Frédéric Lécaillec425e032023-03-20 14:32:59 +01002584 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002585 TRACE_DEVEL("updated partially acked frame",
2586 QUIC_EV_CONN_PRSAFRM, qc, frm);
2587 }
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01002588
2589 strm_frm->dup = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002590 break;
2591 }
2592
2593 default:
2594 break;
2595 }
2596
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002597 /* If <frm> is already a copy of another frame, we must take
2598 * its original frame as source for the copy.
2599 */
2600 origin = frm->origin ? frm->origin : frm;
2601 dup_frm = qc_frm_dup(origin);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002602 if (!dup_frm) {
2603 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2604 break;
2605 }
2606
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002607 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002608 if (origin->pkt) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002609 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
2610 qc, NULL, &origin->pkt->pn_node.key);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002611 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002612 else {
2613 /* <origin> is a frame which was sent from a packet detected as lost. */
2614 TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
2615 }
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002616
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002617 LIST_APPEND(&tmp, &dup_frm->list);
2618 }
2619
2620 LIST_SPLICE(out_frm_list, &tmp);
2621
2622 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2623}
2624
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002625/* Boolean function which return 1 if <pkt> TX packet is only made of
2626 * already acknowledged frame.
2627 */
2628static inline int qc_pkt_with_only_acked_frms(struct quic_tx_packet *pkt)
2629{
2630 struct quic_frame *frm;
2631
2632 list_for_each_entry(frm, &pkt->frms, list)
2633 if (!(frm->flags & QUIC_FL_TX_FRAME_ACKED))
2634 return 0;
2635
2636 return 1;
2637}
2638
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002639/* Prepare a fast retransmission from <qel> encryption level */
2640static void qc_prep_fast_retrans(struct quic_conn *qc,
2641 struct quic_enc_level *qel,
2642 struct list *frms1, struct list *frms2)
2643{
2644 struct eb_root *pkts = &qel->pktns->tx.pkts;
2645 struct list *frms = frms1;
2646 struct eb64_node *node;
2647 struct quic_tx_packet *pkt;
2648
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002649 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002650
2651 BUG_ON(frms1 == frms2);
2652
2653 pkt = NULL;
2654 node = eb64_first(pkts);
2655 start:
2656 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002657 struct quic_tx_packet *p;
2658
2659 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002660 node = eb64_next(node);
2661 /* Skip the empty and coalesced packets */
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002662 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002663 "--> pn=%llu (%d %d %d)", (ull)p->pn_node.key,
2664 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED),
2665 qc_pkt_with_only_acked_frms(p));
2666 if (!LIST_ISEMPTY(&p->frms) && !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002667 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002668 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002669 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002670 }
2671
2672 if (!pkt)
2673 goto leave;
2674
2675 /* When building a packet from another one, the field which may increase the
2676 * packet size is the packet number. And the maximum increase is 4 bytes.
2677 */
2678 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2679 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002680 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002681 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2682 goto leave;
2683 }
2684
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002685 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002686 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2687 if (frms == frms1 && frms2) {
2688 frms = frms2;
2689 goto start;
2690 }
2691 leave:
2692 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
2693}
2694
2695/* Prepare a fast retransmission during a handshake after a client
2696 * has resent Initial packets. According to the RFC a server may retransmit
2697 * Initial packets send them coalescing with others (Handshake here).
2698 * (Listener only function).
2699 */
2700static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2701 struct list *ifrms, struct list *hfrms)
2702{
2703 struct list itmp = LIST_HEAD_INIT(itmp);
2704 struct list htmp = LIST_HEAD_INIT(htmp);
2705
2706 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2707 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2708 struct quic_enc_level *qel = iqel;
2709 struct eb_root *pkts;
2710 struct eb64_node *node;
2711 struct quic_tx_packet *pkt;
2712 struct list *tmp = &itmp;
2713
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002714 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002715 start:
2716 pkt = NULL;
2717 pkts = &qel->pktns->tx.pkts;
2718 node = eb64_first(pkts);
2719 /* Skip the empty packet (they have already been retransmitted) */
2720 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002721 struct quic_tx_packet *p;
2722
2723 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002724 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002725 "--> pn=%llu (%d %d)", (ull)p->pn_node.key,
2726 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED));
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002727 if (!LIST_ISEMPTY(&p->frms) && !(p->flags & QUIC_FL_TX_PACKET_COALESCED) &&
2728 !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002729 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002730 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002731 }
2732
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002733 node = eb64_next(node);
2734 }
2735
2736 if (!pkt)
2737 goto end;
2738
2739 /* When building a packet from another one, the field which may increase the
2740 * packet size is the packet number. And the maximum increase is 4 bytes.
2741 */
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002742 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2743 size_t dglen = pkt->len + 4;
2744
2745 dglen += pkt->next ? pkt->next->len + 4 : 0;
2746 if (dglen > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002747 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002748 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2749 if (pkt->next)
2750 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt->next);
2751 goto end;
2752 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002753 }
2754
2755 qel->pktns->tx.pto_probe += 1;
2756
2757 /* No risk to loop here, #packet per datagram is bounded */
2758 requeue:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002759 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002760 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
2761 if (qel == iqel) {
2762 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2763 pkt = pkt->next;
2764 tmp = &htmp;
2765 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002766 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002767 goto requeue;
2768 }
2769 }
2770
2771 end:
2772 LIST_SPLICE(ifrms, &itmp);
2773 LIST_SPLICE(hfrms, &htmp);
2774
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002775 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002776}
2777
2778static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
2779{
2780 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2781
2782 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2783 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2784 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2785 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
2786 goto out;
2787
2788 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2789 }
2790
2791 out:
2792 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
2793}
2794
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002795/* Cancel a request on connection <qc> for stream id <id>. This is useful when
2796 * the client opens a new stream but the MUX has already been released. A
Amaury Denoyelle75463012023-02-20 10:31:27 +01002797 * STOP_SENDING + RESET_STREAM frames are prepared for emission.
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002798 *
2799 * TODO this function is closely related to H3. Its place should be in H3 layer
2800 * instead of quic-conn but this requires an architecture adjustment.
2801 *
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002802 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002803 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002804static int qc_h3_request_reject(struct quic_conn *qc, uint64_t id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002805{
2806 int ret = 0;
Amaury Denoyelle75463012023-02-20 10:31:27 +01002807 struct quic_frame *ss, *rs;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002808 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002809 const uint64_t app_error_code = H3_REQUEST_REJECTED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002810
2811 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2812
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002813 /* Do not emit rejection for unknown unidirectional stream as it is
2814 * forbidden to close some of them (H3 control stream and QPACK
2815 * encoder/decoder streams).
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002816 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002817 if (quic_stream_is_uni(id)) {
2818 ret = 1;
2819 goto out;
2820 }
2821
Amaury Denoyelle75463012023-02-20 10:31:27 +01002822 ss = qc_frm_alloc(QUIC_FT_STOP_SENDING);
2823 if (!ss) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002824 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2825 goto out;
2826 }
2827
Amaury Denoyelle75463012023-02-20 10:31:27 +01002828 ss->stop_sending.id = id;
2829 ss->stop_sending.app_error_code = app_error_code;
2830
2831 rs = qc_frm_alloc(QUIC_FT_RESET_STREAM);
2832 if (!rs) {
2833 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2834 qc_frm_free(&ss);
2835 goto out;
2836 }
2837
2838 rs->reset_stream.id = id;
2839 rs->reset_stream.app_error_code = app_error_code;
2840 rs->reset_stream.final_size = 0;
2841
2842 LIST_APPEND(&qel->pktns->tx.frms, &ss->list);
2843 LIST_APPEND(&qel->pktns->tx.frms, &rs->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002844 ret = 1;
2845 out:
2846 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2847 return ret;
2848}
2849
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002850/* Release the underlying memory use by <ncbuf> non-contiguous buffer */
2851static void quic_free_ncbuf(struct ncbuf *ncbuf)
2852{
2853 struct buffer buf;
2854
2855 if (ncb_is_null(ncbuf))
2856 return;
2857
2858 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
2859 b_free(&buf);
2860 offer_buffers(NULL, 1);
2861
2862 *ncbuf = NCBUF_NULL;
2863}
2864
2865/* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */
2866static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf)
2867{
2868 struct buffer buf = BUF_NULL;
2869
2870 if (!ncb_is_null(ncbuf))
2871 return ncbuf;
2872
2873 b_alloc(&buf);
2874 BUG_ON(b_is_null(&buf));
2875
2876 *ncbuf = ncb_make(buf.area, buf.size, 0);
2877 ncb_init(ncbuf, 0);
2878
2879 return ncbuf;
2880}
2881
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002882/* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn.
2883 * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the
2884 * speed up handshake completion may be run after having received duplicated
2885 * CRYPTO data.
2886 */
2887static int qc_handle_crypto_frm(struct quic_conn *qc,
2888 struct quic_crypto *frm, struct quic_rx_packet *pkt,
2889 struct quic_enc_level *qel, int *fast_retrans)
2890{
2891 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002892 enum ncb_ret ncb_ret;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002893 /* XXX TO DO: <cfdebug> is used only for the traces. */
2894 struct quic_rx_crypto_frm cfdebug = {
2895 .offset_node.key = frm->offset,
2896 .len = frm->len,
2897 };
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002898 struct quic_cstream *cstream = qel->cstream;
2899 struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002900
2901 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2902 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2903 TRACE_PROTO("CRYPTO data discarded",
2904 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2905 goto done;
2906 }
2907
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002908 if (unlikely(frm->offset < cstream->rx.offset)) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002909 size_t diff;
2910
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002911 if (frm->offset + frm->len <= cstream->rx.offset) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002912 /* Nothing to do */
2913 TRACE_PROTO("Already received CRYPTO data",
2914 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2915 if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
2916 !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP))
2917 *fast_retrans = 1;
2918 goto done;
2919 }
2920
2921 TRACE_PROTO("Partially already received CRYPTO data",
2922 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2923
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002924 diff = cstream->rx.offset - frm->offset;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002925 frm->len -= diff;
2926 frm->data += diff;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002927 frm->offset = cstream->rx.offset;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002928 }
2929
Amaury Denoyelleff95f2d2022-11-18 14:50:06 +01002930 if (frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002931 if (!qc_provide_cdata(qel, qc->xprt_ctx, frm->data, frm->len,
2932 pkt, &cfdebug)) {
2933 // trace already emitted by function above
2934 goto leave;
2935 }
2936
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002937 cstream->rx.offset += frm->len;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002938 TRACE_DEVEL("increment crypto level offset", QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002939 goto done;
2940 }
2941
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002942 if (!quic_get_ncbuf(ncbuf) ||
2943 ncb_is_null(ncbuf)) {
2944 TRACE_ERROR("CRYPTO ncbuf allocation failed", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002945 goto leave;
2946 }
2947
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002948 /* frm->offset > cstream-trx.offset */
2949 ncb_ret = ncb_add(ncbuf, frm->offset - cstream->rx.offset,
2950 (const char *)frm->data, frm->len, NCB_ADD_COMPARE);
2951 if (ncb_ret != NCB_RET_OK) {
2952 if (ncb_ret == NCB_RET_DATA_REJ) {
2953 TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc);
2954 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
2955 }
2956 else if (ncb_ret == NCB_RET_GAP_SIZE) {
2957 TRACE_ERROR("cannot bufferize frame due to gap size limit",
2958 QUIC_EV_CONN_PRSHPKT, qc);
2959 }
2960 goto leave;
2961 }
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002962
2963 done:
2964 ret = 1;
2965 leave:
2966 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2967 return ret;
2968}
2969
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01002970/* Allocate a new connection ID for <qc> connection and build a NEW_CONNECTION_ID
2971 * frame to be sent.
2972 * Return 1 if succeeded, 0 if not.
2973 */
2974static int qc_build_new_connection_id_frm(struct quic_conn *qc,
2975 struct quic_connection_id *cid)
2976{
2977 int ret = 0;
2978 struct quic_frame *frm;
2979 struct quic_enc_level *qel;
2980
2981 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2982
2983 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2984 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
2985 if (!frm) {
2986 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
2987 goto leave;
2988 }
2989
2990 quic_connection_id_to_frm_cpy(frm, cid);
2991 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
2992 ret = 1;
2993 leave:
2994 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2995 return ret;
2996}
2997
2998
2999/* Handle RETIRE_CONNECTION_ID frame from <frm> frame.
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003000 * Return 1 if succeeded, 0 if not. If succeeded, also set <cid_to_retire>
3001 * to the CID to be retired if not already retired.
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003002 */
3003static int qc_handle_retire_connection_id_frm(struct quic_conn *qc,
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003004 struct quic_frame *frm,
3005 struct quic_cid *dcid,
3006 struct quic_connection_id **cid_to_retire)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003007{
3008 int ret = 0;
3009 struct quic_retire_connection_id *rcid = &frm->retire_connection_id;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003010 struct eb64_node *node;
3011 struct quic_connection_id *cid;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003012
3013 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3014
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003015 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3016 * Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number greater
3017 * than any previously sent to the peer MUST be treated as a connection error
3018 * of type PROTOCOL_VIOLATION.
3019 */
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003020 if (rcid->seq_num >= qc->next_cid_seq_num) {
3021 TRACE_PROTO("CID seq. number too big", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003022 goto protocol_violation;
3023 }
3024
3025 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3026 * The sequence number specified in a RETIRE_CONNECTION_ID frame MUST NOT refer to
3027 * the Destination Connection ID field of the packet in which the frame is contained.
3028 * The peer MAY treat this as a connection error of type PROTOCOL_VIOLATION.
3029 */
3030 node = eb64_lookup(&qc->cids, rcid->seq_num);
3031 if (!node) {
3032 TRACE_PROTO("CID already retired", QUIC_EV_CONN_PSTRM, qc, frm);
3033 goto out;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003034 }
3035
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003036 cid = eb64_entry(node, struct quic_connection_id, seq_num);
3037 /* Note that the length of <dcid> has already been checked. It must match the
3038 * length of the CIDs which have been provided to the peer.
3039 */
3040 if (!memcmp(dcid->data, cid->cid.data, QUIC_HAP_CID_LEN)) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003041 TRACE_PROTO("cannot retire the current CID", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003042 goto protocol_violation;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003043 }
3044
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003045 *cid_to_retire = cid;
3046 out:
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003047 ret = 1;
3048 leave:
3049 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3050 return ret;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003051 protocol_violation:
3052 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
3053 goto leave;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003054}
3055
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003056/* Remove a <qc> quic-conn from its ha_thread_ctx list. If <closing> is true,
3057 * it will immediately be reinserted in the ha_thread_ctx quic_conns_clo list.
3058 */
3059static void qc_detach_th_ctx_list(struct quic_conn *qc, int closing)
3060{
3061 struct bref *bref, *back;
3062
3063 /* Detach CLI context watchers currently dumping this connection.
3064 * Reattach them to the next quic_conn instance.
3065 */
3066 list_for_each_entry_safe(bref, back, &qc->back_refs, users) {
3067 /* Remove watcher from this quic_conn instance. */
3068 LIST_DEL_INIT(&bref->users);
3069
3070 /* Attach it to next instance unless it was the last list element. */
3071 if (qc->el_th_ctx.n != &th_ctx->quic_conns &&
3072 qc->el_th_ctx.n != &th_ctx->quic_conns_clo) {
3073 struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx,
3074 struct quic_conn *,
3075 el_th_ctx);
3076 LIST_APPEND(&next->back_refs, &bref->users);
3077 }
3078 bref->ref = qc->el_th_ctx.n;
3079 __ha_barrier_store();
3080 }
3081
3082 /* Remove quic_conn from global ha_thread_ctx list. */
3083 LIST_DEL_INIT(&qc->el_th_ctx);
3084
3085 if (closing)
3086 LIST_APPEND(&th_ctx->quic_conns_clo, &qc->el_th_ctx);
3087}
3088
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003089/* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel>
3090 * as encryption level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003091 * Returns 1 if succeeded, 0 if failed.
3092 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003093static int qc_parse_pkt_frms(struct quic_conn *qc, struct quic_rx_packet *pkt,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003094 struct quic_enc_level *qel)
3095{
3096 struct quic_frame frm;
3097 const unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003098 int fast_retrans = 0, ret = 0;
3099
3100 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3101 /* Skip the AAD */
3102 pos = pkt->data + pkt->aad_len;
3103 end = pkt->data + pkt->len;
3104
3105 while (pos < end) {
3106 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
3107 // trace already emitted by function above
3108 goto leave;
3109 }
3110
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003111 switch (frm.type) {
3112 case QUIC_FT_PADDING:
3113 break;
3114 case QUIC_FT_PING:
3115 break;
3116 case QUIC_FT_ACK:
3117 {
3118 unsigned int rtt_sample;
3119
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003120 rtt_sample = UINT_MAX;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003121 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
3122 // trace already emitted by function above
3123 goto leave;
3124 }
3125
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003126 if (rtt_sample != UINT_MAX) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003127 unsigned int ack_delay;
3128
3129 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
3130 qc->state >= QUIC_HS_ST_CONFIRMED ?
3131 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
3132 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
3133 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
3134 }
3135 break;
3136 }
3137 case QUIC_FT_RESET_STREAM:
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01003138 if (qc->mux_state == QC_MUX_READY) {
3139 struct quic_reset_stream *rs = &frm.reset_stream;
3140 qcc_recv_reset_stream(qc->qcc, rs->id, rs->app_error_code, rs->final_size);
3141 }
3142 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003143 case QUIC_FT_STOP_SENDING:
3144 {
3145 struct quic_stop_sending *stop_sending = &frm.stop_sending;
3146 if (qc->mux_state == QC_MUX_READY) {
3147 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
3148 stop_sending->app_error_code)) {
3149 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
3150 goto leave;
3151 }
3152 }
3153 break;
3154 }
3155 case QUIC_FT_CRYPTO:
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003156 if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003157 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003158 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003159 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
3160 {
3161 struct quic_stream *stream = &frm.stream;
3162 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Amaury Denoyelle2216b082023-02-02 14:59:36 +01003163 const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003164
3165 /* The upper layer may not be allocated. */
3166 if (qc->mux_state != QC_MUX_READY) {
3167 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
3168 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003169 }
3170 else {
3171 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle38836b62023-02-07 14:24:54 +01003172 if (qc->app_ops == &h3_ops) {
Amaury Denoyelle156a89a2023-02-20 10:32:16 +01003173 if (!qc_h3_request_reject(qc, stream->id)) {
3174 TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc);
3175 /* This packet will not be acknowledged */
3176 goto leave;
3177 }
3178 }
3179 else {
3180 /* This packet will not be acknowledged */
3181 goto leave;
Frédéric Lécailled18025e2023-01-20 15:33:50 +01003182 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003183 }
Amaury Denoyelle315a4f62023-03-06 09:10:53 +01003184
3185 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003186 }
3187
Amaury Denoyelle2216b082023-02-02 14:59:36 +01003188 if (!qc_handle_strm_frm(pkt, stream, qc, fin)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003189 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
3190 goto leave;
3191 }
3192
3193 break;
3194 }
3195 case QUIC_FT_MAX_DATA:
3196 if (qc->mux_state == QC_MUX_READY) {
3197 struct quic_max_data *data = &frm.max_data;
3198 qcc_recv_max_data(qc->qcc, data->max_data);
3199 }
3200 break;
3201 case QUIC_FT_MAX_STREAM_DATA:
3202 if (qc->mux_state == QC_MUX_READY) {
3203 struct quic_max_stream_data *data = &frm.max_stream_data;
3204 if (qcc_recv_max_stream_data(qc->qcc, data->id,
3205 data->max_stream_data)) {
3206 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
3207 goto leave;
3208 }
3209 }
3210 break;
3211 case QUIC_FT_MAX_STREAMS_BIDI:
3212 case QUIC_FT_MAX_STREAMS_UNI:
3213 break;
3214 case QUIC_FT_DATA_BLOCKED:
3215 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
3216 break;
3217 case QUIC_FT_STREAM_DATA_BLOCKED:
3218 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
3219 break;
3220 case QUIC_FT_STREAMS_BLOCKED_BIDI:
3221 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
3222 break;
3223 case QUIC_FT_STREAMS_BLOCKED_UNI:
3224 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
3225 break;
3226 case QUIC_FT_NEW_CONNECTION_ID:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003227 /* XXX TO DO XXX */
3228 break;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003229 case QUIC_FT_RETIRE_CONNECTION_ID:
3230 {
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003231 struct quic_connection_id *cid = NULL;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003232
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003233 if (!qc_handle_retire_connection_id_frm(qc, &frm, &pkt->dcid, &cid))
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003234 goto leave;
3235
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003236 if (!cid)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003237 break;
3238
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003239 ebmb_delete(&cid->node);
3240 eb64_delete(&cid->seq_num);
3241 pool_free(pool_head_quic_connection_id, cid);
3242 TRACE_PROTO("CID retired", QUIC_EV_CONN_PSTRM, qc);
3243
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003244 cid = new_quic_cid(&qc->cids, qc, NULL, NULL);
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003245 if (!cid) {
3246 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
3247 }
3248 else {
3249 /* insert the allocated CID in the receiver datagram handler tree */
3250 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
3251 qc_build_new_connection_id_frm(qc, cid);
3252 }
3253 break;
3254 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003255 case QUIC_FT_CONNECTION_CLOSE:
3256 case QUIC_FT_CONNECTION_CLOSE_APP:
3257 /* Increment the error counters */
3258 qc_cc_err_count_inc(qc, &frm);
3259 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
3260 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
3261 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
3262 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
3263 }
3264 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
3265 /* RFC 9000 10.2. Immediate Close:
3266 * The closing and draining connection states exist to ensure
3267 * that connections close cleanly and that delayed or reordered
3268 * packets are properly discarded. These states SHOULD persist
3269 * for at least three times the current PTO interval...
3270 *
3271 * Rearm the idle timeout only one time when entering draining
3272 * state.
3273 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003274 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003275 qc_detach_th_ctx_list(qc, 1);
Frédéric Lécailled7215712023-03-24 18:13:37 +01003276 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003277 qc_notify_close(qc);
3278 }
3279 break;
3280 case QUIC_FT_HANDSHAKE_DONE:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003281 if (qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003282 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
3283 QUIC_EV_CONN_PRSHPKT, qc);
3284 goto leave;
3285 }
3286
3287 qc->state = QUIC_HS_ST_CONFIRMED;
3288 break;
3289 default:
3290 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
3291 goto leave;
3292 }
3293 }
3294
3295 /* Flag this packet number space as having received a packet. */
3296 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
3297
3298 if (fast_retrans) {
3299 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3300 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3301
3302 TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc);
3303 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
3304 qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP;
3305 }
3306
3307 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
3308 * has successfully parse a Handshake packet. The Initial encryption must also
3309 * be discarded.
3310 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003311 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003312 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
3313 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
3314 QUIC_FL_TLS_SECRETS_DCD)) {
3315 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3316 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
3317 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003318 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003319 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3320 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3321 }
3322 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
3323 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
3324 }
3325 }
3326
3327 ret = 1;
3328 leave:
3329 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3330 return ret;
3331}
3332
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003333
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003334/* Allocate Tx buffer from <qc> quic-conn if needed.
3335 *
3336 * Returns allocated buffer or NULL on error.
3337 */
3338static struct buffer *qc_txb_alloc(struct quic_conn *qc)
3339{
3340 struct buffer *buf = &qc->tx.buf;
3341 if (!b_alloc(buf))
3342 return NULL;
3343
3344 return buf;
3345}
3346
3347/* Free Tx buffer from <qc> if it is empty. */
3348static void qc_txb_release(struct quic_conn *qc)
3349{
3350 struct buffer *buf = &qc->tx.buf;
3351
3352 /* For the moment sending function is responsible to purge the buffer
3353 * entirely. It may change in the future but this requires to be able
3354 * to reuse old data.
Frédéric Lécaillebbf86be2023-02-20 09:28:58 +01003355 * For the momemt we do not care to leave data in the buffer for
3356 * a connection which is supposed to be killed asap.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003357 */
3358 BUG_ON_HOT(buf && b_data(buf));
3359
3360 if (!b_data(buf)) {
3361 b_free(buf);
3362 offer_buffers(NULL, 1);
3363 }
3364}
3365
3366/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
3367 * must contains the address of the first packet stored in the payload.
3368 *
3369 * Caller is responsible that there is enough space in the buffer.
3370 */
3371static void qc_txb_store(struct buffer *buf, uint16_t length,
3372 struct quic_tx_packet *first_pkt)
3373{
3374 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
3375 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
3376
3377 write_u16(b_tail(buf), length);
3378 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
3379 b_add(buf, hdlen + length);
3380}
3381
3382/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
3383 * with <frms> as ack-eliciting frame list to send, 0 if not.
3384 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
3385 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
3386 * <force_ack> may be set to 1 if you want to force an ack.
3387 */
3388static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
3389 struct quic_enc_level *qel, int cc, int probe, int force_ack)
3390{
Frédéric Lécailled7215712023-03-24 18:13:37 +01003391 unsigned int must_ack = force_ack || (qc->flags & QUIC_FL_CONN_ACK_TIMER_FIRED);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003392
3393 /* Do not build any more packet if the TX secrets are not available or
3394 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
3395 * and if there is no more packets to send upon PTO expiration
3396 * and if there is no more ack-eliciting frames to send or in flight
3397 * congestion control limit is reached for prepared data
3398 */
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02003399 if (!quic_tls_has_tx_sec(qel) ||
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003400 (!cc && !probe && !must_ack &&
3401 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
3402 return 0;
3403 }
3404
3405 return 1;
3406}
3407
3408/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3409 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
3410 *
3411 * Each datagram is prepended by a two fields header : the datagram length and
3412 * the address of the packet contained in the datagram.
3413 *
3414 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3415 * -1 if something wrong happened.
3416 */
3417static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
3418 struct list *frms)
3419{
3420 int ret = -1;
3421 struct quic_enc_level *qel;
3422 unsigned char *end, *pos;
3423 struct quic_tx_packet *pkt;
3424 size_t total;
3425 /* Each datagram is prepended with its length followed by the address
3426 * of the first packet in the datagram.
3427 */
3428 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
3429
3430 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3431
3432 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3433 total = 0;
3434 pos = (unsigned char *)b_tail(buf);
3435 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
3436 int err, probe, cc;
3437
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003438 TRACE_PROTO("TX prep app pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003439 probe = 0;
3440 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3441 /* We do not probe if an immediate close was asked */
3442 if (!cc)
3443 probe = qel->pktns->tx.pto_probe;
3444
3445 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
3446 break;
3447
3448 /* Leave room for the datagram header */
3449 pos += dg_headlen;
3450 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3451 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3452 }
3453 else {
3454 end = pos + qc->path->mtu;
3455 }
3456
3457 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
3458 QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err);
3459 switch (err) {
3460 case -2:
3461 // trace already emitted by function above
3462 goto leave;
3463 case -1:
3464 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
3465 * MTU, we are here because of the congestion control window. There is
3466 * no need to try to reuse this buffer.
3467 */
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003468 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003469 goto out;
3470 default:
3471 break;
3472 }
3473
3474 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
3475 BUG_ON(!pkt);
3476
3477 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3478 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3479
3480 total += pkt->len;
3481
3482 /* Write datagram header. */
3483 qc_txb_store(buf, pkt->len, pkt);
3484 }
3485
3486 out:
3487 ret = total;
3488 leave:
3489 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3490 return ret;
3491}
3492
3493/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3494 * <frms>. Several packets can be regrouped in a single datagram. The result is
3495 * written into <buf>.
3496 *
3497 * Each datagram is prepended by a two fields header : the datagram length and
3498 * the address of first packet in the datagram.
3499 *
3500 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3501 * -1 if something wrong happened.
3502 */
3503static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
3504 enum quic_tls_enc_level tel, struct list *tel_frms,
3505 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3506{
3507 struct quic_enc_level *qel;
3508 unsigned char *end, *pos;
3509 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3510 /* length of datagrams */
3511 uint16_t dglen;
3512 size_t total;
3513 int ret = -1, padding;
3514 /* Each datagram is prepended with its length followed by the address
3515 * of the first packet in the datagram.
3516 */
3517 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
3518 struct list *frms;
3519
3520 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3521
3522 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05003523 * caller must ensure that buf is reset.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003524 */
3525 BUG_ON_HOT(buf->head || buf->data);
3526
3527 total = 0;
3528 qel = &qc->els[tel];
3529 frms = tel_frms;
3530 dglen = 0;
3531 padding = 0;
3532 pos = (unsigned char *)b_head(buf);
3533 first_pkt = prv_pkt = NULL;
3534 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
3535 int err, probe, cc;
3536 enum quic_pkt_type pkt_type;
3537 struct quic_tls_ctx *tls_ctx;
3538 const struct quic_version *ver;
3539 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3540 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3541 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3542
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003543 TRACE_PROTO("TX prep pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003544 probe = 0;
3545 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3546 /* We do not probe if an immediate close was asked */
3547 if (!cc)
3548 probe = qel->pktns->tx.pto_probe;
3549
3550 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
3551 if (prv_pkt)
3552 qc_txb_store(buf, dglen, first_pkt);
3553 /* Let's select the next encryption level */
3554 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3555 tel = next_tel;
3556 frms = next_tel_frms;
3557 qel = &qc->els[tel];
3558 /* Build a new datagram */
3559 prv_pkt = NULL;
3560 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
3561 continue;
3562 }
3563 break;
3564 }
3565
3566 pkt_type = quic_tls_level_pkt_type(tel);
3567 if (!prv_pkt) {
3568 /* Leave room for the datagram header */
3569 pos += dg_headlen;
3570 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3571 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3572 }
3573 else {
3574 end = pos + qc->path->mtu;
3575 }
3576 }
3577
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003578 /* RFC 9000 14.1 Initial datagram size
3579 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3580 * Initial packets to at least the smallest allowed maximum datagram size of
3581 * 1200 bytes.
3582 *
3583 * Ensure that no ack-eliciting packets are sent into too small datagrams
3584 */
3585 if (pkt_type == QUIC_PACKET_TYPE_INITIAL && !LIST_ISEMPTY(tel_frms)) {
3586 if (end - pos < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01003587 TRACE_PROTO("No more enough room to build an Initial packet",
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003588 QUIC_EV_CONN_PHPKTS, qc);
3589 goto out;
3590 }
3591
3592 /* Pad this Initial packet if there is no ack-eliciting frames to send from
3593 * the next packet number space.
3594 */
Frédéric Lécailleec937212023-03-03 17:34:41 +01003595 if (!next_tel_frms || LIST_ISEMPTY(next_tel_frms))
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003596 padding = 1;
3597 }
3598
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003599 if (qc->negotiated_version) {
3600 ver = qc->negotiated_version;
3601 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3602 tls_ctx = &qc->negotiated_ictx;
3603 else
3604 tls_ctx = &qel->tls_ctx;
3605 }
3606 else {
3607 ver = qc->original_version;
3608 tls_ctx = &qel->tls_ctx;
3609 }
3610
3611 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
3612 qc, ver, dglen, pkt_type,
3613 force_ack, padding, probe, cc, &err);
3614 switch (err) {
3615 case -2:
3616 // trace already emitted by function above
3617 goto leave;
3618 case -1:
3619 /* If there was already a correct packet present, set the
3620 * current datagram as prepared into <cbuf>.
3621 */
3622 if (prv_pkt)
3623 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003624 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003625 goto out;
3626 default:
3627 break;
3628 }
3629
3630 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
3631 BUG_ON(!cur_pkt);
3632
3633 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3634 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3635
3636 total += cur_pkt->len;
3637 /* keep trace of the first packet in the datagram */
3638 if (!first_pkt)
3639 first_pkt = cur_pkt;
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01003640 /* Attach the current one to the previous one and vice versa */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003641 if (prv_pkt) {
3642 prv_pkt->next = cur_pkt;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01003643 cur_pkt->prev = prv_pkt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003644 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3645 }
3646 /* Let's say we have to build a new dgram */
3647 prv_pkt = NULL;
3648 dglen += cur_pkt->len;
3649 /* Client: discard the Initial encryption keys as soon as
3650 * a handshake packet could be built.
3651 */
3652 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
3653 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3654 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3655 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3656 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3657 qc_set_timer(qc);
3658 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3659 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3660 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
3661 }
3662 /* If the data for the current encryption level have all been sent,
3663 * select the next level.
3664 */
3665 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
3666 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) {
3667 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3668 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3669 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3670 tel = next_tel;
3671 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3672 frms = &qc->els[tel].pktns->tx.frms;
3673 else
3674 frms = next_tel_frms;
3675 qel = &qc->els[tel];
3676 if (!LIST_ISEMPTY(frms)) {
3677 /* If there is data for the next level, do not
3678 * consume a datagram.
3679 */
3680 prv_pkt = cur_pkt;
3681 }
3682 }
3683
3684 /* If we have to build a new datagram, set the current datagram as
3685 * prepared into <cbuf>.
3686 */
3687 if (!prv_pkt) {
3688 qc_txb_store(buf, dglen, first_pkt);
3689 first_pkt = NULL;
3690 dglen = 0;
3691 padding = 0;
3692 }
3693 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
3694 (!qc_is_listener(qc) ||
3695 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3696 padding = 1;
3697 }
3698 }
3699
3700 out:
3701 ret = total;
3702 leave:
3703 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3704 return ret;
3705}
3706
3707/* Send datagrams stored in <buf>.
3708 *
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003709 * This function returns 1 for success. On error, there is several behavior
3710 * depending on underlying sendto() error :
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003711 * - for an unrecoverable error, 0 is returned and connection is killed.
3712 * - a transient error is handled differently if connection has its owned
3713 * socket. If this is the case, 0 is returned and socket is subscribed on the
3714 * poller. The other case is assimilated to a success case with 1 returned.
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003715 * Remaining data are purged from the buffer and will eventually be detected
3716 * as lost which gives the opportunity to retry sending.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003717 */
3718int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
3719{
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003720 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003721 struct quic_conn *qc;
3722 char skip_sendto = 0;
3723
3724 qc = ctx->qc;
3725 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
3726 while (b_contig_data(buf, 0)) {
3727 unsigned char *pos;
3728 struct buffer tmpbuf = { };
3729 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3730 uint16_t dglen;
3731 size_t headlen = sizeof dglen + sizeof first_pkt;
3732 unsigned int time_sent;
3733
3734 pos = (unsigned char *)b_head(buf);
3735 dglen = read_u16(pos);
3736 BUG_ON_HOT(!dglen); /* this should not happen */
3737
3738 pos += sizeof dglen;
3739 first_pkt = read_ptr(pos);
3740 pos += sizeof first_pkt;
3741 tmpbuf.area = (char *)pos;
3742 tmpbuf.size = tmpbuf.data = dglen;
3743
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003744 TRACE_PROTO("TX dgram", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003745 /* If sendto is on error just skip the call to it for the rest
3746 * of the loop but continue to purge the buffer. Data will be
3747 * transmitted when QUIC packets are detected as lost on our
3748 * side.
3749 *
3750 * TODO use fd-monitoring to detect when send operation can be
3751 * retry. This should improve the bandwidth without relying on
3752 * retransmission timer. However, it requires a major rework on
3753 * quic-conn fd management.
3754 */
3755 if (!skip_sendto) {
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003756 int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0);
3757 if (ret < 0) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003758 TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003759 qc_kill_conn(qc);
3760 b_del(buf, buf->data);
3761 goto leave;
3762 }
3763 else if (!ret) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003764 /* Connection owned socket : poller will wake us up when transient error is cleared. */
3765 if (qc_test_fd(qc)) {
3766 TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc);
3767 goto leave;
3768 }
3769
3770 /* No connection owned-socket : rely on retransmission to retry sending. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003771 skip_sendto = 1;
3772 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
3773 }
3774 }
3775
3776 b_del(buf, dglen + headlen);
3777 qc->tx.bytes += tmpbuf.data;
3778 time_sent = now_ms;
3779
3780 for (pkt = first_pkt; pkt; pkt = next_pkt) {
Frédéric Lécailleceb88b82023-02-20 14:43:55 +01003781 /* RFC 9000 14.1 Initial datagram size
3782 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3783 * Initial packets to at least the smallest allowed maximum datagram size of
3784 * 1200 bytes.
3785 */
3786 BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3787 (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
3788 dglen < QUIC_INITIAL_PACKET_MINLEN);
3789
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003790 pkt->time_sent = time_sent;
3791 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3792 pkt->pktns->tx.time_of_last_eliciting = time_sent;
3793 qc->path->ifae_pkts++;
3794 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
Frédéric Lécailled7215712023-03-24 18:13:37 +01003795 qc_idle_timer_rearm(qc, 0, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003796 }
3797 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3798 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3799 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003800 qc_detach_th_ctx_list(qc, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003801 qc_notify_close(qc);
3802
3803 /* RFC 9000 10.2. Immediate Close:
3804 * The closing and draining connection states exist to ensure
3805 * that connections close cleanly and that delayed or reordered
3806 * packets are properly discarded. These states SHOULD persist
3807 * for at least three times the current PTO interval...
3808 *
3809 * Rearm the idle timeout only one time when entering closing
3810 * state.
3811 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01003812 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003813 if (qc->timer_task) {
3814 task_destroy(qc->timer_task);
3815 qc->timer_task = NULL;
3816 }
3817 }
3818 qc->path->in_flight += pkt->in_flight_len;
3819 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3820 if (pkt->in_flight_len)
3821 qc_set_timer(qc);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003822 TRACE_PROTO("TX pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003823 next_pkt = pkt->next;
3824 quic_tx_packet_refinc(pkt);
3825 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
3826 }
3827 }
3828
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003829 ret = 1;
3830leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003831 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3832
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003833 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003834}
3835
3836/* Copy into <buf> buffer a stateless reset token depending on the
3837 * <salt> salt input. This is the cluster secret which will be derived
3838 * as HKDF input secret to generate this token.
3839 * Return 1 if succeeded, 0 if not.
3840 */
3841static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3842 unsigned char *buf, size_t len,
3843 const unsigned char *salt, size_t saltlen)
3844{
3845 /* Input secret */
3846 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3847 size_t keylen = strlen(global.cluster_secret);
3848 /* Info */
3849 const unsigned char label[] = "stateless token";
3850 size_t labellen = sizeof label - 1;
3851 int ret;
3852
3853 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3854
3855 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
3856 key, keylen, salt, saltlen, label, labellen);
3857 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3858 return ret;
3859}
3860
3861/* Initialize the stateless reset token attached to <cid> connection ID.
3862 * Returns 1 if succeeded, 0 if not.
3863 */
3864static int quic_stateless_reset_token_init(struct quic_conn *qc,
3865 struct quic_connection_id *quic_cid)
3866{
3867 int ret;
3868
3869 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3870
3871 if (global.cluster_secret) {
3872 /* Output secret */
3873 unsigned char *token = quic_cid->stateless_reset_token;
3874 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3875 /* Salt */
3876 const unsigned char *cid = quic_cid->cid.data;
3877 size_t cidlen = quic_cid->cid.len;
3878
3879 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
3880 }
3881 else {
3882 /* TODO: RAND_bytes() should be replaced */
3883 ret = RAND_bytes(quic_cid->stateless_reset_token,
3884 sizeof quic_cid->stateless_reset_token) == 1;
3885 }
3886
3887 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3888 return ret;
3889}
3890
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003891/* Generate a CID directly derived from <orig> CID and <addr> address. The CID
3892 * is then marked with the current thread ID.
3893 *
3894 * Returns a new 64-bits CID value.
3895 */
3896static uint64_t quic_derive_cid(const struct quic_cid *orig,
3897 const struct sockaddr_storage *addr)
3898{
3899 const struct sockaddr_in *in;
3900 const struct sockaddr_in6 *in6;
3901 char *buf = trash.area;
3902 size_t idx = 0;
3903 uint64_t hash;
3904
3905 /* Prepare buffer for hash using original CID first. */
3906 memcpy(buf, orig->data, orig->len);
3907 idx += orig->len;
3908
3909 /* Concatenate client address. */
3910 switch (addr->ss_family) {
3911 case AF_INET:
3912 in = (struct sockaddr_in *)addr;
3913
3914 memcpy(&buf[idx], &in->sin_addr, sizeof(in->sin_addr));
3915 idx += sizeof(in->sin_addr);
3916 memcpy(&buf[idx], &in->sin_port, sizeof(in->sin_port));
3917 idx += sizeof(in->sin_port);
3918 break;
3919
3920 case AF_INET6:
3921 in6 = (struct sockaddr_in6 *)addr;
3922
3923 memcpy(&buf[idx], &in6->sin6_addr, sizeof(in6->sin6_addr));
3924 idx += sizeof(in6->sin6_addr);
3925 memcpy(&buf[idx], &in6->sin6_port, sizeof(in6->sin6_port));
3926 idx += sizeof(in6->sin6_port);
3927 break;
3928
3929 default:
3930 /* TODO to implement */
3931 ABORT_NOW();
3932 return 0;
3933 }
3934
3935 /* Avoid similar values between multiple haproxy process. */
3936 memcpy(&buf[idx], boot_seed, sizeof(boot_seed));
3937 idx += sizeof(boot_seed);
3938
3939 /* Hash the final buffer content. */
3940 hash = XXH64(buf, idx, 0);
3941
3942 /* Mark the current thread id in the CID. */
3943 quic_pin_cid_to_tid((uchar *)&hash, tid);
3944
3945 return hash;
3946}
3947
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01003948/* Allocate a new CID and attach it to <root> ebtree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003949 *
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003950 * If <orig> and <addr> params are non null, the new CID value is directly
3951 * derived from them. Else a random value is generated. The CID is then marked
3952 * with the current thread ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003953 *
3954 * Returns the new CID if succeeded, NULL if not.
3955 */
3956static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003957 struct quic_conn *qc,
3958 const struct quic_cid *orig,
3959 const struct sockaddr_storage *addr)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003960{
3961 struct quic_connection_id *cid;
3962
3963 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3964
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003965 /* Caller must set either none or both values. */
3966 BUG_ON(!!orig != !!addr);
3967
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003968 cid = pool_alloc(pool_head_quic_connection_id);
3969 if (!cid) {
3970 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3971 goto err;
3972 }
3973
3974 cid->cid.len = QUIC_HAP_CID_LEN;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003975
3976 if (!orig) {
3977 /* TODO: RAND_bytes() should be replaced */
3978 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3979 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
3980 goto err;
3981 }
3982 quic_pin_cid_to_tid(cid->cid.data, tid);
3983 }
3984 else {
3985 /* Derive the new CID value from original CID. */
3986 const uint64_t hash = quic_derive_cid(orig, addr);
3987 memcpy(cid->cid.data, &hash, sizeof(hash));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003988 }
3989
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003990 if (quic_stateless_reset_token_init(qc, cid) != 1) {
3991 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
3992 goto err;
3993 }
3994
3995 cid->qc = qc;
3996
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01003997 cid->seq_num.key = qc->next_cid_seq_num++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003998 cid->retire_prior_to = 0;
3999 /* insert the allocated CID in the quic_conn tree */
4000 eb64_insert(root, &cid->seq_num);
4001
4002 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4003 return cid;
4004
4005 err:
4006 pool_free(pool_head_quic_connection_id, cid);
4007 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4008 return NULL;
4009}
4010
4011/* Build all the frames which must be sent just after the handshake have succeeded.
4012 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
4013 * a HANDSHAKE_DONE frame.
4014 * Return 1 if succeeded, 0 if not.
4015 */
4016static int quic_build_post_handshake_frames(struct quic_conn *qc)
4017{
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004018 int ret = 0, max;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004019 struct quic_enc_level *qel;
4020 struct quic_frame *frm, *frmbak;
4021 struct list frm_list = LIST_HEAD_INIT(frm_list);
4022 struct eb64_node *node;
4023
4024 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4025
4026 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4027 /* Only servers must send a HANDSHAKE_DONE frame. */
4028 if (qc_is_listener(qc)) {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004029 frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004030 if (!frm) {
4031 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4032 goto leave;
4033 }
4034
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004035 LIST_APPEND(&frm_list, &frm->list);
4036 }
4037
4038 /* Initialize <max> connection IDs minus one: there is
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004039 * already one connection ID used for the current connection. Also limit
4040 * the number of connection IDs sent to the peer to 4 (3 from this function
4041 * plus 1 for the current connection.
4042 * Note that active_connection_id_limit >= 2: this has been already checked
4043 * when receiving this parameter.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004044 */
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004045 max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3);
4046 while (max--) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004047 struct quic_connection_id *cid;
4048
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004049 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004050 if (!frm) {
4051 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4052 goto err;
4053 }
4054
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004055 cid = new_quic_cid(&qc->cids, qc, NULL, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004056 if (!cid) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004057 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004058 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
4059 goto err;
4060 }
4061
4062 /* insert the allocated CID in the receiver datagram handler tree */
4063 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
4064
4065 quic_connection_id_to_frm_cpy(frm, cid);
4066 LIST_APPEND(&frm_list, &frm->list);
4067 }
4068
4069 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
4070 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
4071
4072 ret = 1;
4073 leave:
4074 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4075 return ret;
4076
4077 err:
4078 /* free the frames */
4079 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004080 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004081
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004082 /* The first CID sequence number value used to allocated CIDs by this function is 1,
4083 * 0 being the sequence number of the CID for this connection.
4084 */
4085 node = eb64_lookup_ge(&qc->cids, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004086 while (node) {
4087 struct quic_connection_id *cid;
4088
4089 cid = eb64_entry(node, struct quic_connection_id, seq_num);
4090 if (cid->seq_num.key >= max)
4091 break;
4092
4093 node = eb64_next(node);
4094 ebmb_delete(&cid->node);
4095 eb64_delete(&cid->seq_num);
4096 pool_free(pool_head_quic_connection_id, cid);
4097 }
4098 goto leave;
4099}
4100
4101/* Deallocate <l> list of ACK ranges. */
4102void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
4103{
4104 struct eb64_node *n;
4105 struct quic_arng_node *ar;
4106
4107 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4108
4109 n = eb64_first(&arngs->root);
4110 while (n) {
4111 struct eb64_node *next;
4112
4113 ar = eb64_entry(n, struct quic_arng_node, first);
4114 next = eb64_next(n);
4115 eb64_delete(n);
4116 pool_free(pool_head_quic_arng, ar);
4117 n = next;
4118 }
4119
4120 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4121}
4122
4123/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
4124 * descending order.
4125 */
4126static inline size_t sack_gap(struct quic_arng_node *p,
4127 struct quic_arng_node *q)
4128{
4129 return p->first.key - q->last - 2;
4130}
4131
4132
4133/* Remove the last elements of <ack_ranges> list of ack range updating its
4134 * encoded size until it goes below <limit>.
4135 * Returns 1 if succeeded, 0 if not (no more element to remove).
4136 */
4137static int quic_rm_last_ack_ranges(struct quic_conn *qc,
4138 struct quic_arngs *arngs, size_t limit)
4139{
4140 int ret = 0;
4141 struct eb64_node *last, *prev;
4142
4143 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4144
4145 last = eb64_last(&arngs->root);
4146 while (last && arngs->enc_sz > limit) {
4147 struct quic_arng_node *last_node, *prev_node;
4148
4149 prev = eb64_prev(last);
4150 if (!prev) {
4151 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
4152 goto out;
4153 }
4154
4155 last_node = eb64_entry(last, struct quic_arng_node, first);
4156 prev_node = eb64_entry(prev, struct quic_arng_node, first);
4157 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
4158 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
4159 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
4160 --arngs->sz;
4161 eb64_delete(last);
4162 pool_free(pool_head_quic_arng, last);
4163 last = prev;
4164 }
4165
4166 ret = 1;
4167 out:
4168 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4169 return ret;
4170}
4171
4172/* Set the encoded size of <arngs> QUIC ack ranges. */
4173static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
4174{
4175 struct eb64_node *node, *next;
4176 struct quic_arng_node *ar, *ar_next;
4177
4178 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4179
4180 node = eb64_last(&arngs->root);
4181 if (!node)
4182 goto leave;
4183
4184 ar = eb64_entry(node, struct quic_arng_node, first);
4185 arngs->enc_sz = quic_int_getsize(ar->last) +
4186 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
4187
4188 while ((next = eb64_prev(node))) {
4189 ar_next = eb64_entry(next, struct quic_arng_node, first);
4190 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
4191 quic_int_getsize(ar_next->last - ar_next->first.key);
4192 node = next;
4193 ar = eb64_entry(node, struct quic_arng_node, first);
4194 }
4195
4196 leave:
4197 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4198}
4199
4200/* Insert <ar> ack range into <argns> tree of ack ranges.
4201 * Returns the ack range node which has been inserted if succeeded, NULL if not.
4202 */
4203static inline
4204struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
4205 struct quic_arngs *arngs,
4206 struct quic_arng *ar)
4207{
4208 struct quic_arng_node *new_ar;
4209
4210 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4211
4212 new_ar = pool_alloc(pool_head_quic_arng);
4213 if (!new_ar) {
4214 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
4215 goto leave;
4216 }
4217
4218 new_ar->first.key = ar->first;
4219 new_ar->last = ar->last;
4220 eb64_insert(&arngs->root, &new_ar->first);
4221 arngs->sz++;
4222
4223 leave:
4224 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4225 return new_ar;
4226}
4227
4228/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
4229 * Note that this function computes the number of bytes required to encode
4230 * this tree of ACK ranges in descending order.
4231 *
4232 * Descending order
4233 * ------------->
4234 * range1 range2
4235 * ..........|--------|..............|--------|
4236 * ^ ^ ^ ^
4237 * | | | |
4238 * last1 first1 last2 first2
4239 * ..........+--------+--------------+--------+......
4240 * diff1 gap12 diff2
4241 *
4242 * To encode the previous list of ranges we must encode integers as follows in
4243 * descending order:
4244 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
4245 * with diff1 = last1 - first1
4246 * diff2 = last2 - first2
4247 * gap12 = first1 - last2 - 2 (>= 0)
4248 *
4249
4250returns 0 on error
4251
4252 */
4253int quic_update_ack_ranges_list(struct quic_conn *qc,
4254 struct quic_arngs *arngs,
4255 struct quic_arng *ar)
4256{
4257 int ret = 0;
4258 struct eb64_node *le;
4259 struct quic_arng_node *new_node;
4260 struct eb64_node *new;
4261
4262 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4263
4264 new = NULL;
4265 if (eb_is_empty(&arngs->root)) {
4266 new_node = quic_insert_new_range(qc, arngs, ar);
4267 if (new_node)
4268 ret = 1;
4269
4270 goto leave;
4271 }
4272
4273 le = eb64_lookup_le(&arngs->root, ar->first);
4274 if (!le) {
4275 new_node = quic_insert_new_range(qc, arngs, ar);
4276 if (!new_node)
4277 goto leave;
4278
4279 new = &new_node->first;
4280 }
4281 else {
4282 struct quic_arng_node *le_ar =
4283 eb64_entry(le, struct quic_arng_node, first);
4284
4285 /* Already existing range */
4286 if (le_ar->last >= ar->last) {
4287 ret = 1;
4288 }
4289 else if (le_ar->last + 1 >= ar->first) {
4290 le_ar->last = ar->last;
4291 new = le;
4292 new_node = le_ar;
4293 }
4294 else {
4295 new_node = quic_insert_new_range(qc, arngs, ar);
4296 if (!new_node)
4297 goto leave;
4298
4299 new = &new_node->first;
4300 }
4301 }
4302
4303 /* Verify that the new inserted node does not overlap the nodes
4304 * which follow it.
4305 */
4306 if (new) {
4307 struct eb64_node *next;
4308 struct quic_arng_node *next_node;
4309
4310 while ((next = eb64_next(new))) {
4311 next_node =
4312 eb64_entry(next, struct quic_arng_node, first);
4313 if (new_node->last + 1 < next_node->first.key)
4314 break;
4315
4316 if (next_node->last > new_node->last)
4317 new_node->last = next_node->last;
4318 eb64_delete(next);
4319 pool_free(pool_head_quic_arng, next_node);
4320 /* Decrement the size of these ranges. */
4321 arngs->sz--;
4322 }
4323 }
4324
4325 ret = 1;
4326 leave:
4327 quic_arngs_set_enc_sz(qc, arngs);
4328 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4329 return ret;
4330}
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004331
4332/* Detect the value of the spin bit to be used. */
4333static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt,
4334 struct quic_enc_level *qel)
4335{
4336 uint64_t largest_pn = qel->pktns->rx.largest_pn;
4337
4338 if (qel != &qc->els[QUIC_TLS_ENC_LEVEL_APP] || largest_pn == -1 ||
4339 pkt->pn <= largest_pn)
4340 return;
4341
4342 if (qc_is_listener(qc)) {
4343 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4344 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4345 else
4346 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4347 }
4348 else {
4349 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4350 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4351 else
4352 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4353 }
4354}
4355
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004356/* Remove the header protection of packets at <el> encryption level.
4357 * Always succeeds.
4358 */
4359static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
4360{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004361 struct quic_rx_packet *pqpkt, *pkttmp;
4362 struct quic_enc_level *app_qel;
4363
4364 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
4365 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4366 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
4367 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004368 TRACE_PROTO("RX hp not removed (handshake not completed)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004369 QUIC_EV_CONN_ELRMHP, qc);
4370 goto out;
4371 }
Frédéric Lécaille72027782023-02-22 16:20:09 +01004372
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004373 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01004374 struct quic_tls_ctx *tls_ctx;
4375
4376 tls_ctx = qc_select_tls_ctx(qc, el, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004377 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
4378 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004379 TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004380 }
4381 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004382 qc_handle_spin_bit(qc, pqpkt, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004383 /* The AAD includes the packet number field */
4384 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
4385 /* Store the packet into the tree of packets to decrypt. */
4386 pqpkt->pn_node.key = pqpkt->pn;
4387 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
4388 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004389 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004390 }
4391 LIST_DELETE(&pqpkt->list);
4392 quic_rx_packet_refdec(pqpkt);
4393 }
4394
4395 out:
4396 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
4397}
4398
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004399/* Process all the CRYPTO frame at <el> encryption level. This is the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004400 * responsibility of the called to ensure there exists a CRYPTO data
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004401 * stream for this level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004402 * Return 1 if succeeded, 0 if not.
4403 */
4404static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
4405 struct quic_enc_level *el,
4406 struct ssl_sock_ctx *ctx)
4407{
4408 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004409 struct ncbuf *ncbuf;
4410 struct quic_cstream *cstream = el->cstream;
4411 ncb_sz_t data;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004412
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004413 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004414
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004415 BUG_ON(!cstream);
4416 ncbuf = &cstream->rx.ncbuf;
4417 if (ncb_is_null(ncbuf))
4418 goto done;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004419
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004420 /* TODO not working if buffer is wrapping */
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004421 while ((data = ncb_data(ncbuf, 0))) {
4422 const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004423
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004424 if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004425 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004426
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004427 cstream->rx.offset += data;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004428 TRACE_DEVEL("buffered crypto data were provided to TLS stack",
4429 QUIC_EV_CONN_PHPKTS, qc, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004430 }
4431
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004432 done:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004433 ret = 1;
4434 leave:
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004435 if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
4436 TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004437 quic_free_ncbuf(ncbuf);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004438 }
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004439 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004440 return ret;
4441}
4442
4443/* Process all the packets at <el> and <next_el> encryption level.
4444 * This is the caller responsibility to check that <cur_el> is different of <next_el>
4445 * as pointer value.
4446 * Return 1 if succeeded, 0 if not.
4447 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004448int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el,
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004449 struct quic_enc_level *next_el)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004450{
4451 int ret = 0;
4452 struct eb64_node *node;
4453 int64_t largest_pn = -1;
4454 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004455 struct quic_enc_level *qel = cur_el;
4456
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004457 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004458 qel = cur_el;
4459 next_tel:
4460 if (!qel)
4461 goto out;
4462
4463 node = eb64_first(&qel->rx.pkts);
4464 while (node) {
4465 struct quic_rx_packet *pkt;
4466
4467 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
4468 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004469 qc, pkt, NULL, qc->xprt_ctx->ssl);
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01004470 if (!qc_pkt_decrypt(qc, qel, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004471 /* Drop the packet */
4472 TRACE_ERROR("packet decryption failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004473 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004474 }
4475 else {
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004476 if (!qc_parse_pkt_frms(qc, pkt, qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004477 /* Drop the packet */
4478 TRACE_ERROR("packet parsing failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004479 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004480 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
4481 }
4482 else {
4483 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
4484
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004485 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004486 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
4487 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécailled7215712023-03-24 18:13:37 +01004488 qc_idle_timer_rearm(qc, 1, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004489 }
4490 if (pkt->pn > largest_pn) {
4491 largest_pn = pkt->pn;
4492 largest_pn_time_received = pkt->time_received;
4493 }
4494 /* Update the list of ranges to acknowledge. */
4495 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
4496 TRACE_ERROR("Could not update ack range list",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004497 QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004498 }
4499 }
4500 node = eb64_next(node);
4501 eb64_delete(&pkt->pn_node);
4502 quic_rx_packet_refdec(pkt);
4503 }
4504
4505 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
4506 /* Update the largest packet number. */
4507 qel->pktns->rx.largest_pn = largest_pn;
4508 /* Update the largest acknowledged packet timestamps */
4509 qel->pktns->rx.largest_time_received = largest_pn_time_received;
4510 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
4511 }
4512
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004513 if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004514 // trace already emitted by function above
4515 goto leave;
4516 }
4517
4518 if (qel == cur_el) {
4519 BUG_ON(qel == next_el);
4520 qel = next_el;
4521 largest_pn = -1;
4522 goto next_tel;
4523 }
4524
4525 out:
4526 ret = 1;
4527 leave:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004528 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004529 return ret;
4530}
4531
4532/* Check if it's possible to remove header protection for packets related to
4533 * encryption level <qel>. If <qel> is NULL, assume it's false.
4534 *
4535 * Return true if the operation is possible else false.
4536 */
4537static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
4538{
4539 int ret = 0;
4540 enum quic_tls_enc_level tel;
4541
4542 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
4543
4544 if (!qel)
4545 goto cant_rm_hp;
4546
4547 tel = ssl_to_quic_enc_level(qel->level);
4548
4549 /* check if tls secrets are available */
4550 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004551 TRACE_PROTO("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004552 goto cant_rm_hp;
4553 }
4554
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004555 if (!quic_tls_has_rx_sec(qel)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004556 TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004557 goto cant_rm_hp;
4558 }
4559
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004560 if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004561 TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004562 goto cant_rm_hp;
4563 }
4564
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004565 /* check if the connection layer is ready before using app level */
4566 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
4567 qc->mux_state == QC_MUX_NULL) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004568 TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004569 goto cant_rm_hp;
4570 }
4571
4572 ret = 1;
4573 cant_rm_hp:
4574 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
4575 return ret;
4576}
4577
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004578/* Flush txbuf for <qc> connection. This must be called prior to a packet
4579 * preparation when txbuf contains older data. A send will be conducted for
4580 * these data.
4581 *
4582 * Returns 1 on success : buffer is empty and can be use for packet
4583 * preparation. On error 0 is returned.
4584 */
4585static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
4586{
4587 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4588
4589 /* This operation can only be conducted if txbuf is not empty. This
4590 * case only happens for connection with their owned socket due to an
4591 * older transient sendto() error.
4592 */
4593 BUG_ON(!qc_test_fd(qc));
4594
4595 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4596 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4597 qc_txb_release(qc);
4598 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4599 return 0;
4600 }
4601
4602 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4603 return 1;
4604}
4605
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004606/* Try to send application frames from list <frms> on connection <qc>.
4607 *
4608 * Use qc_send_app_probing wrapper when probing with old data.
4609 *
4610 * Returns 1 on success. Some data might not have been sent due to congestion,
4611 * in this case they are left in <frms> input list. The caller may subscribe on
4612 * quic-conn to retry later.
4613 *
4614 * Returns 0 on critical error.
4615 * TODO review and classify more distinctly transient from definitive errors to
4616 * allow callers to properly handle it.
4617 */
4618static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
4619{
4620 int status = 0;
4621 struct buffer *buf;
4622
4623 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4624
4625 buf = qc_txb_alloc(qc);
4626 if (!buf) {
4627 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004628 goto err;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004629 }
4630
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004631 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4632 goto err;
4633
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004634 /* Prepare and send packets until we could not further prepare packets. */
4635 while (1) {
4636 int ret;
4637 /* Currently buf cannot be non-empty at this stage. Even if a
4638 * previous sendto() has failed it is emptied to simulate
4639 * packet emission and rely on QUIC lost detection to try to
4640 * emit it.
4641 */
4642 BUG_ON_HOT(b_data(buf));
4643 b_reset(buf);
4644
4645 ret = qc_prep_app_pkts(qc, buf, frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004646 if (ret == -1) {
4647 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004648 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004649 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004650
Amaury Denoyelle37333862023-02-28 11:53:48 +01004651 if (!ret)
4652 break;
4653
4654 if (!qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004655 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4656 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004657 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004658 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004659 }
4660
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004661 status = 1;
4662 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004663 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4664 return status;
4665
4666 err:
Amaury Denoyelle37333862023-02-28 11:53:48 +01004667 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4668 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004669}
4670
4671/* Try to send application frames from list <frms> on connection <qc>. Use this
4672 * function when probing is required.
4673 *
4674 * Returns the result from qc_send_app_pkts function.
4675 */
4676static forceinline int qc_send_app_probing(struct quic_conn *qc,
4677 struct list *frms)
4678{
4679 int ret;
4680
4681 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4682
4683 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
4684 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4685 ret = qc_send_app_pkts(qc, frms);
4686 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
4687
4688 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4689 return ret;
4690}
4691
4692/* Try to send application frames from list <frms> on connection <qc>. This
4693 * function is provided for MUX upper layer usage only.
4694 *
4695 * Returns the result from qc_send_app_pkts function.
4696 */
4697int qc_send_mux(struct quic_conn *qc, struct list *frms)
4698{
4699 int ret;
4700
4701 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4702 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
4703
4704 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4705 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
4706 ret = qc_send_app_pkts(qc, frms);
4707 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
4708
4709 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4710 return ret;
4711}
4712
4713/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4714 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
4715 * QUIC connection. <old_data> is used as boolean to send data already sent but
4716 * not already acknowledged (in flight).
4717 * Returns 1 if succeeded, 0 if not.
4718 */
4719int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4720 enum quic_tls_enc_level tel, struct list *tel_frms,
4721 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4722{
4723 int ret, status = 0;
4724 struct buffer *buf = qc_txb_alloc(qc);
4725
4726 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4727
4728 if (!buf) {
4729 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4730 goto leave;
4731 }
4732
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004733 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4734 goto out;
4735
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004736 /* Currently buf cannot be non-empty at this stage. Even if a previous
4737 * sendto() has failed it is emptied to simulate packet emission and
4738 * rely on QUIC lost detection to try to emit it.
4739 */
4740 BUG_ON_HOT(b_data(buf));
4741 b_reset(buf);
4742
4743 if (old_data) {
4744 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
4745 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4746 }
4747
4748 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004749 if (ret == -1) {
4750 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004751 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004752 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004753
Amaury Denoyelle37333862023-02-28 11:53:48 +01004754 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004755 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4756 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004757 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004758 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004759
Amaury Denoyelle37333862023-02-28 11:53:48 +01004760 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004761 status = 1;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004762
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004763 out:
4764 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
4765 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004766 leave:
4767 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4768 return status;
4769}
4770
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004771/* Retransmit up to two datagrams depending on packet number space.
4772 * Return 0 when failed, 0 if not.
4773 */
4774static int qc_dgrams_retransmit(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004775{
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004776 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004777 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4778 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4779 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4780
4781 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4782
4783 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004784 int i;
4785
4786 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4787 struct list ifrms = LIST_HEAD_INIT(ifrms);
4788 struct list hfrms = LIST_HEAD_INIT(hfrms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004789
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004790 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
4791 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4792 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
4793 if (!LIST_ISEMPTY(&ifrms)) {
4794 iqel->pktns->tx.pto_probe = 1;
4795 if (!LIST_ISEMPTY(&hfrms))
4796 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004797 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4798 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms))
4799 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004800 /* Put back unsent frames in their packet number spaces */
4801 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4802 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
4803 }
Frédéric Lécailleec937212023-03-03 17:34:41 +01004804 else {
4805 if (!(qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
4806 iqel->pktns->tx.pto_probe = 1;
4807 if (!qc_send_hdshk_pkts(qc, 0, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4808 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4809 goto leave;
4810 }
4811 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004812 }
4813 TRACE_STATE("no more need to probe Initial packet number space",
4814 QUIC_EV_CONN_TXPKT, qc);
4815 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004816 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004817 }
4818 else {
4819 int i;
4820
4821 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004822 hqel->pktns->tx.pto_probe = 0;
4823 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
Frédéric Lécaille7b5d9b12022-11-28 17:21:45 +01004824 struct list frms1 = LIST_HEAD_INIT(frms1);
4825
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004826 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
4827 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4828 if (!LIST_ISEMPTY(&frms1)) {
4829 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004830 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4831 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4832 goto leave;
4833
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004834 /* Put back unsent frames into their packet number spaces */
4835 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
4836 }
4837 }
4838 TRACE_STATE("no more need to probe Handshake packet number space",
4839 QUIC_EV_CONN_TXPKT, qc);
4840 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4841 }
4842 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4843 struct list frms2 = LIST_HEAD_INIT(frms2);
4844 struct list frms1 = LIST_HEAD_INIT(frms1);
4845
4846 aqel->pktns->tx.pto_probe = 0;
4847 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4848 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4849 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4850 if (!LIST_ISEMPTY(&frms1)) {
4851 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004852 if (!qc_send_app_probing(qc, &frms1))
4853 goto leave;
4854
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004855 /* Put back unsent frames into their packet number spaces */
4856 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
4857 }
4858 if (!LIST_ISEMPTY(&frms2)) {
4859 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004860 if (!qc_send_app_probing(qc, &frms2))
4861 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004862 /* Put back unsent frames into their packet number spaces */
4863 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
4864 }
4865 TRACE_STATE("no more need to probe 01RTT packet number space",
4866 QUIC_EV_CONN_TXPKT, qc);
4867 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4868 }
4869 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004870
4871 ret = 1;
4872 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004873 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004874 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004875}
4876
4877/* QUIC connection packet handler task (post handshake) */
4878struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4879{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004880 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004881 struct quic_enc_level *qel;
4882
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004883 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4884
4885 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4886 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
4887
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01004888 if (qc_test_fd(qc))
4889 qc_rcv_buf(qc);
4890
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004891 /* Retranmissions */
4892 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
4893 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
4894 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004895 if (!qc_dgrams_retransmit(qc))
4896 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004897 }
4898
4899 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4900 qc_rm_hp_pkts(qc, qel);
4901
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004902 if (!qc_treat_rx_pkts(qc, qel, NULL)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004903 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4904 goto out;
4905 }
4906
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01004907 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
4908 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
4909 goto out;
4910 }
4911
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004912 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4913 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4914 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
4915 goto out;
4916 }
4917
4918 /* XXX TODO: how to limit the list frames to send */
4919 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4920 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4921 goto out;
4922 }
4923
4924 out:
4925 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4926 return t;
4927}
4928
4929/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4930static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4931{
4932 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4933 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4934 qel->pktns->tx.pto_probe ||
4935 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4936}
4937
4938/* QUIC connection packet handler task. */
4939struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
4940{
4941 int ret, ssl_err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004942 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004943 enum quic_tls_enc_level tel, next_tel;
4944 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004945 /* Early-data encryption level */
4946 struct quic_enc_level *eqel;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004947 struct buffer *buf = NULL;
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004948 int st, zero_rtt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004949
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004950 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004951 eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004952 st = qc->state;
4953 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
4954
4955 /* Retranmissions */
4956 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
4957 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
4958 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004959 if (!qc_dgrams_retransmit(qc))
4960 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004961 }
4962
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004963 ssl_err = SSL_ERROR_NONE;
4964 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004965 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004966 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01004967
4968 if (qc_test_fd(qc))
4969 qc_rcv_buf(qc);
4970
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004971 if (st >= QUIC_HS_ST_COMPLETE &&
4972 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
4973 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
4974 /* There may be remaining Handshake packets to treat and acknowledge. */
4975 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4976 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4977 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004978 else if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, zero_rtt))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004979 goto out;
4980
4981 qel = &qc->els[tel];
4982 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
4983
4984 next_level:
4985 /* Treat packets waiting for header packet protection decryption */
4986 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4987 qc_rm_hp_pkts(qc, qel);
4988
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004989 if (!qc_treat_rx_pkts(qc, qel, next_qel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004990 goto out;
4991
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01004992 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
4993 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
4994 goto out;
4995 }
4996
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004997 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4998 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4999 goto out;
5000
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005001 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005002 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005003 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
5004 if (next_qel && next_qel == eqel && zero_rtt) {
5005 TRACE_DEVEL("select 0RTT as next encryption level",
5006 QUIC_EV_CONN_PHPKTS, qc);
5007 qel = next_qel;
5008 next_qel = NULL;
5009 goto next_level;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005010 }
5011
5012 st = qc->state;
5013 if (st >= QUIC_HS_ST_COMPLETE) {
5014 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
5015 !quic_build_post_handshake_frames(qc))
5016 goto out;
5017
5018 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
5019 QUIC_FL_TLS_SECRETS_DCD)) {
5020 /* Discard the Handshake keys. */
5021 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5022 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
5023 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
5024 qc_set_timer(qc);
5025 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5026 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
5027 }
5028
5029 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
5030 /* There may be remaining handshake to build (acks) */
5031 st = QUIC_HS_ST_SERVER_HANDSHAKE;
5032 }
5033 }
5034
5035 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
5036 * be considered.
5037 */
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005038 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005039 goto out;
5040
5041 if (!qc_need_sending(qc, qel) &&
5042 (!next_qel || !qc_need_sending(qc, next_qel))) {
5043 goto skip_send;
5044 }
5045
5046 buf = qc_txb_alloc(qc);
5047 if (!buf)
5048 goto out;
5049
Amaury Denoyelle147862d2023-02-28 15:10:00 +01005050 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
5051 goto skip_send;
5052
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005053 /* Currently buf cannot be non-empty at this stage. Even if a previous
5054 * sendto() has failed it is emptied to simulate packet emission and
5055 * rely on QUIC lost detection to try to emit it.
5056 */
5057 BUG_ON_HOT(b_data(buf));
5058 b_reset(buf);
5059
5060 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
5061 next_tel, &qc->els[next_tel].pktns->tx.frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01005062 if (ret == -1) {
5063 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005064 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005065 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005066
Amaury Denoyelle37333862023-02-28 11:53:48 +01005067 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01005068 if (qc->flags & QUIC_FL_CONN_TO_KILL)
5069 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005070 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005071 }
5072
5073 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005074
5075 skip_send:
5076 /* Check if there is something to do for the next level.
5077 */
5078 if (next_qel && next_qel != qel &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005079 quic_tls_has_rx_sec(next_qel) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005080 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
5081 qel = next_qel;
5082 next_qel = NULL;
5083 goto next_level;
5084 }
5085
5086 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005087 TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
5088 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005089 return t;
5090}
5091
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005092/* Release the memory allocated for <cs> CRYPTO stream */
5093void quic_cstream_free(struct quic_cstream *cs)
5094{
5095 if (!cs) {
5096 /* This is the case for ORTT encryption level */
5097 return;
5098 }
5099
Amaury Denoyellebc174b22022-11-17 10:12:52 +01005100 quic_free_ncbuf(&cs->rx.ncbuf);
5101
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005102 qc_stream_desc_release(cs->desc);
5103 pool_free(pool_head_quic_cstream, cs);
5104}
5105
5106/* Allocate a new QUIC stream for <qc>.
5107 * Return it if succeeded, NULL if not.
5108 */
5109struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
5110{
5111 struct quic_cstream *cs, *ret_cs = NULL;
5112
5113 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5114 cs = pool_alloc(pool_head_quic_cstream);
5115 if (!cs) {
5116 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5117 goto leave;
5118 }
5119
5120 cs->rx.offset = 0;
5121 cs->rx.ncbuf = NCBUF_NULL;
5122 cs->rx.offset = 0;
5123
5124 cs->tx.offset = 0;
5125 cs->tx.sent_offset = 0;
5126 cs->tx.buf = BUF_NULL;
5127 cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
5128 if (!cs->desc) {
5129 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5130 goto err;
5131 }
5132
5133 ret_cs = cs;
5134 leave:
5135 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5136 return ret_cs;
5137
5138 err:
5139 pool_free(pool_head_quic_cstream, cs);
5140 goto leave;
5141}
5142
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005143/* Uninitialize <qel> QUIC encryption level. Never fails. */
5144static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
5145{
5146 int i;
5147
5148 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5149
5150 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
5151 if (qel->tx.crypto.bufs[i]) {
5152 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
5153 qel->tx.crypto.bufs[i] = NULL;
5154 }
5155 }
5156 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005157 quic_cstream_free(qel->cstream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005158
5159 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5160}
5161
5162/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
5163 * connection allocating everything needed.
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005164 *
5165 * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use
5166 * quic_conn_enc_level_uninit() to cleanup partially allocated content.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005167 */
5168static int quic_conn_enc_level_init(struct quic_conn *qc,
5169 enum quic_tls_enc_level level)
5170{
5171 int ret = 0;
5172 struct quic_enc_level *qel;
5173
5174 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5175
5176 qel = &qc->els[level];
5177 qel->level = quic_to_ssl_enc_level(level);
5178 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
5179 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
5180 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
5181 qel->tls_ctx.flags = 0;
5182
5183 qel->rx.pkts = EB_ROOT;
5184 LIST_INIT(&qel->rx.pqpkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005185
5186 /* Allocate only one buffer. */
5187 /* TODO: use a pool */
5188 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
5189 if (!qel->tx.crypto.bufs)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005190 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005191
5192 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
5193 if (!qel->tx.crypto.bufs[0])
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005194 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005195
5196 qel->tx.crypto.bufs[0]->sz = 0;
5197 qel->tx.crypto.nb_buf = 1;
5198
5199 qel->tx.crypto.sz = 0;
5200 qel->tx.crypto.offset = 0;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005201 /* No CRYPTO data for early data TLS encryption level */
5202 if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA)
5203 qel->cstream = NULL;
5204 else {
5205 qel->cstream = quic_cstream_new(qc);
5206 if (!qel->cstream)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005207 goto leave;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005208 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005209
5210 ret = 1;
5211 leave:
5212 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5213 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005214}
5215
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005216/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
5217 * This is not the case if the remote peer address is not validated and if
5218 * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
5219 */
5220static int qc_may_probe_ipktns(struct quic_conn *qc)
5221{
5222 return quic_peer_validated_addr(qc) ||
5223 (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
5224}
5225
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005226/* Callback called upon loss detection and PTO timer expirations. */
5227struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
5228{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005229 struct quic_conn *qc = ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005230 struct quic_pktns *pktns;
5231
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005232 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
5233 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005234 NULL, NULL, &qc->path->ifae_pkts);
5235 task->expire = TICK_ETERNITY;
5236 pktns = quic_loss_pktns(qc);
5237 if (tick_isset(pktns->tx.loss_time)) {
5238 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
5239
5240 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
5241 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005242 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01005243 if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
5244 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005245 goto out;
5246 }
5247
5248 if (qc->path->in_flight) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +01005249 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
Frédéric Lécaille68737312023-04-07 16:28:46 +02005250 if (!pktns->tx.in_flight) {
5251 TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
5252 goto out;
5253 }
5254
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005255 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
5256 if (qc_may_probe_ipktns(qc)) {
5257 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5258 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5259 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5260 }
5261 else {
5262 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5263 }
5264 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
5265 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5266 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5267 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5268 }
Frédéric Lécaillee25fce02023-03-20 17:23:19 +01005269 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005270 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
5271 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5272 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5273 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5274 if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005275 if (qc_may_probe_ipktns(qc)) {
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005276 qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005277 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5278 }
5279 else {
5280 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5281 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005282 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005283 }
5284 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01005285 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005286 /* Wake up upper layer if waiting to send new data. */
5287 if (!qc_notify_send(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005288 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005289 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5290 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005291 }
5292 }
5293 }
5294 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
5295 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
5296 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
5297
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005298 if (quic_tls_has_tx_sec(hel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005299 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005300 if (quic_tls_has_tx_sec(iel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005301 iel->pktns->tx.pto_probe = 1;
5302 }
5303
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005304 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005305 qc->path->loss.pto_count++;
5306
5307 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005308 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
5309 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005310
5311 return task;
5312}
5313
5314/* Parse the Retry token from buffer <token> with <end> a pointer to
5315 * one byte past the end of this buffer. This will extract the ODCID
5316 * which will be stored into <odcid>
5317 *
5318 * Returns 0 on success else non-zero.
5319 */
5320static int parse_retry_token(struct quic_conn *qc,
5321 const unsigned char *token, const unsigned char *end,
5322 struct quic_cid *odcid)
5323{
5324 int ret = 0;
5325 uint64_t odcid_len;
5326 uint32_t timestamp;
5327
5328 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5329
5330 if (!quic_dec_int(&odcid_len, &token, end)) {
5331 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
5332 goto leave;
5333 }
5334
5335 /* RFC 9000 7.2. Negotiating Connection IDs:
5336 * When an Initial packet is sent by a client that has not previously
5337 * received an Initial or Retry packet from the server, the client
5338 * populates the Destination Connection ID field with an unpredictable
5339 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5340 */
5341 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
5342 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
5343 goto leave;
5344 }
5345
5346 if (end - token < odcid_len + sizeof timestamp) {
5347 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
5348 goto leave;
5349 }
5350
5351 timestamp = ntohl(read_u32(token + odcid_len));
5352 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
5353 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
5354 goto leave;
5355 }
5356
5357 ret = 1;
5358 memcpy(odcid->data, token, odcid_len);
5359 odcid->len = odcid_len;
5360 leave:
5361 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5362 return !ret;
5363}
5364
5365/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
5366 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
5367 * for QUIC servers (or haproxy listeners).
5368 * <dcid> is the destination connection ID, <scid> is the source connection ID,
5369 * <token> the token found to be used for this connection with <token_len> as
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005370 * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005371 * Returns the connection if succeeded, NULL if not.
5372 */
5373static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
5374 struct quic_cid *dcid, struct quic_cid *scid,
5375 const struct quic_cid *token_odcid,
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005376 struct sockaddr_storage *local_addr,
5377 struct sockaddr_storage *peer_addr,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005378 int server, int token, void *owner)
5379{
5380 int i;
5381 struct quic_conn *qc;
5382 /* Initial CID. */
5383 struct quic_connection_id *icid;
5384 char *buf_area = NULL;
5385 struct listener *l = NULL;
5386 struct quic_cc_algo *cc_algo = NULL;
5387 struct quic_tls_ctx *ictx;
5388 TRACE_ENTER(QUIC_EV_CONN_INIT);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005389 /* TODO replace pool_zalloc by pool_alloc(). This requires special care
5390 * to properly initialized internal quic_conn members to safely use
5391 * quic_conn_release() on alloc failure.
5392 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005393 qc = pool_zalloc(pool_head_quic_conn);
5394 if (!qc) {
5395 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
5396 goto err;
5397 }
5398
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005399 /* Initialize in priority qc members required for a safe dealloc. */
5400
5401 /* required to use MTLIST_IN_LIST */
5402 MT_LIST_INIT(&qc->accept_list);
5403
5404 LIST_INIT(&qc->rx.pkt_list);
5405
Amaury Denoyelle42448332022-12-12 11:24:05 +01005406 qc_init_fd(qc);
5407
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005408 LIST_INIT(&qc->back_refs);
5409
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005410 /* Now proceeds to allocation of qc members. */
5411
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005412 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
5413 if (!buf_area) {
5414 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
5415 goto err;
5416 }
5417
5418 qc->cids = EB_ROOT;
5419 /* QUIC Server (or listener). */
5420 if (server) {
5421 struct proxy *prx;
5422
5423 l = owner;
5424 prx = l->bind_conf->frontend;
5425 cc_algo = l->bind_conf->quic_cc_algo;
5426
5427 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
5428 &quic_stats_module);
5429 qc->flags |= QUIC_FL_CONN_LISTENER;
5430 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005431 /* Copy the client original DCID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005432 qc->odcid.len = dcid->len;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005433 memcpy(qc->odcid.data, dcid->data, dcid->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005434
5435 /* copy the packet SCID to reuse it as DCID for sending */
5436 if (scid->len)
5437 memcpy(qc->dcid.data, scid->data, scid->len);
5438 qc->dcid.len = scid->len;
5439 qc->tx.buf = BUF_NULL;
5440 qc->li = l;
5441 }
5442 /* QUIC Client (outgoing connection to servers) */
5443 else {
5444 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
5445 if (dcid->len)
5446 memcpy(qc->dcid.data, dcid->data, dcid->len);
5447 qc->dcid.len = dcid->len;
5448 }
5449 qc->mux_state = QC_MUX_NULL;
5450 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
5451
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01005452 /* Initialize the next CID sequence number to be used for this connection. */
5453 qc->next_cid_seq_num = 0;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02005454 /* Generate the first connection CID. This is derived from the client
5455 * ODCID and address. This allows to retrieve the connection from the
5456 * ODCID without storing it in the CID tree. This is an interesting
5457 * optimization as the client is expected to stop using its ODCID in
5458 * favor of our generated value.
5459 */
5460 icid = new_quic_cid(&qc->cids, qc, dcid, peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005461 if (!icid) {
5462 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
5463 goto err;
5464 }
5465
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005466 if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
5467 is_addr(local_addr)) {
5468 TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
5469 qc_alloc_fd(qc, local_addr, peer_addr);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005470
5471 /* haproxy soft-stop is supported only for QUIC connections
5472 * with their owned socket.
5473 */
5474 if (qc_test_fd(qc))
5475 _HA_ATOMIC_INC(&jobs);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005476 }
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005477
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005478 /* insert the allocated CID in the receiver datagram handler tree */
5479 if (server)
5480 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
5481
5482 /* Select our SCID which is the first CID with 0 as sequence number. */
5483 qc->scid = icid->cid;
5484
5485 /* Packet number spaces initialization. */
5486 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
5487 quic_pktns_init(&qc->pktns[i]);
5488 /* QUIC encryption level context initialization. */
5489 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5490 if (!quic_conn_enc_level_init(qc, i)) {
5491 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
5492 goto err;
5493 }
5494 /* Initialize the packet number space. */
5495 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
5496 }
5497
5498 qc->original_version = qv;
5499 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
5500 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
5501 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
5502 /* TX part. */
5503 LIST_INIT(&qc->tx.frms_to_send);
5504 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
5505 qc->tx.wbuf = qc->tx.rbuf = 0;
5506 qc->tx.bytes = 0;
5507 qc->tx.buf = BUF_NULL;
5508 /* RX part. */
5509 qc->rx.bytes = 0;
5510 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
5511 for (i = 0; i < QCS_MAX_TYPES; i++)
5512 qc->rx.strms[i].nb_streams = 0;
5513
5514 qc->nb_pkt_for_cc = 1;
5515 qc->nb_pkt_since_cc = 0;
5516
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005517 if (!quic_tls_ku_init(qc)) {
5518 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
5519 goto err;
5520 }
5521
5522 /* XXX TO DO: Only one path at this time. */
5523 qc->path = &qc->paths[0];
5524 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
5525
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005526 qc->streams_by_id = EB_ROOT_UNIQUE;
5527 qc->stream_buf_count = 0;
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005528 memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
5529 memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005530
5531 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
5532 icid->stateless_reset_token,
5533 dcid->data, dcid->len,
5534 qc->scid.data, qc->scid.len, token_odcid))
5535 goto err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005536
Frédéric Lécailledeb97812023-03-22 11:29:45 +01005537 /* Initialize the idle timeout of the connection at the "max_idle_timeout"
5538 * value from local transport parameters.
5539 */
5540 qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005541 qc->wait_event.tasklet = tasklet_new();
5542 if (!qc->wait_event.tasklet) {
5543 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
5544 goto err;
5545 }
5546 qc->wait_event.tasklet->process = quic_conn_io_cb;
5547 qc->wait_event.tasklet->context = qc;
5548 qc->wait_event.events = 0;
5549 /* Set tasklet tid based on the SCID selected by us for this
5550 * connection. The upper layer will also be binded on the same thread.
5551 */
Willy Tarreaueed78262022-12-21 09:09:19 +01005552 qc->tid = quic_get_cid_tid(qc->scid.data, &l->rx);
Willy Tarreauf5a0c8a2022-10-13 16:14:11 +02005553 qc->wait_event.tasklet->tid = qc->tid;
Amaury Denoyellebbb1c682022-09-28 15:15:51 +02005554 qc->subs = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005555
5556 if (qc_conn_alloc_ssl_ctx(qc) ||
5557 !quic_conn_init_timer(qc) ||
5558 !quic_conn_init_idle_timer_task(qc))
5559 goto err;
5560
5561 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
5562 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
5563 goto err;
5564
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005565 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
5566 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
5567
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005568 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
5569
5570 return qc;
5571
5572 err:
5573 pool_free(pool_head_quic_conn_rxbuf, buf_area);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005574 if (qc) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005575 qc->rx.buf.area = NULL;
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005576 quic_conn_release(qc);
5577 }
5578 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005579 return NULL;
5580}
5581
5582/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
5583 * The connection tasklet is killed.
5584 *
5585 * This function must only be called by the thread responsible of the quic_conn
5586 * tasklet.
5587 */
5588void quic_conn_release(struct quic_conn *qc)
5589{
5590 int i;
5591 struct ssl_sock_ctx *conn_ctx;
5592 struct eb64_node *node;
5593 struct quic_tls_ctx *app_tls_ctx;
5594 struct quic_rx_packet *pkt, *pktback;
5595
5596 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5597
5598 /* We must not free the quic-conn if the MUX is still allocated. */
5599 BUG_ON(qc->mux_state == QC_MUX_READY);
5600
Amaury Denoyellefb375572023-02-01 09:28:32 +01005601 if (qc_test_fd(qc))
5602 _HA_ATOMIC_DEC(&jobs);
5603
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005604 /* Close quic-conn socket fd. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01005605 qc_release_fd(qc, 0);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005606
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005607 /* in the unlikely (but possible) case the connection was just added to
5608 * the accept_list we must delete it from there.
5609 */
5610 MT_LIST_DELETE(&qc->accept_list);
5611
5612 /* free remaining stream descriptors */
5613 node = eb64_first(&qc->streams_by_id);
5614 while (node) {
5615 struct qc_stream_desc *stream;
5616
5617 stream = eb64_entry(node, struct qc_stream_desc, by_id);
5618 node = eb64_next(node);
5619
5620 /* all streams attached to the quic-conn are released, so
5621 * qc_stream_desc_free will liberate the stream instance.
5622 */
5623 BUG_ON(!stream->release);
5624 qc_stream_desc_free(stream, 1);
5625 }
5626
5627 /* Purge Rx packet list. */
5628 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
5629 LIST_DELETE(&pkt->qc_rx_pkt_list);
5630 pool_free(pool_head_quic_rx_packet, pkt);
5631 }
5632
5633 if (qc->idle_timer_task) {
5634 task_destroy(qc->idle_timer_task);
5635 qc->idle_timer_task = NULL;
5636 }
5637
5638 if (qc->timer_task) {
5639 task_destroy(qc->timer_task);
5640 qc->timer_task = NULL;
5641 }
5642
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005643 if (qc->wait_event.tasklet)
5644 tasklet_free(qc->wait_event.tasklet);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005645
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005646 /* remove the connection from receiver cids trees */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005647 ebmb_delete(&qc->scid_node);
5648 free_quic_conn_cids(qc);
5649
5650 conn_ctx = qc->xprt_ctx;
5651 if (conn_ctx) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005652 SSL_free(conn_ctx->ssl);
5653 pool_free(pool_head_quic_conn_ctx, conn_ctx);
5654 }
5655
5656 quic_tls_ku_free(qc);
5657 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5658 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
5659 quic_conn_enc_level_uninit(qc, &qc->els[i]);
5660 }
5661 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
5662
5663 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
5664 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
5665 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
5666
5667 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
5668 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
5669 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
5670 }
5671
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01005672 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005673
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005674 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
5675 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +02005676 qc = NULL;
Amaury Denoyellefb375572023-02-01 09:28:32 +01005677
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005678 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
5679
5680 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5681}
5682
5683/* Initialize the timer task of <qc> QUIC connection.
5684 * Returns 1 if succeeded, 0 if not.
5685 */
5686static int quic_conn_init_timer(struct quic_conn *qc)
5687{
5688 int ret = 0;
5689 /* Attach this task to the same thread ID used for the connection */
5690 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5691
5692 qc->timer_task = task_new_on(qc->tid);
5693 if (!qc->timer_task) {
5694 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5695 goto leave;
5696 }
5697
5698 qc->timer = TICK_ETERNITY;
5699 qc->timer_task->process = qc_process_timer;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005700 qc->timer_task->context = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005701
5702 ret = 1;
5703 leave:
5704 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5705 return ret;
5706}
5707
Frédéric Lécailled7215712023-03-24 18:13:37 +01005708/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
5709 * connection. */
5710static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005711{
5712 unsigned int expire;
5713
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005714 if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005715 TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005716 qc->ack_expire = TICK_ETERNITY;
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005717 task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
5718 }
5719 else {
5720 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005721 qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
5722 if (arm_ack) {
5723 /* Arm the ack timer only if not already armed. */
5724 if (!tick_isset(qc->ack_expire)) {
5725 qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
5726 qc->idle_timer_task->expire = qc->ack_expire;
5727 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005728 TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005729 }
5730 }
5731 else {
5732 qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
5733 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005734 TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005735 }
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005736 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005737}
5738
Frédéric Lécailled7215712023-03-24 18:13:37 +01005739/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
5740 * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
5741 * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
5742 * which must be rearmed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005743 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01005744static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005745{
5746 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5747
5748 if (read) {
5749 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5750 }
5751 else {
5752 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5753 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01005754 qc_idle_timer_do_rearm(qc, arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005755
5756 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5757}
5758
5759/* The task handling the idle timeout */
5760struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
5761{
5762 struct quic_conn *qc = ctx;
5763 struct quic_counters *prx_counters = qc->prx_counters;
5764 unsigned int qc_flags = qc->flags;
5765
5766 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5767
Frédéric Lécaille12eca3a2023-04-04 10:46:54 +02005768 if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
5769 goto requeue;
5770
Frédéric Lécailled7215712023-03-24 18:13:37 +01005771 if (tick_is_expired(qc->ack_expire, now_ms)) {
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005772 TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005773 qc->ack_expire = TICK_ETERNITY;
5774 /* Note that ->idle_expire is always set. */
5775 t->expire = qc->idle_expire;
5776 qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
5777 tasklet_wakeup(qc->wait_event.tasklet);
5778 goto requeue;
5779 }
5780
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005781 TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005782 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
5783 * might free the quic-conn too early via quic_close().
5784 */
5785 qc_notify_close(qc);
5786
5787 /* If the MUX is still alive, keep the quic-conn. The MUX is
5788 * responsible to call quic_close to release it.
5789 */
5790 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005791 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005792 quic_conn_release(qc);
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005793 qc = NULL;
5794 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005795
5796 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
5797 * least clean some parts of it such as the tasklet.
5798 */
5799
5800 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
5801 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005802 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005803 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
5804 }
5805
Frédéric Lécailled7215712023-03-24 18:13:37 +01005806 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005807 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5808 return NULL;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005809
5810 requeue:
5811 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5812 return t;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005813}
5814
5815/* Initialize the idle timeout task for <qc>.
5816 * Returns 1 if succeeded, 0 if not.
5817 */
5818static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
5819{
5820 int ret = 0;
5821
5822 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5823
5824 qc->idle_timer_task = task_new_here();
5825 if (!qc->idle_timer_task) {
5826 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5827 goto leave;
5828 }
5829
5830 qc->idle_timer_task->process = qc_idle_timer_task;
5831 qc->idle_timer_task->context = qc;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005832 qc->ack_expire = TICK_ETERNITY;
5833 qc_idle_timer_rearm(qc, 1, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005834 task_queue(qc->idle_timer_task);
5835
5836 ret = 1;
5837 leave:
5838 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5839 return ret;
5840}
5841
5842/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
5843 * past one byte of this buffer.
5844 */
5845static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
5846 struct quic_rx_packet *pkt)
5847{
5848 int ret = 0;
5849 unsigned char dcid_len, scid_len;
5850
5851 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5852
5853 if (end == *buf) {
5854 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
5855 goto leave;
5856 }
5857
5858 /* Destination Connection ID Length */
5859 dcid_len = *(*buf)++;
5860 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
5861 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
5862 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
5863 goto leave;
5864 }
5865
5866 if (dcid_len) {
5867 /* Check that the length of this received DCID matches the CID lengths
5868 * of our implementation for non Initials packets only.
5869 */
5870 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
5871 pkt->type != QUIC_PACKET_TYPE_0RTT &&
5872 dcid_len != QUIC_HAP_CID_LEN) {
5873 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
5874 goto leave;
5875 }
5876
5877 memcpy(pkt->dcid.data, *buf, dcid_len);
5878 }
5879
5880 pkt->dcid.len = dcid_len;
5881 *buf += dcid_len;
5882
5883 /* Source Connection ID Length */
5884 scid_len = *(*buf)++;
5885 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
5886 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
5887 goto leave;
5888 }
5889
5890 if (scid_len)
5891 memcpy(pkt->scid.data, *buf, scid_len);
5892 pkt->scid.len = scid_len;
5893 *buf += scid_len;
5894
5895 ret = 1;
5896 leave:
5897 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
5898 return ret;
5899}
5900
5901/* Insert <pkt> RX packet in its <qel> RX packets tree */
5902static void qc_pkt_insert(struct quic_conn *qc,
5903 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
5904{
5905 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
5906
5907 pkt->pn_node.key = pkt->pn;
5908 quic_rx_packet_refinc(pkt);
5909 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
5910
5911 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
5912}
5913
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005914/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
5915 * address of the packet first byte, using the keys from encryption level <el>.
5916 *
5917 * If header protection has been successfully removed, packet data are copied
5918 * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
5919 * proceeded, and the packet is inserted into <qc> protected packets tree. In
5920 * both cases, packet can now be considered handled by the <qc> connection.
5921 *
5922 * If header protection cannot be removed due to <el> secrets already
5923 * discarded, no operation is conducted.
5924 *
5925 * Returns 1 on success : packet data is now handled by the connection. On
5926 * error 0 is returned : packet should be dropped by the caller.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005927 */
5928static inline int qc_try_rm_hp(struct quic_conn *qc,
5929 struct quic_rx_packet *pkt,
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005930 unsigned char *beg,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005931 struct quic_enc_level **el)
5932{
5933 int ret = 0;
5934 unsigned char *pn = NULL; /* Packet number field */
5935 enum quic_tls_enc_level tel;
5936 struct quic_enc_level *qel;
5937 /* Only for traces. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005938
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005939 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005940 BUG_ON(!pkt->pn_offset);
5941
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005942 /* The packet number is here. This is also the start minus
5943 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5944 * protection.
5945 */
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005946 pn = beg + pkt->pn_offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005947
5948 tel = quic_packet_type_enc_level(pkt->type);
5949 qel = &qc->els[tel];
5950
5951 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01005952 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
5953
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005954 /* Note that the following function enables us to unprotect the packet
5955 * number and its length subsequently used to decrypt the entire
5956 * packets.
5957 */
Frédéric Lécaille72027782023-02-22 16:20:09 +01005958 if (!qc_do_rm_hp(qc, pkt, tls_ctx,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005959 qel->pktns->rx.largest_pn, pn, beg)) {
5960 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
5961 goto out;
5962 }
5963
Frédéric Lécailleece86e62023-03-07 11:53:43 +01005964 qc_handle_spin_bit(qc, pkt, qel);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005965 /* The AAD includes the packet number field. */
5966 pkt->aad_len = pkt->pn_offset + pkt->pnl;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005967 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5968 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
5969 goto out;
5970 }
5971
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02005972 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005973 }
5974 else {
5975 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
5976 /* If the packet number space has been discarded, this packet
5977 * will be not parsed.
5978 */
5979 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
5980 goto out;
5981 }
5982
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005983 TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005984 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
5985 quic_rx_packet_refinc(pkt);
5986 }
5987
5988 *el = qel;
5989 /* No reference counter incrementation here!!! */
5990 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
5991 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
5992 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
5993 b_add(&qc->rx.buf, pkt->len);
5994
5995 ret = 1;
5996 out:
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02005997 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005998 return ret;
5999}
6000
6001/* Parse the header form from <byte0> first byte of <pkt> packet to set its type.
6002 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
6003 * of this packet into <*version>.
6004 */
6005static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
6006 unsigned char **buf, const unsigned char *end,
6007 int *long_header, uint32_t *version)
6008{
6009 int ret = 0;
6010 const unsigned char byte0 = **buf;
6011
6012 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
6013
6014 (*buf)++;
6015 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
6016 unsigned char type =
6017 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
6018
6019 *long_header = 1;
6020 /* Version */
6021 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
6022 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
6023 goto out;
6024 }
6025
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +01006026 if (*version != QUIC_PROTOCOL_VERSION_2) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006027 pkt->type = type;
6028 }
6029 else {
6030 switch (type) {
6031 case 0:
6032 pkt->type = QUIC_PACKET_TYPE_RETRY;
6033 break;
6034 case 1:
6035 pkt->type = QUIC_PACKET_TYPE_INITIAL;
6036 break;
6037 case 2:
6038 pkt->type = QUIC_PACKET_TYPE_0RTT;
6039 break;
6040 case 3:
6041 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
6042 break;
6043 }
6044 }
6045 }
6046 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006047 if (byte0 & QUIC_PACKET_SPIN_BIT)
6048 pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006049 pkt->type = QUIC_PACKET_TYPE_SHORT;
6050 *long_header = 0;
6051 }
6052
6053 ret = 1;
6054 out:
6055 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6056 return ret;
6057}
6058
6059/* Return the QUIC version (quic_version struct) with <version> as version number
6060 * if supported or NULL if not.
6061 */
6062static inline const struct quic_version *qc_supported_version(uint32_t version)
6063{
6064 int i;
6065
6066 for (i = 0; i < quic_versions_nb; i++)
6067 if (quic_versions[i].num == version)
6068 return &quic_versions[i];
6069
6070 return NULL;
6071}
6072
6073/*
6074 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
6075 * address <addr>.
6076 * Implementation of RFC9000 6. Version Negotiation
6077 *
6078 * TODO implement a rate-limiting sending of Version Negotiation packets
6079 *
6080 * Returns 0 on success else non-zero
6081 */
6082static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
6083 struct quic_rx_packet *pkt)
6084{
6085 char buf[256];
6086 int ret = 0, i = 0, j;
6087 uint32_t version;
6088 const socklen_t addrlen = get_addr_len(addr);
6089
6090 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6091 /*
6092 * header form
6093 * long header, fixed bit to 0 for Version Negotiation
6094 */
6095 /* TODO: RAND_bytes() should be replaced? */
6096 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
6097 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
6098 goto out;
6099 }
6100
6101 buf[i++] |= '\x80';
6102 /* null version for Version Negotiation */
6103 buf[i++] = '\x00';
6104 buf[i++] = '\x00';
6105 buf[i++] = '\x00';
6106 buf[i++] = '\x00';
6107
6108 /* source connection id */
6109 buf[i++] = pkt->scid.len;
6110 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6111 i += pkt->scid.len;
6112
6113 /* destination connection id */
6114 buf[i++] = pkt->dcid.len;
6115 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
6116 i += pkt->dcid.len;
6117
6118 /* supported version */
6119 for (j = 0; j < quic_versions_nb; j++) {
6120 version = htonl(quic_versions[j].num);
6121 memcpy(&buf[i], &version, sizeof(version));
6122 i += sizeof(version);
6123 }
6124
6125 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
6126 goto out;
6127
6128 ret = 1;
6129 out:
6130 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6131 return !ret;
6132}
6133
6134/* Send a stateless reset packet depending on <pkt> RX packet information
6135 * from <fd> UDP socket to <dst>
6136 * Return 1 if succeeded, 0 if not.
6137 */
6138static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
6139 struct quic_rx_packet *rxpkt)
6140{
6141 int ret = 0, pktlen, rndlen;
6142 unsigned char pkt[64];
6143 const socklen_t addrlen = get_addr_len(dstaddr);
6144 struct proxy *prx;
6145 struct quic_counters *prx_counters;
6146
6147 TRACE_ENTER(QUIC_EV_STATELESS_RST);
6148
6149 prx = l->bind_conf->frontend;
6150 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6151 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
6152 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
6153 * Reset is difficult to distinguish from other packets if the recipient requires
6154 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
6155 * that all packets it sends are at least 22 bytes longer than the minimum
6156 * connection ID length that it requests the peer to include in its packets,
6157 * adding PADDING frames as necessary. This ensures that any Stateless Reset
6158 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
6159 * An endpoint that sends a Stateless Reset in response to a packet that is
6160 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
6161 * than the packet it responds to.
6162 */
6163
6164 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
6165 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
6166 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
6167 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
6168
6169 /* Put a header of random bytes */
6170 /* TODO: RAND_bytes() should be replaced */
6171 if (RAND_bytes(pkt, rndlen) != 1) {
6172 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
6173 goto leave;
6174 }
6175
6176 /* Clear the most significant bit, and set the second one */
6177 *pkt = (*pkt & ~0x80) | 0x40;
6178 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
6179 rxpkt->dcid.data, rxpkt->dcid.len))
6180 goto leave;
6181
6182 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
6183 goto leave;
6184
6185 ret = 1;
6186 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
6187 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
6188 leave:
6189 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
6190 return ret;
6191}
6192
6193/* QUIC server only function.
6194 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
6195 * This is the responsibility of the caller to check <aad> size is big enough
6196 * to contain these data.
6197 * Return the number of bytes copied to <aad>.
6198 */
6199static int quic_generate_retry_token_aad(unsigned char *aad,
6200 uint32_t version,
6201 const struct quic_cid *cid,
6202 const struct sockaddr_storage *addr)
6203{
6204 unsigned char *p;
6205
6206 p = aad;
6207 memcpy(p, &version, sizeof version);
6208 p += sizeof version;
6209 p += quic_saddr_cpy(p, addr);
6210 memcpy(p, cid->data, cid->len);
6211 p += cid->len;
6212
6213 return p - aad;
6214}
6215
6216/* QUIC server only function.
6217 * Generate the token to be used in Retry packets. The token is written to
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05006218 * <buf> with <len> as length. <odcid> is the original destination connection
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006219 * ID and <dcid> is our side destination connection ID (or client source
6220 * connection ID).
6221 * Returns the length of the encoded token or 0 on error.
6222 */
6223static int quic_generate_retry_token(unsigned char *buf, size_t len,
6224 const uint32_t version,
6225 const struct quic_cid *odcid,
6226 const struct quic_cid *dcid,
6227 struct sockaddr_storage *addr)
6228{
6229 int ret = 0;
6230 unsigned char *p;
6231 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006232 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006233 size_t aadlen;
6234 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
6235 unsigned char key[QUIC_TLS_KEY_LEN];
6236 unsigned char iv[QUIC_TLS_IV_LEN];
6237 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6238 size_t seclen = strlen(global.cluster_secret);
6239 EVP_CIPHER_CTX *ctx = NULL;
6240 const EVP_CIPHER *aead = EVP_aes_128_gcm();
6241 uint32_t timestamp = now_ms;
6242
6243 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6244
6245 /* We copy the odcid into the token, prefixed by its one byte
6246 * length, the format token byte. It is followed by an AEAD TAG, and finally
6247 * the random bytes used to derive the secret to encrypt the token.
6248 */
6249 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
6250 goto err;
6251
6252 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
6253 /* TODO: RAND_bytes() should be replaced */
6254 if (RAND_bytes(salt, sizeof salt) != 1) {
6255 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
6256 goto err;
6257 }
6258
6259 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6260 salt, sizeof salt, sec, seclen)) {
6261 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
6262 goto err;
6263 }
6264
6265 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
6266 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
6267 goto err;
6268 }
6269
6270 /* Token build */
6271 p = buf;
6272 *p++ = QUIC_TOKEN_FMT_RETRY,
6273 *p++ = odcid->len;
6274 memcpy(p, odcid->data, odcid->len);
6275 p += odcid->len;
6276 write_u32(p, htonl(timestamp));
6277 p += sizeof timestamp;
6278
6279 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
6280 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
6281 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
6282 goto err;
6283 }
6284
6285 p += QUIC_TLS_TAG_LEN;
6286 memcpy(p, salt, sizeof salt);
6287 p += sizeof salt;
6288 EVP_CIPHER_CTX_free(ctx);
6289
6290 ret = p - buf;
6291 leave:
6292 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6293 return ret;
6294
6295 err:
6296 if (ctx)
6297 EVP_CIPHER_CTX_free(ctx);
6298 goto leave;
6299}
6300
6301/* QUIC server only function.
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006302 *
6303 * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
6304 * the UDP datagram containing <pkt> and <l> is the listener instance on which
6305 * it was received. If the token is valid, the ODCID of <qc> QUIC connection
6306 * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
6307 * to validate the token but it can be NULL : in this case the version will be
6308 * retrieved from the packet.
6309 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006310 * Return 1 if succeeded, 0 if not.
6311 */
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006312
6313static int quic_retry_token_check(struct quic_rx_packet *pkt,
6314 struct quic_dgram *dgram,
6315 struct listener *l,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006316 struct quic_conn *qc,
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006317 struct quic_cid *odcid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006318{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006319 struct proxy *prx;
6320 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006321 int ret = 0;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006322 unsigned char *token = pkt->token;
6323 const uint64_t tokenlen = pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006324 unsigned char buf[128];
6325 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006326 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006327 size_t aadlen;
6328 const unsigned char *salt;
6329 unsigned char key[QUIC_TLS_KEY_LEN];
6330 unsigned char iv[QUIC_TLS_IV_LEN];
6331 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6332 size_t seclen = strlen(global.cluster_secret);
6333 EVP_CIPHER_CTX *ctx = NULL;
6334 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006335 const struct quic_version *qv = qc ? qc->original_version :
6336 pkt->version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006337
6338 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6339
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006340 /* The caller must ensure this. */
6341 BUG_ON(!global.cluster_secret || !pkt->token_len);
6342
6343 prx = l->bind_conf->frontend;
6344 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6345
6346 if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
6347 /* TODO: New token check */
6348 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6349 goto leave;
6350 }
6351
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006352 if (sizeof buf < tokenlen) {
6353 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
6354 goto err;
6355 }
6356
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006357 aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006358 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
6359 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6360 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
6361 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
6362 goto err;
6363 }
6364
6365 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
6366 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
6367 goto err;
6368 }
6369
6370 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
6371 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
6372 ctx, aead, key, iv)) {
6373 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
6374 goto err;
6375 }
6376
6377 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
6378 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
6379 goto err;
6380 }
6381
6382 EVP_CIPHER_CTX_free(ctx);
6383
6384 ret = 1;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006385 HA_ATOMIC_INC(&prx_counters->retry_validated);
6386
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006387 leave:
6388 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6389 return ret;
6390
6391 err:
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006392 HA_ATOMIC_INC(&prx_counters->retry_error);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006393 if (ctx)
6394 EVP_CIPHER_CTX_free(ctx);
6395 goto leave;
6396}
6397
6398/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
6399 * the Initial <pkt> packet.
6400 *
6401 * Returns 0 on success else non-zero.
6402 */
6403static int send_retry(int fd, struct sockaddr_storage *addr,
6404 struct quic_rx_packet *pkt, const struct quic_version *qv)
6405{
6406 int ret = 0;
6407 unsigned char buf[128];
6408 int i = 0, token_len;
6409 const socklen_t addrlen = get_addr_len(addr);
6410 struct quic_cid scid;
6411
6412 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6413
6414 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
6415 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
6416 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
6417 /* version */
6418 buf[i++] = *((unsigned char *)&qv->num + 3);
6419 buf[i++] = *((unsigned char *)&qv->num + 2);
6420 buf[i++] = *((unsigned char *)&qv->num + 1);
6421 buf[i++] = *(unsigned char *)&qv->num;
6422
6423 /* Use the SCID from <pkt> for Retry DCID. */
6424 buf[i++] = pkt->scid.len;
6425 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6426 i += pkt->scid.len;
6427
6428 /* Generate a new CID to be used as SCID for the Retry packet. */
6429 scid.len = QUIC_HAP_CID_LEN;
6430 /* TODO: RAND_bytes() should be replaced */
6431 if (RAND_bytes(scid.data, scid.len) != 1) {
6432 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
6433 goto out;
6434 }
6435
6436 buf[i++] = scid.len;
6437 memcpy(&buf[i], scid.data, scid.len);
6438 i += scid.len;
6439
6440 /* token */
6441 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
6442 &pkt->dcid, &pkt->scid, addr))) {
6443 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
6444 goto out;
6445 }
6446
6447 i += token_len;
6448
6449 /* token integrity tag */
6450 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
6451 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
6452 pkt->dcid.len, buf, i, qv)) {
6453 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6454 goto out;
6455 }
6456
6457 i += QUIC_TLS_TAG_LEN;
6458
6459 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
6460 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6461 goto out;
6462 }
6463
6464 ret = 1;
6465 out:
6466 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6467 return !ret;
6468}
6469
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006470/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
6471 * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
6472 * is used.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006473 *
6474 * Returns the instance or NULL if not found.
6475 */
6476static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
6477 struct listener *l,
6478 struct sockaddr_storage *saddr)
6479{
6480 struct quic_conn *qc = NULL;
6481 struct ebmb_node *node;
6482 struct quic_connection_id *id;
6483 /* set if the quic_conn is found in the second DCID tree */
6484
6485 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
6486
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006487 /* First look into DCID tree. */
6488 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006489
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006490 /* If not found on an Initial/0-RTT packet, it could be because an
6491 * ODCID is reused by the client. Calculate the derived CID value to
6492 * retrieve it from the DCID tree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006493 */
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006494 if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
6495 pkt->type == QUIC_PACKET_TYPE_0RTT)) {
6496 uint64_t hash = quic_derive_cid(&pkt->dcid, saddr);
6497 node = ebmb_lookup(&quic_dghdlrs[tid].cids, &hash, sizeof(hash));
6498 }
6499
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006500 if (!node)
6501 goto end;
6502
6503 id = ebmb_entry(node, struct quic_connection_id, node);
6504 qc = id->qc;
6505
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006506 end:
6507 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6508 return qc;
6509}
6510
6511/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
6512 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
6513 * parameters of this session.
6514 * This is the responsibility of the caller to check the validity of all the
6515 * pointers passed as parameter to this function.
6516 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
6517 * CO_ER_SSL_NO_MEM.
6518 */
6519static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
6520 unsigned char *params, size_t params_len)
6521{
6522 int retry, ret = -1;
6523
6524 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6525
6526 retry = 1;
6527 retry:
6528 *ssl = SSL_new(ssl_ctx);
6529 if (!*ssl) {
6530 if (!retry--)
6531 goto err;
6532
6533 pool_gc(NULL);
6534 goto retry;
6535 }
6536
6537 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
6538 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
6539 SSL_free(*ssl);
6540 *ssl = NULL;
6541 if (!retry--)
6542 goto err;
6543
6544 pool_gc(NULL);
6545 goto retry;
6546 }
6547
6548 ret = 0;
6549 leave:
6550 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6551 return ret;
6552
6553 err:
6554 qc->conn->err_code = CO_ER_SSL_NO_MEM;
6555 goto leave;
6556}
6557
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006558/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
6559 * used to process <qc> received packets. The allocated context is stored in
6560 * <qc.xprt_ctx>.
6561 *
6562 * Returns 0 on success else non-zero.
6563 */
6564static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
6565{
6566 int ret = 0;
6567 struct bind_conf *bc = qc->li->bind_conf;
6568 struct ssl_sock_ctx *ctx = NULL;
6569
6570 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6571
6572 ctx = pool_zalloc(pool_head_quic_conn_ctx);
6573 if (!ctx) {
6574 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
6575 goto err;
6576 }
6577
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006578 ctx->subs = NULL;
6579 ctx->xprt_ctx = NULL;
6580 ctx->qc = qc;
6581
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006582 if (qc_is_listener(qc)) {
6583 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
6584 qc->enc_params, qc->enc_params_len) == -1) {
6585 goto err;
6586 }
6587#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6588 /* Enabling 0-RTT */
6589 if (bc->ssl_conf.early_data)
6590 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
6591#endif
6592
6593 SSL_set_accept_state(ctx->ssl);
6594 }
6595
6596 ctx->xprt = xprt_get(XPRT_QUIC);
6597
6598 /* Store the allocated context in <qc>. */
6599 qc->xprt_ctx = ctx;
6600
6601 ret = 1;
6602 leave:
6603 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6604 return !ret;
6605
6606 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006607 pool_free(pool_head_quic_conn_ctx, ctx);
6608 goto leave;
6609}
6610
6611/* Check that all the bytes between <buf> included and <end> address
6612 * excluded are null. This is the responsibility of the caller to
6613 * check that there is at least one byte between <buf> end <end>.
6614 * Return 1 if this all the bytes are null, 0 if not.
6615 */
6616static inline int quic_padding_check(const unsigned char *buf,
6617 const unsigned char *end)
6618{
6619 while (buf < end && !*buf)
6620 buf++;
6621
6622 return buf == end;
6623}
6624
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006625/* Find the associated connection to the packet <pkt> or create a new one if
6626 * this is an Initial packet. <dgram> is the datagram containing the packet and
6627 * <l> is the listener instance on which it was received.
6628 *
6629 * Returns the quic-conn instance or NULL.
6630 */
6631static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
6632 struct quic_dgram *dgram,
6633 struct listener *l)
6634{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006635 struct quic_cid token_odcid = { .len = 0 };
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006636 struct quic_conn *qc = NULL;
6637 struct proxy *prx;
6638 struct quic_counters *prx_counters;
6639
6640 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6641
6642 prx = l->bind_conf->frontend;
6643 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6644
6645 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
6646
6647 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6648 BUG_ON(!pkt->version); /* This must not happen. */
6649
6650 if (global.cluster_secret && pkt->token_len) {
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006651 if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
6652 goto err;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006653 }
6654
6655 if (!qc) {
6656 int ipv4;
6657
6658 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
6659 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
6660 TRACE_PROTO("Initial without token, sending retry",
6661 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6662 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
6663 TRACE_ERROR("Error during Retry generation",
6664 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6665 goto out;
6666 }
6667
6668 HA_ATOMIC_INC(&prx_counters->retry_sent);
6669 goto out;
6670 }
6671
6672 /* RFC 9000 7.2. Negotiating Connection IDs:
6673 * When an Initial packet is sent by a client that has not previously
6674 * received an Initial or Retry packet from the server, the client
6675 * populates the Destination Connection ID field with an unpredictable
6676 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6677 */
6678 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
6679 TRACE_PROTO("dropped packet",
6680 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6681 goto err;
6682 }
6683
6684 pkt->saddr = dgram->saddr;
6685 ipv4 = dgram->saddr.ss_family == AF_INET;
6686
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006687 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006688 &dgram->daddr, &pkt->saddr, 1,
6689 !!pkt->token_len, l);
6690 if (qc == NULL)
6691 goto err;
6692
6693 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006694 }
6695 }
6696 else if (!qc) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006697 TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006698 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
6699 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
6700 goto err;
6701 }
6702
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006703 out:
6704 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6705 return qc;
6706
6707 err:
6708 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
6709 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
6710 return NULL;
6711}
6712
Amaury Denoyelle98289692022-10-19 15:37:44 +02006713/* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even
6714 * if the packet is incomplete. This function will populate fields of <pkt>
6715 * instance, most notably its length. <dgram> is the UDP datagram which
6716 * contains the parsed packet. <l> is the listener instance on which it was
6717 * received.
6718 *
6719 * Returns 0 on success else non-zero. Packet length is guaranteed to be set to
6720 * the real packet value or to cover all data between <buf> and <end> : this is
6721 * useful to reject a whole datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006722 */
Amaury Denoyelle98289692022-10-19 15:37:44 +02006723static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
6724 unsigned char *buf, const unsigned char *end,
6725 struct quic_dgram *dgram, struct listener *l)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006726{
Amaury Denoyelle98289692022-10-19 15:37:44 +02006727 const unsigned char *beg = buf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006728 struct proxy *prx;
6729 struct quic_counters *prx_counters;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006730 int long_header = 0;
Willy Tarreau33a68702022-11-24 09:16:41 +01006731 uint32_t version = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006732 const struct quic_version *qv = NULL;
6733
6734 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6735
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006736 prx = l->bind_conf->frontend;
6737 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6738 /* This ist only to please to traces and distinguish the
6739 * packet with parsed packet number from others.
6740 */
6741 pkt->pn_node.key = (uint64_t)-1;
6742 if (end <= buf) {
6743 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6744 goto drop;
6745 }
6746
6747 /* Fixed bit */
6748 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006749 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
6750 quic_padding_check(buf, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006751 /* Some browsers may pad the remaining datagram space with null bytes.
6752 * That is what we called add padding out of QUIC packets. Such
6753 * datagrams must be considered as valid. But we can only consume
6754 * the remaining space.
6755 */
6756 pkt->len = end - buf;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006757 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006758 }
6759
6760 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6761 goto drop;
6762 }
6763
6764 /* Header form */
6765 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
6766 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6767 goto drop;
6768 }
6769
6770 if (long_header) {
6771 uint64_t len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006772
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006773 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006774 if (!quic_packet_read_long_header(&buf, end, pkt)) {
6775 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6776 goto drop;
6777 }
6778
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006779 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6780 * they must have the same DCID.
6781 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006782 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006783 (pkt->dcid.len != dgram->dcid_len ||
6784 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006785 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006786 goto drop;
6787 }
6788
6789 /* Retry of Version Negotiation packets are only sent by servers */
6790 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
6791 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6792 goto drop;
6793 }
6794
6795 /* RFC9000 6. Version Negotiation */
6796 qv = qc_supported_version(version);
6797 if (!qv) {
6798 /* unsupported version, send Negotiation packet */
6799 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
6800 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006801 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006802 }
6803
6804 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006805 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006806 }
Amaury Denoyelle0eae5722022-10-17 18:05:18 +02006807 pkt->version = qv;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006808
6809 /* For Initial packets, and for servers (QUIC clients connections),
6810 * there is no Initial connection IDs storage.
6811 */
6812 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6813 uint64_t token_len;
6814
6815 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
6816 end - buf < token_len) {
6817 TRACE_PROTO("Packet dropped",
6818 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6819 goto drop;
6820 }
6821
6822 /* TODO Retry should be automatically activated if
6823 * suspect network usage is detected.
6824 */
6825 if (global.cluster_secret && !token_len) {
6826 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
6827 TRACE_PROTO("Initial without token, sending retry",
Amaury Denoyelle90121b32022-09-27 10:35:29 +02006828 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006829 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
6830 TRACE_PROTO("Error during Retry generation",
Amaury Denoyelle90121b32022-09-27 10:35:29 +02006831 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006832 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006833 }
6834
6835 HA_ATOMIC_INC(&prx_counters->retry_sent);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006836 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006837 }
6838 }
6839 else if (!global.cluster_secret && token_len) {
6840 /* Impossible case: a token was received without configured
6841 * cluster secret.
6842 */
6843 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
6844 NULL, NULL, NULL, qv);
6845 goto drop;
6846 }
6847
6848 pkt->token = buf;
6849 pkt->token_len = token_len;
6850 buf += pkt->token_len;
6851 }
6852 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
6853 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
6854 TRACE_PROTO("Packet dropped",
6855 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6856 goto drop;
6857 }
6858 }
6859
6860 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
6861 end - buf < len) {
6862 TRACE_PROTO("Packet dropped",
6863 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6864 goto drop;
6865 }
6866
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006867 /* Packet Number is stored here. Packet Length totalizes the
6868 * rest of the content.
6869 */
6870 pkt->pn_offset = buf - beg;
6871 pkt->len = pkt->pn_offset + len;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006872
Frédéric Lécaille35218c62023-02-16 11:40:11 +01006873 /* RFC 9000. Initial Datagram Size
6874 *
6875 * A server MUST discard an Initial packet that is carried in a UDP datagram
6876 * with a payload that is smaller than the smallest allowed maximum datagram
6877 * size of 1200 bytes.
6878 */
6879 if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
6880 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006881 TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille35218c62023-02-16 11:40:11 +01006882 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
6883 goto drop;
6884 }
6885
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006886 /* Interrupt parsing after packet length retrieval : this
6887 * ensures that only the packet is dropped but not the whole
6888 * datagram.
6889 */
6890 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006891 TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006892 goto drop;
6893 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006894 }
6895 else {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006896 TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006897 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006898 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006899 goto drop;
6900 }
6901
6902 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
6903 pkt->dcid.len = QUIC_HAP_CID_LEN;
6904
6905 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6906 * they must have the same DCID.
6907 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006908 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006909 (pkt->dcid.len != dgram->dcid_len ||
6910 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006911 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006912 goto drop;
6913 }
6914
6915 buf += QUIC_HAP_CID_LEN;
6916
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006917 pkt->pn_offset = buf - beg;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006918 /* A short packet is the last one of a UDP datagram. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006919 pkt->len = end - beg;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006920 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006921
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006922 TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv);
6923 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02006924 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006925
Amaury Denoyelle98289692022-10-19 15:37:44 +02006926 drop:
6927 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006928 drop_silent:
Amaury Denoyelle98289692022-10-19 15:37:44 +02006929 if (!pkt->len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006930 pkt->len = end - beg;
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006931 TRACE_PROTO("RX pkt parsing failed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv);
6932 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02006933 return -1;
6934}
6935
6936/* Check if received packet <pkt> should be drop due to <qc> already in closing
6937 * state. This can be true if a CONNECTION_CLOSE has already been emitted for
6938 * this connection.
6939 *
6940 * Returns false if connection is not in closing state else true. The caller
6941 * should drop the whole datagram in the last case to not mess up <qc>
6942 * CONNECTION_CLOSE rate limit counter.
6943 */
6944static int qc_rx_check_closing(struct quic_conn *qc,
6945 struct quic_rx_packet *pkt)
6946{
6947 if (!(qc->flags & QUIC_FL_CONN_CLOSING))
6948 return 0;
6949
6950 TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6951
6952 /* Check if CONNECTION_CLOSE rate reemission is reached. */
6953 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6954 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6955 qc->nb_pkt_for_cc++;
6956 qc->nb_pkt_since_cc = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006957 }
6958
Amaury Denoyelle98289692022-10-19 15:37:44 +02006959 return 1;
6960}
6961
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01006962/* React to a connection migration initiated on <qc> by a client with the new
6963 * path addresses <peer_addr>/<local_addr>.
6964 *
6965 * Returns 0 on success else non-zero.
6966 */
6967static int qc_handle_conn_migration(struct quic_conn *qc,
6968 const struct sockaddr_storage *peer_addr,
6969 const struct sockaddr_storage *local_addr)
6970{
6971 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6972
Frédéric Lécaille6fc86972023-01-12 08:29:23 +01006973 /* RFC 9000. Connection Migration
6974 *
6975 * If the peer sent the disable_active_migration transport parameter,
6976 * an endpoint also MUST NOT send packets (including probing packets;
6977 * see Section 9.1) from a different local address to the address the peer
6978 * used during the handshake, unless the endpoint has acted on a
6979 * preferred_address transport parameter from the peer.
6980 */
6981 if (qc->li->bind_conf->quic_params.disable_active_migration) {
6982 TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
6983 goto err;
6984 }
6985
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01006986 /* RFC 9000 9. Connection Migration
6987 *
Amaury Denoyelleeb6be982022-11-21 11:14:45 +01006988 * The design of QUIC relies on endpoints retaining a stable address for
6989 * the duration of the handshake. An endpoint MUST NOT initiate
6990 * connection migration before the handshake is confirmed, as defined in
6991 * Section 4.1.2 of [QUIC-TLS].
6992 */
6993 if (qc->state < QUIC_HS_ST_COMPLETE) {
6994 TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
6995 goto err;
6996 }
6997
6998 /* RFC 9000 9. Connection Migration
6999 *
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007000 * TODO
7001 * An endpoint MUST
7002 * perform path validation (Section 8.2) if it detects any change to a
7003 * peer's address, unless it has previously validated that address.
7004 */
7005
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007006 /* Update quic-conn owned socket if in used.
7007 * TODO try to reuse it instead of closing and opening a new one.
7008 */
7009 if (qc_test_fd(qc)) {
7010 /* TODO try to reuse socket instead of closing it and opening a new one. */
7011 TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
7012 qc_release_fd(qc, 1);
Amaury Denoyellefb375572023-02-01 09:28:32 +01007013 /* TODO need to adjust <jobs> on socket allocation failure. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007014 qc_alloc_fd(qc, local_addr, peer_addr);
7015 }
7016
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007017 qc->local_addr = *local_addr;
7018 qc->peer_addr = *peer_addr;
7019 HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done);
7020
7021 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7022 return 0;
7023
7024 err:
7025 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7026 return 1;
7027}
7028
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007029/* Release the memory for the RX packets which are no more referenced
7030 * and consume their payloads which have been copied to the RX buffer
7031 * for the connection.
7032 * Always succeeds.
7033 */
7034static inline void quic_rx_pkts_del(struct quic_conn *qc)
7035{
7036 struct quic_rx_packet *pkt, *pktback;
7037
7038 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
7039 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007040 "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007041 (long long)pkt->pn_node.key,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007042 pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007043 (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
7044 if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
7045 size_t cdata;
7046
7047 cdata = b_contig_data(&qc->rx.buf, 0);
7048 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007049 "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007050 if (cdata && !*b_head(&qc->rx.buf)) {
7051 /* Consume the remaining data */
7052 b_del(&qc->rx.buf, cdata);
7053 }
7054 break;
7055 }
7056
7057 if (pkt->refcnt)
7058 break;
7059
7060 b_del(&qc->rx.buf, pkt->raw_len);
7061 LIST_DELETE(&pkt->qc_rx_pkt_list);
7062 pool_free(pool_head_quic_rx_packet, pkt);
7063 }
7064
7065 /* In frequent cases the buffer will be emptied at this stage. */
7066 b_realign_if_empty(&qc->rx.buf);
7067}
7068
Amaury Denoyelle98289692022-10-19 15:37:44 +02007069/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
7070 * into <qc> receive buffer after header protection removal procedure.
7071 *
7072 * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
7073 * must point to packet buffer first byte.
7074 *
7075 * <tasklist_head> may be non-NULL when the caller treat several datagrams for
7076 * different quic-conn. In this case, each quic-conn tasklet will be appended
7077 * to it in order to be woken up after the current task.
7078 *
7079 * The caller can safely removed the packet data. If packet refcount was not
7080 * incremented by this function, it means that the connection did not handled
7081 * it and it should be freed by the caller.
7082 */
7083static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
7084 struct quic_dgram *dgram, unsigned char *beg,
7085 struct list **tasklist_head)
7086{
7087 const struct quic_version *qv = pkt->version;
7088 struct quic_enc_level *qel = NULL;
7089 size_t b_cspace;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007090
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007091 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7092 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Amaury Denoyelle3f474e62022-11-24 17:15:08 +01007093
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007094 if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007095 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
7096 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
7097 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007098 TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
7099 QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007100 /* Reset the anti-amplification bit. It will be set again
7101 * when sending the next packet if reached again.
7102 */
7103 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007104 qc_set_timer(qc);
7105 if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
7106 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007107 }
7108
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007109 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
7110 TRACE_PROTO("Connection error",
7111 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7112 goto out;
7113 }
7114
7115 pkt->raw_len = pkt->len;
7116 quic_rx_pkts_del(qc);
7117 b_cspace = b_contig_space(&qc->rx.buf);
7118 if (b_cspace < pkt->len) {
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007119 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007120 "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007121 /* Do not consume buf if space not at the end. */
7122 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
7123 TRACE_PROTO("Packet dropped",
7124 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007125 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007126 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007127 }
7128
7129 /* Let us consume the remaining contiguous space. */
7130 if (b_cspace) {
7131 b_putchr(&qc->rx.buf, 0x00);
7132 b_cspace--;
7133 }
7134 b_add(&qc->rx.buf, b_cspace);
7135 if (b_contig_space(&qc->rx.buf) < pkt->len) {
7136 TRACE_PROTO("Too big packet",
7137 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007138 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007139 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007140 }
7141 }
7142
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007143 if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007144 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7145 goto drop;
7146 }
7147
7148 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7149 if (pkt->aad_len)
7150 qc_pkt_insert(qc, pkt, qel);
7151 out:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02007152 *tasklist_head = tasklet_wakeup_after(*tasklist_head,
7153 qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007154
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007155 drop_silent:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007156 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7157 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007158 return;
7159
7160 drop:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007161 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007162 TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7163 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007164}
7165
7166/* This function builds into <buf> buffer a QUIC long packet header.
7167 * Return 1 if enough room to build this header, 0 if not.
7168 */
7169static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
7170 int type, size_t pn_len,
7171 struct quic_conn *qc, const struct quic_version *ver)
7172{
7173 int ret = 0;
7174
7175 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7176
7177 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
7178 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7179 goto leave;
7180 }
7181
7182 type = quic_pkt_type(type, ver->num);
7183 /* #0 byte flags */
7184 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
7185 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
7186 /* Version */
7187 quic_write_uint32(buf, end, ver->num);
7188 *(*buf)++ = qc->dcid.len;
7189 /* Destination connection ID */
7190 if (qc->dcid.len) {
7191 memcpy(*buf, qc->dcid.data, qc->dcid.len);
7192 *buf += qc->dcid.len;
7193 }
7194 /* Source connection ID */
7195 *(*buf)++ = qc->scid.len;
7196 if (qc->scid.len) {
7197 memcpy(*buf, qc->scid.data, qc->scid.len);
7198 *buf += qc->scid.len;
7199 }
7200
7201 ret = 1;
7202 leave:
7203 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7204 return ret;
7205}
7206
7207/* This function builds into <buf> buffer a QUIC short packet header.
7208 * Return 1 if enough room to build this header, 0 if not.
7209 */
7210static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
7211 size_t pn_len, struct quic_conn *qc,
7212 unsigned char tls_flags)
7213{
7214 int ret = 0;
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007215 unsigned char spin_bit =
7216 (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007217
7218 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7219
7220 if (end - *buf < 1 + qc->dcid.len) {
7221 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7222 goto leave;
7223 }
7224
7225 /* #0 byte flags */
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007226 *(*buf)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007227 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
7228 /* Destination connection ID */
7229 if (qc->dcid.len) {
7230 memcpy(*buf, qc->dcid.data, qc->dcid.len);
7231 *buf += qc->dcid.len;
7232 }
7233
7234 ret = 1;
7235 leave:
7236 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7237 return ret;
7238}
7239
7240/* Apply QUIC header protection to the packet with <buf> as first byte address,
7241 * <pn> as address of the Packet number field, <pnlen> being this field length
7242 * with <aead> as AEAD cipher and <key> as secret key.
7243 * Returns 1 if succeeded or 0 if failed.
7244 */
7245static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
7246 unsigned char *pn, size_t pnlen,
7247 struct quic_tls_ctx *tls_ctx)
7248
7249{
7250 int i, ret = 0;
7251 /* We need an IV of at least 5 bytes: one byte for bytes #0
7252 * and at most 4 bytes for the packet number
7253 */
7254 unsigned char mask[5] = {0};
7255 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
7256
7257 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7258
7259 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
7260 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
7261 goto out;
7262 }
7263
7264 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
7265 for (i = 0; i < pnlen; i++)
7266 pn[i] ^= mask[i + 1];
7267
7268 ret = 1;
7269 out:
7270 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7271 return ret;
7272}
7273
7274/* Reduce the encoded size of <ack_frm> ACK frame removing the last
7275 * ACK ranges if needed to a value below <limit> in bytes.
7276 * Return 1 if succeeded, 0 if not.
7277 */
7278static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
7279 struct quic_frame *ack_frm, size_t limit)
7280{
7281 size_t room, ack_delay_sz;
7282 int ret = 0;
7283
7284 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7285
7286 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
7287 /* A frame is made of 1 byte for the frame type. */
7288 room = limit - ack_delay_sz - 1;
7289 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
7290 goto leave;
7291
7292 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
7293 leave:
7294 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7295 return ret;
7296}
7297
7298/* Prepare into <outlist> as most as possible ack-eliciting frame from their
7299 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
7300 * with <room> as available room, and <*len> the packet Length field initialized
7301 * with the number of bytes already present in this buffer which must be taken
7302 * into an account for the Length packet field value. <headlen> is the number of
7303 * bytes already present in this packet before building frames.
7304 *
7305 * Update consequently <*len> to reflect the size of these frames built
7306 * by this function. Also attach these frames to <l> frame list.
7307 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
7308 */
7309static inline int qc_build_frms(struct list *outlist, struct list *inlist,
7310 size_t room, size_t *len, size_t headlen,
7311 struct quic_enc_level *qel,
7312 struct quic_conn *qc)
7313{
7314 int ret;
7315 struct quic_frame *cf, *cfbak;
7316
7317 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
7318
7319 ret = 0;
7320 if (*len > room)
7321 goto leave;
7322
7323 /* If we are not probing we must take into an account the congestion
7324 * control window.
7325 */
7326 if (!qel->pktns->tx.pto_probe) {
7327 size_t remain = quic_path_prep_data(qc->path);
7328
7329 if (headlen > remain)
7330 goto leave;
7331
7332 room = QUIC_MIN(room, remain - headlen);
7333 }
7334
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007335 TRACE_PROTO("TX frms build (headlen)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007336 QUIC_EV_CONN_BCFRMS, qc, &headlen);
7337
7338 /* NOTE: switch/case block inside a loop, a successful status must be
7339 * returned by this function only if at least one frame could be built
7340 * in the switch/case block.
7341 */
7342 list_for_each_entry_safe(cf, cfbak, inlist, list) {
7343 /* header length, data length, frame length. */
7344 size_t hlen, dlen, dlen_sz, avail_room, flen;
7345
7346 if (!room)
7347 break;
7348
7349 switch (cf->type) {
7350 case QUIC_FT_CRYPTO:
7351 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
7352 QUIC_EV_CONN_BCFRMS, qc, &room, len);
7353 /* Compute the length of this CRYPTO frame header */
7354 hlen = 1 + quic_int_getsize(cf->crypto.offset);
7355 /* Compute the data length of this CRyPTO frame. */
7356 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
7357 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
7358 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
7359 if (!dlen)
7360 continue;
7361
7362 /* CRYPTO frame length. */
7363 flen = hlen + quic_int_getsize(dlen) + dlen;
7364 TRACE_DEVEL(" CRYPTO frame length (flen)",
7365 QUIC_EV_CONN_BCFRMS, qc, &flen);
7366 /* Add the CRYPTO data length and its encoded length to the packet
7367 * length and the length of this length.
7368 */
7369 *len += flen;
7370 room -= flen;
7371 if (dlen == cf->crypto.len) {
7372 /* <cf> CRYPTO data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007373 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007374 LIST_APPEND(outlist, &cf->list);
7375 }
7376 else {
7377 struct quic_frame *new_cf;
7378
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007379 new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007380 if (!new_cf) {
7381 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
7382 continue;
7383 }
7384
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007385 new_cf->crypto.len = dlen;
7386 new_cf->crypto.offset = cf->crypto.offset;
7387 new_cf->crypto.qel = qel;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007388 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007389 if (cf->origin) {
7390 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7391 /* This <cf> frame was duplicated */
7392 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7393 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007394 /* Detach the remaining CRYPTO frame from its original frame */
7395 LIST_DEL_INIT(&cf->ref);
7396 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007397 }
7398 LIST_APPEND(outlist, &new_cf->list);
7399 /* Consume <dlen> bytes of the current frame. */
7400 cf->crypto.len -= dlen;
7401 cf->crypto.offset += dlen;
7402 }
7403 break;
7404
7405 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007406 if (cf->stream.dup) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007407 struct eb64_node *node = NULL;
7408 struct qc_stream_desc *stream_desc = NULL;
7409 struct quic_stream *strm = &cf->stream;
7410
7411 /* As this frame has been already lost, ensure the stream is always
7412 * available or the range of this frame is not consumed before
7413 * resending it.
7414 */
7415 node = eb64_lookup(&qc->streams_by_id, strm->id);
7416 if (!node) {
7417 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007418 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007419 continue;
7420 }
7421
7422 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
7423 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
7424 TRACE_DEVEL("ignored frame frame in already acked range",
7425 QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007426 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007427 continue;
7428 }
7429 else if (strm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01007430 uint64_t diff = stream_desc->ack_offset - strm->offset.key;
7431
Frédéric Lécaillec425e032023-03-20 14:32:59 +01007432 qc_stream_frm_mv_fwd(cf, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007433 TRACE_DEVEL("updated partially acked frame",
7434 QUIC_EV_CONN_PRSAFRM, qc, cf);
7435 }
7436 }
7437 /* Note that these frames are accepted in short packets only without
7438 * "Length" packet field. Here, <*len> is used only to compute the
7439 * sum of the lengths of the already built frames for this packet.
7440 *
7441 * Compute the length of this STREAM frame "header" made a all the field
7442 * excepting the variable ones. Note that +1 is for the type of this frame.
7443 */
7444 hlen = 1 + quic_int_getsize(cf->stream.id) +
7445 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
7446 /* Compute the data length of this STREAM frame. */
7447 avail_room = room - hlen - *len;
7448 if ((ssize_t)avail_room <= 0)
7449 continue;
7450
7451 TRACE_DEVEL(" New STREAM frame build (room, len)",
7452 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007453
7454 /* hlen contains STREAM id and offset. Ensure there is
7455 * enough room for length field.
7456 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007457 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007458 dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
7459 cf->stream.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007460 dlen_sz = quic_int_getsize(dlen);
7461 flen = hlen + dlen_sz + dlen;
7462 }
7463 else {
7464 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
7465 flen = hlen + dlen;
7466 }
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007467
7468 if (cf->stream.len && !dlen) {
7469 /* Only a small gap is left on buffer, not
7470 * enough to encode the STREAM data length.
7471 */
7472 continue;
7473 }
7474
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007475 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
7476 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
7477 TRACE_DEVEL(" STREAM frame length (flen)",
7478 QUIC_EV_CONN_BCFRMS, qc, &flen);
7479 /* Add the STREAM data length and its encoded length to the packet
7480 * length and the length of this length.
7481 */
7482 *len += flen;
7483 room -= flen;
7484 if (dlen == cf->stream.len) {
7485 /* <cf> STREAM data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007486 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007487 LIST_APPEND(outlist, &cf->list);
7488
7489 /* Do not notify MUX on retransmission. */
7490 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7491 qcc_streams_sent_done(cf->stream.stream->ctx,
7492 cf->stream.len,
7493 cf->stream.offset.key);
7494 }
7495 }
7496 else {
7497 struct quic_frame *new_cf;
7498 struct buffer cf_buf;
7499
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007500 new_cf = qc_frm_alloc(cf->type);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007501 if (!new_cf) {
7502 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
7503 continue;
7504 }
7505
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007506 new_cf->stream.stream = cf->stream.stream;
7507 new_cf->stream.buf = cf->stream.buf;
7508 new_cf->stream.id = cf->stream.id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01007509 new_cf->stream.offset = cf->stream.offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007510 new_cf->stream.len = dlen;
7511 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
7512 /* FIN bit reset */
7513 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
7514 new_cf->stream.data = cf->stream.data;
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007515 new_cf->stream.dup = cf->stream.dup;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007516 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007517 if (cf->origin) {
7518 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7519 /* This <cf> frame was duplicated */
7520 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7521 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007522 /* Detach this STREAM frame from its origin */
7523 LIST_DEL_INIT(&cf->ref);
7524 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007525 }
7526 LIST_APPEND(outlist, &new_cf->list);
7527 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
7528 /* Consume <dlen> bytes of the current frame. */
7529 cf_buf = b_make(b_orig(cf->stream.buf),
7530 b_size(cf->stream.buf),
7531 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
7532 cf->stream.len -= dlen;
7533 cf->stream.offset.key += dlen;
7534 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
7535
7536 /* Do not notify MUX on retransmission. */
7537 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7538 qcc_streams_sent_done(new_cf->stream.stream->ctx,
7539 new_cf->stream.len,
7540 new_cf->stream.offset.key);
7541 }
7542 }
7543
7544 /* TODO the MUX is notified about the frame sending via
7545 * previous qcc_streams_sent_done call. However, the
7546 * sending can fail later, for example if the sendto
7547 * system call returns an error. As the MUX has been
7548 * notified, the transport layer is responsible to
7549 * bufferize and resent the announced data later.
7550 */
7551
7552 break;
7553
7554 default:
7555 flen = qc_frm_len(cf);
7556 BUG_ON(!flen);
7557 if (flen > room)
7558 continue;
7559
7560 *len += flen;
7561 room -= flen;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007562 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007563 LIST_APPEND(outlist, &cf->list);
7564 break;
7565 }
7566
7567 /* Successful status as soon as a frame could be built */
7568 ret = 1;
7569 }
7570
7571 leave:
7572 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
7573 return ret;
7574}
7575
7576/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
7577 * is used as return parameter and should be zero'ed by the caller.
7578 */
7579static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
7580 struct quic_frame *out)
7581{
7582 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
7583 *
7584 * A CONNECTION_CLOSE frame should be sent in several packets with
7585 * different encryption levels depending on the client context. This is
7586 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
7587 * more details on how to implement it.
7588 */
7589 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
7590
7591
7592 if (qc->err.app) {
7593 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
7594 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
7595 /* RFC 9000 10.2.3. Immediate Close during the Handshake
7596 *
7597 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
7598 * packet could expose application state or be used to alter application
7599 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
7600 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
7601 * Handshake packets. Otherwise, information about the application
7602 * state might be revealed. Endpoints MUST clear the value of the
7603 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
7604 * converting to a CONNECTION_CLOSE of type 0x1c.
7605 */
7606 out->type = QUIC_FT_CONNECTION_CLOSE;
7607 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
7608 out->connection_close.reason_phrase_len = 0;
7609 }
7610 else {
7611 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
7612 out->connection_close.error_code = qc->err.code;
7613 }
7614 }
7615 else {
7616 out->type = QUIC_FT_CONNECTION_CLOSE;
7617 out->connection_close.error_code = qc->err.code;
7618 }
7619 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
7620
7621}
7622
7623/* This function builds a clear packet from <pkt> information (its type)
7624 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
7625 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
7626 * filling the buffer with as much frames as possible from <frms> list of
7627 * prebuilt frames.
7628 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
7629 * reserved so that to ensure there is enough room to build this AEAD TAG after
7630 * having returned from this function.
7631 * This function also updates the value of <buf_pn> pointer to point to the packet
7632 * number field in this packet. <pn_len> will also have the packet number
7633 * length as value.
7634 *
7635 * Return 1 if succeeded (enough room to buile this packet), O if not.
7636 */
7637static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
7638 size_t dglen, struct quic_tx_packet *pkt,
7639 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
7640 int force_ack, int padding, int cc, int probe,
7641 struct quic_enc_level *qel, struct quic_conn *qc,
7642 const struct quic_version *ver, struct list *frms)
7643{
7644 unsigned char *beg, *payload;
7645 size_t len, len_sz, len_frms, padding_len;
7646 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
7647 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
7648 struct quic_frame cc_frm = { };
7649 size_t ack_frm_len, head_len;
7650 int64_t rx_largest_acked_pn;
7651 int add_ping_frm;
7652 struct list frm_list = LIST_HEAD_INIT(frm_list);
7653 struct quic_frame *cf;
7654 int must_ack, ret = 0;
7655 int nb_aepkts_since_last_ack;
7656
7657 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7658
7659 /* Length field value with CRYPTO frames if present. */
7660 len_frms = 0;
7661 beg = pos;
7662 /* When not probing, and no immediate close is required, reduce the size of this
7663 * buffer to respect the congestion controller window.
7664 * This size will be limited if we have ack-eliciting frames to send from <frms>.
7665 */
7666 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
7667 size_t path_room;
7668
7669 path_room = quic_path_prep_data(qc->path);
7670 if (end - beg > path_room)
7671 end = beg + path_room;
7672 }
7673
7674 /* Ensure there is enough room for the TLS encryption tag and a zero token
7675 * length field if any.
7676 */
7677 if (end - pos < QUIC_TLS_TAG_LEN +
7678 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
7679 goto no_room;
7680
7681 end -= QUIC_TLS_TAG_LEN;
7682 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
7683 /* packet number length */
7684 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
7685 /* Build the header */
7686 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
7687 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
7688 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
7689 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
7690 goto no_room;
7691
7692 /* Encode the token length (0) for an Initial packet. */
7693 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
7694 *pos++ = 0;
7695 head_len = pos - beg;
7696 /* Build an ACK frame if required. */
7697 ack_frm_len = 0;
7698 nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
7699 must_ack = !qel->pktns->tx.pto_probe &&
7700 (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
7701 (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK)));
7702 if (must_ack) {
7703 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
7704 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
7705 ack_frm.tx_ack.arngs = arngs;
7706 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
7707 qel->pktns->tx.ack_delay =
7708 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
7709 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
7710 }
7711 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
7712 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
7713 * smallest frame (PING) and <*pn_len> more for the packet number. Note
7714 * that from here, we do not know if we will have to send a PING frame.
7715 * This will be decided after having computed the ack-eliciting frames
7716 * to be added to this packet.
7717 */
7718 ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos);
7719 if (!ack_frm_len)
7720 goto no_room;
7721 }
7722
7723 /* Length field value without the ack-eliciting frames. */
7724 len = ack_frm_len + *pn_len;
7725 len_frms = 0;
7726 if (!cc && !LIST_ISEMPTY(frms)) {
7727 ssize_t room = end - pos;
7728
7729 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
7730 /* Initialize the length of the frames built below to <len>.
7731 * If any frame could be successfully built by qc_build_frms(),
7732 * we will have len_frms > len.
7733 */
7734 len_frms = len;
7735 if (!qc_build_frms(&frm_list, frms,
7736 end - pos, &len_frms, pos - beg, qel, qc)) {
7737 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7738 qc, NULL, NULL, &room);
7739 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
7740 goto no_room;
7741 }
7742 }
7743
7744 /* Length (of the remaining data). Must not fail because, the buffer size
7745 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
7746 * for the encryption tag. It must be taken into an account for the length
7747 * of this packet.
7748 */
7749 if (len_frms)
7750 len = len_frms + QUIC_TLS_TAG_LEN;
7751 else
7752 len += QUIC_TLS_TAG_LEN;
7753 /* CONNECTION_CLOSE frame */
7754 if (cc) {
7755 qc_build_cc_frm(qc, qel, &cc_frm);
7756 len += qc_frm_len(&cc_frm);
7757 }
7758 add_ping_frm = 0;
7759 padding_len = 0;
7760 len_sz = quic_int_getsize(len);
7761 /* Add this packet size to <dglen> */
7762 dglen += head_len + len_sz + len;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007763 /* Note that <padding> is true only when building an Handshake packet
7764 * coalesced to an Initial packet.
7765 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007766 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
7767 /* This is a maximum padding size */
7768 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7769 /* The length field value is of this packet is <len> + <padding_len>
7770 * the size of which may be greater than the initial computed size
7771 * <len_sz>. So, let's deduce the difference between these to packet
7772 * sizes from <padding_len>.
7773 */
7774 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7775 len += padding_len;
7776 }
Frédéric Lécaille5faf5772023-02-16 17:30:53 +01007777 else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
7778 len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
7779 }
7780 else if (LIST_ISEMPTY(&frm_list)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007781 if (qel->pktns->tx.pto_probe) {
7782 /* If we cannot send a frame, we send a PING frame. */
7783 add_ping_frm = 1;
7784 len += 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007785 dglen += 1;
7786 /* Note that only we are in the case where this Initial packet
7787 * is not coalesced to an Handshake packet. We must directly
7788 * pad the datragram.
7789 */
Frédéric Lécaille9c317b12023-03-28 15:39:11 +02007790 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7791 if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
7792 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7793 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7794 len += padding_len;
7795 }
7796 }
7797 else {
7798 /* Note that +1 is for the PING frame */
7799 if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
7800 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007801 }
7802 }
7803 else {
7804 /* If there is no frame at all to follow, add at least a PADDING frame. */
7805 if (!ack_frm_len && !cc)
7806 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007807 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007808 }
7809
7810 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
7811 goto no_room;
7812
7813 /* Packet number field address. */
7814 *buf_pn = pos;
7815
7816 /* Packet number encoding. */
7817 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
7818 goto no_room;
7819
7820 /* payload building (ack-eliciting or not frames) */
7821 payload = pos;
7822 if (ack_frm_len) {
7823 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
7824 goto no_room;
7825
7826 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
7827 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
7828 }
7829
7830 /* Ack-eliciting frames */
7831 if (!LIST_ISEMPTY(&frm_list)) {
7832 struct quic_frame *tmp_cf;
7833 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
7834 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
7835 ssize_t room = end - pos;
7836 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7837 qc, NULL, NULL, &room);
7838 /* Note that <cf> was added from <frms> to <frm_list> list by
7839 * qc_build_frms().
7840 */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007841 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007842 LIST_INSERT(frms, &cf->list);
7843 continue;
7844 }
7845
7846 quic_tx_packet_refinc(pkt);
7847 cf->pkt = pkt;
7848 }
7849 }
7850
7851 /* Build a PING frame if needed. */
7852 if (add_ping_frm) {
7853 frm.type = QUIC_FT_PING;
7854 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
7855 goto no_room;
7856 }
7857
7858 /* Build a CONNECTION_CLOSE frame if needed. */
7859 if (cc) {
7860 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
7861 goto no_room;
7862
7863 pkt->flags |= QUIC_FL_TX_PACKET_CC;
7864 }
7865
7866 /* Build a PADDING frame if needed. */
7867 if (padding_len) {
7868 frm.type = QUIC_FT_PADDING;
7869 frm.padding.len = padding_len;
7870 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
7871 goto no_room;
7872 }
7873
7874 if (pos == payload) {
7875 /* No payload was built because of congestion control */
7876 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
7877 goto no_room;
7878 }
7879
7880 /* If this packet is ack-eliciting and we are probing let's
7881 * decrement the PTO probe counter.
7882 */
7883 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
7884 qel->pktns->tx.pto_probe)
7885 qel->pktns->tx.pto_probe--;
7886
7887 pkt->len = pos - beg;
7888 LIST_SPLICE(&pkt->frms, &frm_list);
7889
7890 ret = 1;
7891 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
7892 leave:
7893 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7894 return ret;
7895
7896 no_room:
7897 /* Replace the pre-built frames which could not be add to this packet */
7898 LIST_SPLICE(frms, &frm_list);
7899 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
7900 goto leave;
7901}
7902
7903static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
7904{
7905 pkt->type = type;
7906 pkt->len = 0;
7907 pkt->in_flight_len = 0;
7908 pkt->pn_node.key = (uint64_t)-1;
7909 LIST_INIT(&pkt->frms);
7910 pkt->time_sent = TICK_ETERNITY;
7911 pkt->next = NULL;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01007912 pkt->prev = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007913 pkt->largest_acked_pn = -1;
7914 pkt->flags = 0;
7915 pkt->refcnt = 0;
7916}
7917
7918/* Build a packet into <buf> packet buffer with <pkt_type> as packet
7919 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
7920 * of prebuilt frames.
7921 *
7922 * Return -2 if the packet could not be allocated or encrypted for any reason,
7923 * -1 if there was not enough room to build a packet.
7924 * XXX NOTE XXX
7925 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
7926 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
7927 * control window limitation.
7928 */
7929static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
7930 const unsigned char *buf_end,
7931 struct quic_enc_level *qel,
7932 struct quic_tls_ctx *tls_ctx, struct list *frms,
7933 struct quic_conn *qc, const struct quic_version *ver,
7934 size_t dglen, int pkt_type, int force_ack,
7935 int padding, int probe, int cc, int *err)
7936{
7937 struct quic_tx_packet *ret_pkt = NULL;
7938 /* The pointer to the packet number field. */
7939 unsigned char *buf_pn;
7940 unsigned char *beg, *end, *payload;
7941 int64_t pn;
7942 size_t pn_len, payload_len, aad_len;
7943 struct quic_tx_packet *pkt;
7944
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007945 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7946 TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007947 *err = 0;
7948 pkt = pool_alloc(pool_head_quic_tx_packet);
7949 if (!pkt) {
7950 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
7951 *err = -2;
7952 goto err;
7953 }
7954
7955 quic_tx_packet_init(pkt, pkt_type);
7956 beg = *pos;
7957 pn_len = 0;
7958 buf_pn = NULL;
7959
7960 pn = qel->pktns->tx.next_pn + 1;
7961 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
7962 force_ack, padding, cc, probe, qel, qc, ver, frms)) {
7963 // trace already emitted by function above
7964 *err = -1;
7965 goto err;
7966 }
7967
7968 end = beg + pkt->len;
7969 payload = buf_pn + pn_len;
7970 payload_len = end - payload;
7971 aad_len = payload - beg;
7972
7973 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
7974 // trace already emitted by function above
7975 *err = -2;
7976 goto err;
7977 }
7978
7979 end += QUIC_TLS_TAG_LEN;
7980 pkt->len += QUIC_TLS_TAG_LEN;
7981 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
7982 // trace already emitted by function above
7983 *err = -2;
7984 goto err;
7985 }
7986
7987 /* Consume a packet number */
7988 qel->pktns->tx.next_pn++;
7989 qc->tx.prep_bytes += pkt->len;
7990 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
7991 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
7992 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
7993 }
7994 /* Now that a correct packet is built, let us consume <*pos> buffer. */
7995 *pos = end;
7996 /* Attach the built packet to its tree. */
7997 pkt->pn_node.key = pn;
7998 /* Set the packet in fligth length for in flight packet only. */
7999 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
8000 pkt->in_flight_len = pkt->len;
8001 qc->path->prep_in_flight += pkt->len;
8002 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01008003 /* Always reset this flag */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008004 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
8005 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
8006 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
8007 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
Frédéric Lécailled7215712023-03-24 18:13:37 +01008008 qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
8009 if (tick_isset(qc->ack_expire)) {
8010 qc->ack_expire = TICK_ETERNITY;
8011 qc->idle_timer_task->expire = qc->idle_expire;
8012 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02008013 TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01008014 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008015 }
8016
8017 pkt->pktns = qel->pktns;
8018
8019 ret_pkt = pkt;
8020 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008021 TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
8022 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008023 return ret_pkt;
8024
8025 err:
8026 /* TODO: what about the frames which have been built
8027 * for this packet.
8028 */
8029 free_quic_tx_packet(qc, pkt);
8030 goto leave;
8031}
8032
8033
8034static void __quic_conn_init(void)
8035{
8036 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
8037}
8038INITCALL0(STG_REGISTER, __quic_conn_init);
8039
8040static void __quic_conn_deinit(void)
8041{
8042 BIO_meth_free(ha_quic_meth);
8043}
8044REGISTER_POST_DEINIT(__quic_conn_deinit);
8045
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008046/* Handle a new <dgram> received. Parse each QUIC packets and copied their
8047 * content to a quic-conn instance. The datagram content can be released after
8048 * this function.
8049 *
8050 * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
8051 * to the connection instance. <li> is the attached listener. The caller is
8052 * responsible to ensure that the first packet is destined to this connection
8053 * by comparing CIDs.
8054 *
8055 * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
8056 * function will thus retrieve the connection from the CID tree or allocate a
8057 * new one if possible. <li> is the listener attached to the receiver.
8058 *
8059 * Returns 0 on success else non-zero. If an error happens, some packets from
8060 * the datagram may not have been parsed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008061 */
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008062int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
8063 struct listener *li)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008064{
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008065 struct quic_rx_packet *pkt;
8066 struct quic_conn *qc = NULL;
8067 unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008068 struct list *tasklist_head = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008069
8070 TRACE_ENTER(QUIC_EV_CONN_LPKT);
8071
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008072 pos = dgram->buf;
8073 end = pos + dgram->len;
8074 do {
8075 /* TODO replace zalloc -> alloc. */
8076 pkt = pool_zalloc(pool_head_quic_rx_packet);
8077 if (!pkt) {
8078 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
8079 goto err;
8080 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008081
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008082 pkt->version = NULL;
8083 pkt->pn_offset = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008084
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008085 /* Set flag if pkt is the first one in dgram. */
8086 if (pos == dgram->buf)
8087 pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008088
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008089 LIST_INIT(&pkt->qc_rx_pkt_list);
8090 pkt->time_received = now_ms;
8091 quic_rx_packet_refinc(pkt);
8092 if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
8093 goto next;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008094
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008095 /* Search quic-conn instance for first packet of the datagram.
8096 * quic_rx_packet_parse() is responsible to discard packets
8097 * with different DCID as the first one in the same datagram.
8098 */
8099 if (!qc) {
8100 qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li);
8101 /* qc is NULL if receiving a non Initial packet for an
8102 * unknown connection.
8103 */
8104 if (!qc) {
Amaury Denoyelle98289692022-10-19 15:37:44 +02008105 /* Skip the entire datagram. */
8106 pkt->len = end - pos;
8107 goto next;
8108 }
8109
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008110 dgram->qc = qc;
8111 }
Amaury Denoyelle98289692022-10-19 15:37:44 +02008112
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008113 if (qc_rx_check_closing(qc, pkt)) {
8114 /* Skip the entire datagram. */
8115 pkt->len = end - pos;
8116 goto next;
8117 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008118
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008119 /* Detect QUIC connection migration. */
Frédéric Lécaillef6769542023-01-12 10:36:26 +01008120 if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008121 if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
8122 /* Skip the entire datagram. */
8123 TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
8124 pkt->len = end - pos;
8125 goto next;
8126 }
8127 }
8128
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008129 qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008130
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008131 next:
8132 pos += pkt->len;
8133 quic_rx_packet_refdec(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008134
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008135 /* Free rejected packets */
8136 if (!pkt->refcnt) {
8137 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
8138 pool_free(pool_head_quic_rx_packet, pkt);
8139 }
8140 } while (pos < end);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008141
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008142 /* Increasing the received bytes counter by the UDP datagram length
8143 * if this datagram could be associated to a connection.
8144 */
8145 if (dgram->qc)
8146 dgram->qc->rx.bytes += dgram->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008147
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008148 /* This must never happen. */
8149 BUG_ON(pos > end);
8150 BUG_ON(pos < end || pos > dgram->buf + dgram->len);
8151 /* Mark this datagram as consumed */
8152 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008153
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008154 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
8155 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008156
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008157 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008158 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008159 return -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008160}
8161
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008162/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
8163 * CIDs. This can be used to determine if a datagram is addressed to the right
8164 * connection instance.
8165 *
8166 * Returns a boolean value.
8167 */
8168int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len)
8169{
8170 struct ebmb_node *node;
8171 struct quic_connection_id *id;
8172
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008173 if ((qc->scid.len == dcid_len &&
8174 memcmp(qc->scid.data, dcid, dcid_len) == 0) ||
8175 (qc->odcid.len == dcid_len &&
Frédéric Lécaille07846cb2023-02-13 16:14:24 +01008176 memcmp(qc->odcid.data, dcid, dcid_len) == 0)) {
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008177 return 1;
8178 }
8179
8180 node = ebmb_lookup(&quic_dghdlrs[tid].cids, dcid, dcid_len);
8181 if (node) {
8182 id = ebmb_entry(node, struct quic_connection_id, node);
8183 if (qc == id->qc)
8184 return 1;
8185 }
8186
8187 return 0;
8188}
8189
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008190/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
8191 * Returns 1 if succeeded, 0 if not.
8192 */
8193int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
8194 unsigned char **dcid, size_t *dcid_len)
8195{
8196 int ret = 0, long_header;
8197 size_t minlen, skip;
8198
8199 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
8200
8201 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
8202 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
8203 goto err;
8204 }
8205
8206 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
8207 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
8208 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
8209 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
8210 if (end - buf < minlen)
8211 goto err;
8212
8213 buf += skip;
8214 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
8215 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
8216 goto err;
8217
8218 *dcid = buf;
8219
8220 ret = 1;
8221 leave:
8222 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
8223 return ret;
8224
8225 err:
8226 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
8227 goto leave;
8228}
8229
8230/* Notify the MUX layer if alive about an imminent close of <qc>. */
8231void qc_notify_close(struct quic_conn *qc)
8232{
8233 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
8234
8235 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
8236 goto leave;
8237
8238 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
8239 /* wake up the MUX */
8240 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
8241 TRACE_STATE("connection closure notidfied to mux",
8242 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8243 qc->conn->mux->wake(qc->conn);
8244 }
8245 else
8246 TRACE_STATE("connection closure not notidfied to mux",
8247 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8248 leave:
8249 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
8250}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008251
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008252/* Wake-up upper layer for sending if all conditions are met :
8253 * - room in congestion window or probe packet to sent
8254 * - socket FD ready to sent or listener socket used
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008255 *
8256 * Returns 1 if upper layer has been woken up else 0.
8257 */
8258int qc_notify_send(struct quic_conn *qc)
8259{
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008260 const struct quic_pktns *pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8261
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008262 if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008263 /* RFC 9002 7.5. Probe Timeout
8264 *
8265 * Probe packets MUST NOT be blocked by the congestion controller.
8266 */
8267 if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
Amaury Denoyellecaa16542023-02-28 15:11:26 +01008268 (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008269 tasklet_wakeup(qc->subs->tasklet);
8270 qc->subs->events &= ~SUB_RETRY_SEND;
8271 if (!qc->subs->events)
8272 qc->subs = NULL;
8273
8274 return 1;
8275 }
8276 }
8277
8278 return 0;
8279}
8280
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008281
8282/* appctx context used by "show quic" command */
8283struct show_quic_ctx {
8284 unsigned int epoch;
8285 struct bref bref; /* back-reference to the quic-conn being dumped */
8286 unsigned int thr;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008287 int flags;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008288};
8289
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008290#define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */
8291
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008292static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private)
8293{
8294 struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
8295
8296 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
8297 return 1;
8298
8299 ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1);
8300 ctx->thr = 0;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008301 ctx->flags = 0;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008302
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01008303 if (*args[2] && strcmp(args[2], "all") == 0)
8304 ctx->flags |= QC_CLI_FL_SHOW_ALL;
8305
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008306 LIST_INIT(&ctx->bref.users);
8307
8308 return 0;
8309}
8310
8311static int cli_io_handler_dump_quic(struct appctx *appctx)
8312{
8313 struct show_quic_ctx *ctx = appctx->svcctx;
8314 struct stconn *sc = appctx_sc(appctx);
8315 struct quic_conn *qc;
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008316 struct quic_pktns *pktns;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008317 struct eb64_node *node;
8318 struct qc_stream_desc *stream;
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008319 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008320 int expire;
8321 unsigned char cid_len;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008322
8323 thread_isolate();
8324
8325 if (ctx->thr >= global.nbthread)
8326 goto done;
8327
Christopher Faulet87633c32023-04-03 18:32:50 +02008328 /* FIXME: Don't watch the other side !*/
Christopher Faulet7faac7c2023-04-04 10:05:27 +02008329 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUTW)) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008330 /* If we're forced to shut down, we might have to remove our
8331 * reference to the last stream being dumped.
8332 */
8333 if (!LIST_ISEMPTY(&ctx->bref.users))
8334 LIST_DEL_INIT(&ctx->bref.users);
8335 goto done;
8336 }
8337
8338 chunk_reset(&trash);
8339
8340 if (!LIST_ISEMPTY(&ctx->bref.users)) {
8341 /* Remove show_quic_ctx from previous quic_conn instance. */
8342 LIST_DEL_INIT(&ctx->bref.users);
8343 }
8344 else if (!ctx->bref.ref) {
8345 /* First invocation. */
8346 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8347 }
8348
8349 while (1) {
8350 int done = 0;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008351 int i;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008352
8353 if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008354 /* If closing connections requested through "all", move
8355 * to quic_conns_clo list after browsing quic_conns.
8356 * Else move directly to the next quic_conns thread.
8357 */
8358 if (ctx->flags & QC_CLI_FL_SHOW_ALL) {
8359 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns_clo.n;
8360 continue;
8361 }
8362
8363 done = 1;
8364 }
8365 else if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns_clo) {
8366 /* Closing list entirely browsed, go to next quic_conns
8367 * thread.
8368 */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008369 done = 1;
8370 }
8371 else {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008372 /* Retrieve next element of the current list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008373 qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx);
8374 if ((int)(qc->qc_epoch - ctx->epoch) > 0)
8375 done = 1;
8376 }
8377
8378 if (done) {
8379 ++ctx->thr;
8380 if (ctx->thr >= global.nbthread)
8381 break;
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008382 /* Switch to next thread quic_conns list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008383 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8384 continue;
8385 }
8386
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008387 /* CIDs */
8388 chunk_appendf(&trash, "* %p[%02u]: scid=", qc, qc->tid);
8389 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
8390 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
8391 while (cid_len++ < 20)
8392 chunk_appendf(&trash, "..");
8393
8394 chunk_appendf(&trash, " dcid=");
8395 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
8396 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
8397 while (cid_len++ < 20)
8398 chunk_appendf(&trash, "..");
8399
8400 chunk_appendf(&trash, "\n");
8401
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008402 chunk_appendf(&trash, " loc. TPs:");
8403 quic_transport_params_dump(&trash, qc, &qc->rx.params);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008404 chunk_appendf(&trash, "\n");
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008405 chunk_appendf(&trash, " rem. TPs:");
8406 quic_transport_params_dump(&trash, qc, &qc->tx.params);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008407 chunk_appendf(&trash, "\n");
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008408
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008409 /* Connection state */
8410 if (qc->flags & QUIC_FL_CONN_CLOSING)
8411 chunk_appendf(&trash, " st=closing ");
8412 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8413 chunk_appendf(&trash, " st=draining ");
8414 else if (qc->state < QUIC_HS_ST_CONFIRMED)
8415 chunk_appendf(&trash, " st=handshake ");
8416 else
8417 chunk_appendf(&trash, " st=opened ");
8418
8419 if (qc->mux_state == QC_MUX_NULL)
8420 chunk_appendf(&trash, "mux=null ");
8421 else if (qc->mux_state == QC_MUX_READY)
8422 chunk_appendf(&trash, "mux=ready ");
8423 else
8424 chunk_appendf(&trash, "mux=released ");
8425
Frédéric Lécaille92f4a7c2023-04-04 14:31:49 +02008426 expire = qc->idle_expire;
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008427 chunk_appendf(&trash, "expire=%02ds ",
Frédéric Lécaille92f4a7c2023-04-04 14:31:49 +02008428 TICKS_TO_MS(tick_remain(now_ms, expire)) / 1000);
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008429
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008430 chunk_appendf(&trash, "\n");
8431
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008432 /* Socket */
8433 chunk_appendf(&trash, " fd=%d", qc->fd);
8434 if (qc->local_addr.ss_family == AF_INET ||
8435 qc->local_addr.ss_family == AF_INET6) {
8436 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8437 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
8438 chunk_appendf(&trash, " from=%s:%s", bufaddr, bufport);
8439
8440 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8441 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
8442 chunk_appendf(&trash, " to=%s:%s", bufaddr, bufport);
8443 }
8444
8445 chunk_appendf(&trash, "\n");
8446
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008447 /* Packet number spaces information */
8448 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008449 chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008450 pktns->rx.arngs.sz, pktns->tx.in_flight);
8451 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008452 chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008453 pktns->rx.arngs.sz, pktns->tx.in_flight);
8454 pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8455 chunk_appendf(&trash, " [01rtt] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
8456 pktns->rx.arngs.sz, pktns->tx.in_flight);
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008457
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +02008458 chunk_appendf(&trash, " srtt=%-4u rttvar=%-4u rttmin=%-4u ptoc=%-4u cwnd=%-6llu"
8459 " mcwnd=%-6llu lostpkts=%-6llu\n",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008460 qc->path->loss.srtt >> 3, qc->path->loss.rtt_var >> 2,
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +02008461 qc->path->loss.rtt_min, qc->path->loss.pto_count, (ullong)qc->path->cwnd,
8462 (ullong)qc->path->mcwnd, (ullong)qc->path->loss.nb_lost_pkt);
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008463
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008464
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008465 /* Streams */
8466 node = eb64_first(&qc->streams_by_id);
8467 i = 0;
8468 while (node) {
8469 stream = eb64_entry(node, struct qc_stream_desc, by_id);
8470 node = eb64_next(node);
8471
Amaury Denoyellea9de25a2023-02-10 09:25:22 +01008472 chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key);
8473 chunk_appendf(&trash, " off=%-8llu ack=%-8llu",
8474 (unsigned long long)stream->buf_offset,
8475 (unsigned long long)stream->ack_offset);
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008476
8477 if (!(++i % 3)) {
8478 chunk_appendf(&trash, "\n");
8479 i = 0;
8480 }
8481 }
8482
8483 chunk_appendf(&trash, "\n");
8484
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008485 if (applet_putchk(appctx, &trash) == -1) {
8486 /* Register show_quic_ctx to quic_conn instance. */
8487 LIST_APPEND(&qc->back_refs, &ctx->bref.users);
8488 goto full;
8489 }
8490
8491 ctx->bref.ref = qc->el_th_ctx.n;
8492 }
8493
8494 done:
8495 thread_release();
8496 return 1;
8497
8498 full:
8499 thread_release();
8500 return 0;
8501}
8502
8503static void cli_release_show_quic(struct appctx *appctx)
8504{
8505 struct show_quic_ctx *ctx = appctx->svcctx;
8506
8507 if (ctx->thr < global.nbthread) {
8508 thread_isolate();
8509 if (!LIST_ISEMPTY(&ctx->bref.users))
8510 LIST_DEL_INIT(&ctx->bref.users);
8511 thread_release();
8512 }
8513}
8514
8515static struct cli_kw_list cli_kws = {{ }, {
8516 { { "show", "quic", NULL }, "show quic : display quic connections status", cli_parse_show_quic, cli_io_handler_dump_quic, cli_release_show_quic },
Frédéric Lécaille91376d62023-02-11 20:24:42 +01008517 {{},}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008518}};
8519
8520INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
8521
8522static void init_quic()
8523{
8524 int thr;
8525
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008526 for (thr = 0; thr < MAX_THREADS; ++thr) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008527 LIST_INIT(&ha_thread_ctx[thr].quic_conns);
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008528 LIST_INIT(&ha_thread_ctx[thr].quic_conns_clo);
8529 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008530}
8531INITCALL0(STG_INIT, init_quic);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008532
8533/*
8534 * Local variables:
8535 * c-indent-level: 8
8536 * c-basic-offset: 8
8537 * End:
8538 */