blob: 50a562d09719d8355c0a7d4540798a75b2f54b58 [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,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +0200232 int must_ack, int padding, int probe, int cc, int *err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200233struct 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.
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003386 * Also set <*must_ack> to inform the caller if an acknowledgement should be sent.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003387 */
3388static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003389 struct quic_enc_level *qel, int cc, int probe,
3390 int *must_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003391{
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003392 int force_ack =
3393 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3394 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3395 int nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
3396
3397 /* An acknowledgement must be sent if this has been forced by the caller,
3398 * typically during the handshake when the packets must be acknowledged as
3399 * soon as possible. This is also the case when the ack delay timer has been
3400 * triggered, or at least every QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK packets.
3401 */
3402 *must_ack = (qc->flags & QUIC_FL_CONN_ACK_TIMER_FIRED) ||
3403 ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3404 (force_ack || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003405
3406 /* Do not build any more packet if the TX secrets are not available or
3407 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
3408 * and if there is no more packets to send upon PTO expiration
3409 * and if there is no more ack-eliciting frames to send or in flight
3410 * congestion control limit is reached for prepared data
3411 */
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02003412 if (!quic_tls_has_tx_sec(qel) ||
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003413 (!cc && !probe && !*must_ack &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003414 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
3415 return 0;
3416 }
3417
3418 return 1;
3419}
3420
3421/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3422 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
3423 *
3424 * Each datagram is prepended by a two fields header : the datagram length and
3425 * the address of the packet contained in the datagram.
3426 *
3427 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3428 * -1 if something wrong happened.
3429 */
3430static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
3431 struct list *frms)
3432{
3433 int ret = -1;
3434 struct quic_enc_level *qel;
3435 unsigned char *end, *pos;
3436 struct quic_tx_packet *pkt;
3437 size_t total;
3438 /* Each datagram is prepended with its length followed by the address
3439 * of the first packet in the datagram.
3440 */
3441 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
3442
3443 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3444
3445 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3446 total = 0;
3447 pos = (unsigned char *)b_tail(buf);
3448 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003449 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003450
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003451 TRACE_PROTO("TX prep app pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003452 probe = 0;
3453 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3454 /* We do not probe if an immediate close was asked */
3455 if (!cc)
3456 probe = qel->pktns->tx.pto_probe;
3457
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003458 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003459 break;
3460
3461 /* Leave room for the datagram header */
3462 pos += dg_headlen;
3463 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3464 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3465 }
3466 else {
3467 end = pos + qc->path->mtu;
3468 }
3469
3470 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003471 QUIC_PACKET_TYPE_SHORT, must_ack, 0, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003472 switch (err) {
3473 case -2:
3474 // trace already emitted by function above
3475 goto leave;
3476 case -1:
3477 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
3478 * MTU, we are here because of the congestion control window. There is
3479 * no need to try to reuse this buffer.
3480 */
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003481 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003482 goto out;
3483 default:
3484 break;
3485 }
3486
3487 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
3488 BUG_ON(!pkt);
3489
3490 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3491 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3492
3493 total += pkt->len;
3494
3495 /* Write datagram header. */
3496 qc_txb_store(buf, pkt->len, pkt);
3497 }
3498
3499 out:
3500 ret = total;
3501 leave:
3502 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3503 return ret;
3504}
3505
3506/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3507 * <frms>. Several packets can be regrouped in a single datagram. The result is
3508 * written into <buf>.
3509 *
3510 * Each datagram is prepended by a two fields header : the datagram length and
3511 * the address of first packet in the datagram.
3512 *
3513 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3514 * -1 if something wrong happened.
3515 */
3516static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
3517 enum quic_tls_enc_level tel, struct list *tel_frms,
3518 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3519{
3520 struct quic_enc_level *qel;
3521 unsigned char *end, *pos;
3522 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3523 /* length of datagrams */
3524 uint16_t dglen;
3525 size_t total;
3526 int ret = -1, padding;
3527 /* Each datagram is prepended with its length followed by the address
3528 * of the first packet in the datagram.
3529 */
3530 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
3531 struct list *frms;
3532
3533 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3534
3535 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05003536 * caller must ensure that buf is reset.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003537 */
3538 BUG_ON_HOT(buf->head || buf->data);
3539
3540 total = 0;
3541 qel = &qc->els[tel];
3542 frms = tel_frms;
3543 dglen = 0;
3544 padding = 0;
3545 pos = (unsigned char *)b_head(buf);
3546 first_pkt = prv_pkt = NULL;
3547 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003548 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003549 enum quic_pkt_type pkt_type;
3550 struct quic_tls_ctx *tls_ctx;
3551 const struct quic_version *ver;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003552
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003553 TRACE_PROTO("TX prep pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003554 probe = 0;
3555 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3556 /* We do not probe if an immediate close was asked */
3557 if (!cc)
3558 probe = qel->pktns->tx.pto_probe;
3559
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003560 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003561 if (prv_pkt)
3562 qc_txb_store(buf, dglen, first_pkt);
3563 /* Let's select the next encryption level */
3564 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3565 tel = next_tel;
3566 frms = next_tel_frms;
3567 qel = &qc->els[tel];
3568 /* Build a new datagram */
3569 prv_pkt = NULL;
3570 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
3571 continue;
3572 }
3573 break;
3574 }
3575
3576 pkt_type = quic_tls_level_pkt_type(tel);
3577 if (!prv_pkt) {
3578 /* Leave room for the datagram header */
3579 pos += dg_headlen;
3580 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3581 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3582 }
3583 else {
3584 end = pos + qc->path->mtu;
3585 }
3586 }
3587
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003588 /* RFC 9000 14.1 Initial datagram size
3589 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3590 * Initial packets to at least the smallest allowed maximum datagram size of
3591 * 1200 bytes.
3592 *
3593 * Ensure that no ack-eliciting packets are sent into too small datagrams
3594 */
3595 if (pkt_type == QUIC_PACKET_TYPE_INITIAL && !LIST_ISEMPTY(tel_frms)) {
3596 if (end - pos < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01003597 TRACE_PROTO("No more enough room to build an Initial packet",
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003598 QUIC_EV_CONN_PHPKTS, qc);
3599 goto out;
3600 }
3601
3602 /* Pad this Initial packet if there is no ack-eliciting frames to send from
3603 * the next packet number space.
3604 */
Frédéric Lécailleec937212023-03-03 17:34:41 +01003605 if (!next_tel_frms || LIST_ISEMPTY(next_tel_frms))
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003606 padding = 1;
3607 }
3608
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003609 if (qc->negotiated_version) {
3610 ver = qc->negotiated_version;
3611 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3612 tls_ctx = &qc->negotiated_ictx;
3613 else
3614 tls_ctx = &qel->tls_ctx;
3615 }
3616 else {
3617 ver = qc->original_version;
3618 tls_ctx = &qel->tls_ctx;
3619 }
3620
3621 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
3622 qc, ver, dglen, pkt_type,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003623 must_ack, padding, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003624 switch (err) {
3625 case -2:
3626 // trace already emitted by function above
3627 goto leave;
3628 case -1:
3629 /* If there was already a correct packet present, set the
3630 * current datagram as prepared into <cbuf>.
3631 */
3632 if (prv_pkt)
3633 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003634 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003635 goto out;
3636 default:
3637 break;
3638 }
3639
3640 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
3641 BUG_ON(!cur_pkt);
3642
3643 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3644 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3645
3646 total += cur_pkt->len;
3647 /* keep trace of the first packet in the datagram */
3648 if (!first_pkt)
3649 first_pkt = cur_pkt;
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01003650 /* Attach the current one to the previous one and vice versa */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003651 if (prv_pkt) {
3652 prv_pkt->next = cur_pkt;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01003653 cur_pkt->prev = prv_pkt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003654 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3655 }
3656 /* Let's say we have to build a new dgram */
3657 prv_pkt = NULL;
3658 dglen += cur_pkt->len;
3659 /* Client: discard the Initial encryption keys as soon as
3660 * a handshake packet could be built.
3661 */
3662 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
3663 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3664 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3665 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3666 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3667 qc_set_timer(qc);
3668 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3669 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3670 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
3671 }
3672 /* If the data for the current encryption level have all been sent,
3673 * select the next level.
3674 */
3675 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
3676 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) {
3677 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3678 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3679 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3680 tel = next_tel;
3681 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3682 frms = &qc->els[tel].pktns->tx.frms;
3683 else
3684 frms = next_tel_frms;
3685 qel = &qc->els[tel];
3686 if (!LIST_ISEMPTY(frms)) {
3687 /* If there is data for the next level, do not
3688 * consume a datagram.
3689 */
3690 prv_pkt = cur_pkt;
3691 }
3692 }
3693
3694 /* If we have to build a new datagram, set the current datagram as
3695 * prepared into <cbuf>.
3696 */
3697 if (!prv_pkt) {
3698 qc_txb_store(buf, dglen, first_pkt);
3699 first_pkt = NULL;
3700 dglen = 0;
3701 padding = 0;
3702 }
3703 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
3704 (!qc_is_listener(qc) ||
3705 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3706 padding = 1;
3707 }
3708 }
3709
3710 out:
3711 ret = total;
3712 leave:
3713 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3714 return ret;
3715}
3716
3717/* Send datagrams stored in <buf>.
3718 *
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003719 * This function returns 1 for success. On error, there is several behavior
3720 * depending on underlying sendto() error :
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003721 * - for an unrecoverable error, 0 is returned and connection is killed.
3722 * - a transient error is handled differently if connection has its owned
3723 * socket. If this is the case, 0 is returned and socket is subscribed on the
3724 * poller. The other case is assimilated to a success case with 1 returned.
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003725 * Remaining data are purged from the buffer and will eventually be detected
3726 * as lost which gives the opportunity to retry sending.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003727 */
3728int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
3729{
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003730 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003731 struct quic_conn *qc;
3732 char skip_sendto = 0;
3733
3734 qc = ctx->qc;
3735 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
3736 while (b_contig_data(buf, 0)) {
3737 unsigned char *pos;
3738 struct buffer tmpbuf = { };
3739 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3740 uint16_t dglen;
3741 size_t headlen = sizeof dglen + sizeof first_pkt;
3742 unsigned int time_sent;
3743
3744 pos = (unsigned char *)b_head(buf);
3745 dglen = read_u16(pos);
3746 BUG_ON_HOT(!dglen); /* this should not happen */
3747
3748 pos += sizeof dglen;
3749 first_pkt = read_ptr(pos);
3750 pos += sizeof first_pkt;
3751 tmpbuf.area = (char *)pos;
3752 tmpbuf.size = tmpbuf.data = dglen;
3753
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003754 TRACE_PROTO("TX dgram", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003755 /* If sendto is on error just skip the call to it for the rest
3756 * of the loop but continue to purge the buffer. Data will be
3757 * transmitted when QUIC packets are detected as lost on our
3758 * side.
3759 *
3760 * TODO use fd-monitoring to detect when send operation can be
3761 * retry. This should improve the bandwidth without relying on
3762 * retransmission timer. However, it requires a major rework on
3763 * quic-conn fd management.
3764 */
3765 if (!skip_sendto) {
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003766 int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0);
3767 if (ret < 0) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003768 TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003769 qc_kill_conn(qc);
3770 b_del(buf, buf->data);
3771 goto leave;
3772 }
3773 else if (!ret) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003774 /* Connection owned socket : poller will wake us up when transient error is cleared. */
3775 if (qc_test_fd(qc)) {
3776 TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc);
3777 goto leave;
3778 }
3779
3780 /* No connection owned-socket : rely on retransmission to retry sending. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003781 skip_sendto = 1;
3782 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
3783 }
3784 }
3785
3786 b_del(buf, dglen + headlen);
3787 qc->tx.bytes += tmpbuf.data;
3788 time_sent = now_ms;
3789
3790 for (pkt = first_pkt; pkt; pkt = next_pkt) {
Frédéric Lécailleceb88b82023-02-20 14:43:55 +01003791 /* RFC 9000 14.1 Initial datagram size
3792 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3793 * Initial packets to at least the smallest allowed maximum datagram size of
3794 * 1200 bytes.
3795 */
3796 BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3797 (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
3798 dglen < QUIC_INITIAL_PACKET_MINLEN);
3799
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003800 pkt->time_sent = time_sent;
3801 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3802 pkt->pktns->tx.time_of_last_eliciting = time_sent;
3803 qc->path->ifae_pkts++;
3804 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
Frédéric Lécailled7215712023-03-24 18:13:37 +01003805 qc_idle_timer_rearm(qc, 0, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003806 }
3807 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3808 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3809 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003810 qc_detach_th_ctx_list(qc, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003811 qc_notify_close(qc);
3812
3813 /* RFC 9000 10.2. Immediate Close:
3814 * The closing and draining connection states exist to ensure
3815 * that connections close cleanly and that delayed or reordered
3816 * packets are properly discarded. These states SHOULD persist
3817 * for at least three times the current PTO interval...
3818 *
3819 * Rearm the idle timeout only one time when entering closing
3820 * state.
3821 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01003822 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003823 if (qc->timer_task) {
3824 task_destroy(qc->timer_task);
3825 qc->timer_task = NULL;
3826 }
3827 }
3828 qc->path->in_flight += pkt->in_flight_len;
3829 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3830 if (pkt->in_flight_len)
3831 qc_set_timer(qc);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003832 TRACE_PROTO("TX pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003833 next_pkt = pkt->next;
3834 quic_tx_packet_refinc(pkt);
3835 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
3836 }
3837 }
3838
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003839 ret = 1;
3840leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003841 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3842
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003843 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003844}
3845
3846/* Copy into <buf> buffer a stateless reset token depending on the
3847 * <salt> salt input. This is the cluster secret which will be derived
3848 * as HKDF input secret to generate this token.
3849 * Return 1 if succeeded, 0 if not.
3850 */
3851static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3852 unsigned char *buf, size_t len,
3853 const unsigned char *salt, size_t saltlen)
3854{
3855 /* Input secret */
3856 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3857 size_t keylen = strlen(global.cluster_secret);
3858 /* Info */
3859 const unsigned char label[] = "stateless token";
3860 size_t labellen = sizeof label - 1;
3861 int ret;
3862
3863 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3864
3865 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
3866 key, keylen, salt, saltlen, label, labellen);
3867 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3868 return ret;
3869}
3870
3871/* Initialize the stateless reset token attached to <cid> connection ID.
3872 * Returns 1 if succeeded, 0 if not.
3873 */
3874static int quic_stateless_reset_token_init(struct quic_conn *qc,
3875 struct quic_connection_id *quic_cid)
3876{
3877 int ret;
3878
3879 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3880
3881 if (global.cluster_secret) {
3882 /* Output secret */
3883 unsigned char *token = quic_cid->stateless_reset_token;
3884 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3885 /* Salt */
3886 const unsigned char *cid = quic_cid->cid.data;
3887 size_t cidlen = quic_cid->cid.len;
3888
3889 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
3890 }
3891 else {
3892 /* TODO: RAND_bytes() should be replaced */
3893 ret = RAND_bytes(quic_cid->stateless_reset_token,
3894 sizeof quic_cid->stateless_reset_token) == 1;
3895 }
3896
3897 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3898 return ret;
3899}
3900
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003901/* Generate a CID directly derived from <orig> CID and <addr> address. The CID
3902 * is then marked with the current thread ID.
3903 *
3904 * Returns a new 64-bits CID value.
3905 */
3906static uint64_t quic_derive_cid(const struct quic_cid *orig,
3907 const struct sockaddr_storage *addr)
3908{
3909 const struct sockaddr_in *in;
3910 const struct sockaddr_in6 *in6;
3911 char *buf = trash.area;
3912 size_t idx = 0;
3913 uint64_t hash;
3914
3915 /* Prepare buffer for hash using original CID first. */
3916 memcpy(buf, orig->data, orig->len);
3917 idx += orig->len;
3918
3919 /* Concatenate client address. */
3920 switch (addr->ss_family) {
3921 case AF_INET:
3922 in = (struct sockaddr_in *)addr;
3923
3924 memcpy(&buf[idx], &in->sin_addr, sizeof(in->sin_addr));
3925 idx += sizeof(in->sin_addr);
3926 memcpy(&buf[idx], &in->sin_port, sizeof(in->sin_port));
3927 idx += sizeof(in->sin_port);
3928 break;
3929
3930 case AF_INET6:
3931 in6 = (struct sockaddr_in6 *)addr;
3932
3933 memcpy(&buf[idx], &in6->sin6_addr, sizeof(in6->sin6_addr));
3934 idx += sizeof(in6->sin6_addr);
3935 memcpy(&buf[idx], &in6->sin6_port, sizeof(in6->sin6_port));
3936 idx += sizeof(in6->sin6_port);
3937 break;
3938
3939 default:
3940 /* TODO to implement */
3941 ABORT_NOW();
3942 return 0;
3943 }
3944
3945 /* Avoid similar values between multiple haproxy process. */
3946 memcpy(&buf[idx], boot_seed, sizeof(boot_seed));
3947 idx += sizeof(boot_seed);
3948
3949 /* Hash the final buffer content. */
3950 hash = XXH64(buf, idx, 0);
3951
3952 /* Mark the current thread id in the CID. */
3953 quic_pin_cid_to_tid((uchar *)&hash, tid);
3954
3955 return hash;
3956}
3957
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01003958/* Allocate a new CID and attach it to <root> ebtree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003959 *
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003960 * If <orig> and <addr> params are non null, the new CID value is directly
3961 * derived from them. Else a random value is generated. The CID is then marked
3962 * with the current thread ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003963 *
3964 * Returns the new CID if succeeded, NULL if not.
3965 */
3966static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003967 struct quic_conn *qc,
3968 const struct quic_cid *orig,
3969 const struct sockaddr_storage *addr)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003970{
3971 struct quic_connection_id *cid;
3972
3973 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3974
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003975 /* Caller must set either none or both values. */
3976 BUG_ON(!!orig != !!addr);
3977
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003978 cid = pool_alloc(pool_head_quic_connection_id);
3979 if (!cid) {
3980 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3981 goto err;
3982 }
3983
3984 cid->cid.len = QUIC_HAP_CID_LEN;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003985
3986 if (!orig) {
3987 /* TODO: RAND_bytes() should be replaced */
3988 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3989 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
3990 goto err;
3991 }
3992 quic_pin_cid_to_tid(cid->cid.data, tid);
3993 }
3994 else {
3995 /* Derive the new CID value from original CID. */
3996 const uint64_t hash = quic_derive_cid(orig, addr);
3997 memcpy(cid->cid.data, &hash, sizeof(hash));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003998 }
3999
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004000 if (quic_stateless_reset_token_init(qc, cid) != 1) {
4001 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
4002 goto err;
4003 }
4004
4005 cid->qc = qc;
4006
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004007 cid->seq_num.key = qc->next_cid_seq_num++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004008 cid->retire_prior_to = 0;
4009 /* insert the allocated CID in the quic_conn tree */
4010 eb64_insert(root, &cid->seq_num);
4011
4012 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4013 return cid;
4014
4015 err:
4016 pool_free(pool_head_quic_connection_id, cid);
4017 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4018 return NULL;
4019}
4020
4021/* Build all the frames which must be sent just after the handshake have succeeded.
4022 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
4023 * a HANDSHAKE_DONE frame.
4024 * Return 1 if succeeded, 0 if not.
4025 */
4026static int quic_build_post_handshake_frames(struct quic_conn *qc)
4027{
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004028 int ret = 0, max;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004029 struct quic_enc_level *qel;
4030 struct quic_frame *frm, *frmbak;
4031 struct list frm_list = LIST_HEAD_INIT(frm_list);
4032 struct eb64_node *node;
4033
4034 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4035
4036 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4037 /* Only servers must send a HANDSHAKE_DONE frame. */
4038 if (qc_is_listener(qc)) {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004039 frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004040 if (!frm) {
4041 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4042 goto leave;
4043 }
4044
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004045 LIST_APPEND(&frm_list, &frm->list);
4046 }
4047
4048 /* Initialize <max> connection IDs minus one: there is
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004049 * already one connection ID used for the current connection. Also limit
4050 * the number of connection IDs sent to the peer to 4 (3 from this function
4051 * plus 1 for the current connection.
4052 * Note that active_connection_id_limit >= 2: this has been already checked
4053 * when receiving this parameter.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004054 */
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004055 max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3);
4056 while (max--) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004057 struct quic_connection_id *cid;
4058
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004059 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004060 if (!frm) {
4061 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4062 goto err;
4063 }
4064
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004065 cid = new_quic_cid(&qc->cids, qc, NULL, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004066 if (!cid) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004067 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004068 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
4069 goto err;
4070 }
4071
4072 /* insert the allocated CID in the receiver datagram handler tree */
4073 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
4074
4075 quic_connection_id_to_frm_cpy(frm, cid);
4076 LIST_APPEND(&frm_list, &frm->list);
4077 }
4078
4079 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
4080 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
4081
4082 ret = 1;
4083 leave:
4084 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4085 return ret;
4086
4087 err:
4088 /* free the frames */
4089 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004090 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004091
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004092 /* The first CID sequence number value used to allocated CIDs by this function is 1,
4093 * 0 being the sequence number of the CID for this connection.
4094 */
4095 node = eb64_lookup_ge(&qc->cids, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004096 while (node) {
4097 struct quic_connection_id *cid;
4098
4099 cid = eb64_entry(node, struct quic_connection_id, seq_num);
4100 if (cid->seq_num.key >= max)
4101 break;
4102
4103 node = eb64_next(node);
4104 ebmb_delete(&cid->node);
4105 eb64_delete(&cid->seq_num);
4106 pool_free(pool_head_quic_connection_id, cid);
4107 }
4108 goto leave;
4109}
4110
4111/* Deallocate <l> list of ACK ranges. */
4112void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
4113{
4114 struct eb64_node *n;
4115 struct quic_arng_node *ar;
4116
4117 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4118
4119 n = eb64_first(&arngs->root);
4120 while (n) {
4121 struct eb64_node *next;
4122
4123 ar = eb64_entry(n, struct quic_arng_node, first);
4124 next = eb64_next(n);
4125 eb64_delete(n);
4126 pool_free(pool_head_quic_arng, ar);
4127 n = next;
4128 }
4129
4130 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4131}
4132
4133/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
4134 * descending order.
4135 */
4136static inline size_t sack_gap(struct quic_arng_node *p,
4137 struct quic_arng_node *q)
4138{
4139 return p->first.key - q->last - 2;
4140}
4141
4142
4143/* Remove the last elements of <ack_ranges> list of ack range updating its
4144 * encoded size until it goes below <limit>.
4145 * Returns 1 if succeeded, 0 if not (no more element to remove).
4146 */
4147static int quic_rm_last_ack_ranges(struct quic_conn *qc,
4148 struct quic_arngs *arngs, size_t limit)
4149{
4150 int ret = 0;
4151 struct eb64_node *last, *prev;
4152
4153 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4154
4155 last = eb64_last(&arngs->root);
4156 while (last && arngs->enc_sz > limit) {
4157 struct quic_arng_node *last_node, *prev_node;
4158
4159 prev = eb64_prev(last);
4160 if (!prev) {
4161 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
4162 goto out;
4163 }
4164
4165 last_node = eb64_entry(last, struct quic_arng_node, first);
4166 prev_node = eb64_entry(prev, struct quic_arng_node, first);
4167 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
4168 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
4169 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
4170 --arngs->sz;
4171 eb64_delete(last);
4172 pool_free(pool_head_quic_arng, last);
4173 last = prev;
4174 }
4175
4176 ret = 1;
4177 out:
4178 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4179 return ret;
4180}
4181
4182/* Set the encoded size of <arngs> QUIC ack ranges. */
4183static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
4184{
4185 struct eb64_node *node, *next;
4186 struct quic_arng_node *ar, *ar_next;
4187
4188 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4189
4190 node = eb64_last(&arngs->root);
4191 if (!node)
4192 goto leave;
4193
4194 ar = eb64_entry(node, struct quic_arng_node, first);
4195 arngs->enc_sz = quic_int_getsize(ar->last) +
4196 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
4197
4198 while ((next = eb64_prev(node))) {
4199 ar_next = eb64_entry(next, struct quic_arng_node, first);
4200 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
4201 quic_int_getsize(ar_next->last - ar_next->first.key);
4202 node = next;
4203 ar = eb64_entry(node, struct quic_arng_node, first);
4204 }
4205
4206 leave:
4207 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4208}
4209
4210/* Insert <ar> ack range into <argns> tree of ack ranges.
4211 * Returns the ack range node which has been inserted if succeeded, NULL if not.
4212 */
4213static inline
4214struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
4215 struct quic_arngs *arngs,
4216 struct quic_arng *ar)
4217{
4218 struct quic_arng_node *new_ar;
4219
4220 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4221
4222 new_ar = pool_alloc(pool_head_quic_arng);
4223 if (!new_ar) {
4224 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
4225 goto leave;
4226 }
4227
4228 new_ar->first.key = ar->first;
4229 new_ar->last = ar->last;
4230 eb64_insert(&arngs->root, &new_ar->first);
4231 arngs->sz++;
4232
4233 leave:
4234 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4235 return new_ar;
4236}
4237
4238/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
4239 * Note that this function computes the number of bytes required to encode
4240 * this tree of ACK ranges in descending order.
4241 *
4242 * Descending order
4243 * ------------->
4244 * range1 range2
4245 * ..........|--------|..............|--------|
4246 * ^ ^ ^ ^
4247 * | | | |
4248 * last1 first1 last2 first2
4249 * ..........+--------+--------------+--------+......
4250 * diff1 gap12 diff2
4251 *
4252 * To encode the previous list of ranges we must encode integers as follows in
4253 * descending order:
4254 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
4255 * with diff1 = last1 - first1
4256 * diff2 = last2 - first2
4257 * gap12 = first1 - last2 - 2 (>= 0)
4258 *
4259
4260returns 0 on error
4261
4262 */
4263int quic_update_ack_ranges_list(struct quic_conn *qc,
4264 struct quic_arngs *arngs,
4265 struct quic_arng *ar)
4266{
4267 int ret = 0;
4268 struct eb64_node *le;
4269 struct quic_arng_node *new_node;
4270 struct eb64_node *new;
4271
4272 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4273
4274 new = NULL;
4275 if (eb_is_empty(&arngs->root)) {
4276 new_node = quic_insert_new_range(qc, arngs, ar);
4277 if (new_node)
4278 ret = 1;
4279
4280 goto leave;
4281 }
4282
4283 le = eb64_lookup_le(&arngs->root, ar->first);
4284 if (!le) {
4285 new_node = quic_insert_new_range(qc, arngs, ar);
4286 if (!new_node)
4287 goto leave;
4288
4289 new = &new_node->first;
4290 }
4291 else {
4292 struct quic_arng_node *le_ar =
4293 eb64_entry(le, struct quic_arng_node, first);
4294
4295 /* Already existing range */
4296 if (le_ar->last >= ar->last) {
4297 ret = 1;
4298 }
4299 else if (le_ar->last + 1 >= ar->first) {
4300 le_ar->last = ar->last;
4301 new = le;
4302 new_node = le_ar;
4303 }
4304 else {
4305 new_node = quic_insert_new_range(qc, arngs, ar);
4306 if (!new_node)
4307 goto leave;
4308
4309 new = &new_node->first;
4310 }
4311 }
4312
4313 /* Verify that the new inserted node does not overlap the nodes
4314 * which follow it.
4315 */
4316 if (new) {
4317 struct eb64_node *next;
4318 struct quic_arng_node *next_node;
4319
4320 while ((next = eb64_next(new))) {
4321 next_node =
4322 eb64_entry(next, struct quic_arng_node, first);
4323 if (new_node->last + 1 < next_node->first.key)
4324 break;
4325
4326 if (next_node->last > new_node->last)
4327 new_node->last = next_node->last;
4328 eb64_delete(next);
4329 pool_free(pool_head_quic_arng, next_node);
4330 /* Decrement the size of these ranges. */
4331 arngs->sz--;
4332 }
4333 }
4334
4335 ret = 1;
4336 leave:
4337 quic_arngs_set_enc_sz(qc, arngs);
4338 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4339 return ret;
4340}
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004341
4342/* Detect the value of the spin bit to be used. */
4343static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt,
4344 struct quic_enc_level *qel)
4345{
4346 uint64_t largest_pn = qel->pktns->rx.largest_pn;
4347
4348 if (qel != &qc->els[QUIC_TLS_ENC_LEVEL_APP] || largest_pn == -1 ||
4349 pkt->pn <= largest_pn)
4350 return;
4351
4352 if (qc_is_listener(qc)) {
4353 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4354 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4355 else
4356 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4357 }
4358 else {
4359 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4360 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4361 else
4362 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4363 }
4364}
4365
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004366/* Remove the header protection of packets at <el> encryption level.
4367 * Always succeeds.
4368 */
4369static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
4370{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004371 struct quic_rx_packet *pqpkt, *pkttmp;
4372 struct quic_enc_level *app_qel;
4373
4374 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
4375 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4376 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
4377 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004378 TRACE_PROTO("RX hp not removed (handshake not completed)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004379 QUIC_EV_CONN_ELRMHP, qc);
4380 goto out;
4381 }
Frédéric Lécaille72027782023-02-22 16:20:09 +01004382
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004383 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01004384 struct quic_tls_ctx *tls_ctx;
4385
4386 tls_ctx = qc_select_tls_ctx(qc, el, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004387 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
4388 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004389 TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004390 }
4391 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004392 qc_handle_spin_bit(qc, pqpkt, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004393 /* The AAD includes the packet number field */
4394 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
4395 /* Store the packet into the tree of packets to decrypt. */
4396 pqpkt->pn_node.key = pqpkt->pn;
4397 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
4398 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004399 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004400 }
4401 LIST_DELETE(&pqpkt->list);
4402 quic_rx_packet_refdec(pqpkt);
4403 }
4404
4405 out:
4406 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
4407}
4408
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004409/* Process all the CRYPTO frame at <el> encryption level. This is the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004410 * responsibility of the called to ensure there exists a CRYPTO data
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004411 * stream for this level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004412 * Return 1 if succeeded, 0 if not.
4413 */
4414static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
4415 struct quic_enc_level *el,
4416 struct ssl_sock_ctx *ctx)
4417{
4418 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004419 struct ncbuf *ncbuf;
4420 struct quic_cstream *cstream = el->cstream;
4421 ncb_sz_t data;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004422
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004423 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004424
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004425 BUG_ON(!cstream);
4426 ncbuf = &cstream->rx.ncbuf;
4427 if (ncb_is_null(ncbuf))
4428 goto done;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004429
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004430 /* TODO not working if buffer is wrapping */
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004431 while ((data = ncb_data(ncbuf, 0))) {
4432 const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004433
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004434 if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004435 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004436
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004437 cstream->rx.offset += data;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004438 TRACE_DEVEL("buffered crypto data were provided to TLS stack",
4439 QUIC_EV_CONN_PHPKTS, qc, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004440 }
4441
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004442 done:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004443 ret = 1;
4444 leave:
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004445 if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
4446 TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004447 quic_free_ncbuf(ncbuf);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004448 }
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004449 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004450 return ret;
4451}
4452
4453/* Process all the packets at <el> and <next_el> encryption level.
4454 * This is the caller responsibility to check that <cur_el> is different of <next_el>
4455 * as pointer value.
4456 * Return 1 if succeeded, 0 if not.
4457 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004458int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el,
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004459 struct quic_enc_level *next_el)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004460{
4461 int ret = 0;
4462 struct eb64_node *node;
4463 int64_t largest_pn = -1;
4464 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004465 struct quic_enc_level *qel = cur_el;
4466
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004467 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004468 qel = cur_el;
4469 next_tel:
4470 if (!qel)
4471 goto out;
4472
4473 node = eb64_first(&qel->rx.pkts);
4474 while (node) {
4475 struct quic_rx_packet *pkt;
4476
4477 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
4478 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004479 qc, pkt, NULL, qc->xprt_ctx->ssl);
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01004480 if (!qc_pkt_decrypt(qc, qel, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004481 /* Drop the packet */
4482 TRACE_ERROR("packet decryption failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004483 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004484 }
4485 else {
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004486 if (!qc_parse_pkt_frms(qc, pkt, qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004487 /* Drop the packet */
4488 TRACE_ERROR("packet parsing failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004489 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004490 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
4491 }
4492 else {
4493 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
4494
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004495 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004496 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
4497 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécailled7215712023-03-24 18:13:37 +01004498 qc_idle_timer_rearm(qc, 1, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004499 }
4500 if (pkt->pn > largest_pn) {
4501 largest_pn = pkt->pn;
4502 largest_pn_time_received = pkt->time_received;
4503 }
4504 /* Update the list of ranges to acknowledge. */
4505 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
4506 TRACE_ERROR("Could not update ack range list",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004507 QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004508 }
4509 }
4510 node = eb64_next(node);
4511 eb64_delete(&pkt->pn_node);
4512 quic_rx_packet_refdec(pkt);
4513 }
4514
4515 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
4516 /* Update the largest packet number. */
4517 qel->pktns->rx.largest_pn = largest_pn;
4518 /* Update the largest acknowledged packet timestamps */
4519 qel->pktns->rx.largest_time_received = largest_pn_time_received;
4520 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
4521 }
4522
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004523 if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004524 // trace already emitted by function above
4525 goto leave;
4526 }
4527
4528 if (qel == cur_el) {
4529 BUG_ON(qel == next_el);
4530 qel = next_el;
4531 largest_pn = -1;
4532 goto next_tel;
4533 }
4534
4535 out:
4536 ret = 1;
4537 leave:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004538 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004539 return ret;
4540}
4541
4542/* Check if it's possible to remove header protection for packets related to
4543 * encryption level <qel>. If <qel> is NULL, assume it's false.
4544 *
4545 * Return true if the operation is possible else false.
4546 */
4547static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
4548{
4549 int ret = 0;
4550 enum quic_tls_enc_level tel;
4551
4552 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
4553
4554 if (!qel)
4555 goto cant_rm_hp;
4556
4557 tel = ssl_to_quic_enc_level(qel->level);
4558
4559 /* check if tls secrets are available */
4560 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004561 TRACE_PROTO("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004562 goto cant_rm_hp;
4563 }
4564
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004565 if (!quic_tls_has_rx_sec(qel)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004566 TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004567 goto cant_rm_hp;
4568 }
4569
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004570 if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004571 TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004572 goto cant_rm_hp;
4573 }
4574
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004575 /* check if the connection layer is ready before using app level */
4576 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
4577 qc->mux_state == QC_MUX_NULL) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004578 TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004579 goto cant_rm_hp;
4580 }
4581
4582 ret = 1;
4583 cant_rm_hp:
4584 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
4585 return ret;
4586}
4587
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004588/* Flush txbuf for <qc> connection. This must be called prior to a packet
4589 * preparation when txbuf contains older data. A send will be conducted for
4590 * these data.
4591 *
4592 * Returns 1 on success : buffer is empty and can be use for packet
4593 * preparation. On error 0 is returned.
4594 */
4595static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
4596{
4597 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4598
4599 /* This operation can only be conducted if txbuf is not empty. This
4600 * case only happens for connection with their owned socket due to an
4601 * older transient sendto() error.
4602 */
4603 BUG_ON(!qc_test_fd(qc));
4604
4605 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4606 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4607 qc_txb_release(qc);
4608 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4609 return 0;
4610 }
4611
4612 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4613 return 1;
4614}
4615
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004616/* Try to send application frames from list <frms> on connection <qc>.
4617 *
4618 * Use qc_send_app_probing wrapper when probing with old data.
4619 *
4620 * Returns 1 on success. Some data might not have been sent due to congestion,
4621 * in this case they are left in <frms> input list. The caller may subscribe on
4622 * quic-conn to retry later.
4623 *
4624 * Returns 0 on critical error.
4625 * TODO review and classify more distinctly transient from definitive errors to
4626 * allow callers to properly handle it.
4627 */
4628static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
4629{
4630 int status = 0;
4631 struct buffer *buf;
4632
4633 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4634
4635 buf = qc_txb_alloc(qc);
4636 if (!buf) {
4637 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004638 goto err;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004639 }
4640
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004641 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4642 goto err;
4643
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004644 /* Prepare and send packets until we could not further prepare packets. */
4645 while (1) {
4646 int ret;
4647 /* Currently buf cannot be non-empty at this stage. Even if a
4648 * previous sendto() has failed it is emptied to simulate
4649 * packet emission and rely on QUIC lost detection to try to
4650 * emit it.
4651 */
4652 BUG_ON_HOT(b_data(buf));
4653 b_reset(buf);
4654
4655 ret = qc_prep_app_pkts(qc, buf, frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004656 if (ret == -1) {
4657 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004658 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004659 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004660
Amaury Denoyelle37333862023-02-28 11:53:48 +01004661 if (!ret)
4662 break;
4663
4664 if (!qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004665 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4666 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004667 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004668 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004669 }
4670
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004671 status = 1;
4672 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004673 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4674 return status;
4675
4676 err:
Amaury Denoyelle37333862023-02-28 11:53:48 +01004677 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4678 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004679}
4680
4681/* Try to send application frames from list <frms> on connection <qc>. Use this
4682 * function when probing is required.
4683 *
4684 * Returns the result from qc_send_app_pkts function.
4685 */
4686static forceinline int qc_send_app_probing(struct quic_conn *qc,
4687 struct list *frms)
4688{
4689 int ret;
4690
4691 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4692
4693 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
4694 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4695 ret = qc_send_app_pkts(qc, frms);
4696 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
4697
4698 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4699 return ret;
4700}
4701
4702/* Try to send application frames from list <frms> on connection <qc>. This
4703 * function is provided for MUX upper layer usage only.
4704 *
4705 * Returns the result from qc_send_app_pkts function.
4706 */
4707int qc_send_mux(struct quic_conn *qc, struct list *frms)
4708{
4709 int ret;
4710
4711 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4712 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
4713
4714 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4715 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
4716 ret = qc_send_app_pkts(qc, frms);
4717 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
4718
4719 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4720 return ret;
4721}
4722
4723/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4724 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
4725 * QUIC connection. <old_data> is used as boolean to send data already sent but
4726 * not already acknowledged (in flight).
4727 * Returns 1 if succeeded, 0 if not.
4728 */
4729int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4730 enum quic_tls_enc_level tel, struct list *tel_frms,
4731 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4732{
4733 int ret, status = 0;
4734 struct buffer *buf = qc_txb_alloc(qc);
4735
4736 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4737
4738 if (!buf) {
4739 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4740 goto leave;
4741 }
4742
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004743 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4744 goto out;
4745
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004746 /* Currently buf cannot be non-empty at this stage. Even if a previous
4747 * sendto() has failed it is emptied to simulate packet emission and
4748 * rely on QUIC lost detection to try to emit it.
4749 */
4750 BUG_ON_HOT(b_data(buf));
4751 b_reset(buf);
4752
4753 if (old_data) {
4754 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
4755 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4756 }
4757
4758 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004759 if (ret == -1) {
4760 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004761 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004762 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004763
Amaury Denoyelle37333862023-02-28 11:53:48 +01004764 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004765 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4766 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004767 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004768 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004769
Amaury Denoyelle37333862023-02-28 11:53:48 +01004770 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004771 status = 1;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004772
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004773 out:
4774 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
4775 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004776 leave:
4777 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4778 return status;
4779}
4780
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004781/* Retransmit up to two datagrams depending on packet number space.
4782 * Return 0 when failed, 0 if not.
4783 */
4784static int qc_dgrams_retransmit(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004785{
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004786 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004787 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4788 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4789 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4790
4791 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4792
4793 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004794 int i;
4795
4796 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4797 struct list ifrms = LIST_HEAD_INIT(ifrms);
4798 struct list hfrms = LIST_HEAD_INIT(hfrms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004799
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004800 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
4801 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4802 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
4803 if (!LIST_ISEMPTY(&ifrms)) {
4804 iqel->pktns->tx.pto_probe = 1;
4805 if (!LIST_ISEMPTY(&hfrms))
4806 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004807 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4808 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms))
4809 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004810 /* Put back unsent frames in their packet number spaces */
4811 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4812 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
4813 }
Frédéric Lécailleec937212023-03-03 17:34:41 +01004814 else {
4815 if (!(qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
4816 iqel->pktns->tx.pto_probe = 1;
4817 if (!qc_send_hdshk_pkts(qc, 0, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4818 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4819 goto leave;
4820 }
4821 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004822 }
4823 TRACE_STATE("no more need to probe Initial packet number space",
4824 QUIC_EV_CONN_TXPKT, qc);
4825 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004826 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004827 }
4828 else {
4829 int i;
4830
4831 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004832 hqel->pktns->tx.pto_probe = 0;
4833 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
Frédéric Lécaille7b5d9b12022-11-28 17:21:45 +01004834 struct list frms1 = LIST_HEAD_INIT(frms1);
4835
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004836 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
4837 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4838 if (!LIST_ISEMPTY(&frms1)) {
4839 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004840 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4841 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4842 goto leave;
4843
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004844 /* Put back unsent frames into their packet number spaces */
4845 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
4846 }
4847 }
4848 TRACE_STATE("no more need to probe Handshake packet number space",
4849 QUIC_EV_CONN_TXPKT, qc);
4850 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4851 }
4852 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4853 struct list frms2 = LIST_HEAD_INIT(frms2);
4854 struct list frms1 = LIST_HEAD_INIT(frms1);
4855
4856 aqel->pktns->tx.pto_probe = 0;
4857 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4858 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4859 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4860 if (!LIST_ISEMPTY(&frms1)) {
4861 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004862 if (!qc_send_app_probing(qc, &frms1))
4863 goto leave;
4864
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004865 /* Put back unsent frames into their packet number spaces */
4866 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
4867 }
4868 if (!LIST_ISEMPTY(&frms2)) {
4869 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004870 if (!qc_send_app_probing(qc, &frms2))
4871 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004872 /* Put back unsent frames into their packet number spaces */
4873 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
4874 }
4875 TRACE_STATE("no more need to probe 01RTT packet number space",
4876 QUIC_EV_CONN_TXPKT, qc);
4877 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4878 }
4879 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004880
4881 ret = 1;
4882 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004883 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004884 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004885}
4886
4887/* QUIC connection packet handler task (post handshake) */
4888struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4889{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004890 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004891 struct quic_enc_level *qel;
4892
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004893 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4894
4895 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4896 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
4897
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01004898 if (qc_test_fd(qc))
4899 qc_rcv_buf(qc);
4900
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004901 /* Retranmissions */
4902 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
4903 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
4904 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004905 if (!qc_dgrams_retransmit(qc))
4906 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004907 }
4908
4909 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4910 qc_rm_hp_pkts(qc, qel);
4911
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004912 if (!qc_treat_rx_pkts(qc, qel, NULL)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004913 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4914 goto out;
4915 }
4916
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01004917 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
4918 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
4919 goto out;
4920 }
4921
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004922 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4923 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4924 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
4925 goto out;
4926 }
4927
4928 /* XXX TODO: how to limit the list frames to send */
4929 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4930 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4931 goto out;
4932 }
4933
4934 out:
4935 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4936 return t;
4937}
4938
4939/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4940static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4941{
4942 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4943 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4944 qel->pktns->tx.pto_probe ||
4945 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4946}
4947
4948/* QUIC connection packet handler task. */
4949struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
4950{
4951 int ret, ssl_err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004952 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004953 enum quic_tls_enc_level tel, next_tel;
4954 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004955 /* Early-data encryption level */
4956 struct quic_enc_level *eqel;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004957 struct buffer *buf = NULL;
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004958 int st, zero_rtt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004959
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004960 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004961 eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004962 st = qc->state;
4963 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
4964
4965 /* Retranmissions */
4966 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
4967 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
4968 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004969 if (!qc_dgrams_retransmit(qc))
4970 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004971 }
4972
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004973 ssl_err = SSL_ERROR_NONE;
4974 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004975 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004976 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01004977
4978 if (qc_test_fd(qc))
4979 qc_rcv_buf(qc);
4980
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004981 if (st >= QUIC_HS_ST_COMPLETE &&
4982 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
4983 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
4984 /* There may be remaining Handshake packets to treat and acknowledge. */
4985 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4986 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4987 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004988 else if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, zero_rtt))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004989 goto out;
4990
4991 qel = &qc->els[tel];
4992 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
4993
4994 next_level:
4995 /* Treat packets waiting for header packet protection decryption */
4996 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4997 qc_rm_hp_pkts(qc, qel);
4998
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004999 if (!qc_treat_rx_pkts(qc, qel, next_qel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005000 goto out;
5001
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005002 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5003 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
5004 goto out;
5005 }
5006
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005007 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5008 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
5009 goto out;
5010
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005011 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005012 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005013 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
5014 if (next_qel && next_qel == eqel && zero_rtt) {
5015 TRACE_DEVEL("select 0RTT as next encryption level",
5016 QUIC_EV_CONN_PHPKTS, qc);
5017 qel = next_qel;
5018 next_qel = NULL;
5019 goto next_level;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005020 }
5021
5022 st = qc->state;
5023 if (st >= QUIC_HS_ST_COMPLETE) {
5024 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
5025 !quic_build_post_handshake_frames(qc))
5026 goto out;
5027
5028 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
5029 QUIC_FL_TLS_SECRETS_DCD)) {
5030 /* Discard the Handshake keys. */
5031 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5032 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
5033 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
5034 qc_set_timer(qc);
5035 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5036 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
5037 }
5038
5039 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
5040 /* There may be remaining handshake to build (acks) */
5041 st = QUIC_HS_ST_SERVER_HANDSHAKE;
5042 }
5043 }
5044
5045 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
5046 * be considered.
5047 */
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005048 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005049 goto out;
5050
5051 if (!qc_need_sending(qc, qel) &&
5052 (!next_qel || !qc_need_sending(qc, next_qel))) {
5053 goto skip_send;
5054 }
5055
5056 buf = qc_txb_alloc(qc);
5057 if (!buf)
5058 goto out;
5059
Amaury Denoyelle147862d2023-02-28 15:10:00 +01005060 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
5061 goto skip_send;
5062
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005063 /* Currently buf cannot be non-empty at this stage. Even if a previous
5064 * sendto() has failed it is emptied to simulate packet emission and
5065 * rely on QUIC lost detection to try to emit it.
5066 */
5067 BUG_ON_HOT(b_data(buf));
5068 b_reset(buf);
5069
5070 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
5071 next_tel, &qc->els[next_tel].pktns->tx.frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01005072 if (ret == -1) {
5073 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005074 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005075 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005076
Amaury Denoyelle37333862023-02-28 11:53:48 +01005077 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01005078 if (qc->flags & QUIC_FL_CONN_TO_KILL)
5079 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005080 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005081 }
5082
5083 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005084
5085 skip_send:
5086 /* Check if there is something to do for the next level.
5087 */
5088 if (next_qel && next_qel != qel &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005089 quic_tls_has_rx_sec(next_qel) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005090 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
5091 qel = next_qel;
5092 next_qel = NULL;
5093 goto next_level;
5094 }
5095
5096 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005097 TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
5098 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005099 return t;
5100}
5101
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005102/* Release the memory allocated for <cs> CRYPTO stream */
5103void quic_cstream_free(struct quic_cstream *cs)
5104{
5105 if (!cs) {
5106 /* This is the case for ORTT encryption level */
5107 return;
5108 }
5109
Amaury Denoyellebc174b22022-11-17 10:12:52 +01005110 quic_free_ncbuf(&cs->rx.ncbuf);
5111
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005112 qc_stream_desc_release(cs->desc);
5113 pool_free(pool_head_quic_cstream, cs);
5114}
5115
5116/* Allocate a new QUIC stream for <qc>.
5117 * Return it if succeeded, NULL if not.
5118 */
5119struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
5120{
5121 struct quic_cstream *cs, *ret_cs = NULL;
5122
5123 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5124 cs = pool_alloc(pool_head_quic_cstream);
5125 if (!cs) {
5126 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5127 goto leave;
5128 }
5129
5130 cs->rx.offset = 0;
5131 cs->rx.ncbuf = NCBUF_NULL;
5132 cs->rx.offset = 0;
5133
5134 cs->tx.offset = 0;
5135 cs->tx.sent_offset = 0;
5136 cs->tx.buf = BUF_NULL;
5137 cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
5138 if (!cs->desc) {
5139 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5140 goto err;
5141 }
5142
5143 ret_cs = cs;
5144 leave:
5145 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5146 return ret_cs;
5147
5148 err:
5149 pool_free(pool_head_quic_cstream, cs);
5150 goto leave;
5151}
5152
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005153/* Uninitialize <qel> QUIC encryption level. Never fails. */
5154static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
5155{
5156 int i;
5157
5158 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5159
5160 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
5161 if (qel->tx.crypto.bufs[i]) {
5162 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
5163 qel->tx.crypto.bufs[i] = NULL;
5164 }
5165 }
5166 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005167 quic_cstream_free(qel->cstream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005168
5169 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5170}
5171
5172/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
5173 * connection allocating everything needed.
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005174 *
5175 * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use
5176 * quic_conn_enc_level_uninit() to cleanup partially allocated content.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005177 */
5178static int quic_conn_enc_level_init(struct quic_conn *qc,
5179 enum quic_tls_enc_level level)
5180{
5181 int ret = 0;
5182 struct quic_enc_level *qel;
5183
5184 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5185
5186 qel = &qc->els[level];
5187 qel->level = quic_to_ssl_enc_level(level);
5188 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
5189 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
5190 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
5191 qel->tls_ctx.flags = 0;
5192
5193 qel->rx.pkts = EB_ROOT;
5194 LIST_INIT(&qel->rx.pqpkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005195
5196 /* Allocate only one buffer. */
5197 /* TODO: use a pool */
5198 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
5199 if (!qel->tx.crypto.bufs)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005200 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005201
5202 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
5203 if (!qel->tx.crypto.bufs[0])
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005204 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005205
5206 qel->tx.crypto.bufs[0]->sz = 0;
5207 qel->tx.crypto.nb_buf = 1;
5208
5209 qel->tx.crypto.sz = 0;
5210 qel->tx.crypto.offset = 0;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005211 /* No CRYPTO data for early data TLS encryption level */
5212 if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA)
5213 qel->cstream = NULL;
5214 else {
5215 qel->cstream = quic_cstream_new(qc);
5216 if (!qel->cstream)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005217 goto leave;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005218 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005219
5220 ret = 1;
5221 leave:
5222 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5223 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005224}
5225
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005226/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
5227 * This is not the case if the remote peer address is not validated and if
5228 * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
5229 */
5230static int qc_may_probe_ipktns(struct quic_conn *qc)
5231{
5232 return quic_peer_validated_addr(qc) ||
5233 (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
5234}
5235
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005236/* Callback called upon loss detection and PTO timer expirations. */
5237struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
5238{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005239 struct quic_conn *qc = ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005240 struct quic_pktns *pktns;
5241
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005242 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
5243 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005244 NULL, NULL, &qc->path->ifae_pkts);
5245 task->expire = TICK_ETERNITY;
5246 pktns = quic_loss_pktns(qc);
5247 if (tick_isset(pktns->tx.loss_time)) {
5248 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
5249
5250 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
5251 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005252 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01005253 if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
5254 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005255 goto out;
5256 }
5257
5258 if (qc->path->in_flight) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +01005259 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
Frédéric Lécaille68737312023-04-07 16:28:46 +02005260 if (!pktns->tx.in_flight) {
5261 TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
5262 goto out;
5263 }
5264
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005265 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
5266 if (qc_may_probe_ipktns(qc)) {
5267 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5268 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5269 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5270 }
5271 else {
5272 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5273 }
5274 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
5275 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5276 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5277 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5278 }
Frédéric Lécaillee25fce02023-03-20 17:23:19 +01005279 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005280 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
5281 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5282 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5283 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5284 if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005285 if (qc_may_probe_ipktns(qc)) {
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005286 qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005287 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5288 }
5289 else {
5290 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5291 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005292 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005293 }
5294 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01005295 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005296 /* Wake up upper layer if waiting to send new data. */
5297 if (!qc_notify_send(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005298 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005299 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5300 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005301 }
5302 }
5303 }
5304 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
5305 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
5306 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
5307
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005308 if (quic_tls_has_tx_sec(hel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005309 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005310 if (quic_tls_has_tx_sec(iel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005311 iel->pktns->tx.pto_probe = 1;
5312 }
5313
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005314 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005315 qc->path->loss.pto_count++;
5316
5317 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005318 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
5319 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005320
5321 return task;
5322}
5323
5324/* Parse the Retry token from buffer <token> with <end> a pointer to
5325 * one byte past the end of this buffer. This will extract the ODCID
5326 * which will be stored into <odcid>
5327 *
5328 * Returns 0 on success else non-zero.
5329 */
5330static int parse_retry_token(struct quic_conn *qc,
5331 const unsigned char *token, const unsigned char *end,
5332 struct quic_cid *odcid)
5333{
5334 int ret = 0;
5335 uint64_t odcid_len;
5336 uint32_t timestamp;
5337
5338 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5339
5340 if (!quic_dec_int(&odcid_len, &token, end)) {
5341 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
5342 goto leave;
5343 }
5344
5345 /* RFC 9000 7.2. Negotiating Connection IDs:
5346 * When an Initial packet is sent by a client that has not previously
5347 * received an Initial or Retry packet from the server, the client
5348 * populates the Destination Connection ID field with an unpredictable
5349 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5350 */
5351 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
5352 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
5353 goto leave;
5354 }
5355
5356 if (end - token < odcid_len + sizeof timestamp) {
5357 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
5358 goto leave;
5359 }
5360
5361 timestamp = ntohl(read_u32(token + odcid_len));
5362 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
5363 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
5364 goto leave;
5365 }
5366
5367 ret = 1;
5368 memcpy(odcid->data, token, odcid_len);
5369 odcid->len = odcid_len;
5370 leave:
5371 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5372 return !ret;
5373}
5374
5375/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
5376 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
5377 * for QUIC servers (or haproxy listeners).
5378 * <dcid> is the destination connection ID, <scid> is the source connection ID,
5379 * <token> the token found to be used for this connection with <token_len> as
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005380 * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005381 * Returns the connection if succeeded, NULL if not.
5382 */
5383static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
5384 struct quic_cid *dcid, struct quic_cid *scid,
5385 const struct quic_cid *token_odcid,
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005386 struct sockaddr_storage *local_addr,
5387 struct sockaddr_storage *peer_addr,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005388 int server, int token, void *owner)
5389{
5390 int i;
5391 struct quic_conn *qc;
5392 /* Initial CID. */
5393 struct quic_connection_id *icid;
5394 char *buf_area = NULL;
5395 struct listener *l = NULL;
5396 struct quic_cc_algo *cc_algo = NULL;
5397 struct quic_tls_ctx *ictx;
5398 TRACE_ENTER(QUIC_EV_CONN_INIT);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005399 /* TODO replace pool_zalloc by pool_alloc(). This requires special care
5400 * to properly initialized internal quic_conn members to safely use
5401 * quic_conn_release() on alloc failure.
5402 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005403 qc = pool_zalloc(pool_head_quic_conn);
5404 if (!qc) {
5405 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
5406 goto err;
5407 }
5408
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005409 /* Initialize in priority qc members required for a safe dealloc. */
5410
5411 /* required to use MTLIST_IN_LIST */
5412 MT_LIST_INIT(&qc->accept_list);
5413
5414 LIST_INIT(&qc->rx.pkt_list);
5415
Amaury Denoyelle42448332022-12-12 11:24:05 +01005416 qc_init_fd(qc);
5417
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005418 LIST_INIT(&qc->back_refs);
5419
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005420 /* Now proceeds to allocation of qc members. */
5421
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005422 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
5423 if (!buf_area) {
5424 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
5425 goto err;
5426 }
5427
5428 qc->cids = EB_ROOT;
5429 /* QUIC Server (or listener). */
5430 if (server) {
5431 struct proxy *prx;
5432
5433 l = owner;
5434 prx = l->bind_conf->frontend;
5435 cc_algo = l->bind_conf->quic_cc_algo;
5436
5437 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
5438 &quic_stats_module);
5439 qc->flags |= QUIC_FL_CONN_LISTENER;
5440 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005441 /* Copy the client original DCID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005442 qc->odcid.len = dcid->len;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005443 memcpy(qc->odcid.data, dcid->data, dcid->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005444
5445 /* copy the packet SCID to reuse it as DCID for sending */
5446 if (scid->len)
5447 memcpy(qc->dcid.data, scid->data, scid->len);
5448 qc->dcid.len = scid->len;
5449 qc->tx.buf = BUF_NULL;
5450 qc->li = l;
5451 }
5452 /* QUIC Client (outgoing connection to servers) */
5453 else {
5454 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
5455 if (dcid->len)
5456 memcpy(qc->dcid.data, dcid->data, dcid->len);
5457 qc->dcid.len = dcid->len;
5458 }
5459 qc->mux_state = QC_MUX_NULL;
5460 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
5461
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01005462 /* Initialize the next CID sequence number to be used for this connection. */
5463 qc->next_cid_seq_num = 0;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02005464 /* Generate the first connection CID. This is derived from the client
5465 * ODCID and address. This allows to retrieve the connection from the
5466 * ODCID without storing it in the CID tree. This is an interesting
5467 * optimization as the client is expected to stop using its ODCID in
5468 * favor of our generated value.
5469 */
5470 icid = new_quic_cid(&qc->cids, qc, dcid, peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005471 if (!icid) {
5472 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
5473 goto err;
5474 }
5475
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005476 if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
5477 is_addr(local_addr)) {
5478 TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
5479 qc_alloc_fd(qc, local_addr, peer_addr);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005480
5481 /* haproxy soft-stop is supported only for QUIC connections
5482 * with their owned socket.
5483 */
5484 if (qc_test_fd(qc))
5485 _HA_ATOMIC_INC(&jobs);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005486 }
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005487
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005488 /* insert the allocated CID in the receiver datagram handler tree */
5489 if (server)
5490 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
5491
5492 /* Select our SCID which is the first CID with 0 as sequence number. */
5493 qc->scid = icid->cid;
5494
5495 /* Packet number spaces initialization. */
5496 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
5497 quic_pktns_init(&qc->pktns[i]);
5498 /* QUIC encryption level context initialization. */
5499 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5500 if (!quic_conn_enc_level_init(qc, i)) {
5501 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
5502 goto err;
5503 }
5504 /* Initialize the packet number space. */
5505 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
5506 }
5507
5508 qc->original_version = qv;
5509 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
5510 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
5511 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
5512 /* TX part. */
5513 LIST_INIT(&qc->tx.frms_to_send);
5514 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
5515 qc->tx.wbuf = qc->tx.rbuf = 0;
5516 qc->tx.bytes = 0;
5517 qc->tx.buf = BUF_NULL;
5518 /* RX part. */
5519 qc->rx.bytes = 0;
5520 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
5521 for (i = 0; i < QCS_MAX_TYPES; i++)
5522 qc->rx.strms[i].nb_streams = 0;
5523
5524 qc->nb_pkt_for_cc = 1;
5525 qc->nb_pkt_since_cc = 0;
5526
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005527 if (!quic_tls_ku_init(qc)) {
5528 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
5529 goto err;
5530 }
5531
5532 /* XXX TO DO: Only one path at this time. */
5533 qc->path = &qc->paths[0];
5534 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
5535
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005536 qc->streams_by_id = EB_ROOT_UNIQUE;
5537 qc->stream_buf_count = 0;
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005538 memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
5539 memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005540
5541 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
5542 icid->stateless_reset_token,
5543 dcid->data, dcid->len,
5544 qc->scid.data, qc->scid.len, token_odcid))
5545 goto err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005546
Frédéric Lécailledeb97812023-03-22 11:29:45 +01005547 /* Initialize the idle timeout of the connection at the "max_idle_timeout"
5548 * value from local transport parameters.
5549 */
5550 qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005551 qc->wait_event.tasklet = tasklet_new();
5552 if (!qc->wait_event.tasklet) {
5553 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
5554 goto err;
5555 }
5556 qc->wait_event.tasklet->process = quic_conn_io_cb;
5557 qc->wait_event.tasklet->context = qc;
5558 qc->wait_event.events = 0;
5559 /* Set tasklet tid based on the SCID selected by us for this
5560 * connection. The upper layer will also be binded on the same thread.
5561 */
Willy Tarreaueed78262022-12-21 09:09:19 +01005562 qc->tid = quic_get_cid_tid(qc->scid.data, &l->rx);
Willy Tarreauf5a0c8a2022-10-13 16:14:11 +02005563 qc->wait_event.tasklet->tid = qc->tid;
Amaury Denoyellebbb1c682022-09-28 15:15:51 +02005564 qc->subs = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005565
5566 if (qc_conn_alloc_ssl_ctx(qc) ||
5567 !quic_conn_init_timer(qc) ||
5568 !quic_conn_init_idle_timer_task(qc))
5569 goto err;
5570
5571 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
5572 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
5573 goto err;
5574
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005575 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
5576 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
5577
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005578 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
5579
5580 return qc;
5581
5582 err:
5583 pool_free(pool_head_quic_conn_rxbuf, buf_area);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005584 if (qc) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005585 qc->rx.buf.area = NULL;
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005586 quic_conn_release(qc);
5587 }
5588 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005589 return NULL;
5590}
5591
5592/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
5593 * The connection tasklet is killed.
5594 *
5595 * This function must only be called by the thread responsible of the quic_conn
5596 * tasklet.
5597 */
5598void quic_conn_release(struct quic_conn *qc)
5599{
5600 int i;
5601 struct ssl_sock_ctx *conn_ctx;
5602 struct eb64_node *node;
5603 struct quic_tls_ctx *app_tls_ctx;
5604 struct quic_rx_packet *pkt, *pktback;
5605
5606 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5607
5608 /* We must not free the quic-conn if the MUX is still allocated. */
5609 BUG_ON(qc->mux_state == QC_MUX_READY);
5610
Amaury Denoyellefb375572023-02-01 09:28:32 +01005611 if (qc_test_fd(qc))
5612 _HA_ATOMIC_DEC(&jobs);
5613
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005614 /* Close quic-conn socket fd. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01005615 qc_release_fd(qc, 0);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005616
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005617 /* in the unlikely (but possible) case the connection was just added to
5618 * the accept_list we must delete it from there.
5619 */
5620 MT_LIST_DELETE(&qc->accept_list);
5621
5622 /* free remaining stream descriptors */
5623 node = eb64_first(&qc->streams_by_id);
5624 while (node) {
5625 struct qc_stream_desc *stream;
5626
5627 stream = eb64_entry(node, struct qc_stream_desc, by_id);
5628 node = eb64_next(node);
5629
5630 /* all streams attached to the quic-conn are released, so
5631 * qc_stream_desc_free will liberate the stream instance.
5632 */
5633 BUG_ON(!stream->release);
5634 qc_stream_desc_free(stream, 1);
5635 }
5636
5637 /* Purge Rx packet list. */
5638 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
5639 LIST_DELETE(&pkt->qc_rx_pkt_list);
5640 pool_free(pool_head_quic_rx_packet, pkt);
5641 }
5642
5643 if (qc->idle_timer_task) {
5644 task_destroy(qc->idle_timer_task);
5645 qc->idle_timer_task = NULL;
5646 }
5647
5648 if (qc->timer_task) {
5649 task_destroy(qc->timer_task);
5650 qc->timer_task = NULL;
5651 }
5652
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005653 if (qc->wait_event.tasklet)
5654 tasklet_free(qc->wait_event.tasklet);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005655
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005656 /* remove the connection from receiver cids trees */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005657 ebmb_delete(&qc->scid_node);
5658 free_quic_conn_cids(qc);
5659
5660 conn_ctx = qc->xprt_ctx;
5661 if (conn_ctx) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005662 SSL_free(conn_ctx->ssl);
5663 pool_free(pool_head_quic_conn_ctx, conn_ctx);
5664 }
5665
5666 quic_tls_ku_free(qc);
5667 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5668 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
5669 quic_conn_enc_level_uninit(qc, &qc->els[i]);
5670 }
5671 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
5672
5673 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
5674 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
5675 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
5676
5677 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
5678 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
5679 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
5680 }
5681
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01005682 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005683
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005684 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
5685 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +02005686 qc = NULL;
Amaury Denoyellefb375572023-02-01 09:28:32 +01005687
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005688 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
5689
5690 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5691}
5692
5693/* Initialize the timer task of <qc> QUIC connection.
5694 * Returns 1 if succeeded, 0 if not.
5695 */
5696static int quic_conn_init_timer(struct quic_conn *qc)
5697{
5698 int ret = 0;
5699 /* Attach this task to the same thread ID used for the connection */
5700 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5701
5702 qc->timer_task = task_new_on(qc->tid);
5703 if (!qc->timer_task) {
5704 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5705 goto leave;
5706 }
5707
5708 qc->timer = TICK_ETERNITY;
5709 qc->timer_task->process = qc_process_timer;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005710 qc->timer_task->context = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005711
5712 ret = 1;
5713 leave:
5714 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5715 return ret;
5716}
5717
Frédéric Lécailled7215712023-03-24 18:13:37 +01005718/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
5719 * connection. */
5720static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005721{
5722 unsigned int expire;
5723
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005724 if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005725 TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005726 qc->ack_expire = TICK_ETERNITY;
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005727 task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
5728 }
5729 else {
5730 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005731 qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
5732 if (arm_ack) {
5733 /* Arm the ack timer only if not already armed. */
5734 if (!tick_isset(qc->ack_expire)) {
5735 qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
5736 qc->idle_timer_task->expire = qc->ack_expire;
5737 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005738 TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005739 }
5740 }
5741 else {
5742 qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
5743 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005744 TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005745 }
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005746 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005747}
5748
Frédéric Lécailled7215712023-03-24 18:13:37 +01005749/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
5750 * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
5751 * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
5752 * which must be rearmed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005753 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01005754static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005755{
5756 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5757
5758 if (read) {
5759 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5760 }
5761 else {
5762 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5763 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01005764 qc_idle_timer_do_rearm(qc, arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005765
5766 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5767}
5768
5769/* The task handling the idle timeout */
5770struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
5771{
5772 struct quic_conn *qc = ctx;
5773 struct quic_counters *prx_counters = qc->prx_counters;
5774 unsigned int qc_flags = qc->flags;
5775
5776 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5777
Frédéric Lécaille12eca3a2023-04-04 10:46:54 +02005778 if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
5779 goto requeue;
5780
Frédéric Lécailled7215712023-03-24 18:13:37 +01005781 if (tick_is_expired(qc->ack_expire, now_ms)) {
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005782 TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005783 qc->ack_expire = TICK_ETERNITY;
5784 /* Note that ->idle_expire is always set. */
5785 t->expire = qc->idle_expire;
5786 qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
5787 tasklet_wakeup(qc->wait_event.tasklet);
5788 goto requeue;
5789 }
5790
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005791 TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005792 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
5793 * might free the quic-conn too early via quic_close().
5794 */
5795 qc_notify_close(qc);
5796
5797 /* If the MUX is still alive, keep the quic-conn. The MUX is
5798 * responsible to call quic_close to release it.
5799 */
5800 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005801 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005802 quic_conn_release(qc);
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005803 qc = NULL;
5804 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005805
5806 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
5807 * least clean some parts of it such as the tasklet.
5808 */
5809
5810 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
5811 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005812 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005813 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
5814 }
5815
Frédéric Lécailled7215712023-03-24 18:13:37 +01005816 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005817 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5818 return NULL;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005819
5820 requeue:
5821 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5822 return t;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005823}
5824
5825/* Initialize the idle timeout task for <qc>.
5826 * Returns 1 if succeeded, 0 if not.
5827 */
5828static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
5829{
5830 int ret = 0;
5831
5832 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5833
5834 qc->idle_timer_task = task_new_here();
5835 if (!qc->idle_timer_task) {
5836 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5837 goto leave;
5838 }
5839
5840 qc->idle_timer_task->process = qc_idle_timer_task;
5841 qc->idle_timer_task->context = qc;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005842 qc->ack_expire = TICK_ETERNITY;
5843 qc_idle_timer_rearm(qc, 1, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005844 task_queue(qc->idle_timer_task);
5845
5846 ret = 1;
5847 leave:
5848 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5849 return ret;
5850}
5851
5852/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
5853 * past one byte of this buffer.
5854 */
5855static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
5856 struct quic_rx_packet *pkt)
5857{
5858 int ret = 0;
5859 unsigned char dcid_len, scid_len;
5860
5861 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5862
5863 if (end == *buf) {
5864 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
5865 goto leave;
5866 }
5867
5868 /* Destination Connection ID Length */
5869 dcid_len = *(*buf)++;
5870 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
5871 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
5872 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
5873 goto leave;
5874 }
5875
5876 if (dcid_len) {
5877 /* Check that the length of this received DCID matches the CID lengths
5878 * of our implementation for non Initials packets only.
5879 */
5880 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
5881 pkt->type != QUIC_PACKET_TYPE_0RTT &&
5882 dcid_len != QUIC_HAP_CID_LEN) {
5883 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
5884 goto leave;
5885 }
5886
5887 memcpy(pkt->dcid.data, *buf, dcid_len);
5888 }
5889
5890 pkt->dcid.len = dcid_len;
5891 *buf += dcid_len;
5892
5893 /* Source Connection ID Length */
5894 scid_len = *(*buf)++;
5895 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
5896 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
5897 goto leave;
5898 }
5899
5900 if (scid_len)
5901 memcpy(pkt->scid.data, *buf, scid_len);
5902 pkt->scid.len = scid_len;
5903 *buf += scid_len;
5904
5905 ret = 1;
5906 leave:
5907 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
5908 return ret;
5909}
5910
5911/* Insert <pkt> RX packet in its <qel> RX packets tree */
5912static void qc_pkt_insert(struct quic_conn *qc,
5913 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
5914{
5915 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
5916
5917 pkt->pn_node.key = pkt->pn;
5918 quic_rx_packet_refinc(pkt);
5919 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
5920
5921 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
5922}
5923
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005924/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
5925 * address of the packet first byte, using the keys from encryption level <el>.
5926 *
5927 * If header protection has been successfully removed, packet data are copied
5928 * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
5929 * proceeded, and the packet is inserted into <qc> protected packets tree. In
5930 * both cases, packet can now be considered handled by the <qc> connection.
5931 *
5932 * If header protection cannot be removed due to <el> secrets already
5933 * discarded, no operation is conducted.
5934 *
5935 * Returns 1 on success : packet data is now handled by the connection. On
5936 * error 0 is returned : packet should be dropped by the caller.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005937 */
5938static inline int qc_try_rm_hp(struct quic_conn *qc,
5939 struct quic_rx_packet *pkt,
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005940 unsigned char *beg,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005941 struct quic_enc_level **el)
5942{
5943 int ret = 0;
5944 unsigned char *pn = NULL; /* Packet number field */
5945 enum quic_tls_enc_level tel;
5946 struct quic_enc_level *qel;
5947 /* Only for traces. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005948
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005949 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005950 BUG_ON(!pkt->pn_offset);
5951
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005952 /* The packet number is here. This is also the start minus
5953 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5954 * protection.
5955 */
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005956 pn = beg + pkt->pn_offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005957
5958 tel = quic_packet_type_enc_level(pkt->type);
5959 qel = &qc->els[tel];
5960
5961 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01005962 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
5963
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005964 /* Note that the following function enables us to unprotect the packet
5965 * number and its length subsequently used to decrypt the entire
5966 * packets.
5967 */
Frédéric Lécaille72027782023-02-22 16:20:09 +01005968 if (!qc_do_rm_hp(qc, pkt, tls_ctx,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005969 qel->pktns->rx.largest_pn, pn, beg)) {
5970 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
5971 goto out;
5972 }
5973
Frédéric Lécailleece86e62023-03-07 11:53:43 +01005974 qc_handle_spin_bit(qc, pkt, qel);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005975 /* The AAD includes the packet number field. */
5976 pkt->aad_len = pkt->pn_offset + pkt->pnl;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005977 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5978 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
5979 goto out;
5980 }
5981
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02005982 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005983 }
5984 else {
5985 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
5986 /* If the packet number space has been discarded, this packet
5987 * will be not parsed.
5988 */
5989 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
5990 goto out;
5991 }
5992
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005993 TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005994 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
5995 quic_rx_packet_refinc(pkt);
5996 }
5997
5998 *el = qel;
5999 /* No reference counter incrementation here!!! */
6000 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
6001 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
6002 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
6003 b_add(&qc->rx.buf, pkt->len);
6004
6005 ret = 1;
6006 out:
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006007 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006008 return ret;
6009}
6010
6011/* Parse the header form from <byte0> first byte of <pkt> packet to set its type.
6012 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
6013 * of this packet into <*version>.
6014 */
6015static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
6016 unsigned char **buf, const unsigned char *end,
6017 int *long_header, uint32_t *version)
6018{
6019 int ret = 0;
6020 const unsigned char byte0 = **buf;
6021
6022 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
6023
6024 (*buf)++;
6025 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
6026 unsigned char type =
6027 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
6028
6029 *long_header = 1;
6030 /* Version */
6031 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
6032 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
6033 goto out;
6034 }
6035
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +01006036 if (*version != QUIC_PROTOCOL_VERSION_2) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006037 pkt->type = type;
6038 }
6039 else {
6040 switch (type) {
6041 case 0:
6042 pkt->type = QUIC_PACKET_TYPE_RETRY;
6043 break;
6044 case 1:
6045 pkt->type = QUIC_PACKET_TYPE_INITIAL;
6046 break;
6047 case 2:
6048 pkt->type = QUIC_PACKET_TYPE_0RTT;
6049 break;
6050 case 3:
6051 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
6052 break;
6053 }
6054 }
6055 }
6056 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006057 if (byte0 & QUIC_PACKET_SPIN_BIT)
6058 pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006059 pkt->type = QUIC_PACKET_TYPE_SHORT;
6060 *long_header = 0;
6061 }
6062
6063 ret = 1;
6064 out:
6065 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6066 return ret;
6067}
6068
6069/* Return the QUIC version (quic_version struct) with <version> as version number
6070 * if supported or NULL if not.
6071 */
6072static inline const struct quic_version *qc_supported_version(uint32_t version)
6073{
6074 int i;
6075
6076 for (i = 0; i < quic_versions_nb; i++)
6077 if (quic_versions[i].num == version)
6078 return &quic_versions[i];
6079
6080 return NULL;
6081}
6082
6083/*
6084 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
6085 * address <addr>.
6086 * Implementation of RFC9000 6. Version Negotiation
6087 *
6088 * TODO implement a rate-limiting sending of Version Negotiation packets
6089 *
6090 * Returns 0 on success else non-zero
6091 */
6092static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
6093 struct quic_rx_packet *pkt)
6094{
6095 char buf[256];
6096 int ret = 0, i = 0, j;
6097 uint32_t version;
6098 const socklen_t addrlen = get_addr_len(addr);
6099
6100 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6101 /*
6102 * header form
6103 * long header, fixed bit to 0 for Version Negotiation
6104 */
6105 /* TODO: RAND_bytes() should be replaced? */
6106 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
6107 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
6108 goto out;
6109 }
6110
6111 buf[i++] |= '\x80';
6112 /* null version for Version Negotiation */
6113 buf[i++] = '\x00';
6114 buf[i++] = '\x00';
6115 buf[i++] = '\x00';
6116 buf[i++] = '\x00';
6117
6118 /* source connection id */
6119 buf[i++] = pkt->scid.len;
6120 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6121 i += pkt->scid.len;
6122
6123 /* destination connection id */
6124 buf[i++] = pkt->dcid.len;
6125 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
6126 i += pkt->dcid.len;
6127
6128 /* supported version */
6129 for (j = 0; j < quic_versions_nb; j++) {
6130 version = htonl(quic_versions[j].num);
6131 memcpy(&buf[i], &version, sizeof(version));
6132 i += sizeof(version);
6133 }
6134
6135 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
6136 goto out;
6137
6138 ret = 1;
6139 out:
6140 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6141 return !ret;
6142}
6143
6144/* Send a stateless reset packet depending on <pkt> RX packet information
6145 * from <fd> UDP socket to <dst>
6146 * Return 1 if succeeded, 0 if not.
6147 */
6148static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
6149 struct quic_rx_packet *rxpkt)
6150{
6151 int ret = 0, pktlen, rndlen;
6152 unsigned char pkt[64];
6153 const socklen_t addrlen = get_addr_len(dstaddr);
6154 struct proxy *prx;
6155 struct quic_counters *prx_counters;
6156
6157 TRACE_ENTER(QUIC_EV_STATELESS_RST);
6158
6159 prx = l->bind_conf->frontend;
6160 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6161 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
6162 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
6163 * Reset is difficult to distinguish from other packets if the recipient requires
6164 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
6165 * that all packets it sends are at least 22 bytes longer than the minimum
6166 * connection ID length that it requests the peer to include in its packets,
6167 * adding PADDING frames as necessary. This ensures that any Stateless Reset
6168 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
6169 * An endpoint that sends a Stateless Reset in response to a packet that is
6170 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
6171 * than the packet it responds to.
6172 */
6173
6174 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
6175 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
6176 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
6177 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
6178
6179 /* Put a header of random bytes */
6180 /* TODO: RAND_bytes() should be replaced */
6181 if (RAND_bytes(pkt, rndlen) != 1) {
6182 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
6183 goto leave;
6184 }
6185
6186 /* Clear the most significant bit, and set the second one */
6187 *pkt = (*pkt & ~0x80) | 0x40;
6188 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
6189 rxpkt->dcid.data, rxpkt->dcid.len))
6190 goto leave;
6191
6192 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
6193 goto leave;
6194
6195 ret = 1;
6196 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
6197 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
6198 leave:
6199 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
6200 return ret;
6201}
6202
6203/* QUIC server only function.
6204 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
6205 * This is the responsibility of the caller to check <aad> size is big enough
6206 * to contain these data.
6207 * Return the number of bytes copied to <aad>.
6208 */
6209static int quic_generate_retry_token_aad(unsigned char *aad,
6210 uint32_t version,
6211 const struct quic_cid *cid,
6212 const struct sockaddr_storage *addr)
6213{
6214 unsigned char *p;
6215
6216 p = aad;
6217 memcpy(p, &version, sizeof version);
6218 p += sizeof version;
6219 p += quic_saddr_cpy(p, addr);
6220 memcpy(p, cid->data, cid->len);
6221 p += cid->len;
6222
6223 return p - aad;
6224}
6225
6226/* QUIC server only function.
6227 * Generate the token to be used in Retry packets. The token is written to
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05006228 * <buf> with <len> as length. <odcid> is the original destination connection
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006229 * ID and <dcid> is our side destination connection ID (or client source
6230 * connection ID).
6231 * Returns the length of the encoded token or 0 on error.
6232 */
6233static int quic_generate_retry_token(unsigned char *buf, size_t len,
6234 const uint32_t version,
6235 const struct quic_cid *odcid,
6236 const struct quic_cid *dcid,
6237 struct sockaddr_storage *addr)
6238{
6239 int ret = 0;
6240 unsigned char *p;
6241 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006242 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006243 size_t aadlen;
6244 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
6245 unsigned char key[QUIC_TLS_KEY_LEN];
6246 unsigned char iv[QUIC_TLS_IV_LEN];
6247 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6248 size_t seclen = strlen(global.cluster_secret);
6249 EVP_CIPHER_CTX *ctx = NULL;
6250 const EVP_CIPHER *aead = EVP_aes_128_gcm();
6251 uint32_t timestamp = now_ms;
6252
6253 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6254
6255 /* We copy the odcid into the token, prefixed by its one byte
6256 * length, the format token byte. It is followed by an AEAD TAG, and finally
6257 * the random bytes used to derive the secret to encrypt the token.
6258 */
6259 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
6260 goto err;
6261
6262 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
6263 /* TODO: RAND_bytes() should be replaced */
6264 if (RAND_bytes(salt, sizeof salt) != 1) {
6265 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
6266 goto err;
6267 }
6268
6269 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6270 salt, sizeof salt, sec, seclen)) {
6271 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
6272 goto err;
6273 }
6274
6275 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
6276 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
6277 goto err;
6278 }
6279
6280 /* Token build */
6281 p = buf;
6282 *p++ = QUIC_TOKEN_FMT_RETRY,
6283 *p++ = odcid->len;
6284 memcpy(p, odcid->data, odcid->len);
6285 p += odcid->len;
6286 write_u32(p, htonl(timestamp));
6287 p += sizeof timestamp;
6288
6289 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
6290 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
6291 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
6292 goto err;
6293 }
6294
6295 p += QUIC_TLS_TAG_LEN;
6296 memcpy(p, salt, sizeof salt);
6297 p += sizeof salt;
6298 EVP_CIPHER_CTX_free(ctx);
6299
6300 ret = p - buf;
6301 leave:
6302 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6303 return ret;
6304
6305 err:
6306 if (ctx)
6307 EVP_CIPHER_CTX_free(ctx);
6308 goto leave;
6309}
6310
6311/* QUIC server only function.
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006312 *
6313 * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
6314 * the UDP datagram containing <pkt> and <l> is the listener instance on which
6315 * it was received. If the token is valid, the ODCID of <qc> QUIC connection
6316 * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
6317 * to validate the token but it can be NULL : in this case the version will be
6318 * retrieved from the packet.
6319 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006320 * Return 1 if succeeded, 0 if not.
6321 */
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006322
6323static int quic_retry_token_check(struct quic_rx_packet *pkt,
6324 struct quic_dgram *dgram,
6325 struct listener *l,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006326 struct quic_conn *qc,
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006327 struct quic_cid *odcid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006328{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006329 struct proxy *prx;
6330 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006331 int ret = 0;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006332 unsigned char *token = pkt->token;
6333 const uint64_t tokenlen = pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006334 unsigned char buf[128];
6335 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006336 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006337 size_t aadlen;
6338 const unsigned char *salt;
6339 unsigned char key[QUIC_TLS_KEY_LEN];
6340 unsigned char iv[QUIC_TLS_IV_LEN];
6341 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6342 size_t seclen = strlen(global.cluster_secret);
6343 EVP_CIPHER_CTX *ctx = NULL;
6344 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006345 const struct quic_version *qv = qc ? qc->original_version :
6346 pkt->version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006347
6348 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6349
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006350 /* The caller must ensure this. */
6351 BUG_ON(!global.cluster_secret || !pkt->token_len);
6352
6353 prx = l->bind_conf->frontend;
6354 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6355
6356 if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
6357 /* TODO: New token check */
6358 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6359 goto leave;
6360 }
6361
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006362 if (sizeof buf < tokenlen) {
6363 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
6364 goto err;
6365 }
6366
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006367 aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006368 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
6369 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6370 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
6371 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
6372 goto err;
6373 }
6374
6375 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
6376 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
6377 goto err;
6378 }
6379
6380 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
6381 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
6382 ctx, aead, key, iv)) {
6383 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
6384 goto err;
6385 }
6386
6387 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
6388 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
6389 goto err;
6390 }
6391
6392 EVP_CIPHER_CTX_free(ctx);
6393
6394 ret = 1;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006395 HA_ATOMIC_INC(&prx_counters->retry_validated);
6396
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006397 leave:
6398 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6399 return ret;
6400
6401 err:
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006402 HA_ATOMIC_INC(&prx_counters->retry_error);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006403 if (ctx)
6404 EVP_CIPHER_CTX_free(ctx);
6405 goto leave;
6406}
6407
6408/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
6409 * the Initial <pkt> packet.
6410 *
6411 * Returns 0 on success else non-zero.
6412 */
6413static int send_retry(int fd, struct sockaddr_storage *addr,
6414 struct quic_rx_packet *pkt, const struct quic_version *qv)
6415{
6416 int ret = 0;
6417 unsigned char buf[128];
6418 int i = 0, token_len;
6419 const socklen_t addrlen = get_addr_len(addr);
6420 struct quic_cid scid;
6421
6422 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6423
6424 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
6425 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
6426 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
6427 /* version */
6428 buf[i++] = *((unsigned char *)&qv->num + 3);
6429 buf[i++] = *((unsigned char *)&qv->num + 2);
6430 buf[i++] = *((unsigned char *)&qv->num + 1);
6431 buf[i++] = *(unsigned char *)&qv->num;
6432
6433 /* Use the SCID from <pkt> for Retry DCID. */
6434 buf[i++] = pkt->scid.len;
6435 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6436 i += pkt->scid.len;
6437
6438 /* Generate a new CID to be used as SCID for the Retry packet. */
6439 scid.len = QUIC_HAP_CID_LEN;
6440 /* TODO: RAND_bytes() should be replaced */
6441 if (RAND_bytes(scid.data, scid.len) != 1) {
6442 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
6443 goto out;
6444 }
6445
6446 buf[i++] = scid.len;
6447 memcpy(&buf[i], scid.data, scid.len);
6448 i += scid.len;
6449
6450 /* token */
6451 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
6452 &pkt->dcid, &pkt->scid, addr))) {
6453 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
6454 goto out;
6455 }
6456
6457 i += token_len;
6458
6459 /* token integrity tag */
6460 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
6461 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
6462 pkt->dcid.len, buf, i, qv)) {
6463 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6464 goto out;
6465 }
6466
6467 i += QUIC_TLS_TAG_LEN;
6468
6469 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
6470 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6471 goto out;
6472 }
6473
6474 ret = 1;
6475 out:
6476 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6477 return !ret;
6478}
6479
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006480/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
6481 * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
6482 * is used.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006483 *
6484 * Returns the instance or NULL if not found.
6485 */
6486static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
6487 struct listener *l,
6488 struct sockaddr_storage *saddr)
6489{
6490 struct quic_conn *qc = NULL;
6491 struct ebmb_node *node;
6492 struct quic_connection_id *id;
6493 /* set if the quic_conn is found in the second DCID tree */
6494
6495 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
6496
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006497 /* First look into DCID tree. */
6498 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006499
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006500 /* If not found on an Initial/0-RTT packet, it could be because an
6501 * ODCID is reused by the client. Calculate the derived CID value to
6502 * retrieve it from the DCID tree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006503 */
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006504 if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
6505 pkt->type == QUIC_PACKET_TYPE_0RTT)) {
6506 uint64_t hash = quic_derive_cid(&pkt->dcid, saddr);
6507 node = ebmb_lookup(&quic_dghdlrs[tid].cids, &hash, sizeof(hash));
6508 }
6509
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006510 if (!node)
6511 goto end;
6512
6513 id = ebmb_entry(node, struct quic_connection_id, node);
6514 qc = id->qc;
6515
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006516 end:
6517 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6518 return qc;
6519}
6520
6521/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
6522 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
6523 * parameters of this session.
6524 * This is the responsibility of the caller to check the validity of all the
6525 * pointers passed as parameter to this function.
6526 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
6527 * CO_ER_SSL_NO_MEM.
6528 */
6529static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
6530 unsigned char *params, size_t params_len)
6531{
6532 int retry, ret = -1;
6533
6534 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6535
6536 retry = 1;
6537 retry:
6538 *ssl = SSL_new(ssl_ctx);
6539 if (!*ssl) {
6540 if (!retry--)
6541 goto err;
6542
6543 pool_gc(NULL);
6544 goto retry;
6545 }
6546
6547 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
6548 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
6549 SSL_free(*ssl);
6550 *ssl = NULL;
6551 if (!retry--)
6552 goto err;
6553
6554 pool_gc(NULL);
6555 goto retry;
6556 }
6557
6558 ret = 0;
6559 leave:
6560 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6561 return ret;
6562
6563 err:
6564 qc->conn->err_code = CO_ER_SSL_NO_MEM;
6565 goto leave;
6566}
6567
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006568/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
6569 * used to process <qc> received packets. The allocated context is stored in
6570 * <qc.xprt_ctx>.
6571 *
6572 * Returns 0 on success else non-zero.
6573 */
6574static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
6575{
6576 int ret = 0;
6577 struct bind_conf *bc = qc->li->bind_conf;
6578 struct ssl_sock_ctx *ctx = NULL;
6579
6580 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6581
6582 ctx = pool_zalloc(pool_head_quic_conn_ctx);
6583 if (!ctx) {
6584 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
6585 goto err;
6586 }
6587
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006588 ctx->subs = NULL;
6589 ctx->xprt_ctx = NULL;
6590 ctx->qc = qc;
6591
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006592 if (qc_is_listener(qc)) {
6593 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
6594 qc->enc_params, qc->enc_params_len) == -1) {
6595 goto err;
6596 }
6597#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6598 /* Enabling 0-RTT */
6599 if (bc->ssl_conf.early_data)
6600 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
6601#endif
6602
6603 SSL_set_accept_state(ctx->ssl);
6604 }
6605
6606 ctx->xprt = xprt_get(XPRT_QUIC);
6607
6608 /* Store the allocated context in <qc>. */
6609 qc->xprt_ctx = ctx;
6610
6611 ret = 1;
6612 leave:
6613 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6614 return !ret;
6615
6616 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006617 pool_free(pool_head_quic_conn_ctx, ctx);
6618 goto leave;
6619}
6620
6621/* Check that all the bytes between <buf> included and <end> address
6622 * excluded are null. This is the responsibility of the caller to
6623 * check that there is at least one byte between <buf> end <end>.
6624 * Return 1 if this all the bytes are null, 0 if not.
6625 */
6626static inline int quic_padding_check(const unsigned char *buf,
6627 const unsigned char *end)
6628{
6629 while (buf < end && !*buf)
6630 buf++;
6631
6632 return buf == end;
6633}
6634
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006635/* Find the associated connection to the packet <pkt> or create a new one if
6636 * this is an Initial packet. <dgram> is the datagram containing the packet and
6637 * <l> is the listener instance on which it was received.
6638 *
6639 * Returns the quic-conn instance or NULL.
6640 */
6641static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
6642 struct quic_dgram *dgram,
6643 struct listener *l)
6644{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006645 struct quic_cid token_odcid = { .len = 0 };
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006646 struct quic_conn *qc = NULL;
6647 struct proxy *prx;
6648 struct quic_counters *prx_counters;
6649
6650 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6651
6652 prx = l->bind_conf->frontend;
6653 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6654
6655 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
6656
6657 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6658 BUG_ON(!pkt->version); /* This must not happen. */
6659
6660 if (global.cluster_secret && pkt->token_len) {
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006661 if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
6662 goto err;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006663 }
6664
6665 if (!qc) {
6666 int ipv4;
6667
6668 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
6669 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
6670 TRACE_PROTO("Initial without token, sending retry",
6671 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6672 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
6673 TRACE_ERROR("Error during Retry generation",
6674 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6675 goto out;
6676 }
6677
6678 HA_ATOMIC_INC(&prx_counters->retry_sent);
6679 goto out;
6680 }
6681
6682 /* RFC 9000 7.2. Negotiating Connection IDs:
6683 * When an Initial packet is sent by a client that has not previously
6684 * received an Initial or Retry packet from the server, the client
6685 * populates the Destination Connection ID field with an unpredictable
6686 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6687 */
6688 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
6689 TRACE_PROTO("dropped packet",
6690 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6691 goto err;
6692 }
6693
6694 pkt->saddr = dgram->saddr;
6695 ipv4 = dgram->saddr.ss_family == AF_INET;
6696
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006697 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006698 &dgram->daddr, &pkt->saddr, 1,
6699 !!pkt->token_len, l);
6700 if (qc == NULL)
6701 goto err;
6702
6703 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006704 }
6705 }
6706 else if (!qc) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006707 TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006708 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
6709 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
6710 goto err;
6711 }
6712
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006713 out:
6714 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6715 return qc;
6716
6717 err:
6718 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
6719 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
6720 return NULL;
6721}
6722
Amaury Denoyelle98289692022-10-19 15:37:44 +02006723/* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even
6724 * if the packet is incomplete. This function will populate fields of <pkt>
6725 * instance, most notably its length. <dgram> is the UDP datagram which
6726 * contains the parsed packet. <l> is the listener instance on which it was
6727 * received.
6728 *
6729 * Returns 0 on success else non-zero. Packet length is guaranteed to be set to
6730 * the real packet value or to cover all data between <buf> and <end> : this is
6731 * useful to reject a whole datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006732 */
Amaury Denoyelle98289692022-10-19 15:37:44 +02006733static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
6734 unsigned char *buf, const unsigned char *end,
6735 struct quic_dgram *dgram, struct listener *l)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006736{
Amaury Denoyelle98289692022-10-19 15:37:44 +02006737 const unsigned char *beg = buf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006738 struct proxy *prx;
6739 struct quic_counters *prx_counters;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006740 int long_header = 0;
Willy Tarreau33a68702022-11-24 09:16:41 +01006741 uint32_t version = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006742 const struct quic_version *qv = NULL;
6743
6744 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6745
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006746 prx = l->bind_conf->frontend;
6747 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6748 /* This ist only to please to traces and distinguish the
6749 * packet with parsed packet number from others.
6750 */
6751 pkt->pn_node.key = (uint64_t)-1;
6752 if (end <= buf) {
6753 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6754 goto drop;
6755 }
6756
6757 /* Fixed bit */
6758 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006759 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
6760 quic_padding_check(buf, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006761 /* Some browsers may pad the remaining datagram space with null bytes.
6762 * That is what we called add padding out of QUIC packets. Such
6763 * datagrams must be considered as valid. But we can only consume
6764 * the remaining space.
6765 */
6766 pkt->len = end - buf;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006767 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006768 }
6769
6770 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6771 goto drop;
6772 }
6773
6774 /* Header form */
6775 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
6776 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6777 goto drop;
6778 }
6779
6780 if (long_header) {
6781 uint64_t len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006782
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006783 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006784 if (!quic_packet_read_long_header(&buf, end, pkt)) {
6785 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6786 goto drop;
6787 }
6788
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006789 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6790 * they must have the same DCID.
6791 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006792 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006793 (pkt->dcid.len != dgram->dcid_len ||
6794 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006795 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006796 goto drop;
6797 }
6798
6799 /* Retry of Version Negotiation packets are only sent by servers */
6800 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
6801 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6802 goto drop;
6803 }
6804
6805 /* RFC9000 6. Version Negotiation */
6806 qv = qc_supported_version(version);
6807 if (!qv) {
6808 /* unsupported version, send Negotiation packet */
6809 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
6810 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006811 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006812 }
6813
6814 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006815 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006816 }
Amaury Denoyelle0eae5722022-10-17 18:05:18 +02006817 pkt->version = qv;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006818
6819 /* For Initial packets, and for servers (QUIC clients connections),
6820 * there is no Initial connection IDs storage.
6821 */
6822 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6823 uint64_t token_len;
6824
6825 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
6826 end - buf < token_len) {
6827 TRACE_PROTO("Packet dropped",
6828 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6829 goto drop;
6830 }
6831
6832 /* TODO Retry should be automatically activated if
6833 * suspect network usage is detected.
6834 */
6835 if (global.cluster_secret && !token_len) {
6836 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
6837 TRACE_PROTO("Initial without token, sending retry",
Amaury Denoyelle90121b32022-09-27 10:35:29 +02006838 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006839 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
6840 TRACE_PROTO("Error during Retry generation",
Amaury Denoyelle90121b32022-09-27 10:35:29 +02006841 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006842 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006843 }
6844
6845 HA_ATOMIC_INC(&prx_counters->retry_sent);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006846 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006847 }
6848 }
6849 else if (!global.cluster_secret && token_len) {
6850 /* Impossible case: a token was received without configured
6851 * cluster secret.
6852 */
6853 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
6854 NULL, NULL, NULL, qv);
6855 goto drop;
6856 }
6857
6858 pkt->token = buf;
6859 pkt->token_len = token_len;
6860 buf += pkt->token_len;
6861 }
6862 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
6863 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
6864 TRACE_PROTO("Packet dropped",
6865 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6866 goto drop;
6867 }
6868 }
6869
6870 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
6871 end - buf < len) {
6872 TRACE_PROTO("Packet dropped",
6873 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6874 goto drop;
6875 }
6876
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006877 /* Packet Number is stored here. Packet Length totalizes the
6878 * rest of the content.
6879 */
6880 pkt->pn_offset = buf - beg;
6881 pkt->len = pkt->pn_offset + len;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006882
Frédéric Lécaille35218c62023-02-16 11:40:11 +01006883 /* RFC 9000. Initial Datagram Size
6884 *
6885 * A server MUST discard an Initial packet that is carried in a UDP datagram
6886 * with a payload that is smaller than the smallest allowed maximum datagram
6887 * size of 1200 bytes.
6888 */
6889 if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
6890 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006891 TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille35218c62023-02-16 11:40:11 +01006892 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
6893 goto drop;
6894 }
6895
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006896 /* Interrupt parsing after packet length retrieval : this
6897 * ensures that only the packet is dropped but not the whole
6898 * datagram.
6899 */
6900 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006901 TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006902 goto drop;
6903 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006904 }
6905 else {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006906 TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006907 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006908 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006909 goto drop;
6910 }
6911
6912 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
6913 pkt->dcid.len = QUIC_HAP_CID_LEN;
6914
6915 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6916 * they must have the same DCID.
6917 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006918 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006919 (pkt->dcid.len != dgram->dcid_len ||
6920 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006921 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006922 goto drop;
6923 }
6924
6925 buf += QUIC_HAP_CID_LEN;
6926
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006927 pkt->pn_offset = buf - beg;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006928 /* A short packet is the last one of a UDP datagram. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006929 pkt->len = end - beg;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006930 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006931
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006932 TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv);
6933 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02006934 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006935
Amaury Denoyelle98289692022-10-19 15:37:44 +02006936 drop:
6937 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006938 drop_silent:
Amaury Denoyelle98289692022-10-19 15:37:44 +02006939 if (!pkt->len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006940 pkt->len = end - beg;
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006941 TRACE_PROTO("RX pkt parsing failed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv);
6942 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02006943 return -1;
6944}
6945
6946/* Check if received packet <pkt> should be drop due to <qc> already in closing
6947 * state. This can be true if a CONNECTION_CLOSE has already been emitted for
6948 * this connection.
6949 *
6950 * Returns false if connection is not in closing state else true. The caller
6951 * should drop the whole datagram in the last case to not mess up <qc>
6952 * CONNECTION_CLOSE rate limit counter.
6953 */
6954static int qc_rx_check_closing(struct quic_conn *qc,
6955 struct quic_rx_packet *pkt)
6956{
6957 if (!(qc->flags & QUIC_FL_CONN_CLOSING))
6958 return 0;
6959
6960 TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6961
6962 /* Check if CONNECTION_CLOSE rate reemission is reached. */
6963 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6964 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6965 qc->nb_pkt_for_cc++;
6966 qc->nb_pkt_since_cc = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006967 }
6968
Amaury Denoyelle98289692022-10-19 15:37:44 +02006969 return 1;
6970}
6971
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01006972/* React to a connection migration initiated on <qc> by a client with the new
6973 * path addresses <peer_addr>/<local_addr>.
6974 *
6975 * Returns 0 on success else non-zero.
6976 */
6977static int qc_handle_conn_migration(struct quic_conn *qc,
6978 const struct sockaddr_storage *peer_addr,
6979 const struct sockaddr_storage *local_addr)
6980{
6981 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6982
Frédéric Lécaille6fc86972023-01-12 08:29:23 +01006983 /* RFC 9000. Connection Migration
6984 *
6985 * If the peer sent the disable_active_migration transport parameter,
6986 * an endpoint also MUST NOT send packets (including probing packets;
6987 * see Section 9.1) from a different local address to the address the peer
6988 * used during the handshake, unless the endpoint has acted on a
6989 * preferred_address transport parameter from the peer.
6990 */
6991 if (qc->li->bind_conf->quic_params.disable_active_migration) {
6992 TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
6993 goto err;
6994 }
6995
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01006996 /* RFC 9000 9. Connection Migration
6997 *
Amaury Denoyelleeb6be982022-11-21 11:14:45 +01006998 * The design of QUIC relies on endpoints retaining a stable address for
6999 * the duration of the handshake. An endpoint MUST NOT initiate
7000 * connection migration before the handshake is confirmed, as defined in
7001 * Section 4.1.2 of [QUIC-TLS].
7002 */
7003 if (qc->state < QUIC_HS_ST_COMPLETE) {
7004 TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
7005 goto err;
7006 }
7007
7008 /* RFC 9000 9. Connection Migration
7009 *
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007010 * TODO
7011 * An endpoint MUST
7012 * perform path validation (Section 8.2) if it detects any change to a
7013 * peer's address, unless it has previously validated that address.
7014 */
7015
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007016 /* Update quic-conn owned socket if in used.
7017 * TODO try to reuse it instead of closing and opening a new one.
7018 */
7019 if (qc_test_fd(qc)) {
7020 /* TODO try to reuse socket instead of closing it and opening a new one. */
7021 TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
7022 qc_release_fd(qc, 1);
Amaury Denoyellefb375572023-02-01 09:28:32 +01007023 /* TODO need to adjust <jobs> on socket allocation failure. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007024 qc_alloc_fd(qc, local_addr, peer_addr);
7025 }
7026
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007027 qc->local_addr = *local_addr;
7028 qc->peer_addr = *peer_addr;
7029 HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done);
7030
7031 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7032 return 0;
7033
7034 err:
7035 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7036 return 1;
7037}
7038
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007039/* Release the memory for the RX packets which are no more referenced
7040 * and consume their payloads which have been copied to the RX buffer
7041 * for the connection.
7042 * Always succeeds.
7043 */
7044static inline void quic_rx_pkts_del(struct quic_conn *qc)
7045{
7046 struct quic_rx_packet *pkt, *pktback;
7047
7048 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
7049 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007050 "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007051 (long long)pkt->pn_node.key,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007052 pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007053 (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
7054 if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
7055 size_t cdata;
7056
7057 cdata = b_contig_data(&qc->rx.buf, 0);
7058 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007059 "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007060 if (cdata && !*b_head(&qc->rx.buf)) {
7061 /* Consume the remaining data */
7062 b_del(&qc->rx.buf, cdata);
7063 }
7064 break;
7065 }
7066
7067 if (pkt->refcnt)
7068 break;
7069
7070 b_del(&qc->rx.buf, pkt->raw_len);
7071 LIST_DELETE(&pkt->qc_rx_pkt_list);
7072 pool_free(pool_head_quic_rx_packet, pkt);
7073 }
7074
7075 /* In frequent cases the buffer will be emptied at this stage. */
7076 b_realign_if_empty(&qc->rx.buf);
7077}
7078
Amaury Denoyelle98289692022-10-19 15:37:44 +02007079/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
7080 * into <qc> receive buffer after header protection removal procedure.
7081 *
7082 * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
7083 * must point to packet buffer first byte.
7084 *
7085 * <tasklist_head> may be non-NULL when the caller treat several datagrams for
7086 * different quic-conn. In this case, each quic-conn tasklet will be appended
7087 * to it in order to be woken up after the current task.
7088 *
7089 * The caller can safely removed the packet data. If packet refcount was not
7090 * incremented by this function, it means that the connection did not handled
7091 * it and it should be freed by the caller.
7092 */
7093static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
7094 struct quic_dgram *dgram, unsigned char *beg,
7095 struct list **tasklist_head)
7096{
7097 const struct quic_version *qv = pkt->version;
7098 struct quic_enc_level *qel = NULL;
7099 size_t b_cspace;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007100
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007101 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7102 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Amaury Denoyelle3f474e62022-11-24 17:15:08 +01007103
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007104 if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007105 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
7106 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
7107 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007108 TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
7109 QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007110 /* Reset the anti-amplification bit. It will be set again
7111 * when sending the next packet if reached again.
7112 */
7113 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007114 qc_set_timer(qc);
7115 if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
7116 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007117 }
7118
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007119 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
7120 TRACE_PROTO("Connection error",
7121 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7122 goto out;
7123 }
7124
7125 pkt->raw_len = pkt->len;
7126 quic_rx_pkts_del(qc);
7127 b_cspace = b_contig_space(&qc->rx.buf);
7128 if (b_cspace < pkt->len) {
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007129 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007130 "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007131 /* Do not consume buf if space not at the end. */
7132 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
7133 TRACE_PROTO("Packet dropped",
7134 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007135 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007136 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007137 }
7138
7139 /* Let us consume the remaining contiguous space. */
7140 if (b_cspace) {
7141 b_putchr(&qc->rx.buf, 0x00);
7142 b_cspace--;
7143 }
7144 b_add(&qc->rx.buf, b_cspace);
7145 if (b_contig_space(&qc->rx.buf) < pkt->len) {
7146 TRACE_PROTO("Too big packet",
7147 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007148 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007149 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007150 }
7151 }
7152
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007153 if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007154 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7155 goto drop;
7156 }
7157
7158 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7159 if (pkt->aad_len)
7160 qc_pkt_insert(qc, pkt, qel);
7161 out:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02007162 *tasklist_head = tasklet_wakeup_after(*tasklist_head,
7163 qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007164
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007165 drop_silent:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007166 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7167 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007168 return;
7169
7170 drop:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007171 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007172 TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7173 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007174}
7175
7176/* This function builds into <buf> buffer a QUIC long packet header.
7177 * Return 1 if enough room to build this header, 0 if not.
7178 */
7179static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
7180 int type, size_t pn_len,
7181 struct quic_conn *qc, const struct quic_version *ver)
7182{
7183 int ret = 0;
7184
7185 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7186
7187 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
7188 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7189 goto leave;
7190 }
7191
7192 type = quic_pkt_type(type, ver->num);
7193 /* #0 byte flags */
7194 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
7195 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
7196 /* Version */
7197 quic_write_uint32(buf, end, ver->num);
7198 *(*buf)++ = qc->dcid.len;
7199 /* Destination connection ID */
7200 if (qc->dcid.len) {
7201 memcpy(*buf, qc->dcid.data, qc->dcid.len);
7202 *buf += qc->dcid.len;
7203 }
7204 /* Source connection ID */
7205 *(*buf)++ = qc->scid.len;
7206 if (qc->scid.len) {
7207 memcpy(*buf, qc->scid.data, qc->scid.len);
7208 *buf += qc->scid.len;
7209 }
7210
7211 ret = 1;
7212 leave:
7213 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7214 return ret;
7215}
7216
7217/* This function builds into <buf> buffer a QUIC short packet header.
7218 * Return 1 if enough room to build this header, 0 if not.
7219 */
7220static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
7221 size_t pn_len, struct quic_conn *qc,
7222 unsigned char tls_flags)
7223{
7224 int ret = 0;
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007225 unsigned char spin_bit =
7226 (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007227
7228 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7229
7230 if (end - *buf < 1 + qc->dcid.len) {
7231 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7232 goto leave;
7233 }
7234
7235 /* #0 byte flags */
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007236 *(*buf)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007237 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
7238 /* Destination connection ID */
7239 if (qc->dcid.len) {
7240 memcpy(*buf, qc->dcid.data, qc->dcid.len);
7241 *buf += qc->dcid.len;
7242 }
7243
7244 ret = 1;
7245 leave:
7246 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7247 return ret;
7248}
7249
7250/* Apply QUIC header protection to the packet with <buf> as first byte address,
7251 * <pn> as address of the Packet number field, <pnlen> being this field length
7252 * with <aead> as AEAD cipher and <key> as secret key.
7253 * Returns 1 if succeeded or 0 if failed.
7254 */
7255static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
7256 unsigned char *pn, size_t pnlen,
7257 struct quic_tls_ctx *tls_ctx)
7258
7259{
7260 int i, ret = 0;
7261 /* We need an IV of at least 5 bytes: one byte for bytes #0
7262 * and at most 4 bytes for the packet number
7263 */
7264 unsigned char mask[5] = {0};
7265 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
7266
7267 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7268
7269 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
7270 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
7271 goto out;
7272 }
7273
7274 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
7275 for (i = 0; i < pnlen; i++)
7276 pn[i] ^= mask[i + 1];
7277
7278 ret = 1;
7279 out:
7280 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7281 return ret;
7282}
7283
7284/* Reduce the encoded size of <ack_frm> ACK frame removing the last
7285 * ACK ranges if needed to a value below <limit> in bytes.
7286 * Return 1 if succeeded, 0 if not.
7287 */
7288static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
7289 struct quic_frame *ack_frm, size_t limit)
7290{
7291 size_t room, ack_delay_sz;
7292 int ret = 0;
7293
7294 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7295
7296 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
Frédéric Lécaille9d68c6a2023-04-12 20:49:29 +02007297 if (limit <= ack_delay_sz - 1)
7298 goto leave;
7299
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007300 /* A frame is made of 1 byte for the frame type. */
7301 room = limit - ack_delay_sz - 1;
7302 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
7303 goto leave;
7304
7305 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
7306 leave:
7307 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7308 return ret;
7309}
7310
7311/* Prepare into <outlist> as most as possible ack-eliciting frame from their
7312 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
7313 * with <room> as available room, and <*len> the packet Length field initialized
7314 * with the number of bytes already present in this buffer which must be taken
7315 * into an account for the Length packet field value. <headlen> is the number of
7316 * bytes already present in this packet before building frames.
7317 *
7318 * Update consequently <*len> to reflect the size of these frames built
7319 * by this function. Also attach these frames to <l> frame list.
7320 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
7321 */
7322static inline int qc_build_frms(struct list *outlist, struct list *inlist,
7323 size_t room, size_t *len, size_t headlen,
7324 struct quic_enc_level *qel,
7325 struct quic_conn *qc)
7326{
7327 int ret;
7328 struct quic_frame *cf, *cfbak;
7329
7330 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
7331
7332 ret = 0;
7333 if (*len > room)
7334 goto leave;
7335
7336 /* If we are not probing we must take into an account the congestion
7337 * control window.
7338 */
7339 if (!qel->pktns->tx.pto_probe) {
7340 size_t remain = quic_path_prep_data(qc->path);
7341
7342 if (headlen > remain)
7343 goto leave;
7344
7345 room = QUIC_MIN(room, remain - headlen);
7346 }
7347
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007348 TRACE_PROTO("TX frms build (headlen)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007349 QUIC_EV_CONN_BCFRMS, qc, &headlen);
7350
7351 /* NOTE: switch/case block inside a loop, a successful status must be
7352 * returned by this function only if at least one frame could be built
7353 * in the switch/case block.
7354 */
7355 list_for_each_entry_safe(cf, cfbak, inlist, list) {
7356 /* header length, data length, frame length. */
7357 size_t hlen, dlen, dlen_sz, avail_room, flen;
7358
7359 if (!room)
7360 break;
7361
7362 switch (cf->type) {
7363 case QUIC_FT_CRYPTO:
7364 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
7365 QUIC_EV_CONN_BCFRMS, qc, &room, len);
7366 /* Compute the length of this CRYPTO frame header */
7367 hlen = 1 + quic_int_getsize(cf->crypto.offset);
7368 /* Compute the data length of this CRyPTO frame. */
7369 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
7370 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
7371 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
7372 if (!dlen)
7373 continue;
7374
7375 /* CRYPTO frame length. */
7376 flen = hlen + quic_int_getsize(dlen) + dlen;
7377 TRACE_DEVEL(" CRYPTO frame length (flen)",
7378 QUIC_EV_CONN_BCFRMS, qc, &flen);
7379 /* Add the CRYPTO data length and its encoded length to the packet
7380 * length and the length of this length.
7381 */
7382 *len += flen;
7383 room -= flen;
7384 if (dlen == cf->crypto.len) {
7385 /* <cf> CRYPTO data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007386 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007387 LIST_APPEND(outlist, &cf->list);
7388 }
7389 else {
7390 struct quic_frame *new_cf;
7391
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007392 new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007393 if (!new_cf) {
7394 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
7395 continue;
7396 }
7397
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007398 new_cf->crypto.len = dlen;
7399 new_cf->crypto.offset = cf->crypto.offset;
7400 new_cf->crypto.qel = qel;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007401 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007402 if (cf->origin) {
7403 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7404 /* This <cf> frame was duplicated */
7405 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7406 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007407 /* Detach the remaining CRYPTO frame from its original frame */
7408 LIST_DEL_INIT(&cf->ref);
7409 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007410 }
7411 LIST_APPEND(outlist, &new_cf->list);
7412 /* Consume <dlen> bytes of the current frame. */
7413 cf->crypto.len -= dlen;
7414 cf->crypto.offset += dlen;
7415 }
7416 break;
7417
7418 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007419 if (cf->stream.dup) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007420 struct eb64_node *node = NULL;
7421 struct qc_stream_desc *stream_desc = NULL;
7422 struct quic_stream *strm = &cf->stream;
7423
7424 /* As this frame has been already lost, ensure the stream is always
7425 * available or the range of this frame is not consumed before
7426 * resending it.
7427 */
7428 node = eb64_lookup(&qc->streams_by_id, strm->id);
7429 if (!node) {
7430 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007431 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007432 continue;
7433 }
7434
7435 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
7436 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
7437 TRACE_DEVEL("ignored frame frame in already acked range",
7438 QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007439 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007440 continue;
7441 }
7442 else if (strm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01007443 uint64_t diff = stream_desc->ack_offset - strm->offset.key;
7444
Frédéric Lécaillec425e032023-03-20 14:32:59 +01007445 qc_stream_frm_mv_fwd(cf, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007446 TRACE_DEVEL("updated partially acked frame",
7447 QUIC_EV_CONN_PRSAFRM, qc, cf);
7448 }
7449 }
7450 /* Note that these frames are accepted in short packets only without
7451 * "Length" packet field. Here, <*len> is used only to compute the
7452 * sum of the lengths of the already built frames for this packet.
7453 *
7454 * Compute the length of this STREAM frame "header" made a all the field
7455 * excepting the variable ones. Note that +1 is for the type of this frame.
7456 */
7457 hlen = 1 + quic_int_getsize(cf->stream.id) +
7458 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
7459 /* Compute the data length of this STREAM frame. */
7460 avail_room = room - hlen - *len;
7461 if ((ssize_t)avail_room <= 0)
7462 continue;
7463
7464 TRACE_DEVEL(" New STREAM frame build (room, len)",
7465 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007466
7467 /* hlen contains STREAM id and offset. Ensure there is
7468 * enough room for length field.
7469 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007470 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007471 dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
7472 cf->stream.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007473 dlen_sz = quic_int_getsize(dlen);
7474 flen = hlen + dlen_sz + dlen;
7475 }
7476 else {
7477 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
7478 flen = hlen + dlen;
7479 }
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007480
7481 if (cf->stream.len && !dlen) {
7482 /* Only a small gap is left on buffer, not
7483 * enough to encode the STREAM data length.
7484 */
7485 continue;
7486 }
7487
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007488 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
7489 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
7490 TRACE_DEVEL(" STREAM frame length (flen)",
7491 QUIC_EV_CONN_BCFRMS, qc, &flen);
7492 /* Add the STREAM data length and its encoded length to the packet
7493 * length and the length of this length.
7494 */
7495 *len += flen;
7496 room -= flen;
7497 if (dlen == cf->stream.len) {
7498 /* <cf> STREAM data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007499 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007500 LIST_APPEND(outlist, &cf->list);
7501
7502 /* Do not notify MUX on retransmission. */
7503 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7504 qcc_streams_sent_done(cf->stream.stream->ctx,
7505 cf->stream.len,
7506 cf->stream.offset.key);
7507 }
7508 }
7509 else {
7510 struct quic_frame *new_cf;
7511 struct buffer cf_buf;
7512
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007513 new_cf = qc_frm_alloc(cf->type);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007514 if (!new_cf) {
7515 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
7516 continue;
7517 }
7518
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007519 new_cf->stream.stream = cf->stream.stream;
7520 new_cf->stream.buf = cf->stream.buf;
7521 new_cf->stream.id = cf->stream.id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01007522 new_cf->stream.offset = cf->stream.offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007523 new_cf->stream.len = dlen;
7524 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
7525 /* FIN bit reset */
7526 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
7527 new_cf->stream.data = cf->stream.data;
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007528 new_cf->stream.dup = cf->stream.dup;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007529 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007530 if (cf->origin) {
7531 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7532 /* This <cf> frame was duplicated */
7533 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7534 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007535 /* Detach this STREAM frame from its origin */
7536 LIST_DEL_INIT(&cf->ref);
7537 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007538 }
7539 LIST_APPEND(outlist, &new_cf->list);
7540 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
7541 /* Consume <dlen> bytes of the current frame. */
7542 cf_buf = b_make(b_orig(cf->stream.buf),
7543 b_size(cf->stream.buf),
7544 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
7545 cf->stream.len -= dlen;
7546 cf->stream.offset.key += dlen;
7547 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
7548
7549 /* Do not notify MUX on retransmission. */
7550 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7551 qcc_streams_sent_done(new_cf->stream.stream->ctx,
7552 new_cf->stream.len,
7553 new_cf->stream.offset.key);
7554 }
7555 }
7556
7557 /* TODO the MUX is notified about the frame sending via
7558 * previous qcc_streams_sent_done call. However, the
7559 * sending can fail later, for example if the sendto
7560 * system call returns an error. As the MUX has been
7561 * notified, the transport layer is responsible to
7562 * bufferize and resent the announced data later.
7563 */
7564
7565 break;
7566
7567 default:
7568 flen = qc_frm_len(cf);
7569 BUG_ON(!flen);
7570 if (flen > room)
7571 continue;
7572
7573 *len += flen;
7574 room -= flen;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007575 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007576 LIST_APPEND(outlist, &cf->list);
7577 break;
7578 }
7579
7580 /* Successful status as soon as a frame could be built */
7581 ret = 1;
7582 }
7583
7584 leave:
7585 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
7586 return ret;
7587}
7588
7589/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
7590 * is used as return parameter and should be zero'ed by the caller.
7591 */
7592static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
7593 struct quic_frame *out)
7594{
7595 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
7596 *
7597 * A CONNECTION_CLOSE frame should be sent in several packets with
7598 * different encryption levels depending on the client context. This is
7599 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
7600 * more details on how to implement it.
7601 */
7602 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
7603
7604
7605 if (qc->err.app) {
7606 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
7607 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
7608 /* RFC 9000 10.2.3. Immediate Close during the Handshake
7609 *
7610 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
7611 * packet could expose application state or be used to alter application
7612 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
7613 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
7614 * Handshake packets. Otherwise, information about the application
7615 * state might be revealed. Endpoints MUST clear the value of the
7616 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
7617 * converting to a CONNECTION_CLOSE of type 0x1c.
7618 */
7619 out->type = QUIC_FT_CONNECTION_CLOSE;
7620 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
7621 out->connection_close.reason_phrase_len = 0;
7622 }
7623 else {
7624 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
7625 out->connection_close.error_code = qc->err.code;
7626 }
7627 }
7628 else {
7629 out->type = QUIC_FT_CONNECTION_CLOSE;
7630 out->connection_close.error_code = qc->err.code;
7631 }
7632 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
7633
7634}
7635
7636/* This function builds a clear packet from <pkt> information (its type)
7637 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
7638 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
7639 * filling the buffer with as much frames as possible from <frms> list of
7640 * prebuilt frames.
7641 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
7642 * reserved so that to ensure there is enough room to build this AEAD TAG after
7643 * having returned from this function.
7644 * This function also updates the value of <buf_pn> pointer to point to the packet
7645 * number field in this packet. <pn_len> will also have the packet number
7646 * length as value.
7647 *
7648 * Return 1 if succeeded (enough room to buile this packet), O if not.
7649 */
7650static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
7651 size_t dglen, struct quic_tx_packet *pkt,
7652 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007653 int must_ack, int padding, int cc, int probe,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007654 struct quic_enc_level *qel, struct quic_conn *qc,
7655 const struct quic_version *ver, struct list *frms)
7656{
7657 unsigned char *beg, *payload;
7658 size_t len, len_sz, len_frms, padding_len;
7659 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
7660 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
7661 struct quic_frame cc_frm = { };
7662 size_t ack_frm_len, head_len;
7663 int64_t rx_largest_acked_pn;
7664 int add_ping_frm;
7665 struct list frm_list = LIST_HEAD_INIT(frm_list);
7666 struct quic_frame *cf;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007667 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007668
7669 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7670
7671 /* Length field value with CRYPTO frames if present. */
7672 len_frms = 0;
7673 beg = pos;
7674 /* When not probing, and no immediate close is required, reduce the size of this
7675 * buffer to respect the congestion controller window.
7676 * This size will be limited if we have ack-eliciting frames to send from <frms>.
7677 */
7678 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
7679 size_t path_room;
7680
7681 path_room = quic_path_prep_data(qc->path);
7682 if (end - beg > path_room)
7683 end = beg + path_room;
7684 }
7685
7686 /* Ensure there is enough room for the TLS encryption tag and a zero token
7687 * length field if any.
7688 */
7689 if (end - pos < QUIC_TLS_TAG_LEN +
7690 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
7691 goto no_room;
7692
7693 end -= QUIC_TLS_TAG_LEN;
7694 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
7695 /* packet number length */
7696 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
7697 /* Build the header */
7698 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
7699 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
7700 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
7701 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
7702 goto no_room;
7703
7704 /* Encode the token length (0) for an Initial packet. */
7705 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
7706 *pos++ = 0;
7707 head_len = pos - beg;
7708 /* Build an ACK frame if required. */
7709 ack_frm_len = 0;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007710 /* Do not ack and probe at the same time. */
7711 if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007712 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
7713 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
7714 ack_frm.tx_ack.arngs = arngs;
7715 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
7716 qel->pktns->tx.ack_delay =
7717 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
7718 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
7719 }
7720 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
7721 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
7722 * smallest frame (PING) and <*pn_len> more for the packet number. Note
7723 * that from here, we do not know if we will have to send a PING frame.
7724 * This will be decided after having computed the ack-eliciting frames
7725 * to be added to this packet.
7726 */
Frédéric Lécaille9d68c6a2023-04-12 20:49:29 +02007727 if (end - pos <= 1 + *pn_len)
7728 goto no_room;
7729
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007730 ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos);
7731 if (!ack_frm_len)
7732 goto no_room;
7733 }
7734
7735 /* Length field value without the ack-eliciting frames. */
7736 len = ack_frm_len + *pn_len;
7737 len_frms = 0;
7738 if (!cc && !LIST_ISEMPTY(frms)) {
7739 ssize_t room = end - pos;
7740
7741 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
7742 /* Initialize the length of the frames built below to <len>.
7743 * If any frame could be successfully built by qc_build_frms(),
7744 * we will have len_frms > len.
7745 */
7746 len_frms = len;
7747 if (!qc_build_frms(&frm_list, frms,
7748 end - pos, &len_frms, pos - beg, qel, qc)) {
7749 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7750 qc, NULL, NULL, &room);
7751 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
7752 goto no_room;
7753 }
7754 }
7755
7756 /* Length (of the remaining data). Must not fail because, the buffer size
7757 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
7758 * for the encryption tag. It must be taken into an account for the length
7759 * of this packet.
7760 */
7761 if (len_frms)
7762 len = len_frms + QUIC_TLS_TAG_LEN;
7763 else
7764 len += QUIC_TLS_TAG_LEN;
7765 /* CONNECTION_CLOSE frame */
7766 if (cc) {
7767 qc_build_cc_frm(qc, qel, &cc_frm);
7768 len += qc_frm_len(&cc_frm);
7769 }
7770 add_ping_frm = 0;
7771 padding_len = 0;
7772 len_sz = quic_int_getsize(len);
7773 /* Add this packet size to <dglen> */
7774 dglen += head_len + len_sz + len;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007775 /* Note that <padding> is true only when building an Handshake packet
7776 * coalesced to an Initial packet.
7777 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007778 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
7779 /* This is a maximum padding size */
7780 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7781 /* The length field value is of this packet is <len> + <padding_len>
7782 * the size of which may be greater than the initial computed size
7783 * <len_sz>. So, let's deduce the difference between these to packet
7784 * sizes from <padding_len>.
7785 */
7786 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7787 len += padding_len;
7788 }
Frédéric Lécaille5faf5772023-02-16 17:30:53 +01007789 else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
7790 len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
7791 }
7792 else if (LIST_ISEMPTY(&frm_list)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007793 if (qel->pktns->tx.pto_probe) {
7794 /* If we cannot send a frame, we send a PING frame. */
7795 add_ping_frm = 1;
7796 len += 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007797 dglen += 1;
7798 /* Note that only we are in the case where this Initial packet
7799 * is not coalesced to an Handshake packet. We must directly
7800 * pad the datragram.
7801 */
Frédéric Lécaille9c317b12023-03-28 15:39:11 +02007802 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7803 if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
7804 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7805 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7806 len += padding_len;
7807 }
7808 }
7809 else {
7810 /* Note that +1 is for the PING frame */
7811 if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
7812 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007813 }
7814 }
7815 else {
7816 /* If there is no frame at all to follow, add at least a PADDING frame. */
7817 if (!ack_frm_len && !cc)
7818 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007819 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007820 }
7821
7822 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
7823 goto no_room;
7824
7825 /* Packet number field address. */
7826 *buf_pn = pos;
7827
7828 /* Packet number encoding. */
7829 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
7830 goto no_room;
7831
7832 /* payload building (ack-eliciting or not frames) */
7833 payload = pos;
7834 if (ack_frm_len) {
7835 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
7836 goto no_room;
7837
7838 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
7839 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
7840 }
7841
7842 /* Ack-eliciting frames */
7843 if (!LIST_ISEMPTY(&frm_list)) {
7844 struct quic_frame *tmp_cf;
7845 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
7846 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
7847 ssize_t room = end - pos;
7848 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7849 qc, NULL, NULL, &room);
7850 /* Note that <cf> was added from <frms> to <frm_list> list by
7851 * qc_build_frms().
7852 */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007853 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007854 LIST_INSERT(frms, &cf->list);
7855 continue;
7856 }
7857
7858 quic_tx_packet_refinc(pkt);
7859 cf->pkt = pkt;
7860 }
7861 }
7862
7863 /* Build a PING frame if needed. */
7864 if (add_ping_frm) {
7865 frm.type = QUIC_FT_PING;
7866 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
7867 goto no_room;
7868 }
7869
7870 /* Build a CONNECTION_CLOSE frame if needed. */
7871 if (cc) {
7872 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
7873 goto no_room;
7874
7875 pkt->flags |= QUIC_FL_TX_PACKET_CC;
7876 }
7877
7878 /* Build a PADDING frame if needed. */
7879 if (padding_len) {
7880 frm.type = QUIC_FT_PADDING;
7881 frm.padding.len = padding_len;
7882 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
7883 goto no_room;
7884 }
7885
7886 if (pos == payload) {
7887 /* No payload was built because of congestion control */
7888 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
7889 goto no_room;
7890 }
7891
7892 /* If this packet is ack-eliciting and we are probing let's
7893 * decrement the PTO probe counter.
7894 */
7895 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
7896 qel->pktns->tx.pto_probe)
7897 qel->pktns->tx.pto_probe--;
7898
7899 pkt->len = pos - beg;
7900 LIST_SPLICE(&pkt->frms, &frm_list);
7901
7902 ret = 1;
7903 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
7904 leave:
7905 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7906 return ret;
7907
7908 no_room:
7909 /* Replace the pre-built frames which could not be add to this packet */
7910 LIST_SPLICE(frms, &frm_list);
7911 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
7912 goto leave;
7913}
7914
7915static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
7916{
7917 pkt->type = type;
7918 pkt->len = 0;
7919 pkt->in_flight_len = 0;
7920 pkt->pn_node.key = (uint64_t)-1;
7921 LIST_INIT(&pkt->frms);
7922 pkt->time_sent = TICK_ETERNITY;
7923 pkt->next = NULL;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01007924 pkt->prev = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007925 pkt->largest_acked_pn = -1;
7926 pkt->flags = 0;
7927 pkt->refcnt = 0;
7928}
7929
7930/* Build a packet into <buf> packet buffer with <pkt_type> as packet
7931 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
7932 * of prebuilt frames.
7933 *
7934 * Return -2 if the packet could not be allocated or encrypted for any reason,
7935 * -1 if there was not enough room to build a packet.
7936 * XXX NOTE XXX
7937 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
7938 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
7939 * control window limitation.
7940 */
7941static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
7942 const unsigned char *buf_end,
7943 struct quic_enc_level *qel,
7944 struct quic_tls_ctx *tls_ctx, struct list *frms,
7945 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007946 size_t dglen, int pkt_type, int must_ack,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007947 int padding, int probe, int cc, int *err)
7948{
7949 struct quic_tx_packet *ret_pkt = NULL;
7950 /* The pointer to the packet number field. */
7951 unsigned char *buf_pn;
7952 unsigned char *beg, *end, *payload;
7953 int64_t pn;
7954 size_t pn_len, payload_len, aad_len;
7955 struct quic_tx_packet *pkt;
7956
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007957 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7958 TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007959 *err = 0;
7960 pkt = pool_alloc(pool_head_quic_tx_packet);
7961 if (!pkt) {
7962 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
7963 *err = -2;
7964 goto err;
7965 }
7966
7967 quic_tx_packet_init(pkt, pkt_type);
7968 beg = *pos;
7969 pn_len = 0;
7970 buf_pn = NULL;
7971
7972 pn = qel->pktns->tx.next_pn + 1;
7973 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007974 must_ack, padding, cc, probe, qel, qc, ver, frms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007975 // trace already emitted by function above
7976 *err = -1;
7977 goto err;
7978 }
7979
7980 end = beg + pkt->len;
7981 payload = buf_pn + pn_len;
7982 payload_len = end - payload;
7983 aad_len = payload - beg;
7984
7985 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
7986 // trace already emitted by function above
7987 *err = -2;
7988 goto err;
7989 }
7990
7991 end += QUIC_TLS_TAG_LEN;
7992 pkt->len += QUIC_TLS_TAG_LEN;
7993 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
7994 // trace already emitted by function above
7995 *err = -2;
7996 goto err;
7997 }
7998
7999 /* Consume a packet number */
8000 qel->pktns->tx.next_pn++;
8001 qc->tx.prep_bytes += pkt->len;
8002 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
8003 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
8004 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
8005 }
8006 /* Now that a correct packet is built, let us consume <*pos> buffer. */
8007 *pos = end;
8008 /* Attach the built packet to its tree. */
8009 pkt->pn_node.key = pn;
8010 /* Set the packet in fligth length for in flight packet only. */
8011 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
8012 pkt->in_flight_len = pkt->len;
8013 qc->path->prep_in_flight += pkt->len;
8014 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01008015 /* Always reset this flag */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008016 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
8017 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
8018 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
8019 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
Frédéric Lécailled7215712023-03-24 18:13:37 +01008020 qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
8021 if (tick_isset(qc->ack_expire)) {
8022 qc->ack_expire = TICK_ETERNITY;
8023 qc->idle_timer_task->expire = qc->idle_expire;
8024 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02008025 TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01008026 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008027 }
8028
8029 pkt->pktns = qel->pktns;
8030
8031 ret_pkt = pkt;
8032 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008033 TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
8034 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008035 return ret_pkt;
8036
8037 err:
8038 /* TODO: what about the frames which have been built
8039 * for this packet.
8040 */
8041 free_quic_tx_packet(qc, pkt);
8042 goto leave;
8043}
8044
8045
8046static void __quic_conn_init(void)
8047{
8048 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
8049}
8050INITCALL0(STG_REGISTER, __quic_conn_init);
8051
8052static void __quic_conn_deinit(void)
8053{
8054 BIO_meth_free(ha_quic_meth);
8055}
8056REGISTER_POST_DEINIT(__quic_conn_deinit);
8057
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008058/* Handle a new <dgram> received. Parse each QUIC packets and copied their
8059 * content to a quic-conn instance. The datagram content can be released after
8060 * this function.
8061 *
8062 * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
8063 * to the connection instance. <li> is the attached listener. The caller is
8064 * responsible to ensure that the first packet is destined to this connection
8065 * by comparing CIDs.
8066 *
8067 * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
8068 * function will thus retrieve the connection from the CID tree or allocate a
8069 * new one if possible. <li> is the listener attached to the receiver.
8070 *
8071 * Returns 0 on success else non-zero. If an error happens, some packets from
8072 * the datagram may not have been parsed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008073 */
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008074int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
8075 struct listener *li)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008076{
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008077 struct quic_rx_packet *pkt;
8078 struct quic_conn *qc = NULL;
8079 unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008080 struct list *tasklist_head = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008081
8082 TRACE_ENTER(QUIC_EV_CONN_LPKT);
8083
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008084 pos = dgram->buf;
8085 end = pos + dgram->len;
8086 do {
8087 /* TODO replace zalloc -> alloc. */
8088 pkt = pool_zalloc(pool_head_quic_rx_packet);
8089 if (!pkt) {
8090 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
8091 goto err;
8092 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008093
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008094 pkt->version = NULL;
8095 pkt->pn_offset = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008096
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008097 /* Set flag if pkt is the first one in dgram. */
8098 if (pos == dgram->buf)
8099 pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008100
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008101 LIST_INIT(&pkt->qc_rx_pkt_list);
8102 pkt->time_received = now_ms;
8103 quic_rx_packet_refinc(pkt);
8104 if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
8105 goto next;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008106
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008107 /* Search quic-conn instance for first packet of the datagram.
8108 * quic_rx_packet_parse() is responsible to discard packets
8109 * with different DCID as the first one in the same datagram.
8110 */
8111 if (!qc) {
8112 qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li);
8113 /* qc is NULL if receiving a non Initial packet for an
8114 * unknown connection.
8115 */
8116 if (!qc) {
Amaury Denoyelle98289692022-10-19 15:37:44 +02008117 /* Skip the entire datagram. */
8118 pkt->len = end - pos;
8119 goto next;
8120 }
8121
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008122 dgram->qc = qc;
8123 }
Amaury Denoyelle98289692022-10-19 15:37:44 +02008124
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008125 if (qc_rx_check_closing(qc, pkt)) {
8126 /* Skip the entire datagram. */
8127 pkt->len = end - pos;
8128 goto next;
8129 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008130
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008131 /* Detect QUIC connection migration. */
Frédéric Lécaillef6769542023-01-12 10:36:26 +01008132 if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008133 if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
8134 /* Skip the entire datagram. */
8135 TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
8136 pkt->len = end - pos;
8137 goto next;
8138 }
8139 }
8140
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008141 qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008142
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008143 next:
8144 pos += pkt->len;
8145 quic_rx_packet_refdec(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008146
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008147 /* Free rejected packets */
8148 if (!pkt->refcnt) {
8149 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
8150 pool_free(pool_head_quic_rx_packet, pkt);
8151 }
8152 } while (pos < end);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008153
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008154 /* Increasing the received bytes counter by the UDP datagram length
8155 * if this datagram could be associated to a connection.
8156 */
8157 if (dgram->qc)
8158 dgram->qc->rx.bytes += dgram->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008159
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008160 /* This must never happen. */
8161 BUG_ON(pos > end);
8162 BUG_ON(pos < end || pos > dgram->buf + dgram->len);
8163 /* Mark this datagram as consumed */
8164 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008165
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008166 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
8167 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008168
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008169 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008170 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008171 return -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008172}
8173
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008174/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
8175 * CIDs. This can be used to determine if a datagram is addressed to the right
8176 * connection instance.
8177 *
8178 * Returns a boolean value.
8179 */
8180int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len)
8181{
8182 struct ebmb_node *node;
8183 struct quic_connection_id *id;
8184
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008185 if ((qc->scid.len == dcid_len &&
8186 memcmp(qc->scid.data, dcid, dcid_len) == 0) ||
8187 (qc->odcid.len == dcid_len &&
Frédéric Lécaille07846cb2023-02-13 16:14:24 +01008188 memcmp(qc->odcid.data, dcid, dcid_len) == 0)) {
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008189 return 1;
8190 }
8191
8192 node = ebmb_lookup(&quic_dghdlrs[tid].cids, dcid, dcid_len);
8193 if (node) {
8194 id = ebmb_entry(node, struct quic_connection_id, node);
8195 if (qc == id->qc)
8196 return 1;
8197 }
8198
8199 return 0;
8200}
8201
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008202/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
8203 * Returns 1 if succeeded, 0 if not.
8204 */
8205int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
8206 unsigned char **dcid, size_t *dcid_len)
8207{
8208 int ret = 0, long_header;
8209 size_t minlen, skip;
8210
8211 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
8212
8213 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
8214 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
8215 goto err;
8216 }
8217
8218 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
8219 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
8220 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
8221 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
8222 if (end - buf < minlen)
8223 goto err;
8224
8225 buf += skip;
8226 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
8227 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
8228 goto err;
8229
8230 *dcid = buf;
8231
8232 ret = 1;
8233 leave:
8234 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
8235 return ret;
8236
8237 err:
8238 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
8239 goto leave;
8240}
8241
8242/* Notify the MUX layer if alive about an imminent close of <qc>. */
8243void qc_notify_close(struct quic_conn *qc)
8244{
8245 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
8246
8247 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
8248 goto leave;
8249
8250 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
8251 /* wake up the MUX */
8252 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
8253 TRACE_STATE("connection closure notidfied to mux",
8254 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8255 qc->conn->mux->wake(qc->conn);
8256 }
8257 else
8258 TRACE_STATE("connection closure not notidfied to mux",
8259 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8260 leave:
8261 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
8262}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008263
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008264/* Wake-up upper layer for sending if all conditions are met :
8265 * - room in congestion window or probe packet to sent
8266 * - socket FD ready to sent or listener socket used
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008267 *
8268 * Returns 1 if upper layer has been woken up else 0.
8269 */
8270int qc_notify_send(struct quic_conn *qc)
8271{
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008272 const struct quic_pktns *pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8273
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008274 if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008275 /* RFC 9002 7.5. Probe Timeout
8276 *
8277 * Probe packets MUST NOT be blocked by the congestion controller.
8278 */
8279 if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
Amaury Denoyellecaa16542023-02-28 15:11:26 +01008280 (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008281 tasklet_wakeup(qc->subs->tasklet);
8282 qc->subs->events &= ~SUB_RETRY_SEND;
8283 if (!qc->subs->events)
8284 qc->subs = NULL;
8285
8286 return 1;
8287 }
8288 }
8289
8290 return 0;
8291}
8292
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008293
8294/* appctx context used by "show quic" command */
8295struct show_quic_ctx {
8296 unsigned int epoch;
8297 struct bref bref; /* back-reference to the quic-conn being dumped */
8298 unsigned int thr;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008299 int flags;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008300};
8301
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008302#define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */
8303
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008304static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private)
8305{
8306 struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
8307
8308 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
8309 return 1;
8310
8311 ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1);
8312 ctx->thr = 0;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008313 ctx->flags = 0;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008314
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01008315 if (*args[2] && strcmp(args[2], "all") == 0)
8316 ctx->flags |= QC_CLI_FL_SHOW_ALL;
8317
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008318 LIST_INIT(&ctx->bref.users);
8319
8320 return 0;
8321}
8322
8323static int cli_io_handler_dump_quic(struct appctx *appctx)
8324{
8325 struct show_quic_ctx *ctx = appctx->svcctx;
8326 struct stconn *sc = appctx_sc(appctx);
8327 struct quic_conn *qc;
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008328 struct quic_pktns *pktns;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008329 struct eb64_node *node;
8330 struct qc_stream_desc *stream;
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008331 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008332 int expire;
8333 unsigned char cid_len;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008334
8335 thread_isolate();
8336
8337 if (ctx->thr >= global.nbthread)
8338 goto done;
8339
Christopher Faulet87633c32023-04-03 18:32:50 +02008340 /* FIXME: Don't watch the other side !*/
Christopher Faulet7faac7c2023-04-04 10:05:27 +02008341 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUTW)) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008342 /* If we're forced to shut down, we might have to remove our
8343 * reference to the last stream being dumped.
8344 */
8345 if (!LIST_ISEMPTY(&ctx->bref.users))
8346 LIST_DEL_INIT(&ctx->bref.users);
8347 goto done;
8348 }
8349
8350 chunk_reset(&trash);
8351
8352 if (!LIST_ISEMPTY(&ctx->bref.users)) {
8353 /* Remove show_quic_ctx from previous quic_conn instance. */
8354 LIST_DEL_INIT(&ctx->bref.users);
8355 }
8356 else if (!ctx->bref.ref) {
8357 /* First invocation. */
8358 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8359 }
8360
8361 while (1) {
8362 int done = 0;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008363 int i;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008364
8365 if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008366 /* If closing connections requested through "all", move
8367 * to quic_conns_clo list after browsing quic_conns.
8368 * Else move directly to the next quic_conns thread.
8369 */
8370 if (ctx->flags & QC_CLI_FL_SHOW_ALL) {
8371 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns_clo.n;
8372 continue;
8373 }
8374
8375 done = 1;
8376 }
8377 else if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns_clo) {
8378 /* Closing list entirely browsed, go to next quic_conns
8379 * thread.
8380 */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008381 done = 1;
8382 }
8383 else {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008384 /* Retrieve next element of the current list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008385 qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx);
8386 if ((int)(qc->qc_epoch - ctx->epoch) > 0)
8387 done = 1;
8388 }
8389
8390 if (done) {
8391 ++ctx->thr;
8392 if (ctx->thr >= global.nbthread)
8393 break;
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008394 /* Switch to next thread quic_conns list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008395 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8396 continue;
8397 }
8398
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008399 /* CIDs */
8400 chunk_appendf(&trash, "* %p[%02u]: scid=", qc, qc->tid);
8401 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
8402 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
8403 while (cid_len++ < 20)
8404 chunk_appendf(&trash, "..");
8405
8406 chunk_appendf(&trash, " dcid=");
8407 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
8408 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
8409 while (cid_len++ < 20)
8410 chunk_appendf(&trash, "..");
8411
8412 chunk_appendf(&trash, "\n");
8413
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008414 chunk_appendf(&trash, " loc. TPs:");
8415 quic_transport_params_dump(&trash, qc, &qc->rx.params);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008416 chunk_appendf(&trash, "\n");
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008417 chunk_appendf(&trash, " rem. TPs:");
8418 quic_transport_params_dump(&trash, qc, &qc->tx.params);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008419 chunk_appendf(&trash, "\n");
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008420
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008421 /* Connection state */
8422 if (qc->flags & QUIC_FL_CONN_CLOSING)
8423 chunk_appendf(&trash, " st=closing ");
8424 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8425 chunk_appendf(&trash, " st=draining ");
8426 else if (qc->state < QUIC_HS_ST_CONFIRMED)
8427 chunk_appendf(&trash, " st=handshake ");
8428 else
8429 chunk_appendf(&trash, " st=opened ");
8430
8431 if (qc->mux_state == QC_MUX_NULL)
8432 chunk_appendf(&trash, "mux=null ");
8433 else if (qc->mux_state == QC_MUX_READY)
8434 chunk_appendf(&trash, "mux=ready ");
8435 else
8436 chunk_appendf(&trash, "mux=released ");
8437
Frédéric Lécaille92f4a7c2023-04-04 14:31:49 +02008438 expire = qc->idle_expire;
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008439 chunk_appendf(&trash, "expire=%02ds ",
Frédéric Lécaille92f4a7c2023-04-04 14:31:49 +02008440 TICKS_TO_MS(tick_remain(now_ms, expire)) / 1000);
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008441
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008442 chunk_appendf(&trash, "\n");
8443
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008444 /* Socket */
8445 chunk_appendf(&trash, " fd=%d", qc->fd);
8446 if (qc->local_addr.ss_family == AF_INET ||
8447 qc->local_addr.ss_family == AF_INET6) {
8448 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8449 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
8450 chunk_appendf(&trash, " from=%s:%s", bufaddr, bufport);
8451
8452 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8453 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
8454 chunk_appendf(&trash, " to=%s:%s", bufaddr, bufport);
8455 }
8456
8457 chunk_appendf(&trash, "\n");
8458
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008459 /* Packet number spaces information */
8460 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008461 chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008462 pktns->rx.arngs.sz, pktns->tx.in_flight);
8463 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008464 chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008465 pktns->rx.arngs.sz, pktns->tx.in_flight);
8466 pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8467 chunk_appendf(&trash, " [01rtt] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
8468 pktns->rx.arngs.sz, pktns->tx.in_flight);
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008469
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +02008470 chunk_appendf(&trash, " srtt=%-4u rttvar=%-4u rttmin=%-4u ptoc=%-4u cwnd=%-6llu"
8471 " mcwnd=%-6llu lostpkts=%-6llu\n",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008472 qc->path->loss.srtt >> 3, qc->path->loss.rtt_var >> 2,
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +02008473 qc->path->loss.rtt_min, qc->path->loss.pto_count, (ullong)qc->path->cwnd,
8474 (ullong)qc->path->mcwnd, (ullong)qc->path->loss.nb_lost_pkt);
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008475
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008476
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008477 /* Streams */
8478 node = eb64_first(&qc->streams_by_id);
8479 i = 0;
8480 while (node) {
8481 stream = eb64_entry(node, struct qc_stream_desc, by_id);
8482 node = eb64_next(node);
8483
Amaury Denoyellea9de25a2023-02-10 09:25:22 +01008484 chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key);
8485 chunk_appendf(&trash, " off=%-8llu ack=%-8llu",
8486 (unsigned long long)stream->buf_offset,
8487 (unsigned long long)stream->ack_offset);
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008488
8489 if (!(++i % 3)) {
8490 chunk_appendf(&trash, "\n");
8491 i = 0;
8492 }
8493 }
8494
8495 chunk_appendf(&trash, "\n");
8496
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008497 if (applet_putchk(appctx, &trash) == -1) {
8498 /* Register show_quic_ctx to quic_conn instance. */
8499 LIST_APPEND(&qc->back_refs, &ctx->bref.users);
8500 goto full;
8501 }
8502
8503 ctx->bref.ref = qc->el_th_ctx.n;
8504 }
8505
8506 done:
8507 thread_release();
8508 return 1;
8509
8510 full:
8511 thread_release();
8512 return 0;
8513}
8514
8515static void cli_release_show_quic(struct appctx *appctx)
8516{
8517 struct show_quic_ctx *ctx = appctx->svcctx;
8518
8519 if (ctx->thr < global.nbthread) {
8520 thread_isolate();
8521 if (!LIST_ISEMPTY(&ctx->bref.users))
8522 LIST_DEL_INIT(&ctx->bref.users);
8523 thread_release();
8524 }
8525}
8526
8527static struct cli_kw_list cli_kws = {{ }, {
8528 { { "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 +01008529 {{},}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008530}};
8531
8532INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
8533
8534static void init_quic()
8535{
8536 int thr;
8537
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008538 for (thr = 0; thr < MAX_THREADS; ++thr) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008539 LIST_INIT(&ha_thread_ctx[thr].quic_conns);
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008540 LIST_INIT(&ha_thread_ctx[thr].quic_conns_clo);
8541 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008542}
8543INITCALL0(STG_INIT, init_quic);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008544
8545/*
8546 * Local variables:
8547 * c-indent-level: 8
8548 * c-basic-offset: 8
8549 * End:
8550 */