blob: 0a6a59e267b1389c499196af76eca7117be9227e [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>
Amaury Denoyelle331b8b12023-10-25 10:52:23 +020042#include <haproxy/frontend.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020043#include <haproxy/global.h>
44#include <haproxy/h3.h>
45#include <haproxy/hq_interop.h>
46#include <haproxy/log.h>
47#include <haproxy/mux_quic.h>
48#include <haproxy/ncbuf.h>
49#include <haproxy/pipe.h>
50#include <haproxy/proxy.h>
51#include <haproxy/quic_cc.h>
52#include <haproxy/quic_frame.h>
53#include <haproxy/quic_enc.h>
54#include <haproxy/quic_loss.h>
55#include <haproxy/quic_sock.h>
56#include <haproxy/quic_stats.h>
57#include <haproxy/quic_stream.h>
58#include <haproxy/quic_tp.h>
59#include <haproxy/cbuf.h>
60#include <haproxy/proto_quic.h>
61#include <haproxy/quic_tls.h>
62#include <haproxy/ssl_sock.h>
63#include <haproxy/task.h>
Amaury Denoyelle15c74702023-02-01 10:18:26 +010064#include <haproxy/thread.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020065#include <haproxy/trace.h>
66
Amaury Denoyelle15c74702023-02-01 10:18:26 +010067/* incremented by each "show quic". */
68static unsigned int qc_epoch = 0;
69
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020070/* list of supported QUIC versions by this implementation */
71const struct quic_version quic_versions[] = {
72 {
73 .num = QUIC_PROTOCOL_VERSION_DRAFT_29,
74 .initial_salt = initial_salt_draft_29,
75 .initial_salt_len = sizeof initial_salt_draft_29,
76 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
77 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
78 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
79 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
80 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
81 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
82 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
83 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
84 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT,
85 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT,
86 },
87 {
88 .num = QUIC_PROTOCOL_VERSION_1,
89 .initial_salt = initial_salt_v1,
90 .initial_salt_len = sizeof initial_salt_v1,
91 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
92 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
93 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
94 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
95 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
96 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
97 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
98 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
99 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1,
100 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1,
101 },
102 {
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +0100103 .num = QUIC_PROTOCOL_VERSION_2,
104 .initial_salt = initial_salt_v2,
105 .initial_salt_len = sizeof initial_salt_v2,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200106 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2,
107 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1,
108 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2,
109 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1,
110 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2,
111 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1,
112 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2,
113 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1,
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +0100114 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2,
115 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200116 },
117};
118
119/* The total number of supported versions */
120const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions;
121/* Listener only preferred version */
122const struct quic_version *preferred_version;
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +0200123/* RFC 8999 5.4. Version
124 * A Version field with a
125 * value of 0x00000000 is reserved for version negotiation
126 */
127const struct quic_version quic_version_VN_reserved = { .num = 0, };
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200128
129/* trace source and events */
130static void quic_trace(enum trace_level level, uint64_t mask, \
131 const struct trace_source *src,
132 const struct ist where, const struct ist func,
133 const void *a1, const void *a2, const void *a3, const void *a4);
134
135static const struct trace_event quic_trace_events[] = {
136 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
137 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
138 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
139 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
140 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
141 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
142 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
143 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
144 { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" },
145 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
146 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
147 { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" },
148 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
149 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
150 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
151 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
152 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
153 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
154 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
155 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
156 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
157 { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" },
158 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
159 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
160 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
161 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
162 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
163 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
164 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
165 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
166 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
167 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
168 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
169 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
170 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
171 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
172 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
173 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
174 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
175 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
176 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
177 { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"},
178 { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"},
179 { .mask = QUIC_EV_CONN_SUB, .name = "xprt_sub", .desc = "RX/TX subcription or unsubscription to QUIC xprt"},
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200180 { .mask = QUIC_EV_CONN_RCV, .name = "conn_recv", .desc = "RX on connection" },
Amaury Denoyelle25174d52023-04-05 17:52:05 +0200181 { .mask = QUIC_EV_CONN_SET_AFFINITY, .name = "conn_set_affinity", .desc = "set connection thread affinity" },
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200182 { /* end */ }
183};
184
185static const struct name_desc quic_trace_lockon_args[4] = {
186 /* arg1 */ { /* already used by the connection */ },
187 /* arg2 */ { .name="quic", .desc="QUIC transport" },
188 /* arg3 */ { },
189 /* arg4 */ { }
190};
191
192static const struct name_desc quic_trace_decoding[] = {
193#define QUIC_VERB_CLEAN 1
194 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
195 { /* end */ }
196};
197
198
199struct trace_source trace_quic = {
200 .name = IST("quic"),
201 .desc = "QUIC xprt",
202 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
203 .default_cb = quic_trace,
204 .known_events = quic_trace_events,
205 .lockon_args = quic_trace_lockon_args,
206 .decoding = quic_trace_decoding,
207 .report_events = ~0, /* report everything by default */
208};
209
210#define TRACE_SOURCE &trace_quic
211INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
212
213static BIO_METHOD *ha_quic_meth;
214
215DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ);
216DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
217DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
218 "quic_conn_ctx", sizeof(struct ssl_sock_ctx));
219DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
220DECLARE_POOL(pool_head_quic_connection_id,
Frédéric Lécaillea9461252023-04-24 18:20:44 +0200221 "quic_connection_id", sizeof(struct quic_connection_id));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200222DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
223DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet));
224DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet));
225DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm));
226DECLARE_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 +0200227DECLARE_STATIC_POOL(pool_head_quic_cstream, "quic_cstream", sizeof(struct quic_cstream));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200228DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame));
229DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node));
230
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +0100231static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +0200232 struct quic_conn *qc,
233 const struct quic_cid *odcid,
234 const struct sockaddr_storage *saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200235static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
236 struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
237 struct list *frms, struct quic_conn *qc,
238 const struct quic_version *ver, size_t dglen, int pkt_type,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +0200239 int must_ack, int padding, int probe, int cc, int *err);
Frédéric Lécaille21017002023-11-08 11:31:21 +0100240static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf);
241static void qc_purge_tx_buf(struct buffer *buf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200242struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécailled7215712023-03-24 18:13:37 +0100243static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack);
244static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200245static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
246static int quic_conn_init_timer(struct quic_conn *qc);
247static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
248
249/* Only for debug purpose */
250struct enc_debug_info {
251 unsigned char *payload;
252 size_t payload_len;
253 unsigned char *aad;
254 size_t aad_len;
255 uint64_t pn;
256};
257
258/* Initializes a enc_debug_info struct (only for debug purpose) */
259static inline void enc_debug_info_init(struct enc_debug_info *edi,
260 unsigned char *payload, size_t payload_len,
261 unsigned char *aad, size_t aad_len, uint64_t pn)
262{
263 edi->payload = payload;
264 edi->payload_len = payload_len;
265 edi->aad = aad;
266 edi->aad_len = aad_len;
267 edi->pn = pn;
268}
269
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100270/* Used only for QUIC TLS key phase traces */
271struct quic_kp_trace {
272 const unsigned char *rx_sec;
273 size_t rx_seclen;
274 const struct quic_tls_kp *rx;
275 const unsigned char *tx_sec;
276 size_t tx_seclen;
277 const struct quic_tls_kp *tx;
278};
279
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200280/* Trace callback for QUIC.
281 * These traces always expect that arg1, if non-null, is of type connection.
282 */
283static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
284 const struct ist where, const struct ist func,
285 const void *a1, const void *a2, const void *a3, const void *a4)
286{
287 const struct quic_conn *qc = a1;
288
289 if (qc) {
290 const struct quic_tls_ctx *tls_ctx;
291
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +0200292 chunk_appendf(&trace_buf, " : qc@%p flags=0x%x", qc, qc->flags);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200293 if (mask & QUIC_EV_CONN_INIT) {
294 chunk_appendf(&trace_buf, "\n odcid");
295 quic_cid_dump(&trace_buf, &qc->odcid);
296 chunk_appendf(&trace_buf, "\n dcid");
297 quic_cid_dump(&trace_buf, &qc->dcid);
298 chunk_appendf(&trace_buf, "\n scid");
299 quic_cid_dump(&trace_buf, &qc->scid);
300 }
301
302 if (mask & QUIC_EV_TRANSP_PARAMS) {
303 const struct quic_transport_params *p = a2;
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100304
305 if (p)
306 quic_transport_params_dump(&trace_buf, qc, p);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200307 }
308
309 if (mask & QUIC_EV_CONN_ADDDATA) {
310 const enum ssl_encryption_level_t *level = a2;
311 const size_t *len = a3;
312
313 if (level) {
314 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
315
316 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
317 }
318 if (len)
319 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
320 }
321 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
322 /* Initial read & write secrets. */
323 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
324 const unsigned char *rx_sec = a2;
325 const unsigned char *tx_sec = a3;
326
327 tls_ctx = &qc->els[level].tls_ctx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200328 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
329 if (rx_sec)
330 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
331 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
332 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
333 if (tx_sec)
334 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
335 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200336 }
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100337
338 if ((mask & QUIC_EV_CONN_KP) && qc) {
339 /* Initial read & write secrets. */
340 const struct quic_kp_trace *kp = a2;
341
342 if (kp) {
343 if (kp->rx) {
344 chunk_appendf(&trace_buf, "\n RX kp");
345 if (kp->rx_sec)
346 quic_tls_secret_hexdump(&trace_buf, kp->rx_sec, kp->rx_seclen);
347 quic_tls_kp_keys_hexdump(&trace_buf, kp->rx);
348 }
349 if (kp->tx) {
350 chunk_appendf(&trace_buf, "\n TX kp");
351 if (kp->tx_sec)
352 quic_tls_secret_hexdump(&trace_buf, kp->tx_sec, kp->tx_seclen);
353 quic_tls_kp_keys_hexdump(&trace_buf, kp->tx);
354 }
355 }
356 }
357
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200358 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
359 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200360
361 if (level) {
362 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
363
364 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200365 if (quic_tls_has_rx_sec(&qc->els[lvl])) {
366 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200367 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200368 }
369 else
370 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200371 }
372 }
373
374 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
375 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200376
377 if (level) {
378 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
379
380 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200381 if (quic_tls_has_tx_sec(&qc->els[lvl])) {
382 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200383 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200384 }
385 else
386 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200387 }
388
389 }
390
391 if (mask & QUIC_EV_CONN_FRMLIST) {
392 const struct list *l = a2;
393
394 if (l) {
395 const struct quic_frame *frm;
396 list_for_each_entry(frm, l, list) {
397 chunk_appendf(&trace_buf, " frm@%p", frm);
398 chunk_frm_appendf(&trace_buf, frm);
399 }
400 }
401 }
402
403 if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) {
404 const struct quic_tx_packet *pkt = a2;
405 const struct quic_enc_level *qel = a3;
406 const ssize_t *room = a4;
407
408 if (qel) {
409 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200410 chunk_appendf(&trace_buf, " qel=%c flags=0x%x pto_count=%d cwnd=%llu ppif=%lld pif=%llu "
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200411 "if=%llu pp=%u",
412 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200413 qel->pktns->flags,
Frédéric Lécaille45400532023-02-13 18:39:19 +0100414 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200415 (unsigned long long)qc->path->cwnd,
416 (unsigned long long)qc->path->prep_in_flight,
417 (unsigned long long)qc->path->in_flight,
418 (unsigned long long)pktns->tx.in_flight,
419 pktns->tx.pto_probe);
420 }
421 if (pkt) {
422 const struct quic_frame *frm;
423 if (pkt->pn_node.key != (uint64_t)-1)
424 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
425 list_for_each_entry(frm, &pkt->frms, list) {
426 chunk_appendf(&trace_buf, " frm@%p", frm);
427 chunk_frm_appendf(&trace_buf, frm);
428 }
429 }
430
431 if (room) {
432 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
433 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
434 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
435 }
436 }
437
438 if (mask & QUIC_EV_CONN_IO_CB) {
439 const enum quic_handshake_state *state = a2;
440 const int *err = a3;
441
442 if (state)
443 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
444 if (err)
445 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
446 }
447
448 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
449 const struct quic_rx_packet *pkt = a2;
450 const unsigned long *pktlen = a3;
451 const SSL *ssl = a4;
452
453 if (pkt) {
454 chunk_appendf(&trace_buf, " pkt@%p", pkt);
455 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
456 chunk_appendf(&trace_buf, " kp=%d",
457 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
458 chunk_appendf(&trace_buf, " el=%c",
459 quic_packet_type_enc_level_char(pkt->type));
460 if (pkt->pnl)
461 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
462 (unsigned long long)pkt->pn);
463 if (pkt->token_len)
464 chunk_appendf(&trace_buf, " toklen=%llu",
465 (unsigned long long)pkt->token_len);
466 if (pkt->aad_len)
467 chunk_appendf(&trace_buf, " aadlen=%llu",
468 (unsigned long long)pkt->aad_len);
469 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
470 pkt->flags, (unsigned long long)pkt->len);
471 }
472 if (pktlen)
473 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
474 if (ssl) {
475 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
476 chunk_appendf(&trace_buf, " el=%c",
477 quic_enc_level_char(ssl_to_quic_enc_level(level)));
478 }
479 }
480
481 if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
482 const struct quic_rx_packet *pkt = a2;
483 const struct quic_rx_crypto_frm *cf = a3;
484 const SSL *ssl = a4;
485
486 if (pkt)
487 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
488 quic_packet_type_enc_level_char(pkt->type),
489 (unsigned long long)pkt->pn);
490 if (cf)
491 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
492 (unsigned long long)cf->offset_node.key,
493 (unsigned long long)cf->len);
494 if (ssl) {
495 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
496 chunk_appendf(&trace_buf, " rel=%c",
497 quic_enc_level_char(ssl_to_quic_enc_level(level)));
498 }
499
500 if (qc->err.code)
501 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code);
502 }
503
504 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
505 const struct quic_frame *frm = a2;
506
507 if (frm)
508 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
509 }
510
511 if (mask & QUIC_EV_CONN_PHPKTS) {
512 const struct quic_enc_level *qel = a2;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +0200513 const struct list *l = a3;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200514
515 if (qel) {
516 const struct quic_pktns *pktns = qel->pktns;
517 chunk_appendf(&trace_buf,
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200518 " qel=%c flags=0x%x 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 +0200519 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200520 qel->pktns->flags,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200521 quic_hdshk_state_str(qc->state),
522 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille45400532023-02-13 18:39:19 +0100523 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200524 (unsigned long long)qc->path->cwnd,
525 (unsigned long long)qc->path->prep_in_flight,
526 (unsigned long long)qc->path->in_flight,
527 (unsigned long long)pktns->tx.in_flight,
Amaury Denoyelle2f668f02022-11-18 15:24:08 +0100528 pktns->tx.pto_probe,
529 qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200530 }
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +0200531
532 if (l) {
533 const struct quic_frame *frm;
534 list_for_each_entry(frm, l, list) {
535 chunk_appendf(&trace_buf, " frm@%p", frm);
536 chunk_frm_appendf(&trace_buf, frm);
537 }
538 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200539 }
540
541 if (mask & QUIC_EV_CONN_ENCPKT) {
542 const struct enc_debug_info *edi = a2;
543
544 if (edi)
545 chunk_appendf(&trace_buf,
546 " payload=@%p payload_len=%llu"
547 " aad=@%p aad_len=%llu pn=%llu",
548 edi->payload, (unsigned long long)edi->payload_len,
549 edi->aad, (unsigned long long)edi->aad_len,
550 (unsigned long long)edi->pn);
551 }
552
553 if (mask & QUIC_EV_CONN_RMHP) {
554 const struct quic_rx_packet *pkt = a2;
555
556 if (pkt) {
557 const int *ret = a3;
558
559 chunk_appendf(&trace_buf, " pkt@%p", pkt);
560 if (ret && *ret)
561 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
562 pkt->pnl, (unsigned long long)pkt->pn);
563 }
564 }
565
566 if (mask & QUIC_EV_CONN_PRSAFRM) {
567 const struct quic_frame *frm = a2;
568 const unsigned long *val1 = a3;
569 const unsigned long *val2 = a4;
570
571 if (frm) {
572 chunk_appendf(&trace_buf, " frm@%p", frm);
573 chunk_frm_appendf(&trace_buf, frm);
574 }
575 if (val1)
576 chunk_appendf(&trace_buf, " %lu", *val1);
577 if (val2)
578 chunk_appendf(&trace_buf, "..%lu", *val2);
579 }
580
581 if (mask & QUIC_EV_CONN_ACKSTRM) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +0200582 const struct qf_stream *strm_frm = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200583 const struct qc_stream_desc *stream = a3;
584
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +0200585 if (strm_frm)
586 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)strm_frm->offset.key, (ull)strm_frm->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200587 if (stream)
588 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
589 }
590
591 if (mask & QUIC_EV_CONN_RTTUPDT) {
592 const unsigned int *rtt_sample = a2;
593 const unsigned int *ack_delay = a3;
594 const struct quic_loss *ql = a4;
595
596 if (rtt_sample)
597 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
598 if (ack_delay)
599 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
600 if (ql)
601 chunk_appendf(&trace_buf,
602 " srtt=%ums rttvar=%ums min_rtt=%ums",
Frédéric Lécaille35fb5932023-09-05 15:24:11 +0200603 ql->srtt, ql->rtt_var, ql->rtt_min);
604
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200605 }
606 if (mask & QUIC_EV_CONN_CC) {
607 const struct quic_cc_event *ev = a2;
608 const struct quic_cc *cc = a3;
609
610 if (a2)
611 quic_cc_event_trace(&trace_buf, ev);
612 if (a3)
613 quic_cc_state_trace(&trace_buf, cc);
614 }
615
616 if (mask & QUIC_EV_CONN_PKTLOSS) {
617 const struct quic_pktns *pktns = a2;
618 const struct list *lost_pkts = a3;
619
620 if (pktns) {
621 chunk_appendf(&trace_buf, " pktns=%s",
622 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
623 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
624 if (pktns->tx.loss_time)
625 chunk_appendf(&trace_buf, " loss_time=%dms",
626 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
627 }
628 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
629 struct quic_tx_packet *pkt;
630
631 chunk_appendf(&trace_buf, " lost_pkts:");
632 list_for_each_entry(pkt, lost_pkts, list)
633 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
634 }
635 }
636
637 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
638 const struct quic_pktns *pktns = a2;
639 const int *duration = a3;
640 const uint64_t *ifae_pkts = a4;
641
642 if (ifae_pkts)
643 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
644 (unsigned long long)*ifae_pkts);
645 if (pktns) {
646 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
647 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
648 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
649 pktns->tx.pto_probe);
650 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
651 if (pktns->tx.in_flight)
652 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
653 if (pktns->tx.loss_time)
654 chunk_appendf(&trace_buf, " loss_time=%dms",
655 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
656 }
657 if (mask & QUIC_EV_CONN_SPTO) {
658 if (pktns->tx.time_of_last_eliciting)
659 chunk_appendf(&trace_buf, " tole=%dms",
660 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
661 if (duration)
662 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
663 }
664 }
665
666 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
667 chunk_appendf(&trace_buf,
668 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
669 }
670 }
671
672 if (mask & QUIC_EV_CONN_SPPKTS) {
673 const struct quic_tx_packet *pkt = a2;
674
Frédéric Lécaille45400532023-02-13 18:39:19 +0100675 chunk_appendf(&trace_buf, " pto_count=%d cwnd=%llu ppif=%llu pif=%llu",
676 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200677 (unsigned long long)qc->path->cwnd,
678 (unsigned long long)qc->path->prep_in_flight,
679 (unsigned long long)qc->path->in_flight);
680 if (pkt) {
681 const struct quic_frame *frm;
Frédéric Lécaille6fd25762023-04-07 19:01:33 +0200682 if (pkt->flags & QUIC_FL_TX_PACKET_ACK)
683 chunk_appendf(&trace_buf, " ack");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200684 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
685 (unsigned long)pkt->pn_node.key,
686 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
687 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
688 (unsigned long long)pkt->in_flight_len);
689 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
690 (unsigned long long)qc->rx.bytes,
691 (unsigned long long)qc->tx.bytes);
692 list_for_each_entry(frm, &pkt->frms, list) {
693 chunk_appendf(&trace_buf, " frm@%p", frm);
694 chunk_frm_appendf(&trace_buf, frm);
695 }
Frédéric Lécaillebc09f742023-02-13 17:45:36 +0100696
697 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
698 chunk_appendf(&trace_buf, " with scid");
699 quic_cid_dump(&trace_buf, &qc->scid);
700 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200701 }
702 }
703
704 if (mask & QUIC_EV_CONN_SSLALERT) {
705 const uint8_t *alert = a2;
706 const enum ssl_encryption_level_t *level = a3;
707
708 if (alert)
709 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
710 if (level)
711 chunk_appendf(&trace_buf, " el=%c",
712 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
713 }
714
715 if (mask & QUIC_EV_CONN_BCFRMS) {
716 const size_t *sz1 = a2;
717 const size_t *sz2 = a3;
718 const size_t *sz3 = a4;
719
720 if (sz1)
721 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
722 if (sz2)
723 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
724 if (sz3)
725 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
726 }
727
728 if (mask & QUIC_EV_CONN_PSTRM) {
729 const struct quic_frame *frm = a2;
730
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100731 if (frm)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200732 chunk_frm_appendf(&trace_buf, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200733 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +0200734
735 if (mask & QUIC_EV_CONN_ELEVELSEL) {
736 const enum quic_handshake_state *state = a2;
737 const enum quic_tls_enc_level *level = a3;
738 const enum quic_tls_enc_level *next_level = a4;
739
740 if (state)
741 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state));
742 if (level)
743 chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level));
744 if (next_level)
745 chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level));
746
747 }
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200748
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200749 if (mask & QUIC_EV_CONN_IDLE_TIMER) {
750 if (tick_isset(qc->ack_expire))
751 chunk_appendf(&trace_buf, " ack_expire=%ums",
752 TICKS_TO_MS(tick_remain(now_ms, qc->ack_expire)));
753 if (tick_isset(qc->idle_expire))
754 chunk_appendf(&trace_buf, " idle_expire=%ums",
755 TICKS_TO_MS(tick_remain(now_ms, qc->idle_expire)));
Frédéric Lécaillece5c1452023-04-05 09:44:21 +0200756 if (qc->idle_timer_task && tick_isset(qc->idle_timer_task->expire))
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200757 chunk_appendf(&trace_buf, " expire=%ums",
758 TICKS_TO_MS(tick_remain(now_ms, qc->idle_timer_task->expire)));
759 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200760 }
Frédéric Lécailleaaf32f02023-05-12 17:37:29 +0200761
762 if (mask & QUIC_EV_CONN_RCV) {
763 int i;
764 const struct quic_dgram *dgram = a2;
765 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
766
767 if (qc) {
768 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
769 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
770 chunk_appendf(&trace_buf, " peer_addr=%s:%s ", bufaddr, bufport);
771 }
772
773 if (dgram) {
774 chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len);
775 /* Socket */
776 if (dgram->saddr.ss_family == AF_INET ||
777 dgram->saddr.ss_family == AF_INET6) {
778 addr_to_str(&dgram->saddr, bufaddr, sizeof(bufaddr));
779 port_to_str(&dgram->saddr, bufport, sizeof(bufport));
780 chunk_appendf(&trace_buf, "saddr=%s:%s ", bufaddr, bufport);
781
782 addr_to_str(&dgram->daddr, bufaddr, sizeof(bufaddr));
783 port_to_str(&dgram->daddr, bufport, sizeof(bufport));
784 chunk_appendf(&trace_buf, "daddr=%s:%s ", bufaddr, bufport);
785 }
786 /* DCID */
787 for (i = 0; i < dgram->dcid_len; ++i)
788 chunk_appendf(&trace_buf, "%02x", dgram->dcid[i]);
789
790 }
791 }
792
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200793 if (mask & QUIC_EV_CONN_LPKT) {
794 const struct quic_rx_packet *pkt = a2;
795 const uint64_t *len = a3;
796 const struct quic_version *ver = a4;
797
798 if (pkt) {
799 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
800 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
801 if (pkt->pn_node.key != (uint64_t)-1)
802 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
803 }
804
805 if (len)
806 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
807
808 if (ver)
809 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
810 }
811
812 if (mask & QUIC_EV_STATELESS_RST) {
813 const struct quic_cid *cid = a2;
814
815 if (cid)
816 quic_cid_dump(&trace_buf, cid);
817 }
818
819}
820
821/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
822static inline int quic_peer_validated_addr(struct quic_conn *qc)
823{
824 struct quic_pktns *hdshk_pktns, *app_pktns;
825
826 if (!qc_is_listener(qc))
827 return 1;
828
829 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
830 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
831 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
832 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
833 qc->state >= QUIC_HS_ST_COMPLETE)
834 return 1;
835
836 return 0;
837}
838
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100839/* To be called to kill a connection as soon as possible (without sending any packet). */
840void qc_kill_conn(struct quic_conn *qc)
841{
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100842 TRACE_ENTER(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200843 TRACE_PROTO("killing the connection", QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100844 qc->flags |= QUIC_FL_CONN_TO_KILL;
Frédéric Lécaillef4bec872023-11-20 16:54:57 +0100845 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Amaury Denoyelleeaa32c62024-06-04 11:56:09 +0200846
847 if (!(qc->flags & QUIC_FL_CONN_EXP_TIMER))
848 task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER);
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +0200849
850 qc_notify_err(qc);
851
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100852 TRACE_LEAVE(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100853}
854
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200855/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
856 * both loss detection and PTO and schedule the task assiated to this timer if needed.
857 */
858static inline void qc_set_timer(struct quic_conn *qc)
859{
860 struct quic_pktns *pktns;
861 unsigned int pto;
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100862 int handshake_confirmed;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200863
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100864 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc);
865 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200866
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100867 pktns = NULL;
868 if (!qc->timer_task) {
869 TRACE_PROTO("already released timer task", QUIC_EV_CONN_STIMER, qc);
870 goto leave;
871 }
872
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200873 pktns = quic_loss_pktns(qc);
874 if (tick_isset(pktns->tx.loss_time)) {
875 qc->timer = pktns->tx.loss_time;
876 goto out;
877 }
878
879 /* anti-amplification: the timer must be
880 * cancelled for a server which reached the anti-amplification limit.
881 */
882 if (!quic_peer_validated_addr(qc) &&
883 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
884 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
885 qc->timer = TICK_ETERNITY;
886 goto out;
887 }
888
889 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
890 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
891 /* Timer cancellation. */
892 qc->timer = TICK_ETERNITY;
893 goto out;
894 }
895
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100896 handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED;
897 pktns = quic_pto_pktns(qc, handshake_confirmed, &pto);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200898 if (tick_isset(pto))
899 qc->timer = pto;
900 out:
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100901 if (qc->timer == TICK_ETERNITY) {
902 qc->timer_task->expire = TICK_ETERNITY;
903 }
904 else if (tick_is_expired(qc->timer, now_ms)) {
905 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
906 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
907 }
908 else {
909 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
910 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200911 }
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100912 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100913 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, pktns);
914 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200915}
916
917/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
918 * connection.
919 * Return 1 if succeeded, 0 if not.
920 */
921static int quic_tls_key_update(struct quic_conn *qc)
922{
923 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 +0100924 struct quic_tls_secrets *rx = &tls_ctx->rx;
925 struct quic_tls_secrets *tx = &tls_ctx->tx;
926 /* Used only for the traces */
927 struct quic_kp_trace kp_trace = {
928 .rx_sec = rx->secret,
929 .rx_seclen = rx->secretlen,
930 .tx_sec = tx->secret,
931 .tx_seclen = tx->secretlen,
932 };
933 /* The next key phase secrets to be derived */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200934 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
935 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
936 const struct quic_version *ver =
937 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
938 int ret = 0;
939
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100940 TRACE_ENTER(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200941
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200942 nxt_rx = &qc->ku.nxt_rx;
943 nxt_tx = &qc->ku.nxt_tx;
944
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100945 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
946 "nxt_rx->secretlen=%llu rx->secretlen=%llu",
947 (ull)nxt_rx->secretlen, (ull)rx->secretlen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200948 /* Prepare new RX secrets */
949 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
950 rx->secret, rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100951 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200952 goto leave;
953 }
954
955 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
956 nxt_rx->key, nxt_rx->keylen,
957 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
958 nxt_rx->secret, nxt_rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100959 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200960 goto leave;
961 }
962
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100963 kp_trace.rx = nxt_rx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200964 /* Prepare new TX secrets */
965 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
966 tx->secret, tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100967 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200968 goto leave;
969 }
970
971 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
972 nxt_tx->key, nxt_tx->keylen,
973 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
974 nxt_tx->secret, nxt_tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100975 TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200976 goto leave;
977 }
978
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100979 kp_trace.tx = nxt_tx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200980 if (nxt_rx->ctx) {
981 EVP_CIPHER_CTX_free(nxt_rx->ctx);
982 nxt_rx->ctx = NULL;
983 }
984
985 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
Frédéric Lécaille7a01ff72023-05-02 20:03:19 +0200986 TRACE_ERROR("could not initialize RX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200987 goto leave;
988 }
989
990 if (nxt_tx->ctx) {
991 EVP_CIPHER_CTX_free(nxt_tx->ctx);
992 nxt_tx->ctx = NULL;
993 }
994
Frédéric Lécaille7a01ff72023-05-02 20:03:19 +0200995 if (!quic_tls_tx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
996 TRACE_ERROR("could not initialize TX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200997 goto leave;
998 }
999
1000 ret = 1;
1001 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01001002 TRACE_PROTO("key update", QUIC_EV_CONN_KP, qc, &kp_trace);
1003 TRACE_LEAVE(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001004 return ret;
1005}
1006
1007/* Rotate the Key Update information for <qc> QUIC connection.
1008 * Must be used after having updated them.
1009 * Always succeeds.
1010 */
1011static void quic_tls_rotate_keys(struct quic_conn *qc)
1012{
1013 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
1014 unsigned char *curr_secret, *curr_iv, *curr_key;
1015 EVP_CIPHER_CTX *curr_ctx;
1016
1017 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1018
1019 /* Rotate the RX secrets */
1020 curr_ctx = tls_ctx->rx.ctx;
1021 curr_secret = tls_ctx->rx.secret;
1022 curr_iv = tls_ctx->rx.iv;
1023 curr_key = tls_ctx->rx.key;
1024
1025 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
1026 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
1027 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
1028 tls_ctx->rx.key = qc->ku.nxt_rx.key;
1029
1030 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
1031 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
1032 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
1033 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
1034
1035 qc->ku.prv_rx.ctx = curr_ctx;
1036 qc->ku.prv_rx.secret = curr_secret;
1037 qc->ku.prv_rx.iv = curr_iv;
1038 qc->ku.prv_rx.key = curr_key;
1039 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
1040
1041 /* Update the TX secrets */
1042 curr_ctx = tls_ctx->tx.ctx;
1043 curr_secret = tls_ctx->tx.secret;
1044 curr_iv = tls_ctx->tx.iv;
1045 curr_key = tls_ctx->tx.key;
1046
1047 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
1048 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
1049 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
1050 tls_ctx->tx.key = qc->ku.nxt_tx.key;
1051
1052 qc->ku.nxt_tx.ctx = curr_ctx;
1053 qc->ku.nxt_tx.secret = curr_secret;
1054 qc->ku.nxt_tx.iv = curr_iv;
1055 qc->ku.nxt_tx.key = curr_key;
1056
1057 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1058}
1059
1060/* returns 0 on error, 1 on success */
1061int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
1062 const uint8_t *read_secret,
1063 const uint8_t *write_secret, size_t secret_len)
1064{
1065 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1066 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
1067 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001068 struct quic_tls_secrets *rx = NULL, *tx = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001069 const struct quic_version *ver =
1070 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
1071 int ret = 0;
1072
1073 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
1074 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001075
1076 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1077 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1078 goto out;
1079 }
1080
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001081 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1082 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001083 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001084 }
1085
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001086 if (!read_secret)
1087 goto write;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001088
1089 rx = &tls_ctx->rx;
Frédéric Lécaille8fbabea2023-10-11 09:28:36 +02001090 rx->aead = tls_aead(cipher);
1091 rx->md = tls_md(cipher);
1092 rx->hp = tls_hp(cipher);
1093 if (!rx->aead || !rx->md || !rx->hp)
1094 goto leave;
1095
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001096 if (!quic_tls_secrets_keys_alloc(rx)) {
1097 TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1098 goto leave;
1099 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001100
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001101 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen,
1102 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
1103 read_secret, secret_len)) {
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001104 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001105 goto leave;
1106 }
1107
1108 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
1109 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1110 goto leave;
1111 }
1112
1113 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
1114 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1115 goto leave;
1116 }
1117
1118 /* Enqueue this connection asap if we could derive O-RTT secrets as
1119 * listener. Note that a listener derives only RX secrets for this
1120 * level.
1121 */
1122 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
1123 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
1124 quic_accept_push_qc(qc);
1125 }
1126
1127write:
1128
1129 if (!write_secret)
Frédéric Lécailleaada8812023-06-06 17:40:41 +02001130 goto keyupdate_init;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001131
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001132 tx = &tls_ctx->tx;
Frédéric Lécaille8fbabea2023-10-11 09:28:36 +02001133 tx->aead = tls_aead(cipher);
1134 tx->md = tls_md(cipher);
1135 tx->hp = tls_hp(cipher);
1136 if (!tx->aead || !tx->md || !tx->hp)
1137 goto leave;
1138
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001139 if (!quic_tls_secrets_keys_alloc(tx)) {
1140 TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1141 goto leave;
1142 }
1143
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001144 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen,
1145 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
1146 write_secret, secret_len)) {
1147 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
1148 goto leave;
1149 }
1150
1151 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
1152 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1153 goto leave;
1154 }
1155
1156 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
1157 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1158 goto leave;
1159 }
1160
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01001161 if (level == ssl_encryption_handshake && qc_is_listener(qc)) {
1162 qc->enc_params_len =
1163 quic_transport_params_encode(qc->enc_params,
1164 qc->enc_params + sizeof qc->enc_params,
1165 &qc->rx.params, ver, 1);
1166 if (!qc->enc_params_len) {
1167 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC);
1168 goto leave;
1169 }
1170
1171 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
1172 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC);
1173 goto leave;
1174 }
1175 }
1176
Frédéric Lécailleaada8812023-06-06 17:40:41 +02001177 keyupdate_init:
1178 /* Store the secret provided by the TLS stack, required for keyupdate. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001179 if (level == ssl_encryption_application) {
1180 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
1181 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
1182 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
1183
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001184 if (rx) {
1185 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1186 TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1187 goto leave;
1188 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001189
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001190 memcpy(rx->secret, read_secret, secret_len);
1191 rx->secretlen = secret_len;
1192 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001193
1194 if (tx) {
1195 if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1196 TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1197 goto leave;
1198 }
1199
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001200 memcpy(tx->secret, write_secret, secret_len);
1201 tx->secretlen = secret_len;
1202 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001203
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001204 /* Initialize all the secret keys lengths */
1205 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001206 }
1207
1208 out:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001209 ret = 1;
1210 leave:
Frédéric Lécaille8fbabea2023-10-11 09:28:36 +02001211 if (!ret) {
1212 /* Release the CRYPTO frames which have been provided by the TLS stack
1213 * to prevent the transmission of ack-eliciting packets.
1214 */
1215 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
1216 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
1217 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns);
1218 quic_set_tls_alert(qc, SSL_AD_HANDSHAKE_FAILURE);
1219 }
1220
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001221 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
1222 return ret;
1223}
1224
1225/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1226 * with <len> as size in CRYPTO buffers dedicated to store the information about
1227 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1228 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
1229 * buffers to store all the data.
1230 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1231 */
1232static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
1233 const unsigned char *data, size_t len)
1234{
1235 struct quic_crypto_buf **qcb;
1236 /* The remaining byte to store in CRYPTO buffers. */
1237 size_t cf_offset, cf_len, *nb_buf;
1238 unsigned char *pos;
1239 int ret = 0;
1240
1241 nb_buf = &qel->tx.crypto.nb_buf;
1242 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1243 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1244 cf_len = len;
1245
1246 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1247
1248 while (len) {
1249 size_t to_copy, room;
1250
1251 pos = (*qcb)->data + (*qcb)->sz;
1252 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1253 to_copy = len > room ? room : len;
1254 if (to_copy) {
1255 memcpy(pos, data, to_copy);
1256 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1257 qel->tx.crypto.sz += to_copy;
1258 (*qcb)->sz += to_copy;
1259 len -= to_copy;
1260 data += to_copy;
1261 }
1262 else {
1263 struct quic_crypto_buf **tmp;
1264
1265 // FIXME: realloc!
1266 tmp = realloc(qel->tx.crypto.bufs,
1267 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1268 if (tmp) {
1269 qel->tx.crypto.bufs = tmp;
1270 qcb = &qel->tx.crypto.bufs[*nb_buf];
1271 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1272 if (!*qcb) {
1273 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1274 goto leave;
1275 }
1276
1277 (*qcb)->sz = 0;
1278 ++*nb_buf;
1279 }
1280 else {
1281 break;
1282 }
1283 }
1284 }
1285
1286 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1287 * have been buffered.
1288 */
1289 if (!len) {
1290 struct quic_frame *frm;
1291 struct quic_frame *found = NULL;
1292
1293 /* There is at most one CRYPTO frame in this packet number
1294 * space. Let's look for it.
1295 */
1296 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
1297 if (frm->type != QUIC_FT_CRYPTO)
1298 continue;
1299
1300 /* Found */
1301 found = frm;
1302 break;
1303 }
1304
1305 if (found) {
1306 found->crypto.len += cf_len;
1307 }
1308 else {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001309 frm = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001310 if (!frm) {
1311 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1312 goto leave;
1313 }
1314
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001315 frm->crypto.offset = cf_offset;
1316 frm->crypto.len = cf_len;
1317 frm->crypto.qel = qel;
1318 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
1319 }
1320 }
1321 ret = len == 0;
1322 leave:
1323 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1324 return ret;
1325}
1326
1327/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1328 * activity for <qc> will be interrupted.
1329 */
1330void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
1331{
1332 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1333 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
1334 goto leave;
1335
1336 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
1337 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
1338 qc->err.code = err.code;
1339 qc->err.app = err.app;
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02001340
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001341 leave:
1342 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1343}
1344
1345/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1346void quic_set_tls_alert(struct quic_conn *qc, int alert)
1347{
1348 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1349
1350 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1351 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
1352 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
1353 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1354 }
1355 quic_set_connection_close(qc, quic_err_tls(alert));
1356 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
1357 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1358
1359 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1360}
1361
1362/* Set the application for <qc> QUIC connection.
1363 * Return 1 if succeeded, 0 if not.
1364 */
1365int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1366{
1367 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1368 qc->app_ops = &h3_ops;
1369 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1370 qc->app_ops = &hq_interop_ops;
1371 else
1372 return 0;
1373
1374 return 1;
1375}
1376
1377/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1378 * wants to provide the QUIC layer with CRYPTO data.
1379 * Returns 1 if succeeded, 0 if not.
1380 */
1381int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1382 const uint8_t *data, size_t len)
1383{
1384 struct quic_conn *qc;
1385 enum quic_tls_enc_level tel;
1386 struct quic_enc_level *qel;
1387 int ret = 0;
1388
1389 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1390 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1391
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001392 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1393 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1394 goto out;
1395 }
1396
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001397 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1398 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
1399 goto out;
1400 }
1401
1402 tel = ssl_to_quic_enc_level(level);
1403 if (tel == -1) {
1404 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1405 goto leave;
1406 }
1407
1408 qel = &qc->els[tel];
1409 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1410 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1411 goto leave;
1412 }
1413
1414 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
1415 qc, &level, &len);
1416 out:
1417 ret = 1;
1418 leave:
1419 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1420 return ret;
1421}
1422
1423int ha_quic_flush_flight(SSL *ssl)
1424{
1425 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1426
1427 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1428 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
1429
1430 return 1;
1431}
1432
1433int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1434{
1435 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1436
1437 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1438
1439 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1440
1441 quic_set_tls_alert(qc, alert);
1442 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1443 return 1;
1444}
1445
1446/* QUIC TLS methods */
1447static SSL_QUIC_METHOD ha_quic_method = {
1448 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1449 .add_handshake_data = ha_quic_add_handshake_data,
1450 .flush_flight = ha_quic_flush_flight,
1451 .send_alert = ha_quic_send_alert,
1452};
1453
1454/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1455 * Returns an error count.
1456 */
1457int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1458{
1459 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1460 int cfgerr = 0;
1461
1462 long options =
1463 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1464 SSL_OP_SINGLE_ECDH_USE |
1465 SSL_OP_CIPHER_SERVER_PREFERENCE;
1466 SSL_CTX *ctx;
1467
1468 ctx = SSL_CTX_new(TLS_server_method());
1469 bind_conf->initial_ctx = ctx;
1470
1471 SSL_CTX_set_options(ctx, options);
1472 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1473 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1474 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1475
1476#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1477# if defined(HAVE_SSL_CLIENT_HELLO_CB)
1478# if defined(SSL_OP_NO_ANTI_REPLAY)
1479 if (bind_conf->ssl_conf.early_data) {
1480 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
William Lallemand3ccfc102023-08-21 13:51:56 +02001481# ifdef USE_QUIC_OPENSSL_COMPAT
1482 ha_warning("Binding [%s:%d] for %s %s: 0-RTT is not supported in limited QUIC compatibility mode, ignored.\n",
1483 bind_conf->file, bind_conf->line, proxy_type_str(bind_conf->frontend), bind_conf->frontend->id);
1484# else
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001485 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
William Lallemand3ccfc102023-08-21 13:51:56 +02001486# endif /* ! USE_QUIC_OPENSSL_COMPAT */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001487 }
1488# endif /* !SSL_OP_NO_ANTI_REPLAY */
1489 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1490 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1491# else /* ! HAVE_SSL_CLIENT_HELLO_CB */
1492 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1493# endif
1494 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1495#endif
Frédéric Lécaille29d2ce32023-06-08 09:28:31 +02001496#ifdef USE_QUIC_OPENSSL_COMPAT
1497 if (!quic_tls_compat_init(bind_conf, ctx))
William Lallemand48d3c302023-08-21 15:22:57 +02001498 cfgerr++;
Frédéric Lécaille29d2ce32023-06-08 09:28:31 +02001499#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001500
1501 return cfgerr;
1502}
1503
1504/* Decode an expected packet number from <truncated_on> its truncated value,
1505 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1506 * the number of bits used to encode this packet number (its length in bytes * 8).
1507 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1508 */
1509static uint64_t decode_packet_number(uint64_t largest_pn,
1510 uint32_t truncated_pn, unsigned int pn_nbits)
1511{
1512 uint64_t expected_pn = largest_pn + 1;
1513 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1514 uint64_t pn_hwin = pn_win / 2;
1515 uint64_t pn_mask = pn_win - 1;
1516 uint64_t candidate_pn;
1517
1518
1519 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1520 /* Note that <pn_win> > <pn_hwin>. */
1521 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1522 candidate_pn + pn_hwin <= expected_pn)
1523 return candidate_pn + pn_win;
1524
1525 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1526 return candidate_pn - pn_win;
1527
1528 return candidate_pn;
1529}
1530
1531/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1532 * cryptographic context.
1533 * <largest_pn> is the largest received packet number and <pn> the address of
1534 * the packet number field for this packet with <byte0> address of its first byte.
1535 * <end> points to one byte past the end of this packet.
1536 * Returns 1 if succeeded, 0 if not.
1537 */
1538static int qc_do_rm_hp(struct quic_conn *qc,
1539 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1540 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
1541{
1542 int ret, i, pnlen;
1543 uint64_t packet_number;
1544 uint32_t truncated_pn = 0;
1545 unsigned char mask[5] = {0};
1546 unsigned char *sample;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001547
1548 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1549
1550 ret = 0;
1551
1552 /* Check there is enough data in this packet. */
1553 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1554 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1555 goto leave;
1556 }
1557
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001558 sample = pn + QUIC_PACKET_PN_MAXLEN;
1559
1560 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1561 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
1562 goto leave;
1563 }
1564
1565 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1566 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1567 for (i = 0; i < pnlen; i++) {
1568 pn[i] ^= mask[i + 1];
1569 truncated_pn = (truncated_pn << 8) | pn[i];
1570 }
1571
1572 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1573 /* Store remaining information for this unprotected header */
1574 pkt->pn = packet_number;
1575 pkt->pnl = pnlen;
1576
1577 ret = 1;
1578 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001579 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
1580 return ret;
1581}
1582
1583/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1584 * address, with <payload_len> as payload length, <aad> as address of
1585 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1586 * context.
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02001587 *
1588 * TODO no error is expected as encryption is done in place but encryption
1589 * manual is unclear. <fail> will be set to true if an error is detected.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001590 */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02001591static void quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1592 unsigned char *aad, size_t aad_len, uint64_t pn,
1593 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc,
1594 int *fail)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001595{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001596 unsigned char iv[QUIC_TLS_IV_LEN];
1597 unsigned char *tx_iv = tls_ctx->tx.iv;
1598 size_t tx_iv_sz = tls_ctx->tx.ivlen;
1599 struct enc_debug_info edi;
1600
1601 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02001602 *fail = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001603
Amaury Denoyelle5eadc272023-05-16 18:11:01 +02001604 quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001605
1606 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Emeric Brune0190c62023-07-11 14:53:41 +02001607 tls_ctx->tx.ctx, tls_ctx->tx.aead, iv)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001608 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02001609 *fail = 1;
1610 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001611 }
1612
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001613 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001614}
1615
Frédéric Lécaille72027782023-02-22 16:20:09 +01001616/* Select the correct TLS cipher context to used to decipher <pkt> packet
1617 * attached to <qc> connection from <qel> encryption level.
1618 */
1619static inline struct quic_tls_ctx *qc_select_tls_ctx(struct quic_conn *qc,
1620 struct quic_enc_level *qel,
1621 struct quic_rx_packet *pkt)
1622{
1623 return pkt->type != QUIC_PACKET_TYPE_INITIAL ? &qel->tls_ctx :
1624 pkt->version == qc->negotiated_version ? &qc->negotiated_ictx : &qel->tls_ctx;
1625}
1626
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001627/* Decrypt <pkt> packet using encryption level <qel> for <qc> connection.
1628 * Decryption is done in place in packet buffer.
1629 *
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +05001630 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001631 */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001632static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel,
1633 struct quic_rx_packet *pkt)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001634{
1635 int ret, kp_changed;
1636 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaille72027782023-02-22 16:20:09 +01001637 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001638 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
1639 unsigned char *rx_iv = tls_ctx->rx.iv;
1640 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1641 unsigned char *rx_key = tls_ctx->rx.key;
1642
1643 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1644
1645 ret = 0;
1646 kp_changed = 0;
1647
1648 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1649 /* The two tested bits are not at the same position,
1650 * this is why they are first both inversed.
1651 */
1652 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1653 if (pkt->pn < tls_ctx->rx.pn) {
1654 /* The lowest packet number of a previous key phase
1655 * cannot be null if it really stores previous key phase
1656 * secrets.
1657 */
1658 // TODO: check if BUG_ON() more suitable
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001659 if (!qc->ku.prv_rx.pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001660 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1661 goto leave;
1662 }
1663
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001664 rx_ctx = qc->ku.prv_rx.ctx;
1665 rx_iv = qc->ku.prv_rx.iv;
1666 rx_key = qc->ku.prv_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001667 }
1668 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1669 /* Next key phase */
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +01001670 TRACE_PROTO("Key phase changed", QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001671 kp_changed = 1;
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001672 rx_ctx = qc->ku.nxt_rx.ctx;
1673 rx_iv = qc->ku.nxt_rx.iv;
1674 rx_key = qc->ku.nxt_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001675 }
1676 }
1677 }
1678
Amaury Denoyelle5eadc272023-05-16 18:11:01 +02001679 quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001680
1681 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1682 pkt->data, pkt->aad_len,
1683 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
1684 if (!ret) {
1685 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1686 goto leave;
1687 }
1688
1689 /* Update the keys only if the packet decryption succeeded. */
1690 if (kp_changed) {
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001691 quic_tls_rotate_keys(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001692 /* Toggle the Key Phase bit */
1693 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1694 /* Store the lowest packet number received for the current key phase */
1695 tls_ctx->rx.pn = pkt->pn;
1696 /* Prepare the next key update */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001697 if (!quic_tls_key_update(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001698 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1699 goto leave;
1700 }
1701 }
1702
1703 /* Update the packet length (required to parse the frames). */
1704 pkt->len -= QUIC_TLS_TAG_LEN;
1705 ret = 1;
1706 leave:
1707 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1708 return ret;
1709}
1710
1711
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001712/* Release <frm> frame and mark its copies as acknowledged */
1713void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
1714{
1715 uint64_t pn;
1716 struct quic_frame *origin, *f, *tmp;
1717
1718 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1719
1720 /* Identify this frame: a frame copy or one of its copies */
1721 origin = frm->origin ? frm->origin : frm;
1722 /* Ensure the source of the copies is flagged as acked, <frm> being
1723 * possibly a copy of <origin>
1724 */
1725 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1726 /* Mark all the copy of <origin> as acknowledged. We must
1727 * not release the packets (releasing the frames) at this time as
1728 * they are possibly also to be acknowledged alongside the
1729 * the current one.
1730 */
1731 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1732 if (f->pkt) {
1733 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1734 f->origin = NULL;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001735 LIST_DEL_INIT(&f->ref);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001736 pn = f->pkt->pn_node.key;
1737 TRACE_DEVEL("mark frame as acked from packet",
1738 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1739 }
1740 else {
1741 TRACE_DEVEL("freeing unsent frame",
1742 QUIC_EV_CONN_PRSAFRM, qc, f);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001743 LIST_DEL_INIT(&f->ref);
1744 qc_frm_free(&f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001745 }
1746 }
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001747 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001748 pn = frm->pkt->pn_node.key;
1749 quic_tx_packet_refdec(frm->pkt);
1750 TRACE_DEVEL("freeing frame from packet",
1751 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001752 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001753
1754 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1755}
1756
1757/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1758 * and all STREAM data are acknowledged. The MUX is responsible to have set
1759 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1760 *
1761 * TODO this should also be called on lost packet detection
1762 */
1763void qc_check_close_on_released_mux(struct quic_conn *qc)
1764{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001765 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1766
1767 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1768 /* Reuse errcode which should have been previously set by the MUX on release. */
1769 quic_set_connection_close(qc, qc->err);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02001770 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001771 }
1772
1773 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1774}
1775
1776/* Remove from <stream> the acknowledged frames.
1777 *
1778 * Returns 1 if at least one frame was removed else 0.
1779 */
1780static int quic_stream_try_to_consume(struct quic_conn *qc,
1781 struct qc_stream_desc *stream)
1782{
1783 int ret;
1784 struct eb64_node *frm_node;
1785
1786 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1787
1788 ret = 0;
1789 frm_node = eb64_first(&stream->acked_frms);
1790 while (frm_node) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001791 struct qf_stream *strm_frm;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001792 struct quic_frame *frm;
1793 size_t offset, len;
1794
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001795 strm_frm = eb64_entry(frm_node, struct qf_stream, offset);
1796 offset = strm_frm->offset.key;
1797 len = strm_frm->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001798
1799 if (offset > stream->ack_offset)
1800 break;
1801
1802 if (qc_stream_desc_ack(&stream, offset, len)) {
1803 /* cf. next comment : frame may be freed at this stage. */
1804 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001805 qc, stream ? strm_frm : NULL, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001806 ret = 1;
1807 }
1808
1809 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1810 * has been freed. with the stream frames tree. Nothing to do
1811 * anymore in here.
1812 */
1813 if (!stream) {
1814 qc_check_close_on_released_mux(qc);
1815 ret = 1;
1816 goto leave;
1817 }
1818
1819 frm_node = eb64_next(frm_node);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001820 eb64_delete(&strm_frm->offset);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001821
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001822 frm = container_of(strm_frm, struct quic_frame, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001823 qc_release_frm(qc, frm);
1824 }
1825
1826 leave:
1827 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
1828 return ret;
1829}
1830
1831/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1832static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1833 struct quic_frame *frm)
1834{
Frédéric Lécaille8f991942023-03-24 15:14:45 +01001835 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1836 TRACE_PROTO("RX ack TX frm", QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001837
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001838 switch (frm->type) {
1839 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1840 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02001841 struct qf_stream *strm_frm = &frm->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001842 struct eb64_node *node = NULL;
1843 struct qc_stream_desc *stream = NULL;
1844 const size_t offset = strm_frm->offset.key;
1845 const size_t len = strm_frm->len;
1846
1847 /* do not use strm_frm->stream as the qc_stream_desc instance
1848 * might be freed at this stage. Use the id to do a proper
1849 * lookup.
1850 *
1851 * TODO if lookup operation impact on the perf is noticeable,
1852 * implement a refcount on qc_stream_desc instances.
1853 */
1854 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1855 if (!node) {
1856 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
1857 qc_release_frm(qc, frm);
1858 /* early return */
1859 goto leave;
1860 }
1861 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1862
1863 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
1864 if (offset <= stream->ack_offset) {
1865 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001866 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1867 qc, strm_frm, stream);
1868 }
1869
1870 if (!stream) {
1871 /* no need to continue if stream freed. */
1872 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
1873 qc_release_frm(qc, frm);
1874 qc_check_close_on_released_mux(qc);
1875 break;
1876 }
1877
1878 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1879 qc, strm_frm, stream);
1880 qc_release_frm(qc, frm);
1881 }
1882 else {
1883 eb64_insert(&stream->acked_frms, &strm_frm->offset);
1884 }
1885
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01001886 quic_stream_try_to_consume(qc, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001887 }
1888 break;
1889 default:
1890 qc_release_frm(qc, frm);
1891 }
1892
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001893 leave:
1894 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1895}
1896
Frédéric Lécaille39bde282023-08-29 14:48:54 +02001897/* Collect newly acknowledged TX packets from <pkts> ebtree into <newly_acked_pkts>
1898 * list depending on <largest> and <smallest> packet number of a range of acknowledged
1899 * packets announced in an ACK frame. <largest_node> may be provided to start
1900 * looking from this packet node.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001901 */
Frédéric Lécaille39bde282023-08-29 14:48:54 +02001902static void qc_newly_acked_pkts(struct quic_conn *qc, struct eb_root *pkts,
1903 struct list *newly_acked_pkts,
1904 struct eb64_node *largest_node,
1905 uint64_t largest, uint64_t smallest)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001906{
1907 struct eb64_node *node;
1908 struct quic_tx_packet *pkt;
1909
1910 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1911
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001912 node = eb64_lookup_ge(pkts, smallest);
1913 if (!node)
1914 goto leave;
1915
1916 largest_node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
1917 if (!largest_node)
1918 goto leave;
1919
1920 while (node && node->key <= largest_node->key) {
Frédéric Lécaille39bde282023-08-29 14:48:54 +02001921 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
1922 LIST_APPEND(newly_acked_pkts, &pkt->list);
1923 node = eb64_next(node);
1924 eb64_delete(&pkt->pn_node);
1925 }
1926
1927 leave:
1928 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1929}
1930
1931/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1932 * deallocating them, and their TX frames.
1933 * May be NULL if <largest> node could not be found.
1934 */
1935static void qc_ackrng_pkts(struct quic_conn *qc,
1936 unsigned int *pkt_flags, struct list *newly_acked_pkts)
1937{
1938 struct quic_tx_packet *pkt, *tmp;
1939
1940 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1941
1942 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001943 struct quic_frame *frm, *frmbak;
1944
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001945 *pkt_flags |= pkt->flags;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001946 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
1947 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1948 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaille814645f2022-11-18 18:15:28 +01001949 /* If there are others packet in the same datagram <pkt> is attached to,
1950 * detach the previous one and the next one from <pkt>.
1951 */
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01001952 quic_tx_packet_dgram_detach(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001953 eb64_delete(&pkt->pn_node);
1954 }
1955
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001956 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001957 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001958}
1959
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001960/* Remove all frames from <pkt_frm_list> and reinsert them in the same order
1961 * they have been sent into <pktns_frm_list>. The loss counter of each frame is
1962 * incremented and checked if it does not exceed retransmission limit.
1963 *
1964 * Returns 1 on success, 0 if a frame loss limit is exceeded. A
1965 * CONNECTION_CLOSE is scheduled in this case.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001966 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001967static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1968 struct quic_tx_packet *pkt,
1969 struct list *pktns_frm_list)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001970{
1971 struct quic_frame *frm, *frmbak;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001972 struct list *pkt_frm_list = &pkt->frms;
1973 uint64_t pn = pkt->pn_node.key;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001974 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001975
1976 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1977
1978 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1979 /* First remove this frame from the packet it was attached to */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001980 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001981 quic_tx_packet_refdec(pkt);
1982 /* At this time, this frame is not freed but removed from its packet */
1983 frm->pkt = NULL;
1984 /* Remove any reference to this frame */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001985 qc_frm_unref(frm, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001986 switch (frm->type) {
1987 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1988 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02001989 struct qf_stream *strm_frm = &frm->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001990 struct eb64_node *node = NULL;
1991 struct qc_stream_desc *stream_desc;
1992
1993 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1994 if (!node) {
1995 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1996 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1997 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001998 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001999 continue;
2000 }
2001
2002 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2003 /* Do not resend this frame if in the "already acked range" */
2004 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2005 TRACE_DEVEL("ignored frame in already acked range",
2006 QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002007 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002008 continue;
2009 }
2010 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01002011 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
2012
Frédéric Lécaillec425e032023-03-20 14:32:59 +01002013 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002014 TRACE_DEVEL("updated partially acked frame",
2015 QUIC_EV_CONN_PRSAFRM, qc, frm);
2016 }
2017 break;
2018 }
2019
2020 default:
2021 break;
2022 }
2023
2024 /* Do not resend probing packet with old data */
2025 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
2026 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
2027 qc, frm, &pn);
2028 if (frm->origin)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002029 LIST_DEL_INIT(&frm->ref);
2030 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002031 continue;
2032 }
2033
2034 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
2035 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2036 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
2037 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002038 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002039 }
2040 else {
Amaury Denoyelle24d5b722023-01-31 11:44:50 +01002041 if (++frm->loss_count >= global.tune.quic_max_frame_loss) {
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002042 TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc);
2043 quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR));
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02002044 qc_notify_err(qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002045 close = 1;
2046 }
2047
Frédéric Lécaillebe795ce2023-03-08 18:23:13 +01002048 LIST_APPEND(pktns_frm_list, &frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002049 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
2050 }
2051 }
2052
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002053 end:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002054 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002055 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002056}
2057
2058/* Free <pkt> TX packet and its attached frames.
2059 * This is the responsibility of the caller to remove this packet of
2060 * any data structure it was possibly attached to.
2061 */
2062static inline void free_quic_tx_packet(struct quic_conn *qc,
2063 struct quic_tx_packet *pkt)
2064{
2065 struct quic_frame *frm, *frmbak;
2066
2067 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
2068
2069 if (!pkt)
2070 goto leave;
2071
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002072 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
2073 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002074 pool_free(pool_head_quic_tx_packet, pkt);
2075
2076 leave:
2077 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
2078}
2079
2080/* Free the TX packets of <pkts> list */
Frédéric Lécaille39bde282023-08-29 14:48:54 +02002081static inline __maybe_unused void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002082{
2083 struct quic_tx_packet *pkt, *tmp;
2084
2085 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
2086
2087 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2088 LIST_DELETE(&pkt->list);
2089 eb64_delete(&pkt->pn_node);
2090 free_quic_tx_packet(qc, pkt);
2091 }
2092
2093 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
2094}
2095
2096/* Remove already sent ranges of acknowledged packet numbers from
2097 * <pktns> packet number space tree below <largest_acked_pn> possibly
2098 * updating the range which contains <largest_acked_pn>.
2099 * Never fails.
2100 */
2101static void qc_treat_ack_of_ack(struct quic_conn *qc,
2102 struct quic_pktns *pktns,
2103 int64_t largest_acked_pn)
2104{
2105 struct eb64_node *ar, *next_ar;
2106 struct quic_arngs *arngs = &pktns->rx.arngs;
2107
2108 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2109
2110 ar = eb64_first(&arngs->root);
2111 while (ar) {
2112 struct quic_arng_node *ar_node;
2113
2114 next_ar = eb64_next(ar);
2115 ar_node = eb64_entry(ar, struct quic_arng_node, first);
2116
2117 if ((int64_t)ar_node->first.key > largest_acked_pn) {
2118 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
2119 break;
2120 }
2121
2122 if (largest_acked_pn < ar_node->last) {
2123 eb64_delete(ar);
2124 ar_node->first.key = largest_acked_pn + 1;
2125 eb64_insert(&arngs->root, ar);
2126 break;
2127 }
2128
Frédéric Lécaille0dd4fa52023-05-02 08:57:37 +02002129 /* Do not empty the tree: the first ACK range contains the
2130 * largest acknowledged packet number.
2131 */
2132 if (arngs->sz == 1)
2133 break;
2134
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002135 eb64_delete(ar);
2136 pool_free(pool_head_quic_arng, ar_node);
2137 arngs->sz--;
2138 ar = next_ar;
2139 }
2140
2141 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2142}
2143
2144/* Send a packet ack event nofication for each newly acked packet of
2145 * <newly_acked_pkts> list and free them.
2146 * Always succeeds.
2147 */
2148static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
2149 struct list *newly_acked_pkts)
2150{
2151 struct quic_tx_packet *pkt, *tmp;
2152 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
2153
2154 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2155
2156 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
2157 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2158 qc->path->prep_in_flight -= pkt->in_flight_len;
2159 qc->path->in_flight -= pkt->in_flight_len;
2160 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2161 qc->path->ifae_pkts--;
2162 /* If this packet contained an ACK frame, proceed to the
2163 * acknowledging of range of acks from the largest acknowledged
2164 * packet number which was sent in an ACK frame by this packet.
2165 */
2166 if (pkt->largest_acked_pn != -1)
2167 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
2168 ev.ack.acked = pkt->in_flight_len;
2169 ev.ack.time_sent = pkt->time_sent;
2170 quic_cc_event(&qc->path->cc, &ev);
Frédéric Lécaille39bde282023-08-29 14:48:54 +02002171 LIST_DEL_INIT(&pkt->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002172 quic_tx_packet_refdec(pkt);
2173 }
2174
2175 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2176
2177}
2178
2179/* Release all the frames attached to <pktns> packet number space */
Frédéric Lécaille8fbabea2023-10-11 09:28:36 +02002180void qc_release_pktns_frms(struct quic_conn *qc, struct quic_pktns *pktns)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002181{
2182 struct quic_frame *frm, *frmbak;
2183
2184 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2185
Frédéric Lécaille8fbabea2023-10-11 09:28:36 +02002186 if (!pktns)
2187 goto leave;
2188
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002189 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list)
2190 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002191
Frédéric Lécaille8fbabea2023-10-11 09:28:36 +02002192 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002193 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2194}
2195
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002196/* Handle <pkts> list of lost packets detected at <now_us> handling their TX
2197 * frames. Send a packet loss event to the congestion controller if in flight
2198 * packet have been lost. Also frees the packet in <pkts> list.
2199 *
2200 * Returns 1 on success else 0 if loss limit has been exceeded. A
2201 * CONNECTION_CLOSE was prepared to close the connection ASAP.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002202 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002203static inline int qc_release_lost_pkts(struct quic_conn *qc,
2204 struct quic_pktns *pktns,
2205 struct list *pkts,
2206 uint64_t now_us)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002207{
2208 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002209 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002210
2211 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2212
2213 if (LIST_ISEMPTY(pkts))
2214 goto leave;
2215
2216 oldest_lost = newest_lost = NULL;
2217 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2218 struct list tmp = LIST_HEAD_INIT(tmp);
2219
2220 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2221 qc->path->prep_in_flight -= pkt->in_flight_len;
2222 qc->path->in_flight -= pkt->in_flight_len;
2223 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2224 qc->path->ifae_pkts--;
2225 /* Treat the frames of this lost packet. */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002226 if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms))
2227 close = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002228 LIST_DELETE(&pkt->list);
2229 if (!oldest_lost) {
2230 oldest_lost = newest_lost = pkt;
2231 }
2232 else {
2233 if (newest_lost != oldest_lost)
2234 quic_tx_packet_refdec(newest_lost);
2235 newest_lost = pkt;
2236 }
2237 }
2238
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002239 if (!close) {
2240 if (newest_lost) {
2241 /* Sent a congestion event to the controller */
2242 struct quic_cc_event ev = { };
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002243
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002244 ev.type = QUIC_CC_EVT_LOSS;
2245 ev.loss.time_sent = newest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002246
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002247 quic_cc_event(&qc->path->cc, &ev);
2248 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002249
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002250 /* If an RTT have been already sampled, <rtt_min> has been set.
2251 * We must check if we are experiencing a persistent congestion.
2252 * If this is the case, the congestion controller must re-enter
2253 * slow start state.
2254 */
2255 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2256 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002257
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002258 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2259 now_ms, qc->max_ack_delay))
2260 qc->path->cc.algo->slow_start(&qc->path->cc);
2261 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002262 }
2263
Amaury Denoyelle3a72ba22022-11-14 11:41:34 +01002264 /* <oldest_lost> cannot be NULL at this stage because we have ensured
2265 * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a
2266 * possible overflow on a 0 byte region with O2 optimization.
2267 */
2268 ALREADY_CHECKED(oldest_lost);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002269 quic_tx_packet_refdec(oldest_lost);
2270 if (newest_lost != oldest_lost)
2271 quic_tx_packet_refdec(newest_lost);
2272
2273 leave:
2274 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002275 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002276}
2277
2278/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2279 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
2280 * if the largest acked packet was newly acked and if there was at least one newly
2281 * acked ack-eliciting packet.
2282 * Return 1, if succeeded, 0 if not.
2283 */
2284static inline int qc_parse_ack_frm(struct quic_conn *qc,
2285 struct quic_frame *frm,
2286 struct quic_enc_level *qel,
2287 unsigned int *rtt_sample,
2288 const unsigned char **pos, const unsigned char *end)
2289{
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002290 struct qf_ack *ack_frm = &frm->ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002291 uint64_t smallest, largest;
2292 struct eb_root *pkts;
2293 struct eb64_node *largest_node;
2294 unsigned int time_sent, pkt_flags;
2295 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2296 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
Frédéric Lécaillef078d782023-08-29 11:01:19 +02002297 int ret = 0, new_largest_acked_pn = 0;
Frédéric Lécaille39bde282023-08-29 14:48:54 +02002298 struct quic_tx_packet *pkt, *tmp;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002299
2300 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2301
Frédéric Lécaille39bde282023-08-29 14:48:54 +02002302 pkts = &qel->pktns->tx.pkts;
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002303 if (ack_frm->largest_ack > qel->pktns->tx.next_pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002304 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002305 qc, NULL, &ack_frm->largest_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002306 goto err;
2307 }
2308
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002309 if (ack_frm->first_ack_range > ack_frm->largest_ack) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002310 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002311 qc, NULL, &ack_frm->first_ack_range);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002312 goto err;
2313 }
2314
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002315 largest = ack_frm->largest_ack;
2316 smallest = largest - ack_frm->first_ack_range;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002317 pkt_flags = 0;
2318 largest_node = NULL;
2319 time_sent = 0;
2320
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002321 if ((int64_t)ack_frm->largest_ack > qel->pktns->rx.largest_acked_pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002322 largest_node = eb64_lookup(pkts, largest);
2323 if (!largest_node) {
2324 TRACE_DEVEL("Largest acked packet not found",
2325 QUIC_EV_CONN_PRSAFRM, qc);
2326 }
2327 else {
2328 time_sent = eb64_entry(largest_node,
2329 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillef078d782023-08-29 11:01:19 +02002330 new_largest_acked_pn = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002331 }
2332 }
2333
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002334 TRACE_PROTO("RX ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002335 qc, NULL, &largest, &smallest);
2336 do {
2337 uint64_t gap, ack_range;
2338
Frédéric Lécaille39bde282023-08-29 14:48:54 +02002339 qc_newly_acked_pkts(qc, pkts, &newly_acked_pkts,
2340 largest_node, largest, smallest);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002341 if (!ack_frm->ack_range_num--)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002342 break;
2343
2344 if (!quic_dec_int(&gap, pos, end)) {
2345 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
2346 goto err;
2347 }
2348
2349 if (smallest < gap + 2) {
2350 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
2351 qc, NULL, &gap, &smallest);
2352 goto err;
2353 }
2354
2355 largest = smallest - gap - 2;
2356 if (!quic_dec_int(&ack_range, pos, end)) {
2357 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
2358 goto err;
2359 }
2360
2361 if (largest < ack_range) {
2362 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
2363 qc, NULL, &largest, &ack_range);
2364 goto err;
2365 }
2366
2367 /* Do not use this node anymore. */
2368 largest_node = NULL;
2369 /* Next range */
2370 smallest = largest - ack_range;
2371
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002372 TRACE_PROTO("RX next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002373 qc, NULL, &largest, &smallest);
2374 } while (1);
2375
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002376 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
Frédéric Lécaille39bde282023-08-29 14:48:54 +02002377 qc_ackrng_pkts(qc, &pkt_flags, &newly_acked_pkts);
2378 if (new_largest_acked_pn && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2379 *rtt_sample = tick_remain(time_sent, now_ms);
2380 qel->pktns->rx.largest_acked_pn = ack_frm->largest_ack;
2381 }
2382
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002383 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
2384 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002385 if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms))
2386 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002387 }
2388 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2389 if (quic_peer_validated_addr(qc))
2390 qc->path->loss.pto_count = 0;
2391 qc_set_timer(qc);
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01002392 qc_notify_send(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002393 }
2394
2395 ret = 1;
2396 leave:
2397 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2398 return ret;
2399
2400 err:
Frédéric Lécaille39bde282023-08-29 14:48:54 +02002401 /* Move back these packets into their tree. */
2402 list_for_each_entry_safe(pkt, tmp, &newly_acked_pkts, list) {
2403 LIST_DEL_INIT(&pkt->list);
2404 eb64_insert(pkts, &pkt->pn_node);
2405 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002406 goto leave;
2407}
2408
2409/* This function gives the detail of the SSL error. It is used only
2410 * if the debug mode and the verbose mode are activated. It dump all
2411 * the SSL error until the stack was empty.
2412 */
2413static forceinline void qc_ssl_dump_errors(struct connection *conn)
2414{
2415 if (unlikely(global.mode & MODE_DEBUG)) {
2416 while (1) {
2417 const char *func = NULL;
2418 unsigned long ret;
2419
2420 ERR_peek_error_func(&func);
2421 ret = ERR_get_error();
2422 if (!ret)
2423 return;
2424
2425 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
2426 func, ERR_reason_error_string(ret));
2427 }
2428 }
2429}
2430
2431int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2432 const char **str, int *len);
2433
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002434/* Finalize <qc> QUIC connection:
2435 * - initialize the Initial QUIC TLS context for negotiated version,
2436 * - derive the secrets for this context,
2437 * - set them into the TLS stack,
2438 *
2439 * MUST be called after having received the remote transport parameters which
2440 * are parsed when the TLS callback for the ClientHello message is called upon
2441 * SSL_do_handshake() calls, not necessarily at the first time as this TLS
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002442 * message may be split between packets
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002443 * Return 1 if succeeded, 0 if not.
2444 */
2445static int qc_conn_finalize(struct quic_conn *qc, int server)
2446{
2447 int ret = 0;
2448
2449 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
2450
2451 if (qc->flags & QUIC_FL_CONN_FINALIZED)
2452 goto finalized;
2453
2454 if (qc->negotiated_version &&
2455 !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
2456 qc->odcid.data, qc->odcid.len, server))
2457 goto out;
2458
2459 /* This connection is functional (ready to send/receive) */
2460 qc->flags |= QUIC_FL_CONN_FINALIZED;
2461
2462 finalized:
2463 ret = 1;
2464 out:
2465 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
2466 return ret;
2467}
2468
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002469/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2470 * from <qel> encryption level with <ctx> as QUIC connection context.
2471 * Remaining parameter are there for debugging purposes.
2472 * Return 1 if succeeded, 0 if not.
2473 */
2474static inline int qc_provide_cdata(struct quic_enc_level *el,
2475 struct ssl_sock_ctx *ctx,
2476 const unsigned char *data, size_t len,
2477 struct quic_rx_packet *pkt,
2478 struct quic_rx_crypto_frm *cf)
2479{
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002480#ifdef DEBUG_STRICT
2481 enum ncb_ret ncb_ret;
2482#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002483 int ssl_err, state;
2484 struct quic_conn *qc;
2485 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002486 struct ncbuf *ncbuf = &el->cstream->rx.ncbuf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002487
2488 ssl_err = SSL_ERROR_NONE;
2489 qc = ctx->qc;
2490
2491 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2492
2493 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2494 TRACE_ERROR("SSL_provide_quic_data() error",
2495 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
2496 goto leave;
2497 }
2498
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002499 TRACE_PROTO("in order CRYPTO data",
2500 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
2501
2502 state = qc->state;
2503 if (state < QUIC_HS_ST_COMPLETE) {
2504 ssl_err = SSL_do_handshake(ctx->ssl);
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002505
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01002506 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
2507 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
2508 goto leave;
2509 }
2510
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002511 /* Finalize the connection as soon as possible if the peer transport parameters
2512 * have been received. This may be useful to send packets even if this
2513 * handshake fails.
2514 */
2515 if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) {
2516 TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state);
2517 goto leave;
2518 }
2519
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002520 if (ssl_err != 1) {
2521 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2522 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2523 TRACE_PROTO("SSL handshake in progress",
2524 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2525 goto out;
2526 }
2527
2528 /* TODO: Should close the connection asap */
2529 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2530 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2531 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2532 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2533 }
2534 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2535 qc_ssl_dump_errors(ctx->conn);
2536 ERR_clear_error();
2537 goto leave;
2538 }
Frederic Lecaille314a4c02024-05-22 10:22:09 +02002539#if defined(LIBRESSL_VERSION_NUMBER)
2540 else if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
2541 /* Some libressl versions emit TLS alerts without making the handshake
2542 * (SSL_do_handshake()) fail. This is at least the case for
2543 * libressl-3.9.0 when forcing the TLS cipher to TLS_AES_128_CCM_SHA256.
2544 */
2545 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2546 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2547 goto leave;
2548 }
2549#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002550
2551 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
2552
2553 /* Check the alpn could be negotiated */
2554 if (!qc->app_ops) {
2555 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2556 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2557 goto leave;
2558 }
2559
2560 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2561 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
2562 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2563 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2564 }
2565 /* I/O callback switch */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02002566 qc->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002567 if (qc_is_listener(ctx->qc)) {
Amaury Denoyelle1304d192023-04-11 16:46:03 +02002568 qc->flags |= QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002569 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyelleb75338e2024-03-04 18:41:39 +01002570
2571 if (!(qc->flags & QUIC_FL_CONN_ACCEPT_REGISTERED)) {
2572 quic_accept_push_qc(qc);
2573 }
2574 else {
2575 /* Connection already accepted if 0-RTT used.
2576 * In this case, schedule quic-conn to ensure
2577 * post-handshake frames are emitted.
2578 */
2579 tasklet_wakeup(qc->wait_event.tasklet);
2580 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002581 }
2582 else {
2583 qc->state = QUIC_HS_ST_COMPLETE;
2584 }
2585
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02002586 /* Prepare the next key update */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002587 if (!quic_tls_key_update(qc)) {
2588 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc);
2589 goto leave;
2590 }
2591 } else {
2592 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2593 if (ssl_err != 1) {
2594 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2595 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2596 TRACE_PROTO("SSL post handshake in progress",
2597 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2598 goto out;
2599 }
2600
2601 TRACE_ERROR("SSL post handshake error",
2602 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2603 goto leave;
2604 }
2605
2606 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
2607 }
2608
2609 out:
2610 ret = 1;
2611 leave:
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002612 /* The CRYPTO data are consumed even in case of an error to release
2613 * the memory asap.
2614 */
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002615 if (!ncb_is_null(ncbuf)) {
2616#ifdef DEBUG_STRICT
2617 ncb_ret = ncb_advance(ncbuf, len);
2618 /* ncb_advance() must always succeed. This is guaranteed as
2619 * this is only done inside a data block. If false, this will
2620 * lead to handshake failure with quic_enc_level offset shifted
2621 * from buffer data.
2622 */
2623 BUG_ON(ncb_ret != NCB_RET_OK);
2624#else
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002625 ncb_advance(ncbuf, len);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002626#endif
2627 }
2628
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002629 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
2630 return ret;
2631}
2632
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002633/* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc>
2634 * connection. <fin> is true if FIN bit is set on frame type.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002635 *
2636 * Return 1 on success. On error, 0 is returned. In this case, the packet
2637 * containing the frame must not be acknowledged.
2638 */
2639static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02002640 struct qf_stream *strm_frm,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002641 struct quic_conn *qc, char fin)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002642{
2643 int ret;
2644
2645 /* RFC9000 13.1. Packet Processing
2646 *
2647 * A packet MUST NOT be acknowledged until packet protection has been
2648 * successfully removed and all frames contained in the packet have
2649 * been processed. For STREAM frames, this means the data has been
2650 * enqueued in preparation to be received by the application protocol,
2651 * but it does not require that data be delivered and consumed.
2652 */
2653 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2654
2655 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002656 strm_frm->offset.key, fin, (char *)strm_frm->data);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002657
2658 /* frame rejected - packet must not be acknowledeged */
2659 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2660 return !ret;
2661}
2662
2663/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2664 * for <qc> QUIC connection.
2665 * This is a best effort function which never fails even if no memory could be
2666 * allocated to duplicate these frames.
2667 */
2668static void qc_dup_pkt_frms(struct quic_conn *qc,
2669 struct list *pkt_frm_list, struct list *out_frm_list)
2670{
2671 struct quic_frame *frm, *frmbak;
2672 struct list tmp = LIST_HEAD_INIT(tmp);
2673
2674 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2675
2676 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2677 struct quic_frame *dup_frm, *origin;
2678
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002679 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
2680 TRACE_DEVEL("already acknowledged frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2681 continue;
2682 }
2683
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002684 switch (frm->type) {
2685 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2686 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02002687 struct qf_stream *strm_frm = &frm->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002688 struct eb64_node *node = NULL;
2689 struct qc_stream_desc *stream_desc;
2690
2691 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2692 if (!node) {
2693 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
2694 continue;
2695 }
2696
2697 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2698 /* Do not resend this frame if in the "already acked range" */
2699 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2700 TRACE_DEVEL("ignored frame in already acked range",
2701 QUIC_EV_CONN_PRSAFRM, qc, frm);
2702 continue;
2703 }
2704 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01002705 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
2706
Frédéric Lécaillec425e032023-03-20 14:32:59 +01002707 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002708 TRACE_DEVEL("updated partially acked frame",
2709 QUIC_EV_CONN_PRSAFRM, qc, frm);
2710 }
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01002711
2712 strm_frm->dup = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002713 break;
2714 }
2715
2716 default:
2717 break;
2718 }
2719
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002720 /* If <frm> is already a copy of another frame, we must take
2721 * its original frame as source for the copy.
2722 */
2723 origin = frm->origin ? frm->origin : frm;
2724 dup_frm = qc_frm_dup(origin);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002725 if (!dup_frm) {
2726 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2727 break;
2728 }
2729
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002730 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002731 if (origin->pkt) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002732 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaillefe97c6b2023-11-21 20:31:32 +01002733 qc, dup_frm, &origin->pkt->pn_node.key);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002734 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002735 else {
2736 /* <origin> is a frame which was sent from a packet detected as lost. */
2737 TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
2738 }
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002739
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002740 LIST_APPEND(&tmp, &dup_frm->list);
2741 }
2742
2743 LIST_SPLICE(out_frm_list, &tmp);
2744
2745 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2746}
2747
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002748/* Boolean function which return 1 if <pkt> TX packet is only made of
2749 * already acknowledged frame.
2750 */
2751static inline int qc_pkt_with_only_acked_frms(struct quic_tx_packet *pkt)
2752{
2753 struct quic_frame *frm;
2754
2755 list_for_each_entry(frm, &pkt->frms, list)
2756 if (!(frm->flags & QUIC_FL_TX_FRAME_ACKED))
2757 return 0;
2758
2759 return 1;
2760}
2761
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002762/* Prepare a fast retransmission from <qel> encryption level */
2763static void qc_prep_fast_retrans(struct quic_conn *qc,
2764 struct quic_enc_level *qel,
2765 struct list *frms1, struct list *frms2)
2766{
2767 struct eb_root *pkts = &qel->pktns->tx.pkts;
2768 struct list *frms = frms1;
2769 struct eb64_node *node;
2770 struct quic_tx_packet *pkt;
2771
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002772 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002773
2774 BUG_ON(frms1 == frms2);
2775
2776 pkt = NULL;
2777 node = eb64_first(pkts);
2778 start:
2779 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002780 struct quic_tx_packet *p;
2781
2782 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002783 node = eb64_next(node);
2784 /* Skip the empty and coalesced packets */
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002785 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002786 "--> pn=%llu (%d %d %d)", (ull)p->pn_node.key,
2787 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED),
2788 qc_pkt_with_only_acked_frms(p));
2789 if (!LIST_ISEMPTY(&p->frms) && !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002790 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002791 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002792 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002793 }
2794
2795 if (!pkt)
2796 goto leave;
2797
2798 /* When building a packet from another one, the field which may increase the
2799 * packet size is the packet number. And the maximum increase is 4 bytes.
2800 */
2801 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2802 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002803 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002804 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2805 goto leave;
2806 }
2807
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002808 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002809 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2810 if (frms == frms1 && frms2) {
2811 frms = frms2;
2812 goto start;
2813 }
2814 leave:
2815 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
2816}
2817
2818/* Prepare a fast retransmission during a handshake after a client
2819 * has resent Initial packets. According to the RFC a server may retransmit
2820 * Initial packets send them coalescing with others (Handshake here).
2821 * (Listener only function).
2822 */
2823static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2824 struct list *ifrms, struct list *hfrms)
2825{
2826 struct list itmp = LIST_HEAD_INIT(itmp);
2827 struct list htmp = LIST_HEAD_INIT(htmp);
2828
2829 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2830 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2831 struct quic_enc_level *qel = iqel;
2832 struct eb_root *pkts;
2833 struct eb64_node *node;
2834 struct quic_tx_packet *pkt;
2835 struct list *tmp = &itmp;
2836
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002837 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002838 start:
2839 pkt = NULL;
2840 pkts = &qel->pktns->tx.pkts;
2841 node = eb64_first(pkts);
2842 /* Skip the empty packet (they have already been retransmitted) */
2843 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002844 struct quic_tx_packet *p;
2845
2846 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002847 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002848 "--> pn=%llu (%d %d)", (ull)p->pn_node.key,
2849 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED));
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002850 if (!LIST_ISEMPTY(&p->frms) && !(p->flags & QUIC_FL_TX_PACKET_COALESCED) &&
2851 !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002852 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002853 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002854 }
2855
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002856 node = eb64_next(node);
2857 }
2858
2859 if (!pkt)
2860 goto end;
2861
2862 /* When building a packet from another one, the field which may increase the
2863 * packet size is the packet number. And the maximum increase is 4 bytes.
2864 */
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002865 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2866 size_t dglen = pkt->len + 4;
2867
2868 dglen += pkt->next ? pkt->next->len + 4 : 0;
2869 if (dglen > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002870 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002871 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2872 if (pkt->next)
2873 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt->next);
2874 goto end;
2875 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002876 }
2877
2878 qel->pktns->tx.pto_probe += 1;
2879
2880 /* No risk to loop here, #packet per datagram is bounded */
2881 requeue:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002882 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002883 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
2884 if (qel == iqel) {
2885 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2886 pkt = pkt->next;
2887 tmp = &htmp;
2888 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002889 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002890 goto requeue;
2891 }
2892 }
2893
2894 end:
2895 LIST_SPLICE(ifrms, &itmp);
2896 LIST_SPLICE(hfrms, &htmp);
2897
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002898 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002899}
2900
2901static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
2902{
2903 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2904
2905 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2906 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2907 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2908 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
2909 goto out;
2910
2911 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2912 }
2913
2914 out:
2915 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
2916}
2917
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002918/* Cancel a request on connection <qc> for stream id <id>. This is useful when
2919 * the client opens a new stream but the MUX has already been released. A
Amaury Denoyelle75463012023-02-20 10:31:27 +01002920 * STOP_SENDING + RESET_STREAM frames are prepared for emission.
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002921 *
2922 * TODO this function is closely related to H3. Its place should be in H3 layer
2923 * instead of quic-conn but this requires an architecture adjustment.
2924 *
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002925 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002926 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002927static int qc_h3_request_reject(struct quic_conn *qc, uint64_t id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002928{
2929 int ret = 0;
Amaury Denoyelle75463012023-02-20 10:31:27 +01002930 struct quic_frame *ss, *rs;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002931 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002932 const uint64_t app_error_code = H3_REQUEST_REJECTED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002933
2934 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2935
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002936 /* Do not emit rejection for unknown unidirectional stream as it is
2937 * forbidden to close some of them (H3 control stream and QPACK
2938 * encoder/decoder streams).
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002939 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002940 if (quic_stream_is_uni(id)) {
2941 ret = 1;
2942 goto out;
2943 }
2944
Amaury Denoyelle75463012023-02-20 10:31:27 +01002945 ss = qc_frm_alloc(QUIC_FT_STOP_SENDING);
2946 if (!ss) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002947 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2948 goto out;
2949 }
2950
Amaury Denoyelle75463012023-02-20 10:31:27 +01002951 ss->stop_sending.id = id;
2952 ss->stop_sending.app_error_code = app_error_code;
2953
2954 rs = qc_frm_alloc(QUIC_FT_RESET_STREAM);
2955 if (!rs) {
2956 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2957 qc_frm_free(&ss);
2958 goto out;
2959 }
2960
2961 rs->reset_stream.id = id;
2962 rs->reset_stream.app_error_code = app_error_code;
2963 rs->reset_stream.final_size = 0;
2964
2965 LIST_APPEND(&qel->pktns->tx.frms, &ss->list);
2966 LIST_APPEND(&qel->pktns->tx.frms, &rs->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002967 ret = 1;
2968 out:
2969 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2970 return ret;
2971}
2972
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002973/* Release the underlying memory use by <ncbuf> non-contiguous buffer */
2974static void quic_free_ncbuf(struct ncbuf *ncbuf)
2975{
2976 struct buffer buf;
2977
2978 if (ncb_is_null(ncbuf))
2979 return;
2980
2981 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
2982 b_free(&buf);
2983 offer_buffers(NULL, 1);
2984
2985 *ncbuf = NCBUF_NULL;
2986}
2987
2988/* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */
2989static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf)
2990{
2991 struct buffer buf = BUF_NULL;
2992
2993 if (!ncb_is_null(ncbuf))
2994 return ncbuf;
2995
2996 b_alloc(&buf);
2997 BUG_ON(b_is_null(&buf));
2998
2999 *ncbuf = ncb_make(buf.area, buf.size, 0);
3000 ncb_init(ncbuf, 0);
3001
3002 return ncbuf;
3003}
3004
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003005/* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn.
3006 * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the
3007 * speed up handshake completion may be run after having received duplicated
3008 * CRYPTO data.
3009 */
3010static int qc_handle_crypto_frm(struct quic_conn *qc,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003011 struct qf_crypto *crypto_frm, struct quic_rx_packet *pkt,
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003012 struct quic_enc_level *qel, int *fast_retrans)
3013{
3014 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02003015 enum ncb_ret ncb_ret;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003016 /* XXX TO DO: <cfdebug> is used only for the traces. */
3017 struct quic_rx_crypto_frm cfdebug = {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003018 .offset_node.key = crypto_frm->offset,
3019 .len = crypto_frm->len,
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003020 };
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02003021 struct quic_cstream *cstream = qel->cstream;
3022 struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003023
3024 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3025 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
3026 TRACE_PROTO("CRYPTO data discarded",
3027 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
3028 goto done;
3029 }
3030
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003031 if (unlikely(crypto_frm->offset < cstream->rx.offset)) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003032 size_t diff;
3033
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003034 if (crypto_frm->offset + crypto_frm->len <= cstream->rx.offset) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003035 /* Nothing to do */
3036 TRACE_PROTO("Already received CRYPTO data",
3037 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
3038 if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
3039 !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP))
3040 *fast_retrans = 1;
3041 goto done;
3042 }
3043
3044 TRACE_PROTO("Partially already received CRYPTO data",
3045 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
3046
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003047 diff = cstream->rx.offset - crypto_frm->offset;
3048 crypto_frm->len -= diff;
3049 crypto_frm->data += diff;
3050 crypto_frm->offset = cstream->rx.offset;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003051 }
3052
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003053 if (crypto_frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) {
3054 if (!qc_provide_cdata(qel, qc->xprt_ctx, crypto_frm->data, crypto_frm->len,
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003055 pkt, &cfdebug)) {
3056 // trace already emitted by function above
3057 goto leave;
3058 }
3059
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003060 cstream->rx.offset += crypto_frm->len;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01003061 TRACE_DEVEL("increment crypto level offset", QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003062 goto done;
3063 }
3064
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02003065 if (!quic_get_ncbuf(ncbuf) ||
3066 ncb_is_null(ncbuf)) {
3067 TRACE_ERROR("CRYPTO ncbuf allocation failed", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003068 goto leave;
3069 }
3070
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003071 /* crypto_frm->offset > cstream-trx.offset */
3072 ncb_ret = ncb_add(ncbuf, crypto_frm->offset - cstream->rx.offset,
3073 (const char *)crypto_frm->data, crypto_frm->len, NCB_ADD_COMPARE);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02003074 if (ncb_ret != NCB_RET_OK) {
3075 if (ncb_ret == NCB_RET_DATA_REJ) {
3076 TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc);
3077 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02003078 qc_notify_err(qc);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02003079 }
3080 else if (ncb_ret == NCB_RET_GAP_SIZE) {
3081 TRACE_ERROR("cannot bufferize frame due to gap size limit",
3082 QUIC_EV_CONN_PRSHPKT, qc);
3083 }
3084 goto leave;
3085 }
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003086
3087 done:
3088 ret = 1;
3089 leave:
3090 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3091 return ret;
3092}
3093
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003094/* Build a NEW_CONNECTION_ID frame for <conn_id> CID of <qc> connection.
3095 *
3096 * Returns 1 on success else 0.
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003097 */
3098static int qc_build_new_connection_id_frm(struct quic_conn *qc,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003099 struct quic_connection_id *conn_id)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003100{
3101 int ret = 0;
3102 struct quic_frame *frm;
3103 struct quic_enc_level *qel;
3104
3105 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3106
3107 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3108 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
3109 if (!frm) {
3110 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3111 goto leave;
3112 }
3113
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003114 quic_connection_id_to_frm_cpy(frm, conn_id);
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003115 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
3116 ret = 1;
3117 leave:
3118 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3119 return ret;
3120}
3121
3122
3123/* Handle RETIRE_CONNECTION_ID frame from <frm> frame.
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003124 * Return 1 if succeeded, 0 if not. If succeeded, also set <to_retire>
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003125 * to the CID to be retired if not already retired.
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003126 */
3127static int qc_handle_retire_connection_id_frm(struct quic_conn *qc,
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003128 struct quic_frame *frm,
3129 struct quic_cid *dcid,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003130 struct quic_connection_id **to_retire)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003131{
3132 int ret = 0;
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003133 struct qf_retire_connection_id *rcid_frm = &frm->retire_connection_id;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003134 struct eb64_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003135 struct quic_connection_id *conn_id;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003136
3137 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3138
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003139 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3140 * Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number greater
3141 * than any previously sent to the peer MUST be treated as a connection error
3142 * of type PROTOCOL_VIOLATION.
3143 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003144 if (rcid_frm->seq_num >= qc->next_cid_seq_num) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003145 TRACE_PROTO("CID seq. number too big", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003146 goto protocol_violation;
3147 }
3148
3149 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3150 * The sequence number specified in a RETIRE_CONNECTION_ID frame MUST NOT refer to
3151 * the Destination Connection ID field of the packet in which the frame is contained.
3152 * The peer MAY treat this as a connection error of type PROTOCOL_VIOLATION.
3153 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003154 node = eb64_lookup(&qc->cids, rcid_frm->seq_num);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003155 if (!node) {
3156 TRACE_PROTO("CID already retired", QUIC_EV_CONN_PSTRM, qc, frm);
3157 goto out;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003158 }
3159
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003160 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003161 /* Note that the length of <dcid> has already been checked. It must match the
3162 * length of the CIDs which have been provided to the peer.
3163 */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003164 if (!memcmp(dcid->data, conn_id->cid.data, QUIC_HAP_CID_LEN)) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003165 TRACE_PROTO("cannot retire the current CID", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003166 goto protocol_violation;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003167 }
3168
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003169 *to_retire = conn_id;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003170 out:
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003171 ret = 1;
3172 leave:
3173 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3174 return ret;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003175 protocol_violation:
3176 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02003177 qc_notify_err(qc);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003178 goto leave;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003179}
3180
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003181/* Remove a <qc> quic-conn from its ha_thread_ctx list. If <closing> is true,
3182 * it will immediately be reinserted in the ha_thread_ctx quic_conns_clo list.
3183 */
3184static void qc_detach_th_ctx_list(struct quic_conn *qc, int closing)
3185{
3186 struct bref *bref, *back;
3187
3188 /* Detach CLI context watchers currently dumping this connection.
3189 * Reattach them to the next quic_conn instance.
3190 */
3191 list_for_each_entry_safe(bref, back, &qc->back_refs, users) {
3192 /* Remove watcher from this quic_conn instance. */
3193 LIST_DEL_INIT(&bref->users);
3194
3195 /* Attach it to next instance unless it was the last list element. */
3196 if (qc->el_th_ctx.n != &th_ctx->quic_conns &&
3197 qc->el_th_ctx.n != &th_ctx->quic_conns_clo) {
3198 struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx,
3199 struct quic_conn *,
3200 el_th_ctx);
3201 LIST_APPEND(&next->back_refs, &bref->users);
3202 }
3203 bref->ref = qc->el_th_ctx.n;
3204 __ha_barrier_store();
3205 }
3206
3207 /* Remove quic_conn from global ha_thread_ctx list. */
3208 LIST_DEL_INIT(&qc->el_th_ctx);
3209
3210 if (closing)
3211 LIST_APPEND(&th_ctx->quic_conns_clo, &qc->el_th_ctx);
3212}
3213
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003214/* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel>
3215 * as encryption level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003216 * Returns 1 if succeeded, 0 if failed.
3217 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003218static int qc_parse_pkt_frms(struct quic_conn *qc, struct quic_rx_packet *pkt,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003219 struct quic_enc_level *qel)
3220{
3221 struct quic_frame frm;
3222 const unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003223 int fast_retrans = 0, ret = 0;
3224
3225 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3226 /* Skip the AAD */
3227 pos = pkt->data + pkt->aad_len;
3228 end = pkt->data + pkt->len;
3229
Amaury Denoyelle3a893482023-10-09 10:42:35 +02003230 /* Packet with no frame. */
3231 if (pos == end) {
3232 /* RFC9000 12.4. Frames and Frame Types
3233 *
3234 * The payload of a packet that contains frames MUST contain at least
3235 * one frame, and MAY contain multiple frames and multiple frame types.
3236 * An endpoint MUST treat receipt of a packet containing no frames as a
3237 * connection error of type PROTOCOL_VIOLATION. Frames always fit within
3238 * a single QUIC packet and cannot span multiple packets.
3239 */
3240 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
3241 goto leave;
3242 }
3243
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003244 while (pos < end) {
3245 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
3246 // trace already emitted by function above
3247 goto leave;
3248 }
3249
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003250 switch (frm.type) {
3251 case QUIC_FT_PADDING:
3252 break;
3253 case QUIC_FT_PING:
3254 break;
3255 case QUIC_FT_ACK:
3256 {
3257 unsigned int rtt_sample;
3258
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003259 rtt_sample = UINT_MAX;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003260 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
3261 // trace already emitted by function above
3262 goto leave;
3263 }
3264
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003265 if (rtt_sample != UINT_MAX) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003266 unsigned int ack_delay;
3267
3268 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
3269 qc->state >= QUIC_HS_ST_CONFIRMED ?
3270 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
3271 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
3272 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
3273 }
3274 break;
3275 }
3276 case QUIC_FT_RESET_STREAM:
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01003277 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003278 struct qf_reset_stream *rs_frm = &frm.reset_stream;
3279 qcc_recv_reset_stream(qc->qcc, rs_frm->id, rs_frm->app_error_code, rs_frm->final_size);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01003280 }
3281 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003282 case QUIC_FT_STOP_SENDING:
3283 {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003284 struct qf_stop_sending *ss_frm = &frm.stop_sending;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003285 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003286 if (qcc_recv_stop_sending(qc->qcc, ss_frm->id,
3287 ss_frm->app_error_code)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003288 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
3289 goto leave;
3290 }
3291 }
3292 break;
3293 }
3294 case QUIC_FT_CRYPTO:
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003295 if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003296 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003297 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003298 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
3299 {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003300 struct qf_stream *strm_frm = &frm.stream;
3301 unsigned nb_streams = qc->rx.strms[qcs_id_type(strm_frm->id)].nb_streams;
Amaury Denoyelle2216b082023-02-02 14:59:36 +01003302 const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003303
3304 /* The upper layer may not be allocated. */
3305 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003306 if ((strm_frm->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003307 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003308 }
3309 else {
3310 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle38836b62023-02-07 14:24:54 +01003311 if (qc->app_ops == &h3_ops) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003312 if (!qc_h3_request_reject(qc, strm_frm->id)) {
Amaury Denoyelle156a89a2023-02-20 10:32:16 +01003313 TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc);
3314 /* This packet will not be acknowledged */
3315 goto leave;
3316 }
3317 }
3318 else {
3319 /* This packet will not be acknowledged */
3320 goto leave;
Frédéric Lécailled18025e2023-01-20 15:33:50 +01003321 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003322 }
Amaury Denoyelle315a4f62023-03-06 09:10:53 +01003323
3324 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003325 }
3326
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003327 if (!qc_handle_strm_frm(pkt, strm_frm, qc, fin)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003328 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
3329 goto leave;
3330 }
3331
3332 break;
3333 }
3334 case QUIC_FT_MAX_DATA:
3335 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003336 struct qf_max_data *md_frm = &frm.max_data;
3337 qcc_recv_max_data(qc->qcc, md_frm->max_data);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003338 }
3339 break;
3340 case QUIC_FT_MAX_STREAM_DATA:
3341 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003342 struct qf_max_stream_data *msd_frm = &frm.max_stream_data;
3343 if (qcc_recv_max_stream_data(qc->qcc, msd_frm->id,
3344 msd_frm->max_stream_data)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003345 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
3346 goto leave;
3347 }
3348 }
3349 break;
3350 case QUIC_FT_MAX_STREAMS_BIDI:
3351 case QUIC_FT_MAX_STREAMS_UNI:
3352 break;
3353 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02003354 qc->cntrs.data_blocked++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003355 break;
3356 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02003357 qc->cntrs.stream_data_blocked++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003358 break;
3359 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Amaury Denoyelle6d6ee0d2023-05-25 10:36:04 +02003360 qc->cntrs.streams_blocked_bidi++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003361 break;
3362 case QUIC_FT_STREAMS_BLOCKED_UNI:
Amaury Denoyelle6d6ee0d2023-05-25 10:36:04 +02003363 qc->cntrs.streams_blocked_uni++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003364 break;
3365 case QUIC_FT_NEW_CONNECTION_ID:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003366 /* XXX TO DO XXX */
3367 break;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003368 case QUIC_FT_RETIRE_CONNECTION_ID:
3369 {
Willy Tarreau7f9656c2024-05-24 11:04:30 +02003370 struct quic_cid_tree *tree __maybe_unused;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003371 struct quic_connection_id *conn_id = NULL;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003372
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003373 if (!qc_handle_retire_connection_id_frm(qc, &frm, &pkt->dcid, &conn_id))
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003374 goto leave;
3375
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003376 if (!conn_id)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003377 break;
3378
Frédéric Lécaille0b25d702023-12-13 11:45:43 +01003379 tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)];
3380 HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003381 ebmb_delete(&conn_id->node);
Frédéric Lécaille0b25d702023-12-13 11:45:43 +01003382 HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003383 eb64_delete(&conn_id->seq_num);
3384 pool_free(pool_head_quic_connection_id, conn_id);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003385 TRACE_PROTO("CID retired", QUIC_EV_CONN_PSTRM, qc);
3386
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003387 conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
3388 if (!conn_id) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003389 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
3390 }
3391 else {
Amaury Denoyellee83f9372023-04-18 11:10:54 +02003392 quic_cid_insert(conn_id);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003393 qc_build_new_connection_id_frm(qc, conn_id);
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003394 }
3395 break;
3396 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003397 case QUIC_FT_CONNECTION_CLOSE:
3398 case QUIC_FT_CONNECTION_CLOSE_APP:
3399 /* Increment the error counters */
3400 qc_cc_err_count_inc(qc, &frm);
3401 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
3402 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
3403 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
3404 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
3405 }
3406 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
3407 /* RFC 9000 10.2. Immediate Close:
3408 * The closing and draining connection states exist to ensure
3409 * that connections close cleanly and that delayed or reordered
3410 * packets are properly discarded. These states SHOULD persist
3411 * for at least three times the current PTO interval...
3412 *
3413 * Rearm the idle timeout only one time when entering draining
3414 * state.
3415 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003416 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003417 qc_detach_th_ctx_list(qc, 1);
Frédéric Lécailled7215712023-03-24 18:13:37 +01003418 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02003419 qc_notify_err(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003420 }
3421 break;
3422 case QUIC_FT_HANDSHAKE_DONE:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003423 if (qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003424 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
3425 QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle2e4d3e42024-02-14 18:13:08 +01003426
3427 /* RFC 9000 19.20. HANDSHAKE_DONE Frames
3428 *
3429 * A
3430 * server MUST treat receipt of a HANDSHAKE_DONE frame as a connection
3431 * error of type PROTOCOL_VIOLATION.
3432 */
3433 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003434 goto leave;
3435 }
3436
3437 qc->state = QUIC_HS_ST_CONFIRMED;
3438 break;
3439 default:
3440 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
3441 goto leave;
3442 }
3443 }
3444
3445 /* Flag this packet number space as having received a packet. */
3446 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
3447
3448 if (fast_retrans) {
3449 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3450 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3451
3452 TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc);
3453 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
3454 qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP;
3455 }
3456
3457 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
3458 * has successfully parse a Handshake packet. The Initial encryption must also
3459 * be discarded.
3460 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003461 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003462 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
3463 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
3464 QUIC_FL_TLS_SECRETS_DCD)) {
3465 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3466 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
3467 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003468 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003469 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3470 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3471 }
3472 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
3473 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
3474 }
3475 }
3476
3477 ret = 1;
3478 leave:
3479 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3480 return ret;
3481}
3482
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003483
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003484/* Allocate Tx buffer from <qc> quic-conn if needed.
3485 *
3486 * Returns allocated buffer or NULL on error.
3487 */
3488static struct buffer *qc_txb_alloc(struct quic_conn *qc)
3489{
3490 struct buffer *buf = &qc->tx.buf;
3491 if (!b_alloc(buf))
3492 return NULL;
3493
3494 return buf;
3495}
3496
3497/* Free Tx buffer from <qc> if it is empty. */
3498static void qc_txb_release(struct quic_conn *qc)
3499{
3500 struct buffer *buf = &qc->tx.buf;
3501
3502 /* For the moment sending function is responsible to purge the buffer
3503 * entirely. It may change in the future but this requires to be able
3504 * to reuse old data.
Frédéric Lécaillebbf86be2023-02-20 09:28:58 +01003505 * For the momemt we do not care to leave data in the buffer for
3506 * a connection which is supposed to be killed asap.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003507 */
3508 BUG_ON_HOT(buf && b_data(buf));
3509
3510 if (!b_data(buf)) {
3511 b_free(buf);
3512 offer_buffers(NULL, 1);
3513 }
3514}
3515
3516/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
3517 * must contains the address of the first packet stored in the payload.
3518 *
3519 * Caller is responsible that there is enough space in the buffer.
3520 */
3521static void qc_txb_store(struct buffer *buf, uint16_t length,
3522 struct quic_tx_packet *first_pkt)
3523{
3524 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
3525 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
3526
3527 write_u16(b_tail(buf), length);
3528 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
3529 b_add(buf, hdlen + length);
3530}
3531
3532/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
3533 * with <frms> as ack-eliciting frame list to send, 0 if not.
3534 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
3535 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003536 * Also set <*must_ack> to inform the caller if an acknowledgement should be sent.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003537 */
3538static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003539 struct quic_enc_level *qel, int cc, int probe,
3540 int *must_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003541{
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003542 int force_ack =
3543 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3544 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3545 int nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
3546
3547 /* An acknowledgement must be sent if this has been forced by the caller,
3548 * typically during the handshake when the packets must be acknowledged as
3549 * soon as possible. This is also the case when the ack delay timer has been
3550 * triggered, or at least every QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK packets.
3551 */
3552 *must_ack = (qc->flags & QUIC_FL_CONN_ACK_TIMER_FIRED) ||
3553 ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3554 (force_ack || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003555
3556 /* Do not build any more packet if the TX secrets are not available or
3557 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
3558 * and if there is no more packets to send upon PTO expiration
3559 * and if there is no more ack-eliciting frames to send or in flight
3560 * congestion control limit is reached for prepared data
3561 */
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02003562 if (!quic_tls_has_tx_sec(qel) ||
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003563 (!cc && !probe && !*must_ack &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003564 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
3565 return 0;
3566 }
3567
3568 return 1;
3569}
3570
3571/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3572 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
3573 *
3574 * Each datagram is prepended by a two fields header : the datagram length and
3575 * the address of the packet contained in the datagram.
3576 *
3577 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3578 * -1 if something wrong happened.
3579 */
3580static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
3581 struct list *frms)
3582{
3583 int ret = -1;
3584 struct quic_enc_level *qel;
3585 unsigned char *end, *pos;
3586 struct quic_tx_packet *pkt;
3587 size_t total;
3588 /* Each datagram is prepended with its length followed by the address
3589 * of the first packet in the datagram.
3590 */
3591 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
3592
3593 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3594
3595 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3596 total = 0;
3597 pos = (unsigned char *)b_tail(buf);
3598 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003599 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003600
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02003601 TRACE_PROTO("TX prep app pkts", QUIC_EV_CONN_PHPKTS, qc, qel, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003602 probe = 0;
3603 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3604 /* We do not probe if an immediate close was asked */
3605 if (!cc)
3606 probe = qel->pktns->tx.pto_probe;
3607
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003608 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003609 break;
3610
3611 /* Leave room for the datagram header */
3612 pos += dg_headlen;
3613 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3614 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3615 }
3616 else {
3617 end = pos + qc->path->mtu;
3618 }
3619
3620 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003621 QUIC_PACKET_TYPE_SHORT, must_ack, 0, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003622 switch (err) {
Frédéric Lécaille21017002023-11-08 11:31:21 +01003623 case -3:
3624 qc_purge_txbuf(qc, buf);
3625 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003626 case -2:
3627 // trace already emitted by function above
3628 goto leave;
3629 case -1:
3630 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
3631 * MTU, we are here because of the congestion control window. There is
3632 * no need to try to reuse this buffer.
3633 */
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 && !pkt) */
3641 BUG_ON(!pkt);
3642
3643 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3644 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3645
3646 total += pkt->len;
3647
3648 /* Write datagram header. */
3649 qc_txb_store(buf, pkt->len, pkt);
3650 }
3651
3652 out:
3653 ret = total;
3654 leave:
3655 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3656 return ret;
3657}
3658
3659/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3660 * <frms>. Several packets can be regrouped in a single datagram. The result is
3661 * written into <buf>.
3662 *
3663 * Each datagram is prepended by a two fields header : the datagram length and
3664 * the address of first packet in the datagram.
3665 *
3666 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3667 * -1 if something wrong happened.
3668 */
3669static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
3670 enum quic_tls_enc_level tel, struct list *tel_frms,
3671 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3672{
3673 struct quic_enc_level *qel;
3674 unsigned char *end, *pos;
3675 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3676 /* length of datagrams */
3677 uint16_t dglen;
3678 size_t total;
3679 int ret = -1, padding;
3680 /* Each datagram is prepended with its length followed by the address
3681 * of the first packet in the datagram.
3682 */
3683 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
3684 struct list *frms;
3685
3686 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3687
3688 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05003689 * caller must ensure that buf is reset.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003690 */
3691 BUG_ON_HOT(buf->head || buf->data);
3692
3693 total = 0;
3694 qel = &qc->els[tel];
3695 frms = tel_frms;
3696 dglen = 0;
3697 padding = 0;
3698 pos = (unsigned char *)b_head(buf);
3699 first_pkt = prv_pkt = NULL;
3700 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003701 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003702 enum quic_pkt_type pkt_type;
3703 struct quic_tls_ctx *tls_ctx;
3704 const struct quic_version *ver;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003705
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003706 TRACE_PROTO("TX prep pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003707 probe = 0;
3708 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3709 /* We do not probe if an immediate close was asked */
3710 if (!cc)
3711 probe = qel->pktns->tx.pto_probe;
3712
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003713 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003714 if (prv_pkt)
3715 qc_txb_store(buf, dglen, first_pkt);
3716 /* Let's select the next encryption level */
3717 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3718 tel = next_tel;
3719 frms = next_tel_frms;
3720 qel = &qc->els[tel];
3721 /* Build a new datagram */
3722 prv_pkt = NULL;
3723 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
3724 continue;
3725 }
3726 break;
3727 }
3728
3729 pkt_type = quic_tls_level_pkt_type(tel);
3730 if (!prv_pkt) {
3731 /* Leave room for the datagram header */
3732 pos += dg_headlen;
3733 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3734 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3735 }
3736 else {
3737 end = pos + qc->path->mtu;
3738 }
3739 }
3740
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003741 /* RFC 9000 14.1 Initial datagram size
3742 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3743 * Initial packets to at least the smallest allowed maximum datagram size of
3744 * 1200 bytes.
3745 *
3746 * Ensure that no ack-eliciting packets are sent into too small datagrams
3747 */
3748 if (pkt_type == QUIC_PACKET_TYPE_INITIAL && !LIST_ISEMPTY(tel_frms)) {
3749 if (end - pos < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01003750 TRACE_PROTO("No more enough room to build an Initial packet",
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003751 QUIC_EV_CONN_PHPKTS, qc);
3752 goto out;
3753 }
3754
3755 /* Pad this Initial packet if there is no ack-eliciting frames to send from
3756 * the next packet number space.
3757 */
Frédéric Lécailleec937212023-03-03 17:34:41 +01003758 if (!next_tel_frms || LIST_ISEMPTY(next_tel_frms))
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003759 padding = 1;
3760 }
3761
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003762 if (qc->negotiated_version) {
3763 ver = qc->negotiated_version;
3764 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3765 tls_ctx = &qc->negotiated_ictx;
3766 else
3767 tls_ctx = &qel->tls_ctx;
3768 }
3769 else {
3770 ver = qc->original_version;
3771 tls_ctx = &qel->tls_ctx;
3772 }
3773
3774 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
3775 qc, ver, dglen, pkt_type,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003776 must_ack, padding, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003777 switch (err) {
Frédéric Lécaille21017002023-11-08 11:31:21 +01003778 case -3:
3779 qc_purge_tx_buf(buf);
3780 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003781 case -2:
3782 // trace already emitted by function above
3783 goto leave;
3784 case -1:
3785 /* If there was already a correct packet present, set the
3786 * current datagram as prepared into <cbuf>.
3787 */
3788 if (prv_pkt)
3789 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003790 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003791 goto out;
3792 default:
3793 break;
3794 }
3795
3796 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
3797 BUG_ON(!cur_pkt);
3798
3799 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3800 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3801
3802 total += cur_pkt->len;
3803 /* keep trace of the first packet in the datagram */
3804 if (!first_pkt)
3805 first_pkt = cur_pkt;
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01003806 /* Attach the current one to the previous one and vice versa */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003807 if (prv_pkt) {
3808 prv_pkt->next = cur_pkt;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01003809 cur_pkt->prev = prv_pkt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003810 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3811 }
3812 /* Let's say we have to build a new dgram */
3813 prv_pkt = NULL;
3814 dglen += cur_pkt->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003815 /* If the data for the current encryption level have all been sent,
3816 * select the next level.
3817 */
3818 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillea576c1b2023-04-13 15:59:02 +02003819 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms))) {
Frédéric Lécailleb5461932023-06-14 08:54:51 +02003820 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3821 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003822 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3823 tel = next_tel;
3824 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3825 frms = &qc->els[tel].pktns->tx.frms;
3826 else
3827 frms = next_tel_frms;
3828 qel = &qc->els[tel];
3829 if (!LIST_ISEMPTY(frms)) {
3830 /* If there is data for the next level, do not
3831 * consume a datagram.
3832 */
3833 prv_pkt = cur_pkt;
3834 }
3835 }
3836
3837 /* If we have to build a new datagram, set the current datagram as
3838 * prepared into <cbuf>.
3839 */
3840 if (!prv_pkt) {
3841 qc_txb_store(buf, dglen, first_pkt);
3842 first_pkt = NULL;
3843 dglen = 0;
3844 padding = 0;
3845 }
3846 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
3847 (!qc_is_listener(qc) ||
3848 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3849 padding = 1;
3850 }
3851 }
3852
3853 out:
3854 ret = total;
3855 leave:
3856 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3857 return ret;
3858}
3859
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003860/* Free all frames in <l> list. In addition also remove all these frames
3861 * from the original ones if they are the results of duplications.
3862 */
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003863static inline void qc_free_frm_list(struct list *l)
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003864{
3865 struct quic_frame *frm, *frmbak;
3866
3867 list_for_each_entry_safe(frm, frmbak, l, list) {
3868 LIST_DEL_INIT(&frm->ref);
3869 qc_frm_free(&frm);
3870 }
3871}
3872
3873/* Free <pkt> TX packet and all the packets coalesced to it. */
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003874static inline void qc_free_tx_coalesced_pkts(struct quic_tx_packet *p)
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003875{
3876 struct quic_tx_packet *pkt, *nxt_pkt;
3877
3878 for (pkt = p; pkt; pkt = nxt_pkt) {
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003879 qc_free_frm_list(&pkt->frms);
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003880 nxt_pkt = pkt->next;
3881 pool_free(pool_head_quic_tx_packet, pkt);
3882 }
3883}
3884
3885/* Purge <buf> TX buffer from its prepare packets. */
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003886static void qc_purge_tx_buf(struct buffer *buf)
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003887{
3888 while (b_contig_data(buf, 0)) {
3889 uint16_t dglen;
3890 struct quic_tx_packet *pkt;
3891 size_t headlen = sizeof dglen + sizeof pkt;
3892
3893 dglen = read_u16(b_head(buf));
3894 pkt = read_ptr(b_head(buf) + sizeof dglen);
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003895 qc_free_tx_coalesced_pkts(pkt);
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003896 b_del(buf, dglen + headlen);
3897 }
3898
3899 BUG_ON(b_data(buf));
3900}
3901
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003902/* Send datagrams stored in <buf>.
3903 *
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003904 * This function returns 1 for success. On error, there is several behavior
3905 * depending on underlying sendto() error :
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003906 * - for an unrecoverable error, 0 is returned and connection is killed.
3907 * - a transient error is handled differently if connection has its owned
3908 * socket. If this is the case, 0 is returned and socket is subscribed on the
3909 * poller. The other case is assimilated to a success case with 1 returned.
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003910 * Remaining data are purged from the buffer and will eventually be detected
3911 * as lost which gives the opportunity to retry sending.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003912 */
3913int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
3914{
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003915 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003916 struct quic_conn *qc;
3917 char skip_sendto = 0;
3918
3919 qc = ctx->qc;
3920 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
3921 while (b_contig_data(buf, 0)) {
3922 unsigned char *pos;
3923 struct buffer tmpbuf = { };
3924 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3925 uint16_t dglen;
3926 size_t headlen = sizeof dglen + sizeof first_pkt;
3927 unsigned int time_sent;
3928
3929 pos = (unsigned char *)b_head(buf);
3930 dglen = read_u16(pos);
3931 BUG_ON_HOT(!dglen); /* this should not happen */
3932
3933 pos += sizeof dglen;
3934 first_pkt = read_ptr(pos);
3935 pos += sizeof first_pkt;
3936 tmpbuf.area = (char *)pos;
3937 tmpbuf.size = tmpbuf.data = dglen;
3938
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003939 TRACE_PROTO("TX dgram", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003940 /* If sendto is on error just skip the call to it for the rest
3941 * of the loop but continue to purge the buffer. Data will be
3942 * transmitted when QUIC packets are detected as lost on our
3943 * side.
3944 *
3945 * TODO use fd-monitoring to detect when send operation can be
3946 * retry. This should improve the bandwidth without relying on
3947 * retransmission timer. However, it requires a major rework on
3948 * quic-conn fd management.
3949 */
3950 if (!skip_sendto) {
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003951 int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0);
3952 if (ret < 0) {
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003953 TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc, first_pkt);
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003954 qc_kill_conn(qc);
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003955 qc_free_tx_coalesced_pkts(first_pkt);
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003956 b_del(buf, dglen + headlen);
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003957 qc_purge_tx_buf(buf);
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003958 goto leave;
3959 }
3960 else if (!ret) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003961 /* Connection owned socket : poller will wake us up when transient error is cleared. */
3962 if (qc_test_fd(qc)) {
3963 TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc);
3964 goto leave;
3965 }
3966
3967 /* No connection owned-socket : rely on retransmission to retry sending. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003968 skip_sendto = 1;
3969 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
3970 }
3971 }
3972
3973 b_del(buf, dglen + headlen);
3974 qc->tx.bytes += tmpbuf.data;
3975 time_sent = now_ms;
3976
3977 for (pkt = first_pkt; pkt; pkt = next_pkt) {
Frédéric Lécailleceb88b82023-02-20 14:43:55 +01003978 /* RFC 9000 14.1 Initial datagram size
3979 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3980 * Initial packets to at least the smallest allowed maximum datagram size of
3981 * 1200 bytes.
3982 */
Frédéric Lécaille12a815a2023-05-24 15:55:14 +02003983 qc->cntrs.sent_pkt++;
Frédéric Lécailleceb88b82023-02-20 14:43:55 +01003984 BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3985 (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
3986 dglen < QUIC_INITIAL_PACKET_MINLEN);
3987
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003988 pkt->time_sent = time_sent;
3989 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3990 pkt->pktns->tx.time_of_last_eliciting = time_sent;
3991 qc->path->ifae_pkts++;
3992 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
Frédéric Lécailled7215712023-03-24 18:13:37 +01003993 qc_idle_timer_rearm(qc, 0, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003994 }
3995 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3996 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3997 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003998 qc_detach_th_ctx_list(qc, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003999
4000 /* RFC 9000 10.2. Immediate Close:
4001 * The closing and draining connection states exist to ensure
4002 * that connections close cleanly and that delayed or reordered
4003 * packets are properly discarded. These states SHOULD persist
4004 * for at least three times the current PTO interval...
4005 *
4006 * Rearm the idle timeout only one time when entering closing
4007 * state.
4008 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01004009 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004010 if (qc->timer_task) {
4011 task_destroy(qc->timer_task);
4012 qc->timer_task = NULL;
4013 }
4014 }
4015 qc->path->in_flight += pkt->in_flight_len;
4016 pkt->pktns->tx.in_flight += pkt->in_flight_len;
4017 if (pkt->in_flight_len)
4018 qc_set_timer(qc);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004019 TRACE_PROTO("TX pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004020 next_pkt = pkt->next;
4021 quic_tx_packet_refinc(pkt);
4022 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
4023 }
4024 }
4025
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01004026 ret = 1;
4027leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004028 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
4029
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01004030 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004031}
4032
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004033/* Copy at <pos> position a stateless reset token depending on the
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004034 * <salt> salt input. This is the cluster secret which will be derived
4035 * as HKDF input secret to generate this token.
4036 * Return 1 if succeeded, 0 if not.
4037 */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004038static int quic_stateless_reset_token_cpy(unsigned char *pos, size_t len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004039 const unsigned char *salt, size_t saltlen)
4040{
4041 /* Input secret */
Frédéric Lécaille0499db42023-09-07 18:43:52 +02004042 const unsigned char *key = global.cluster_secret;
4043 size_t keylen = sizeof global.cluster_secret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004044 /* Info */
4045 const unsigned char label[] = "stateless token";
4046 size_t labellen = sizeof label - 1;
4047 int ret;
4048
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004049 ret = quic_hkdf_extract_and_expand(EVP_sha256(), pos, len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004050 key, keylen, salt, saltlen, label, labellen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004051 return ret;
4052}
4053
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004054/* Initialize the stateless reset token attached to <conn_id> connection ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004055 * Returns 1 if succeeded, 0 if not.
4056 */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004057static int quic_stateless_reset_token_init(struct quic_connection_id *conn_id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004058{
Frédéric Lécaille0499db42023-09-07 18:43:52 +02004059 /* Output secret */
4060 unsigned char *token = conn_id->stateless_reset_token;
4061 size_t tokenlen = sizeof conn_id->stateless_reset_token;
4062 /* Salt */
4063 const unsigned char *cid = conn_id->cid.data;
4064 size_t cidlen = conn_id->cid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004065
Frédéric Lécaille0499db42023-09-07 18:43:52 +02004066 return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004067}
4068
Amaury Denoyelle1e959ad2023-04-13 17:34:56 +02004069/* Generate a CID directly derived from <orig> CID and <addr> address.
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004070 *
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004071 * Returns the derived CID.
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004072 */
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004073struct quic_cid quic_derive_cid(const struct quic_cid *orig,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004074 const struct sockaddr_storage *addr)
4075{
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004076 struct quic_cid cid;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004077 const struct sockaddr_in *in;
4078 const struct sockaddr_in6 *in6;
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004079 char *pos = trash.area;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004080 size_t idx = 0;
4081 uint64_t hash;
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004082 int i;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004083
4084 /* Prepare buffer for hash using original CID first. */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004085 memcpy(pos, orig->data, orig->len);
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004086 idx += orig->len;
4087
4088 /* Concatenate client address. */
4089 switch (addr->ss_family) {
4090 case AF_INET:
4091 in = (struct sockaddr_in *)addr;
4092
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004093 memcpy(&pos[idx], &in->sin_addr, sizeof(in->sin_addr));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004094 idx += sizeof(in->sin_addr);
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004095 memcpy(&pos[idx], &in->sin_port, sizeof(in->sin_port));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004096 idx += sizeof(in->sin_port);
4097 break;
4098
4099 case AF_INET6:
4100 in6 = (struct sockaddr_in6 *)addr;
4101
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004102 memcpy(&pos[idx], &in6->sin6_addr, sizeof(in6->sin6_addr));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004103 idx += sizeof(in6->sin6_addr);
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004104 memcpy(&pos[idx], &in6->sin6_port, sizeof(in6->sin6_port));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004105 idx += sizeof(in6->sin6_port);
4106 break;
4107
4108 default:
4109 /* TODO to implement */
4110 ABORT_NOW();
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004111 }
4112
4113 /* Avoid similar values between multiple haproxy process. */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004114 memcpy(&pos[idx], boot_seed, sizeof(boot_seed));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004115 idx += sizeof(boot_seed);
4116
4117 /* Hash the final buffer content. */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004118 hash = XXH64(pos, idx, 0);
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004119
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004120 for (i = 0; i < sizeof(hash); ++i)
4121 cid.data[i] = hash >> ((sizeof(hash) * 7) - (8 * i));
4122 cid.len = sizeof(hash);
4123
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004124 return cid;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004125}
4126
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004127/* Retrieve the thread ID associated to QUIC connection ID <cid> of length
4128 * <cid_len>. CID may be not found on the CID tree because it is an ODCID. In
4129 * this case, it will derived using client address <cli_addr> as hash
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004130 * parameter. However, this is done only if <pos> points to an INITIAL or 0RTT
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004131 * packet of length <len>.
4132 *
4133 * Returns the thread ID or a negative error code.
4134 */
4135int quic_get_cid_tid(const unsigned char *cid, size_t cid_len,
4136 const struct sockaddr_storage *cli_addr,
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004137 unsigned char *pos, size_t len)
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004138{
4139 struct quic_cid_tree *tree;
4140 struct quic_connection_id *conn_id;
4141 struct ebmb_node *node;
Amaury Denoyellefd5caa22024-06-27 17:15:55 +02004142 int cid_tid = -1;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004143
4144 tree = &quic_cid_trees[_quic_cid_tree_idx(cid)];
4145 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
4146 node = ebmb_lookup(&tree->root, cid, cid_len);
Amaury Denoyellefd5caa22024-06-27 17:15:55 +02004147 if (node) {
4148 conn_id = ebmb_entry(node, struct quic_connection_id, node);
4149 cid_tid = HA_ATOMIC_LOAD(&conn_id->tid);
4150 }
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004151 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
4152
Amaury Denoyellefd5caa22024-06-27 17:15:55 +02004153 /* If CID not found, it may be an ODCID, thus not stored in global CID
4154 * tree. Derive it to its associated DCID value and reperform a lookup.
4155 */
4156 if (cid_tid < 0) {
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004157 struct quic_cid orig, derive_cid;
4158 struct quic_rx_packet pkt;
4159
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004160 if (!qc_parse_hd_form(&pkt, &pos, pos + len))
Amaury Denoyellefd5caa22024-06-27 17:15:55 +02004161 goto out;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004162
Amaury Denoyellefd5caa22024-06-27 17:15:55 +02004163 /* ODCID are only used in INITIAL or 0-RTT packets */
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004164 if (pkt.type != QUIC_PACKET_TYPE_INITIAL &&
4165 pkt.type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyellefd5caa22024-06-27 17:15:55 +02004166 goto out;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004167 }
4168
4169 memcpy(orig.data, cid, cid_len);
4170 orig.len = cid_len;
4171 derive_cid = quic_derive_cid(&orig, cli_addr);
4172
4173 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
4174 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
4175 node = ebmb_lookup(&tree->root, cid, cid_len);
Amaury Denoyellefd5caa22024-06-27 17:15:55 +02004176 if (node) {
4177 conn_id = ebmb_entry(node, struct quic_connection_id, node);
4178 cid_tid = HA_ATOMIC_LOAD(&conn_id->tid);
4179 }
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004180 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
4181 }
4182
Amaury Denoyellefd5caa22024-06-27 17:15:55 +02004183 out:
4184 return cid_tid;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004185}
4186
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004187/* Allocate a new CID and attach it to <root> ebtree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004188 *
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004189 * If <orig> and <addr> params are non null, the new CID value is directly
4190 * derived from them. Else a random value is generated. The CID is then marked
4191 * with the current thread ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004192 *
4193 * Returns the new CID if succeeded, NULL if not.
4194 */
4195static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004196 struct quic_conn *qc,
4197 const struct quic_cid *orig,
4198 const struct sockaddr_storage *addr)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004199{
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004200 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004201
4202 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4203
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004204 /* Caller must set either none or both values. */
4205 BUG_ON(!!orig != !!addr);
4206
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004207 conn_id = pool_alloc(pool_head_quic_connection_id);
4208 if (!conn_id) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004209 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
4210 goto err;
4211 }
4212
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004213 conn_id->cid.len = QUIC_HAP_CID_LEN;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004214
4215 if (!orig) {
4216 /* TODO: RAND_bytes() should be replaced */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004217 if (RAND_bytes(conn_id->cid.data, conn_id->cid.len) != 1) {
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004218 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
4219 goto err;
4220 }
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004221 }
4222 else {
4223 /* Derive the new CID value from original CID. */
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004224 conn_id->cid = quic_derive_cid(orig, addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004225 }
4226
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004227 if (quic_stateless_reset_token_init(conn_id) != 1) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004228 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
4229 goto err;
4230 }
4231
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004232 conn_id->qc = qc;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004233 HA_ATOMIC_STORE(&conn_id->tid, tid);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004234
Amaury Denoyellef16ec342023-04-13 17:42:34 +02004235 conn_id->seq_num.key = qc ? qc->next_cid_seq_num++ : 0;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004236 conn_id->retire_prior_to = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004237 /* insert the allocated CID in the quic_conn tree */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02004238 if (root)
4239 eb64_insert(root, &conn_id->seq_num);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004240
4241 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004242 return conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004243
4244 err:
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004245 pool_free(pool_head_quic_connection_id, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004246 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4247 return NULL;
4248}
4249
4250/* Build all the frames which must be sent just after the handshake have succeeded.
4251 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
4252 * a HANDSHAKE_DONE frame.
4253 * Return 1 if succeeded, 0 if not.
4254 */
4255static int quic_build_post_handshake_frames(struct quic_conn *qc)
4256{
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004257 int ret = 0, max;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004258 struct quic_enc_level *qel;
4259 struct quic_frame *frm, *frmbak;
4260 struct list frm_list = LIST_HEAD_INIT(frm_list);
4261 struct eb64_node *node;
4262
4263 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4264
4265 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4266 /* Only servers must send a HANDSHAKE_DONE frame. */
4267 if (qc_is_listener(qc)) {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004268 frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004269 if (!frm) {
4270 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4271 goto leave;
4272 }
4273
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004274 LIST_APPEND(&frm_list, &frm->list);
4275 }
4276
4277 /* Initialize <max> connection IDs minus one: there is
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004278 * already one connection ID used for the current connection. Also limit
4279 * the number of connection IDs sent to the peer to 4 (3 from this function
4280 * plus 1 for the current connection.
4281 * Note that active_connection_id_limit >= 2: this has been already checked
4282 * when receiving this parameter.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004283 */
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004284 max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3);
4285 while (max--) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004286 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004287
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004288 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004289 if (!frm) {
4290 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4291 goto err;
4292 }
4293
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004294 conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
4295 if (!conn_id) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004296 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004297 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
4298 goto err;
4299 }
4300
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004301 /* TODO To prevent CID tree locking, all CIDs created here
4302 * could be allocated at the same time as the first one.
4303 */
4304 quic_cid_insert(conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004305
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004306 quic_connection_id_to_frm_cpy(frm, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004307 LIST_APPEND(&frm_list, &frm->list);
4308 }
4309
4310 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004311 qc->flags &= ~QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004312
4313 ret = 1;
4314 leave:
4315 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4316 return ret;
4317
4318 err:
4319 /* free the frames */
4320 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004321 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004322
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004323 /* The first CID sequence number value used to allocated CIDs by this function is 1,
4324 * 0 being the sequence number of the CID for this connection.
4325 */
4326 node = eb64_lookup_ge(&qc->cids, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004327 while (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004328 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004329
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004330 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
4331 if (conn_id->seq_num.key >= max)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004332 break;
4333
4334 node = eb64_next(node);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004335 quic_cid_delete(conn_id);
4336
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004337 eb64_delete(&conn_id->seq_num);
4338 pool_free(pool_head_quic_connection_id, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004339 }
4340 goto leave;
4341}
4342
4343/* Deallocate <l> list of ACK ranges. */
4344void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
4345{
4346 struct eb64_node *n;
4347 struct quic_arng_node *ar;
4348
4349 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4350
4351 n = eb64_first(&arngs->root);
4352 while (n) {
4353 struct eb64_node *next;
4354
4355 ar = eb64_entry(n, struct quic_arng_node, first);
4356 next = eb64_next(n);
4357 eb64_delete(n);
4358 pool_free(pool_head_quic_arng, ar);
4359 n = next;
4360 }
4361
4362 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4363}
4364
4365/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
4366 * descending order.
4367 */
4368static inline size_t sack_gap(struct quic_arng_node *p,
4369 struct quic_arng_node *q)
4370{
4371 return p->first.key - q->last - 2;
4372}
4373
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004374/* Set the encoded size of <arngs> QUIC ack ranges. */
4375static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
4376{
4377 struct eb64_node *node, *next;
4378 struct quic_arng_node *ar, *ar_next;
4379
4380 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4381
4382 node = eb64_last(&arngs->root);
4383 if (!node)
4384 goto leave;
4385
4386 ar = eb64_entry(node, struct quic_arng_node, first);
4387 arngs->enc_sz = quic_int_getsize(ar->last) +
4388 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
4389
4390 while ((next = eb64_prev(node))) {
4391 ar_next = eb64_entry(next, struct quic_arng_node, first);
4392 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
4393 quic_int_getsize(ar_next->last - ar_next->first.key);
4394 node = next;
4395 ar = eb64_entry(node, struct quic_arng_node, first);
4396 }
4397
4398 leave:
4399 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4400}
4401
4402/* Insert <ar> ack range into <argns> tree of ack ranges.
4403 * Returns the ack range node which has been inserted if succeeded, NULL if not.
4404 */
4405static inline
4406struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
4407 struct quic_arngs *arngs,
4408 struct quic_arng *ar)
4409{
4410 struct quic_arng_node *new_ar;
4411
4412 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4413
Frédéric Lécaille0ed94032023-04-17 14:10:14 +02004414 if (arngs->sz >= QUIC_MAX_ACK_RANGES) {
Frederic Lecaille3ce49432024-02-05 14:07:51 +01004415 struct eb64_node *first;
Frédéric Lécaille0ed94032023-04-17 14:10:14 +02004416
Frederic Lecaille3ce49432024-02-05 14:07:51 +01004417 first = eb64_first(&arngs->root);
4418 BUG_ON(first == NULL);
4419 eb64_delete(first);
4420 pool_free(pool_head_quic_arng, first);
Frédéric Lécaille0ed94032023-04-17 14:10:14 +02004421 arngs->sz--;
4422 }
4423
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004424 new_ar = pool_alloc(pool_head_quic_arng);
4425 if (!new_ar) {
4426 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
4427 goto leave;
4428 }
4429
4430 new_ar->first.key = ar->first;
4431 new_ar->last = ar->last;
4432 eb64_insert(&arngs->root, &new_ar->first);
4433 arngs->sz++;
4434
4435 leave:
4436 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4437 return new_ar;
4438}
4439
4440/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
4441 * Note that this function computes the number of bytes required to encode
4442 * this tree of ACK ranges in descending order.
4443 *
4444 * Descending order
4445 * ------------->
4446 * range1 range2
4447 * ..........|--------|..............|--------|
4448 * ^ ^ ^ ^
4449 * | | | |
4450 * last1 first1 last2 first2
4451 * ..........+--------+--------------+--------+......
4452 * diff1 gap12 diff2
4453 *
4454 * To encode the previous list of ranges we must encode integers as follows in
4455 * descending order:
4456 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
4457 * with diff1 = last1 - first1
4458 * diff2 = last2 - first2
4459 * gap12 = first1 - last2 - 2 (>= 0)
4460 *
4461
4462returns 0 on error
4463
4464 */
4465int quic_update_ack_ranges_list(struct quic_conn *qc,
4466 struct quic_arngs *arngs,
4467 struct quic_arng *ar)
4468{
4469 int ret = 0;
4470 struct eb64_node *le;
4471 struct quic_arng_node *new_node;
4472 struct eb64_node *new;
4473
4474 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4475
4476 new = NULL;
4477 if (eb_is_empty(&arngs->root)) {
4478 new_node = quic_insert_new_range(qc, arngs, ar);
4479 if (new_node)
4480 ret = 1;
4481
4482 goto leave;
4483 }
4484
4485 le = eb64_lookup_le(&arngs->root, ar->first);
4486 if (!le) {
4487 new_node = quic_insert_new_range(qc, arngs, ar);
4488 if (!new_node)
4489 goto leave;
4490
4491 new = &new_node->first;
4492 }
4493 else {
4494 struct quic_arng_node *le_ar =
4495 eb64_entry(le, struct quic_arng_node, first);
4496
4497 /* Already existing range */
4498 if (le_ar->last >= ar->last) {
4499 ret = 1;
4500 }
4501 else if (le_ar->last + 1 >= ar->first) {
4502 le_ar->last = ar->last;
4503 new = le;
4504 new_node = le_ar;
4505 }
4506 else {
4507 new_node = quic_insert_new_range(qc, arngs, ar);
4508 if (!new_node)
4509 goto leave;
4510
4511 new = &new_node->first;
4512 }
4513 }
4514
4515 /* Verify that the new inserted node does not overlap the nodes
4516 * which follow it.
4517 */
4518 if (new) {
4519 struct eb64_node *next;
4520 struct quic_arng_node *next_node;
4521
4522 while ((next = eb64_next(new))) {
4523 next_node =
4524 eb64_entry(next, struct quic_arng_node, first);
4525 if (new_node->last + 1 < next_node->first.key)
4526 break;
4527
4528 if (next_node->last > new_node->last)
4529 new_node->last = next_node->last;
4530 eb64_delete(next);
4531 pool_free(pool_head_quic_arng, next_node);
4532 /* Decrement the size of these ranges. */
4533 arngs->sz--;
4534 }
4535 }
4536
4537 ret = 1;
4538 leave:
4539 quic_arngs_set_enc_sz(qc, arngs);
4540 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4541 return ret;
4542}
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004543
4544/* Detect the value of the spin bit to be used. */
4545static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt,
4546 struct quic_enc_level *qel)
4547{
4548 uint64_t largest_pn = qel->pktns->rx.largest_pn;
4549
4550 if (qel != &qc->els[QUIC_TLS_ENC_LEVEL_APP] || largest_pn == -1 ||
4551 pkt->pn <= largest_pn)
4552 return;
4553
4554 if (qc_is_listener(qc)) {
4555 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4556 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4557 else
4558 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4559 }
4560 else {
4561 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4562 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4563 else
4564 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4565 }
4566}
4567
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004568/* Remove the header protection of packets at <el> encryption level.
4569 * Always succeeds.
4570 */
4571static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
4572{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004573 struct quic_rx_packet *pqpkt, *pkttmp;
4574 struct quic_enc_level *app_qel;
4575
4576 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
4577 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4578 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
4579 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004580 TRACE_PROTO("RX hp not removed (handshake not completed)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004581 QUIC_EV_CONN_ELRMHP, qc);
4582 goto out;
4583 }
Frédéric Lécaille72027782023-02-22 16:20:09 +01004584
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004585 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01004586 struct quic_tls_ctx *tls_ctx;
4587
4588 tls_ctx = qc_select_tls_ctx(qc, el, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004589 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
4590 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004591 TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004592 }
4593 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004594 qc_handle_spin_bit(qc, pqpkt, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004595 /* The AAD includes the packet number field */
4596 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
4597 /* Store the packet into the tree of packets to decrypt. */
4598 pqpkt->pn_node.key = pqpkt->pn;
4599 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
4600 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004601 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004602 }
Amaury Denoyelle90424652024-07-29 10:42:50 +02004603 LIST_DEL_INIT(&pqpkt->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004604 quic_rx_packet_refdec(pqpkt);
4605 }
4606
4607 out:
4608 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
4609}
4610
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004611/* Process all the CRYPTO frame at <el> encryption level. This is the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004612 * responsibility of the called to ensure there exists a CRYPTO data
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004613 * stream for this level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004614 * Return 1 if succeeded, 0 if not.
4615 */
4616static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
4617 struct quic_enc_level *el,
4618 struct ssl_sock_ctx *ctx)
4619{
4620 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004621 struct ncbuf *ncbuf;
4622 struct quic_cstream *cstream = el->cstream;
4623 ncb_sz_t data;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004624
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004625 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004626
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004627 BUG_ON(!cstream);
4628 ncbuf = &cstream->rx.ncbuf;
4629 if (ncb_is_null(ncbuf))
4630 goto done;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004631
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004632 /* TODO not working if buffer is wrapping */
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004633 while ((data = ncb_data(ncbuf, 0))) {
4634 const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004635
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004636 if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004637 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004638
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004639 cstream->rx.offset += data;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004640 TRACE_DEVEL("buffered crypto data were provided to TLS stack",
4641 QUIC_EV_CONN_PHPKTS, qc, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004642 }
4643
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004644 done:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004645 ret = 1;
4646 leave:
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004647 if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
4648 TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004649 quic_free_ncbuf(ncbuf);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004650 }
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004651 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004652 return ret;
4653}
4654
4655/* Process all the packets at <el> and <next_el> encryption level.
4656 * This is the caller responsibility to check that <cur_el> is different of <next_el>
4657 * as pointer value.
4658 * Return 1 if succeeded, 0 if not.
4659 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004660int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el,
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004661 struct quic_enc_level *next_el)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004662{
4663 int ret = 0;
4664 struct eb64_node *node;
4665 int64_t largest_pn = -1;
4666 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004667 struct quic_enc_level *qel = cur_el;
4668
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004669 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004670 qel = cur_el;
4671 next_tel:
4672 if (!qel)
4673 goto out;
4674
4675 node = eb64_first(&qel->rx.pkts);
4676 while (node) {
4677 struct quic_rx_packet *pkt;
4678
4679 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
4680 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004681 qc, pkt, NULL, qc->xprt_ctx->ssl);
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01004682 if (!qc_pkt_decrypt(qc, qel, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004683 /* Drop the packet */
4684 TRACE_ERROR("packet decryption failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004685 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004686 }
4687 else {
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004688 if (!qc_parse_pkt_frms(qc, pkt, qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004689 /* Drop the packet */
4690 TRACE_ERROR("packet parsing failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004691 QUIC_EV_CONN_RXPKT, qc, pkt);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02004692 qc->cntrs.dropped_parsing++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004693 }
4694 else {
4695 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
4696
Frédéric Lécailleddb9b1b2023-11-07 18:27:50 +01004697 /* Update the list of ranges to acknowledge. */
4698 if (quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar)) {
4699 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
4700 int arm_ack_timer =
4701 qc->state >= QUIC_HS_ST_COMPLETE &&
4702 qel->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT];
Frédéric Lécailleb5efe792023-04-14 09:56:17 +02004703
Frédéric Lécailleddb9b1b2023-11-07 18:27:50 +01004704 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
4705 qel->pktns->rx.nb_aepkts_since_last_ack++;
4706 qc_idle_timer_rearm(qc, 1, arm_ack_timer);
4707 }
4708
4709 if (pkt->pn > largest_pn) {
4710 largest_pn = pkt->pn;
4711 largest_pn_time_received = pkt->time_received;
4712 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004713 }
Frédéric Lécailleddb9b1b2023-11-07 18:27:50 +01004714 else {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004715 TRACE_ERROR("Could not update ack range list",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004716 QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécailleddb9b1b2023-11-07 18:27:50 +01004717 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004718 }
4719 }
4720 node = eb64_next(node);
4721 eb64_delete(&pkt->pn_node);
4722 quic_rx_packet_refdec(pkt);
4723 }
4724
4725 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
4726 /* Update the largest packet number. */
4727 qel->pktns->rx.largest_pn = largest_pn;
4728 /* Update the largest acknowledged packet timestamps */
4729 qel->pktns->rx.largest_time_received = largest_pn_time_received;
4730 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
4731 }
4732
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004733 if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004734 // trace already emitted by function above
4735 goto leave;
4736 }
4737
4738 if (qel == cur_el) {
4739 BUG_ON(qel == next_el);
4740 qel = next_el;
4741 largest_pn = -1;
4742 goto next_tel;
4743 }
4744
4745 out:
4746 ret = 1;
4747 leave:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004748 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004749 return ret;
4750}
4751
4752/* Check if it's possible to remove header protection for packets related to
4753 * encryption level <qel>. If <qel> is NULL, assume it's false.
4754 *
4755 * Return true if the operation is possible else false.
4756 */
4757static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
4758{
4759 int ret = 0;
4760 enum quic_tls_enc_level tel;
4761
4762 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
4763
4764 if (!qel)
4765 goto cant_rm_hp;
4766
4767 tel = ssl_to_quic_enc_level(qel->level);
4768
4769 /* check if tls secrets are available */
4770 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004771 TRACE_PROTO("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004772 goto cant_rm_hp;
4773 }
4774
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004775 if (!quic_tls_has_rx_sec(qel)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004776 TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004777 goto cant_rm_hp;
4778 }
4779
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004780 if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004781 TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004782 goto cant_rm_hp;
4783 }
4784
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004785 /* check if the connection layer is ready before using app level */
4786 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
4787 qc->mux_state == QC_MUX_NULL) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004788 TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004789 goto cant_rm_hp;
4790 }
4791
4792 ret = 1;
4793 cant_rm_hp:
4794 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
4795 return ret;
4796}
4797
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004798/* Flush txbuf for <qc> connection. This must be called prior to a packet
4799 * preparation when txbuf contains older data. A send will be conducted for
4800 * these data.
4801 *
4802 * Returns 1 on success : buffer is empty and can be use for packet
4803 * preparation. On error 0 is returned.
4804 */
4805static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
4806{
4807 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4808
4809 /* This operation can only be conducted if txbuf is not empty. This
4810 * case only happens for connection with their owned socket due to an
4811 * older transient sendto() error.
4812 */
4813 BUG_ON(!qc_test_fd(qc));
4814
4815 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4816 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4817 qc_txb_release(qc);
4818 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4819 return 0;
4820 }
4821
4822 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4823 return 1;
4824}
4825
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004826/* Try to send application frames from list <frms> on connection <qc>.
4827 *
4828 * Use qc_send_app_probing wrapper when probing with old data.
4829 *
4830 * Returns 1 on success. Some data might not have been sent due to congestion,
4831 * in this case they are left in <frms> input list. The caller may subscribe on
4832 * quic-conn to retry later.
4833 *
4834 * Returns 0 on critical error.
4835 * TODO review and classify more distinctly transient from definitive errors to
4836 * allow callers to properly handle it.
4837 */
4838static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
4839{
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004840 int status = 0, ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004841 struct buffer *buf;
4842
4843 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4844
4845 buf = qc_txb_alloc(qc);
4846 if (!buf) {
4847 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004848 goto err;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004849 }
4850
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004851 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4852 goto err;
4853
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004854 /* Prepare and send packets until we could not further prepare packets. */
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004855 do {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004856 /* Currently buf cannot be non-empty at this stage. Even if a
4857 * previous sendto() has failed it is emptied to simulate
4858 * packet emission and rely on QUIC lost detection to try to
4859 * emit it.
4860 */
4861 BUG_ON_HOT(b_data(buf));
4862 b_reset(buf);
4863
4864 ret = qc_prep_app_pkts(qc, buf, frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004865
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004866 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004867 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4868 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004869 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004870 }
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004871 } while (ret > 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004872
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004873 qc_txb_release(qc);
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004874 if (ret < 0)
4875 goto err;
4876
4877 status = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004878 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4879 return status;
4880
4881 err:
Amaury Denoyelle37333862023-02-28 11:53:48 +01004882 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4883 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004884}
4885
4886/* Try to send application frames from list <frms> on connection <qc>. Use this
4887 * function when probing is required.
4888 *
4889 * Returns the result from qc_send_app_pkts function.
4890 */
4891static forceinline int qc_send_app_probing(struct quic_conn *qc,
4892 struct list *frms)
4893{
4894 int ret;
4895
4896 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4897
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02004898 TRACE_PROTO("preparing old data (probing)", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004899 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4900 ret = qc_send_app_pkts(qc, frms);
4901 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
4902
4903 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4904 return ret;
4905}
4906
4907/* Try to send application frames from list <frms> on connection <qc>. This
4908 * function is provided for MUX upper layer usage only.
4909 *
4910 * Returns the result from qc_send_app_pkts function.
4911 */
4912int qc_send_mux(struct quic_conn *qc, struct list *frms)
4913{
4914 int ret;
4915
4916 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4917 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
4918
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02004919 if (qc->conn->flags & CO_FL_SOCK_WR_SH) {
4920 qc->conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH;
4921 TRACE_DEVEL("connection on error", QUIC_EV_CONN_TXPKT, qc);
4922 return 0;
4923 }
4924
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004925 /* Try to send post handshake frames first unless on 0-RTT. */
4926 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) &&
4927 qc->state >= QUIC_HS_ST_COMPLETE) {
4928 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4929 quic_build_post_handshake_frames(qc);
4930 qc_send_app_pkts(qc, &qel->pktns->tx.frms);
4931 }
4932
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004933 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4934 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
4935 ret = qc_send_app_pkts(qc, frms);
4936 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
4937
4938 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4939 return ret;
4940}
4941
4942/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4943 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
4944 * QUIC connection. <old_data> is used as boolean to send data already sent but
4945 * not already acknowledged (in flight).
4946 * Returns 1 if succeeded, 0 if not.
4947 */
4948int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4949 enum quic_tls_enc_level tel, struct list *tel_frms,
4950 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4951{
4952 int ret, status = 0;
4953 struct buffer *buf = qc_txb_alloc(qc);
4954
4955 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4956
4957 if (!buf) {
4958 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4959 goto leave;
4960 }
4961
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004962 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4963 goto out;
4964
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004965 /* Currently buf cannot be non-empty at this stage. Even if a previous
4966 * sendto() has failed it is emptied to simulate packet emission and
4967 * rely on QUIC lost detection to try to emit it.
4968 */
4969 BUG_ON_HOT(b_data(buf));
4970 b_reset(buf);
4971
4972 if (old_data) {
4973 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
4974 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4975 }
4976
4977 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004978 if (ret == -1) {
4979 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004980 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004981 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004982
Amaury Denoyelle37333862023-02-28 11:53:48 +01004983 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004984 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4985 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004986 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004987 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004988
Amaury Denoyelle37333862023-02-28 11:53:48 +01004989 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004990 status = 1;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004991
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004992 out:
4993 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
4994 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004995 leave:
4996 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4997 return status;
4998}
4999
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005000/* Retransmit up to two datagrams depending on packet number space.
5001 * Return 0 when failed, 0 if not.
5002 */
5003static int qc_dgrams_retransmit(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005004{
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005005 int ret = 0;
Frédéric Lécaillefe97c6b2023-11-21 20:31:32 +01005006 int sret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005007 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
5008 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
5009 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
5010
5011 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
5012
5013 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01005014 int i;
5015
5016 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
5017 struct list ifrms = LIST_HEAD_INIT(ifrms);
5018 struct list hfrms = LIST_HEAD_INIT(hfrms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005019
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01005020 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
5021 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
5022 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
5023 if (!LIST_ISEMPTY(&ifrms)) {
5024 iqel->pktns->tx.pto_probe = 1;
5025 if (!LIST_ISEMPTY(&hfrms))
5026 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillefe97c6b2023-11-21 20:31:32 +01005027 sret = qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
5028 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
5029 qc_free_frm_list(&ifrms);
5030 qc_free_frm_list(&hfrms);
5031 if (!sret)
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005032 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005033 }
Frédéric Lécailleec937212023-03-03 17:34:41 +01005034 else {
5035 if (!(qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
5036 iqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillefe97c6b2023-11-21 20:31:32 +01005037 sret = qc_send_hdshk_pkts(qc, 0, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
5038 QUIC_TLS_ENC_LEVEL_NONE, NULL);
5039 qc_free_frm_list(&hfrms);
5040 if (!sret)
Frédéric Lécailleec937212023-03-03 17:34:41 +01005041 goto leave;
5042 }
5043 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005044 }
5045 TRACE_STATE("no more need to probe Initial packet number space",
5046 QUIC_EV_CONN_TXPKT, qc);
5047 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01005048 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005049 }
5050 else {
5051 int i;
5052
5053 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005054 hqel->pktns->tx.pto_probe = 0;
5055 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
Frédéric Lécaille7b5d9b12022-11-28 17:21:45 +01005056 struct list frms1 = LIST_HEAD_INIT(frms1);
5057
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005058 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
5059 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
5060 if (!LIST_ISEMPTY(&frms1)) {
5061 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillefe97c6b2023-11-21 20:31:32 +01005062 sret = qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
5063 QUIC_TLS_ENC_LEVEL_NONE, NULL);
5064 qc_free_frm_list(&frms1);
5065 if (!sret)
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005066 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005067 }
5068 }
5069 TRACE_STATE("no more need to probe Handshake packet number space",
5070 QUIC_EV_CONN_TXPKT, qc);
5071 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
5072 }
5073 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
5074 struct list frms2 = LIST_HEAD_INIT(frms2);
5075 struct list frms1 = LIST_HEAD_INIT(frms1);
5076
5077 aqel->pktns->tx.pto_probe = 0;
5078 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
5079 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
5080 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
5081 if (!LIST_ISEMPTY(&frms1)) {
5082 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillefe97c6b2023-11-21 20:31:32 +01005083 sret = qc_send_app_probing(qc, &frms1);
5084 qc_free_frm_list(&frms1);
5085 if (!sret) {
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02005086 qc_free_frm_list(&frms2);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005087 goto leave;
Frédéric Lécaillec6bec2a2023-04-24 11:20:32 +02005088 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005089 }
5090 if (!LIST_ISEMPTY(&frms2)) {
5091 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillefe97c6b2023-11-21 20:31:32 +01005092 sret = qc_send_app_probing(qc, &frms2);
5093 qc_free_frm_list(&frms2);
5094 if (!sret)
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005095 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005096 }
5097 TRACE_STATE("no more need to probe 01RTT packet number space",
5098 QUIC_EV_CONN_TXPKT, qc);
5099 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
5100 }
5101 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005102
5103 ret = 1;
5104 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005105 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005106 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005107}
5108
5109/* QUIC connection packet handler task (post handshake) */
5110struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
5111{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005112 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005113 struct quic_enc_level *qel;
5114
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005115 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02005116
5117 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005118 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
5119
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01005120 if (qc_test_fd(qc))
5121 qc_rcv_buf(qc);
5122
Amaury Denoyelle1304d192023-04-11 16:46:03 +02005123 /* Prepare post-handshake frames
5124 * - after connection is instantiated (accept is done)
5125 * - handshake state is completed (may not be the case here in 0-RTT)
5126 */
5127 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) && qc->conn &&
5128 qc->state >= QUIC_HS_ST_COMPLETE) {
5129 quic_build_post_handshake_frames(qc);
5130 }
5131
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005132 /* Retranmissions */
5133 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
5134 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
5135 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005136 if (!qc_dgrams_retransmit(qc))
5137 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005138 }
5139
5140 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
5141 qc_rm_hp_pkts(qc, qel);
5142
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005143 if (!qc_treat_rx_pkts(qc, qel, NULL)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005144 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
5145 goto out;
5146 }
5147
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005148 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5149 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
5150 goto out;
5151 }
5152
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005153 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5154 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
5155 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
5156 goto out;
5157 }
5158
5159 /* XXX TODO: how to limit the list frames to send */
5160 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
5161 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
5162 goto out;
5163 }
5164
5165 out:
5166 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
5167 return t;
5168}
5169
5170/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
5171static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
5172{
5173 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
5174 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
5175 qel->pktns->tx.pto_probe ||
5176 !LIST_ISEMPTY(&qel->pktns->tx.frms);
5177}
5178
5179/* QUIC connection packet handler task. */
5180struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
5181{
5182 int ret, ssl_err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005183 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005184 enum quic_tls_enc_level tel, next_tel;
5185 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005186 /* Early-data encryption level */
5187 struct quic_enc_level *eqel;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005188 struct buffer *buf = NULL;
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005189 int st, zero_rtt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005190
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005191 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02005192
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005193 eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005194 st = qc->state;
5195 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
5196
5197 /* Retranmissions */
5198 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
5199 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
5200 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005201 if (!qc_dgrams_retransmit(qc))
5202 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005203 }
5204
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005205 ssl_err = SSL_ERROR_NONE;
5206 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005207 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005208 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01005209
5210 if (qc_test_fd(qc))
5211 qc_rcv_buf(qc);
5212
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005213 if (st >= QUIC_HS_ST_COMPLETE &&
5214 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
5215 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
5216 /* There may be remaining Handshake packets to treat and acknowledge. */
5217 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
5218 next_tel = QUIC_TLS_ENC_LEVEL_APP;
5219 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005220 else if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, zero_rtt))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005221 goto out;
5222
5223 qel = &qc->els[tel];
5224 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
5225
5226 next_level:
5227 /* Treat packets waiting for header packet protection decryption */
5228 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
5229 qc_rm_hp_pkts(qc, qel);
5230
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005231 if (!qc_treat_rx_pkts(qc, qel, next_qel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005232 goto out;
5233
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005234 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5235 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
5236 goto out;
5237 }
5238
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005239 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5240 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
5241 goto out;
5242
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005243 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005244 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005245 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
5246 if (next_qel && next_qel == eqel && zero_rtt) {
5247 TRACE_DEVEL("select 0RTT as next encryption level",
5248 QUIC_EV_CONN_PHPKTS, qc);
5249 qel = next_qel;
5250 next_qel = NULL;
5251 goto next_level;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005252 }
5253
5254 st = qc->state;
5255 if (st >= QUIC_HS_ST_COMPLETE) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005256 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
5257 QUIC_FL_TLS_SECRETS_DCD)) {
5258 /* Discard the Handshake keys. */
5259 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5260 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
5261 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
5262 qc_set_timer(qc);
5263 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5264 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
5265 }
5266
5267 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
5268 /* There may be remaining handshake to build (acks) */
5269 st = QUIC_HS_ST_SERVER_HANDSHAKE;
5270 }
Amaury Denoyelle90424652024-07-29 10:42:50 +02005271
5272 /* Release 0RTT packets still waiting for HP removal. These
5273 * packets are considered unneeded after handshake completion.
5274 * They will be freed later from Rx buf via quic_rx_pkts_del().
5275 */
5276 if (!LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts)) {
5277 struct quic_rx_packet *pqpkt, *pkttmp;
5278 list_for_each_entry_safe(pqpkt, pkttmp, &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts, list) {
5279 LIST_DEL_INIT(&pqpkt->list);
5280 quic_rx_packet_refdec(pqpkt);
5281 }
5282
5283 /* RFC 9001 4.9.3. Discarding 0-RTT Keys
5284 * Additionally, a server MAY discard 0-RTT keys as soon as it receives
5285 * a 1-RTT packet. However, due to packet reordering, a 0-RTT packet
5286 * could arrive after a 1-RTT packet. Servers MAY temporarily retain 0-
5287 * RTT keys to allow decrypting reordered packets without requiring
5288 * their contents to be retransmitted with 1-RTT keys. After receiving a
5289 * 1-RTT packet, servers MUST discard 0-RTT keys within a short time;
5290 * the RECOMMENDED time period is three times the Probe Timeout (PTO,
5291 * see [QUIC-RECOVERY]). A server MAY discard 0-RTT keys earlier if it
5292 * determines that it has received all 0-RTT packets, which can be done
5293 * by keeping track of missing packet numbers.
5294 *
5295 * TODO implement discarding of 0-RTT keys
5296 */
5297 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005298 }
5299
5300 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
5301 * be considered.
5302 */
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005303 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005304 goto out;
5305
5306 if (!qc_need_sending(qc, qel) &&
5307 (!next_qel || !qc_need_sending(qc, next_qel))) {
5308 goto skip_send;
5309 }
5310
5311 buf = qc_txb_alloc(qc);
5312 if (!buf)
5313 goto out;
5314
Amaury Denoyelle147862d2023-02-28 15:10:00 +01005315 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
5316 goto skip_send;
5317
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005318 /* Currently buf cannot be non-empty at this stage. Even if a previous
5319 * sendto() has failed it is emptied to simulate packet emission and
5320 * rely on QUIC lost detection to try to emit it.
5321 */
5322 BUG_ON_HOT(b_data(buf));
5323 b_reset(buf);
5324
5325 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
5326 next_tel, &qc->els[next_tel].pktns->tx.frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01005327 if (ret == -1) {
5328 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005329 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005330 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005331
Amaury Denoyelle37333862023-02-28 11:53:48 +01005332 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01005333 if (qc->flags & QUIC_FL_CONN_TO_KILL)
5334 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005335 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005336 }
5337
5338 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005339
5340 skip_send:
5341 /* Check if there is something to do for the next level.
5342 */
5343 if (next_qel && next_qel != qel &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005344 quic_tls_has_rx_sec(next_qel) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005345 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
5346 qel = next_qel;
5347 next_qel = NULL;
5348 goto next_level;
5349 }
5350
5351 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005352 TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
5353 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005354 return t;
5355}
5356
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005357/* Release the memory allocated for <cs> CRYPTO stream */
5358void quic_cstream_free(struct quic_cstream *cs)
5359{
5360 if (!cs) {
5361 /* This is the case for ORTT encryption level */
5362 return;
5363 }
5364
Amaury Denoyellebc174b22022-11-17 10:12:52 +01005365 quic_free_ncbuf(&cs->rx.ncbuf);
5366
Amaury Denoyellebbb18202024-01-26 14:41:04 +01005367 qc_stream_desc_release(cs->desc, 0);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005368 pool_free(pool_head_quic_cstream, cs);
5369}
5370
5371/* Allocate a new QUIC stream for <qc>.
5372 * Return it if succeeded, NULL if not.
5373 */
5374struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
5375{
5376 struct quic_cstream *cs, *ret_cs = NULL;
5377
5378 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5379 cs = pool_alloc(pool_head_quic_cstream);
5380 if (!cs) {
5381 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5382 goto leave;
5383 }
5384
5385 cs->rx.offset = 0;
5386 cs->rx.ncbuf = NCBUF_NULL;
5387 cs->rx.offset = 0;
5388
5389 cs->tx.offset = 0;
5390 cs->tx.sent_offset = 0;
5391 cs->tx.buf = BUF_NULL;
5392 cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
5393 if (!cs->desc) {
5394 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5395 goto err;
5396 }
5397
5398 ret_cs = cs;
5399 leave:
5400 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5401 return ret_cs;
5402
5403 err:
5404 pool_free(pool_head_quic_cstream, cs);
5405 goto leave;
5406}
5407
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005408/* Uninitialize <qel> QUIC encryption level. Never fails. */
5409static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
5410{
5411 int i;
5412
5413 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5414
5415 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
5416 if (qel->tx.crypto.bufs[i]) {
5417 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
5418 qel->tx.crypto.bufs[i] = NULL;
5419 }
5420 }
5421 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005422 quic_cstream_free(qel->cstream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005423
5424 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5425}
5426
5427/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
5428 * connection allocating everything needed.
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005429 *
5430 * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use
5431 * quic_conn_enc_level_uninit() to cleanup partially allocated content.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005432 */
5433static int quic_conn_enc_level_init(struct quic_conn *qc,
5434 enum quic_tls_enc_level level)
5435{
5436 int ret = 0;
5437 struct quic_enc_level *qel;
5438
Willy Tarreaua4fc3a52024-08-06 15:22:50 +02005439 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005440
5441 qel = &qc->els[level];
5442 qel->level = quic_to_ssl_enc_level(level);
5443 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
5444 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
5445 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
5446 qel->tls_ctx.flags = 0;
5447
5448 qel->rx.pkts = EB_ROOT;
5449 LIST_INIT(&qel->rx.pqpkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005450
5451 /* Allocate only one buffer. */
5452 /* TODO: use a pool */
5453 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
5454 if (!qel->tx.crypto.bufs)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005455 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005456
5457 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
5458 if (!qel->tx.crypto.bufs[0])
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005459 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005460
5461 qel->tx.crypto.bufs[0]->sz = 0;
5462 qel->tx.crypto.nb_buf = 1;
5463
5464 qel->tx.crypto.sz = 0;
5465 qel->tx.crypto.offset = 0;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005466 /* No CRYPTO data for early data TLS encryption level */
5467 if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA)
5468 qel->cstream = NULL;
5469 else {
5470 qel->cstream = quic_cstream_new(qc);
5471 if (!qel->cstream)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005472 goto leave;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005473 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005474
5475 ret = 1;
5476 leave:
Willy Tarreaua4fc3a52024-08-06 15:22:50 +02005477 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005478 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005479}
5480
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005481/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
5482 * This is not the case if the remote peer address is not validated and if
5483 * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
5484 */
5485static int qc_may_probe_ipktns(struct quic_conn *qc)
5486{
5487 return quic_peer_validated_addr(qc) ||
5488 (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
5489}
5490
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005491/* Callback called upon loss detection and PTO timer expirations. */
5492struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
5493{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005494 struct quic_conn *qc = ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005495 struct quic_pktns *pktns;
5496
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005497 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
5498 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005499 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005500
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005501 task->expire = TICK_ETERNITY;
5502 pktns = quic_loss_pktns(qc);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005503
5504 if (qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL)) {
5505 TRACE_PROTO("cancelled action (draining state)", QUIC_EV_CONN_PTIMER, qc);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005506 goto out;
5507 }
5508
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005509 if (tick_isset(pktns->tx.loss_time)) {
5510 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
5511
5512 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
5513 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005514 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01005515 if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
5516 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005517 goto out;
5518 }
5519
5520 if (qc->path->in_flight) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +01005521 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
Frédéric Lécaille68737312023-04-07 16:28:46 +02005522 if (!pktns->tx.in_flight) {
5523 TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
5524 goto out;
5525 }
5526
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005527 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
5528 if (qc_may_probe_ipktns(qc)) {
5529 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5530 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5531 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5532 }
5533 else {
5534 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5535 }
5536 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
5537 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5538 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5539 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5540 }
Frédéric Lécaillee25fce02023-03-20 17:23:19 +01005541 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005542 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
5543 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5544 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5545 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5546 if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005547 if (qc_may_probe_ipktns(qc)) {
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005548 qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005549 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5550 }
5551 else {
5552 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5553 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005554 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005555 }
5556 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01005557 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005558 /* Wake up upper layer if waiting to send new data. */
5559 if (!qc_notify_send(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005560 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005561 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5562 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005563 }
5564 }
5565 }
5566 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
5567 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
5568 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
5569
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005570 if (quic_tls_has_tx_sec(hel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005571 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005572 if (quic_tls_has_tx_sec(iel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005573 iel->pktns->tx.pto_probe = 1;
5574 }
5575
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005576 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005577 qc->path->loss.pto_count++;
5578
5579 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005580 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
5581 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005582
5583 return task;
5584}
5585
5586/* Parse the Retry token from buffer <token> with <end> a pointer to
5587 * one byte past the end of this buffer. This will extract the ODCID
5588 * which will be stored into <odcid>
5589 *
5590 * Returns 0 on success else non-zero.
5591 */
5592static int parse_retry_token(struct quic_conn *qc,
5593 const unsigned char *token, const unsigned char *end,
5594 struct quic_cid *odcid)
5595{
5596 int ret = 0;
5597 uint64_t odcid_len;
5598 uint32_t timestamp;
Emeric Brun7875f122023-07-11 16:13:19 +02005599 uint32_t now_sec = (uint32_t)date.tv_sec;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005600
5601 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5602
5603 if (!quic_dec_int(&odcid_len, &token, end)) {
5604 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
5605 goto leave;
5606 }
5607
5608 /* RFC 9000 7.2. Negotiating Connection IDs:
5609 * When an Initial packet is sent by a client that has not previously
5610 * received an Initial or Retry packet from the server, the client
5611 * populates the Destination Connection ID field with an unpredictable
5612 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5613 */
5614 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
5615 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
5616 goto leave;
5617 }
5618
5619 if (end - token < odcid_len + sizeof timestamp) {
5620 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
5621 goto leave;
5622 }
5623
5624 timestamp = ntohl(read_u32(token + odcid_len));
Emeric Brun7875f122023-07-11 16:13:19 +02005625 /* check if elapsed time is +/- QUIC_RETRY_DURATION_SEC
5626 * to tolerate token generator is not perfectly time synced
5627 */
5628 if ((uint32_t)(now_sec - timestamp) > QUIC_RETRY_DURATION_SEC &&
5629 (uint32_t)(timestamp - now_sec) > QUIC_RETRY_DURATION_SEC) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005630 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
5631 goto leave;
5632 }
5633
5634 ret = 1;
5635 memcpy(odcid->data, token, odcid_len);
5636 odcid->len = odcid_len;
5637 leave:
5638 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5639 return !ret;
5640}
5641
5642/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
5643 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
5644 * for QUIC servers (or haproxy listeners).
Frédéric Lécaille8a586582023-06-26 10:39:56 +02005645 * <dcid> is the destination connection ID, <scid> is the source connection ID.
5646 * This latter <scid> CID as the same value on the wire as the one for <conn_id>
5647 * which is the first CID of this connection but a different internal representation used to build
5648 * NEW_CONNECTION_ID frames. This is the responsability of the caller to insert
5649 * <conn_id> in the CIDs tree for this connection (qc->cids).
5650 * <token> is the token found to be used for this connection with <token_len> as
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005651 * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005652 * Returns the connection if succeeded, NULL if not.
5653 */
5654static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
5655 struct quic_cid *dcid, struct quic_cid *scid,
5656 const struct quic_cid *token_odcid,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005657 struct quic_connection_id *conn_id,
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005658 struct sockaddr_storage *local_addr,
5659 struct sockaddr_storage *peer_addr,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005660 int server, int token, void *owner)
5661{
5662 int i;
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005663 struct quic_conn *qc = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005664 /* Initial CID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005665 char *buf_area = NULL;
5666 struct listener *l = NULL;
5667 struct quic_cc_algo *cc_algo = NULL;
5668 struct quic_tls_ctx *ictx;
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01005669 unsigned int next_actconn = 0, next_sslconn = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005670 TRACE_ENTER(QUIC_EV_CONN_INIT);
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005671
5672 next_actconn = increment_actconn();
5673 if (!next_actconn) {
5674 _HA_ATOMIC_INC(&maxconn_reached);
5675 TRACE_STATE("maxconn reached", QUIC_EV_CONN_INIT);
5676 goto err;
5677 }
5678
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01005679 next_sslconn = increment_sslconn();
5680 if (!next_sslconn) {
5681 TRACE_STATE("sslconn reached", QUIC_EV_CONN_INIT);
5682 goto err;
5683 }
5684
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005685 /* TODO replace pool_zalloc by pool_alloc(). This requires special care
5686 * to properly initialized internal quic_conn members to safely use
5687 * quic_conn_release() on alloc failure.
5688 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005689 qc = pool_zalloc(pool_head_quic_conn);
5690 if (!qc) {
5691 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
5692 goto err;
5693 }
5694
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005695 /* Now that quic_conn instance is allocated, quic_conn_release() will
5696 * ensure global accounting is decremented.
5697 */
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01005698 next_sslconn = next_actconn = 0;
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005699
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005700 /* Initialize in priority qc members required for a safe dealloc. */
5701
5702 /* required to use MTLIST_IN_LIST */
5703 MT_LIST_INIT(&qc->accept_list);
5704
5705 LIST_INIT(&qc->rx.pkt_list);
5706
Amaury Denoyelle42448332022-12-12 11:24:05 +01005707 qc_init_fd(qc);
5708
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005709 LIST_INIT(&qc->back_refs);
Amaury Denoyelled537ca72023-04-19 10:45:40 +02005710 LIST_INIT(&qc->el_th_ctx);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005711
Frédéric Lécaille1b0a5a02023-12-05 20:12:51 +01005712 /* Packet number spaces initialization. */
5713 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
5714 quic_pktns_init(&qc->pktns[i]);
5715
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005716 /* Now proceeds to allocation of qc members. */
5717
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005718 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
5719 if (!buf_area) {
5720 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
5721 goto err;
5722 }
5723
5724 qc->cids = EB_ROOT;
5725 /* QUIC Server (or listener). */
5726 if (server) {
5727 struct proxy *prx;
5728
5729 l = owner;
5730 prx = l->bind_conf->frontend;
5731 cc_algo = l->bind_conf->quic_cc_algo;
5732
5733 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
5734 &quic_stats_module);
5735 qc->flags |= QUIC_FL_CONN_LISTENER;
5736 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005737 /* Copy the client original DCID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005738 qc->odcid.len = dcid->len;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005739 memcpy(qc->odcid.data, dcid->data, dcid->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005740
5741 /* copy the packet SCID to reuse it as DCID for sending */
5742 if (scid->len)
5743 memcpy(qc->dcid.data, scid->data, scid->len);
5744 qc->dcid.len = scid->len;
5745 qc->tx.buf = BUF_NULL;
5746 qc->li = l;
5747 }
5748 /* QUIC Client (outgoing connection to servers) */
5749 else {
5750 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
5751 if (dcid->len)
5752 memcpy(qc->dcid.data, dcid->data, dcid->len);
5753 qc->dcid.len = dcid->len;
5754 }
5755 qc->mux_state = QC_MUX_NULL;
5756 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
5757
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005758 conn_id->qc = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005759
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005760 if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
5761 is_addr(local_addr)) {
5762 TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
5763 qc_alloc_fd(qc, local_addr, peer_addr);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005764
5765 /* haproxy soft-stop is supported only for QUIC connections
5766 * with their owned socket.
5767 */
5768 if (qc_test_fd(qc))
5769 _HA_ATOMIC_INC(&jobs);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005770 }
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005771
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005772 /* Select our SCID which is the first CID with 0 as sequence number. */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005773 qc->scid = conn_id->cid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005774
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005775 /* QUIC encryption level context initialization. */
5776 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5777 if (!quic_conn_enc_level_init(qc, i)) {
5778 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
5779 goto err;
5780 }
5781 /* Initialize the packet number space. */
5782 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
5783 }
5784
5785 qc->original_version = qv;
5786 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
5787 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
5788 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
5789 /* TX part. */
5790 LIST_INIT(&qc->tx.frms_to_send);
5791 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
5792 qc->tx.wbuf = qc->tx.rbuf = 0;
5793 qc->tx.bytes = 0;
5794 qc->tx.buf = BUF_NULL;
5795 /* RX part. */
5796 qc->rx.bytes = 0;
5797 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
5798 for (i = 0; i < QCS_MAX_TYPES; i++)
5799 qc->rx.strms[i].nb_streams = 0;
5800
5801 qc->nb_pkt_for_cc = 1;
5802 qc->nb_pkt_since_cc = 0;
5803
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005804 if (!quic_tls_ku_init(qc)) {
5805 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
5806 goto err;
5807 }
5808
5809 /* XXX TO DO: Only one path at this time. */
5810 qc->path = &qc->paths[0];
5811 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
5812
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005813 qc->streams_by_id = EB_ROOT_UNIQUE;
5814 qc->stream_buf_count = 0;
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005815 memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
5816 memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005817
5818 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005819 conn_id->stateless_reset_token,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005820 dcid->data, dcid->len,
5821 qc->scid.data, qc->scid.len, token_odcid))
5822 goto err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005823
Frédéric Lécailledeb97812023-03-22 11:29:45 +01005824 /* Initialize the idle timeout of the connection at the "max_idle_timeout"
5825 * value from local transport parameters.
5826 */
5827 qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005828 qc->wait_event.tasklet = tasklet_new();
5829 if (!qc->wait_event.tasklet) {
5830 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
5831 goto err;
5832 }
5833 qc->wait_event.tasklet->process = quic_conn_io_cb;
5834 qc->wait_event.tasklet->context = qc;
5835 qc->wait_event.events = 0;
Amaury Denoyellebbb1c682022-09-28 15:15:51 +02005836 qc->subs = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005837
5838 if (qc_conn_alloc_ssl_ctx(qc) ||
5839 !quic_conn_init_timer(qc) ||
5840 !quic_conn_init_idle_timer_task(qc))
5841 goto err;
5842
5843 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
5844 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
5845 goto err;
5846
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005847 /* Counters initialization */
5848 memset(&qc->cntrs, 0, sizeof qc->cntrs);
5849
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005850 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
5851 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
5852
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005853 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
5854
5855 return qc;
5856
5857 err:
5858 pool_free(pool_head_quic_conn_rxbuf, buf_area);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005859 if (qc) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005860 qc->rx.buf.area = NULL;
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005861 quic_conn_release(qc);
5862 }
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005863
5864 /* Decrement global counters. Done only for errors happening before or
5865 * on pool_head_quic_conn alloc. All other cases are covered by
5866 * quic_conn_release().
5867 */
5868 if (next_actconn)
5869 _HA_ATOMIC_DEC(&actconn);
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01005870 if (next_sslconn)
5871 _HA_ATOMIC_DEC(&global.sslconns);
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005872
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005873 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005874 return NULL;
5875}
5876
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005877/* Update the proxy counters of <qc> QUIC connection from its counters */
5878static inline void quic_conn_prx_cntrs_update(struct quic_conn *qc)
5879{
Frédéric Lécaille557d30a2023-06-14 18:09:54 +02005880 if (!qc->prx_counters)
5881 return;
5882
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005883 HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt, qc->cntrs.dropped_pkt);
5884 HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt_bufoverrun, qc->cntrs.dropped_pkt_bufoverrun);
5885 HA_ATOMIC_ADD(&qc->prx_counters->dropped_parsing, qc->cntrs.dropped_parsing);
5886 HA_ATOMIC_ADD(&qc->prx_counters->socket_full, qc->cntrs.socket_full);
5887 HA_ATOMIC_ADD(&qc->prx_counters->sendto_err, qc->cntrs.sendto_err);
5888 HA_ATOMIC_ADD(&qc->prx_counters->sendto_err_unknown, qc->cntrs.sendto_err_unknown);
Frédéric Lécaille12a815a2023-05-24 15:55:14 +02005889 HA_ATOMIC_ADD(&qc->prx_counters->sent_pkt, qc->cntrs.sent_pkt);
Frédéric Lécaillea216e062023-07-03 10:40:32 +02005890 /* It is possible that ->path was not initialized. For instance if a
5891 * QUIC connection allocation has failed.
5892 */
5893 if (qc->path)
5894 HA_ATOMIC_ADD(&qc->prx_counters->lost_pkt, qc->path->loss.nb_lost_pkt);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005895 HA_ATOMIC_ADD(&qc->prx_counters->conn_migration_done, qc->cntrs.conn_migration_done);
5896 /* Stream related counters */
5897 HA_ATOMIC_ADD(&qc->prx_counters->data_blocked, qc->cntrs.data_blocked);
5898 HA_ATOMIC_ADD(&qc->prx_counters->stream_data_blocked, qc->cntrs.stream_data_blocked);
Amaury Denoyelle6d6ee0d2023-05-25 10:36:04 +02005899 HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_bidi, qc->cntrs.streams_blocked_bidi);
5900 HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_uni, qc->cntrs.streams_blocked_uni);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005901}
5902
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005903/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
5904 * The connection tasklet is killed.
5905 *
5906 * This function must only be called by the thread responsible of the quic_conn
5907 * tasklet.
5908 */
5909void quic_conn_release(struct quic_conn *qc)
5910{
5911 int i;
5912 struct ssl_sock_ctx *conn_ctx;
5913 struct eb64_node *node;
5914 struct quic_tls_ctx *app_tls_ctx;
5915 struct quic_rx_packet *pkt, *pktback;
5916
5917 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5918
5919 /* We must not free the quic-conn if the MUX is still allocated. */
5920 BUG_ON(qc->mux_state == QC_MUX_READY);
5921
Amaury Denoyellefb375572023-02-01 09:28:32 +01005922 if (qc_test_fd(qc))
5923 _HA_ATOMIC_DEC(&jobs);
5924
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005925 /* Close quic-conn socket fd. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01005926 qc_release_fd(qc, 0);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005927
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005928 /* in the unlikely (but possible) case the connection was just added to
5929 * the accept_list we must delete it from there.
5930 */
5931 MT_LIST_DELETE(&qc->accept_list);
5932
5933 /* free remaining stream descriptors */
5934 node = eb64_first(&qc->streams_by_id);
5935 while (node) {
5936 struct qc_stream_desc *stream;
5937
5938 stream = eb64_entry(node, struct qc_stream_desc, by_id);
5939 node = eb64_next(node);
5940
5941 /* all streams attached to the quic-conn are released, so
5942 * qc_stream_desc_free will liberate the stream instance.
5943 */
5944 BUG_ON(!stream->release);
5945 qc_stream_desc_free(stream, 1);
5946 }
5947
5948 /* Purge Rx packet list. */
5949 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
5950 LIST_DELETE(&pkt->qc_rx_pkt_list);
5951 pool_free(pool_head_quic_rx_packet, pkt);
5952 }
5953
5954 if (qc->idle_timer_task) {
5955 task_destroy(qc->idle_timer_task);
5956 qc->idle_timer_task = NULL;
5957 }
5958
5959 if (qc->timer_task) {
5960 task_destroy(qc->timer_task);
5961 qc->timer_task = NULL;
5962 }
5963
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02005964 tasklet_free(qc->wait_event.tasklet);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005965
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005966 /* remove the connection from receiver cids trees */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005967 free_quic_conn_cids(qc);
5968
5969 conn_ctx = qc->xprt_ctx;
5970 if (conn_ctx) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005971 SSL_free(conn_ctx->ssl);
5972 pool_free(pool_head_quic_conn_ctx, conn_ctx);
5973 }
5974
5975 quic_tls_ku_free(qc);
5976 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5977 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
5978 quic_conn_enc_level_uninit(qc, &qc->els[i]);
5979 }
5980 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
5981
5982 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
5983 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
5984 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
5985
5986 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
5987 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
5988 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
Frédéric Lécaille73e29442023-09-13 09:28:10 +02005989 qc_release_pktns_frms(qc, &qc->pktns[i]);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005990 }
5991
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01005992 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005993
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005994 quic_conn_prx_cntrs_update(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005995 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
5996 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +02005997 qc = NULL;
Amaury Denoyellefb375572023-02-01 09:28:32 +01005998
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005999 /* Decrement global counters when quic_conn is deallocated.
6000 * quic_cc_conn instances are not accounted as they run for a short
6001 * time with limited ressources.
6002 */
6003 _HA_ATOMIC_DEC(&actconn);
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01006004 _HA_ATOMIC_DEC(&global.sslconns);
Amaury Denoyelledc654e82023-11-06 17:45:14 +01006005
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006006 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
6007
6008 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
6009}
6010
6011/* Initialize the timer task of <qc> QUIC connection.
6012 * Returns 1 if succeeded, 0 if not.
6013 */
6014static int quic_conn_init_timer(struct quic_conn *qc)
6015{
6016 int ret = 0;
6017 /* Attach this task to the same thread ID used for the connection */
6018 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6019
Amaury Denoyelle66947282023-04-13 11:48:38 +02006020 qc->timer_task = task_new_here();
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006021 if (!qc->timer_task) {
6022 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
6023 goto leave;
6024 }
6025
6026 qc->timer = TICK_ETERNITY;
6027 qc->timer_task->process = qc_process_timer;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02006028 qc->timer_task->context = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006029
6030 ret = 1;
6031 leave:
6032 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6033 return ret;
6034}
6035
Frédéric Lécailled7215712023-03-24 18:13:37 +01006036/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
6037 * connection. */
6038static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006039{
6040 unsigned int expire;
6041
Frédéric Lécaille11ffacf2023-11-06 14:16:10 +01006042 /* It is possible the idle timer task has been already released. */
6043 if (!qc->idle_timer_task)
6044 return;
6045
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01006046 if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille495968e2023-04-03 17:42:05 +02006047 TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01006048 qc->ack_expire = TICK_ETERNITY;
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01006049 task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
6050 }
6051 else {
Amaury Denoyelle10197d62023-10-25 14:45:53 +02006052 if (qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
6053 /* RFC 9000 10.2. Immediate Close
6054 *
6055 * The closing and draining connection states exist to ensure that
6056 * connections close cleanly and that delayed or reordered packets are
6057 * properly discarded. These states SHOULD persist for at least three
6058 * times the current PTO interval as defined in [QUIC-RECOVERY].
6059 */
6060
6061 /* Delay is limited to 1s which should cover most of
6062 * network conditions. The process should not be
6063 * impacted by a connection with a high RTT.
6064 */
6065 expire = MIN(3 * quic_pto(qc), 1000);
6066 }
6067 else {
6068 /* RFC 9000 10.1. Idle Timeout
6069 *
6070 * To avoid excessively small idle timeout periods, endpoints MUST
6071 * increase the idle timeout period to be at least three times the
6072 * current Probe Timeout (PTO). This allows for multiple PTOs to expire,
6073 * and therefore multiple probes to be sent and lost, prior to idle
6074 * timeout.
6075 */
6076 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
6077 }
6078
Frédéric Lécailled7215712023-03-24 18:13:37 +01006079 qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
6080 if (arm_ack) {
6081 /* Arm the ack timer only if not already armed. */
6082 if (!tick_isset(qc->ack_expire)) {
6083 qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
6084 qc->idle_timer_task->expire = qc->ack_expire;
6085 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02006086 TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01006087 }
6088 }
6089 else {
6090 qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
6091 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02006092 TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01006093 }
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01006094 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006095}
6096
Frédéric Lécailled7215712023-03-24 18:13:37 +01006097/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
6098 * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
6099 * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
6100 * which must be rearmed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006101 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01006102static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006103{
6104 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
6105
6106 if (read) {
6107 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
6108 }
6109 else {
6110 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
6111 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01006112 qc_idle_timer_do_rearm(qc, arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006113
6114 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
6115}
6116
6117/* The task handling the idle timeout */
6118struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
6119{
6120 struct quic_conn *qc = ctx;
6121 struct quic_counters *prx_counters = qc->prx_counters;
6122 unsigned int qc_flags = qc->flags;
6123
6124 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
6125
Frédéric Lécaille12eca3a2023-04-04 10:46:54 +02006126 if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
6127 goto requeue;
6128
Frédéric Lécailled7215712023-03-24 18:13:37 +01006129 if (tick_is_expired(qc->ack_expire, now_ms)) {
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006130 TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01006131 qc->ack_expire = TICK_ETERNITY;
6132 /* Note that ->idle_expire is always set. */
6133 t->expire = qc->idle_expire;
Frédéric Lécailleb73762a2023-04-24 11:32:22 +02006134 /* Do not wakeup the I/O handler in DRAINING state or if the
6135 * connection must be killed as soon as possible.
6136 */
6137 if (!(qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL))) {
6138 qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
6139 tasklet_wakeup(qc->wait_event.tasklet);
6140 }
6141
Frédéric Lécailled7215712023-03-24 18:13:37 +01006142 goto requeue;
6143 }
6144
Frédéric Lécaille495968e2023-04-03 17:42:05 +02006145 TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006146 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
6147 * might free the quic-conn too early via quic_close().
6148 */
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02006149 qc_notify_err(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006150
6151 /* If the MUX is still alive, keep the quic-conn. The MUX is
6152 * responsible to call quic_close to release it.
6153 */
6154 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006155 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006156 quic_conn_release(qc);
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006157 qc = NULL;
Frédéric Lécaille11ffacf2023-11-06 14:16:10 +01006158 }
6159 else {
6160 task_destroy(t);
6161 qc->idle_timer_task = NULL;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006162 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006163
Frédéric Lécaille11ffacf2023-11-06 14:16:10 +01006164 t = NULL;
6165
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006166 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
6167 * least clean some parts of it such as the tasklet.
6168 */
6169
6170 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
6171 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006172 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006173 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
6174 }
6175
Frédéric Lécailled7215712023-03-24 18:13:37 +01006176 requeue:
6177 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
6178 return t;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006179}
6180
6181/* Initialize the idle timeout task for <qc>.
6182 * Returns 1 if succeeded, 0 if not.
6183 */
6184static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
6185{
6186 int ret = 0;
6187
6188 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6189
6190 qc->idle_timer_task = task_new_here();
6191 if (!qc->idle_timer_task) {
6192 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
6193 goto leave;
6194 }
6195
6196 qc->idle_timer_task->process = qc_idle_timer_task;
6197 qc->idle_timer_task->context = qc;
Frédéric Lécailled7215712023-03-24 18:13:37 +01006198 qc->ack_expire = TICK_ETERNITY;
6199 qc_idle_timer_rearm(qc, 1, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006200 task_queue(qc->idle_timer_task);
6201
6202 ret = 1;
6203 leave:
6204 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6205 return ret;
6206}
6207
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006208/* Parse into <pkt> a long header located at <*pos> position, <end> begin a pointer to the end
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006209 * past one byte of this buffer.
6210 */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006211static inline int quic_packet_read_long_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006212 struct quic_rx_packet *pkt)
6213{
6214 int ret = 0;
6215 unsigned char dcid_len, scid_len;
6216
6217 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
6218
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006219 if (end == *pos) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006220 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
6221 goto leave;
6222 }
6223
6224 /* Destination Connection ID Length */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006225 dcid_len = *(*pos)++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006226 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006227 if (dcid_len > QUIC_CID_MAXLEN || end - *pos < dcid_len + 1) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006228 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
6229 goto leave;
6230 }
6231
6232 if (dcid_len) {
6233 /* Check that the length of this received DCID matches the CID lengths
6234 * of our implementation for non Initials packets only.
6235 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006236 if (pkt->version && pkt->version->num &&
6237 pkt->type != QUIC_PACKET_TYPE_INITIAL &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006238 pkt->type != QUIC_PACKET_TYPE_0RTT &&
6239 dcid_len != QUIC_HAP_CID_LEN) {
6240 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
6241 goto leave;
6242 }
6243
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006244 memcpy(pkt->dcid.data, *pos, dcid_len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006245 }
6246
6247 pkt->dcid.len = dcid_len;
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006248 *pos += dcid_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006249
6250 /* Source Connection ID Length */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006251 scid_len = *(*pos)++;
6252 if (scid_len > QUIC_CID_MAXLEN || end - *pos < scid_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006253 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
6254 goto leave;
6255 }
6256
6257 if (scid_len)
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006258 memcpy(pkt->scid.data, *pos, scid_len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006259 pkt->scid.len = scid_len;
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006260 *pos += scid_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006261
6262 ret = 1;
6263 leave:
6264 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6265 return ret;
6266}
6267
6268/* Insert <pkt> RX packet in its <qel> RX packets tree */
6269static void qc_pkt_insert(struct quic_conn *qc,
6270 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
6271{
6272 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
6273
6274 pkt->pn_node.key = pkt->pn;
6275 quic_rx_packet_refinc(pkt);
6276 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
6277
6278 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6279}
6280
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006281/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
6282 * address of the packet first byte, using the keys from encryption level <el>.
6283 *
6284 * If header protection has been successfully removed, packet data are copied
6285 * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
6286 * proceeded, and the packet is inserted into <qc> protected packets tree. In
6287 * both cases, packet can now be considered handled by the <qc> connection.
6288 *
6289 * If header protection cannot be removed due to <el> secrets already
6290 * discarded, no operation is conducted.
6291 *
6292 * Returns 1 on success : packet data is now handled by the connection. On
6293 * error 0 is returned : packet should be dropped by the caller.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006294 */
6295static inline int qc_try_rm_hp(struct quic_conn *qc,
6296 struct quic_rx_packet *pkt,
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006297 unsigned char *beg,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006298 struct quic_enc_level **el)
6299{
6300 int ret = 0;
6301 unsigned char *pn = NULL; /* Packet number field */
6302 enum quic_tls_enc_level tel;
6303 struct quic_enc_level *qel;
6304 /* Only for traces. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006305
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006306 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006307 BUG_ON(!pkt->pn_offset);
6308
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006309 /* The packet number is here. This is also the start minus
6310 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
6311 * protection.
6312 */
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006313 pn = beg + pkt->pn_offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006314
6315 tel = quic_packet_type_enc_level(pkt->type);
6316 qel = &qc->els[tel];
6317
6318 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01006319 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
6320
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006321 /* Note that the following function enables us to unprotect the packet
6322 * number and its length subsequently used to decrypt the entire
6323 * packets.
6324 */
Frédéric Lécaille72027782023-02-22 16:20:09 +01006325 if (!qc_do_rm_hp(qc, pkt, tls_ctx,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006326 qel->pktns->rx.largest_pn, pn, beg)) {
6327 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
6328 goto out;
6329 }
6330
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006331 qc_handle_spin_bit(qc, pkt, qel);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006332 /* The AAD includes the packet number field. */
6333 pkt->aad_len = pkt->pn_offset + pkt->pnl;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006334 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
6335 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
6336 goto out;
6337 }
6338
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006339 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006340 }
6341 else {
6342 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
6343 /* If the packet number space has been discarded, this packet
6344 * will be not parsed.
6345 */
6346 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
6347 goto out;
6348 }
6349
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006350 TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006351 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
6352 quic_rx_packet_refinc(pkt);
6353 }
6354
6355 *el = qel;
6356 /* No reference counter incrementation here!!! */
6357 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
6358 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
6359 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
6360 b_add(&qc->rx.buf, pkt->len);
Amaury Denoyelledc654e82023-11-06 17:45:14 +01006361
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006362 ret = 1;
6363 out:
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006364 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006365 return ret;
6366}
6367
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006368/* Return the QUIC version (quic_version struct) with <version> as version number
6369 * if supported or NULL if not.
6370 */
6371static inline const struct quic_version *qc_supported_version(uint32_t version)
6372{
6373 int i;
6374
6375 if (unlikely(!version))
6376 return &quic_version_VN_reserved;
6377
6378 for (i = 0; i < quic_versions_nb; i++)
6379 if (quic_versions[i].num == version)
6380 return &quic_versions[i];
6381
6382 return NULL;
6383}
6384
Willy Tarreaudd9f9212023-05-07 07:07:44 +02006385/* Parse a QUIC packet header starting at <pos> position without exceeding <end>.
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006386 * Version and type are stored in <pkt> packet instance. Type is set to unknown
6387 * on two occasions : for unsupported version, in this case version field is
6388 * set to NULL; for Version Negotiation packet with version number set to 0.
6389 *
6390 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006391 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006392int qc_parse_hd_form(struct quic_rx_packet *pkt,
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006393 unsigned char **pos, const unsigned char *end)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006394{
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006395 uint32_t version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006396 int ret = 0;
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006397 const unsigned char byte0 = **pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006398
6399 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006400 pkt->version = NULL;
6401 pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006402
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006403 (*pos)++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006404 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
6405 unsigned char type =
6406 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
6407
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006408 /* Version */
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006409 if (!quic_read_uint32(&version, (const unsigned char **)pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006410 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
6411 goto out;
6412 }
6413
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006414 pkt->version = qc_supported_version(version);
6415 if (version && pkt->version) {
6416 if (version != QUIC_PROTOCOL_VERSION_2) {
6417 pkt->type = type;
6418 }
6419 else {
6420 switch (type) {
6421 case 0:
6422 pkt->type = QUIC_PACKET_TYPE_RETRY;
6423 break;
6424 case 1:
6425 pkt->type = QUIC_PACKET_TYPE_INITIAL;
6426 break;
6427 case 2:
6428 pkt->type = QUIC_PACKET_TYPE_0RTT;
6429 break;
6430 case 3:
6431 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
6432 break;
6433 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006434 }
6435 }
6436 }
6437 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006438 if (byte0 & QUIC_PACKET_SPIN_BIT)
6439 pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006440 pkt->type = QUIC_PACKET_TYPE_SHORT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006441 }
6442
6443 ret = 1;
6444 out:
6445 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6446 return ret;
6447}
6448
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006449/*
6450 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
6451 * address <addr>.
6452 * Implementation of RFC9000 6. Version Negotiation
6453 *
6454 * TODO implement a rate-limiting sending of Version Negotiation packets
6455 *
6456 * Returns 0 on success else non-zero
6457 */
6458static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
6459 struct quic_rx_packet *pkt)
6460{
6461 char buf[256];
6462 int ret = 0, i = 0, j;
6463 uint32_t version;
6464 const socklen_t addrlen = get_addr_len(addr);
6465
6466 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6467 /*
6468 * header form
6469 * long header, fixed bit to 0 for Version Negotiation
6470 */
6471 /* TODO: RAND_bytes() should be replaced? */
6472 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
6473 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
6474 goto out;
6475 }
6476
6477 buf[i++] |= '\x80';
6478 /* null version for Version Negotiation */
6479 buf[i++] = '\x00';
6480 buf[i++] = '\x00';
6481 buf[i++] = '\x00';
6482 buf[i++] = '\x00';
6483
6484 /* source connection id */
6485 buf[i++] = pkt->scid.len;
6486 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6487 i += pkt->scid.len;
6488
6489 /* destination connection id */
6490 buf[i++] = pkt->dcid.len;
6491 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
6492 i += pkt->dcid.len;
6493
6494 /* supported version */
6495 for (j = 0; j < quic_versions_nb; j++) {
6496 version = htonl(quic_versions[j].num);
6497 memcpy(&buf[i], &version, sizeof(version));
6498 i += sizeof(version);
6499 }
6500
6501 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
6502 goto out;
6503
6504 ret = 1;
6505 out:
6506 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6507 return !ret;
6508}
6509
6510/* Send a stateless reset packet depending on <pkt> RX packet information
6511 * from <fd> UDP socket to <dst>
6512 * Return 1 if succeeded, 0 if not.
6513 */
6514static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
6515 struct quic_rx_packet *rxpkt)
6516{
6517 int ret = 0, pktlen, rndlen;
6518 unsigned char pkt[64];
6519 const socklen_t addrlen = get_addr_len(dstaddr);
6520 struct proxy *prx;
6521 struct quic_counters *prx_counters;
6522
6523 TRACE_ENTER(QUIC_EV_STATELESS_RST);
6524
Amaury Denoyelle53b7b8e2024-05-22 15:55:58 +02006525 /* RFC 9000 10.3. Stateless Reset
6526 *
6527 * Endpoints MUST discard packets that are too small to be valid QUIC
6528 * packets. To give an example, with the set of AEAD functions defined
6529 * in [QUIC-TLS], short header packets that are smaller than 21 bytes
6530 * are never valid.
6531 *
6532 * [...]
6533 *
6534 * RFC 9000 10.3.3. Looping
6535 *
6536 * An endpoint MUST ensure that every Stateless Reset that it sends is
6537 * smaller than the packet that triggered it, unless it maintains state
6538 * sufficient to prevent looping. In the event of a loop, this results
6539 * in packets eventually being too small to trigger a response.
6540 */
6541 if (rxpkt->len <= QUIC_STATELESS_RESET_PACKET_MINLEN) {
6542 TRACE_DEVEL("rxpkt too short", QUIC_EV_STATELESS_RST);
6543 goto leave;
6544 }
6545
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006546 prx = l->bind_conf->frontend;
6547 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Amaury Denoyelle53b7b8e2024-05-22 15:55:58 +02006548
6549 /* RFC 9000 10.3. Stateless Reset
6550 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006551 * An endpoint that sends a Stateless Reset in response to a packet that is
6552 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
6553 * than the packet it responds to.
6554 */
Amaury Denoyelle53b7b8e2024-05-22 15:55:58 +02006555 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 :
6556 QUIC_STATELESS_RESET_PACKET_MINLEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006557 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
6558
6559 /* Put a header of random bytes */
6560 /* TODO: RAND_bytes() should be replaced */
6561 if (RAND_bytes(pkt, rndlen) != 1) {
6562 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
6563 goto leave;
6564 }
6565
6566 /* Clear the most significant bit, and set the second one */
6567 *pkt = (*pkt & ~0x80) | 0x40;
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02006568 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006569 rxpkt->dcid.data, rxpkt->dcid.len))
6570 goto leave;
6571
6572 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
6573 goto leave;
6574
6575 ret = 1;
6576 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
6577 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
6578 leave:
6579 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
6580 return ret;
6581}
6582
6583/* QUIC server only function.
6584 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
6585 * This is the responsibility of the caller to check <aad> size is big enough
6586 * to contain these data.
6587 * Return the number of bytes copied to <aad>.
6588 */
6589static int quic_generate_retry_token_aad(unsigned char *aad,
6590 uint32_t version,
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006591 const struct quic_cid *cid,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006592 const struct sockaddr_storage *addr)
6593{
6594 unsigned char *p;
6595
6596 p = aad;
Willy Tarreaub4cf4ba2024-04-05 23:54:17 +02006597 write_u32(p, htonl(version));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006598 p += sizeof version;
6599 p += quic_saddr_cpy(p, addr);
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006600 memcpy(p, cid->data, cid->len);
6601 p += cid->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006602
6603 return p - aad;
6604}
6605
6606/* QUIC server only function.
6607 * Generate the token to be used in Retry packets. The token is written to
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006608 * <token> with <len> as length. <odcid> is the original destination connection
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006609 * ID and <dcid> is our side destination connection ID (or client source
6610 * connection ID).
6611 * Returns the length of the encoded token or 0 on error.
6612 */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006613static int quic_generate_retry_token(unsigned char *token, size_t len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006614 const uint32_t version,
6615 const struct quic_cid *odcid,
6616 const struct quic_cid *dcid,
6617 struct sockaddr_storage *addr)
6618{
6619 int ret = 0;
6620 unsigned char *p;
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006621 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
6622 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006623 size_t aadlen;
6624 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
6625 unsigned char key[QUIC_TLS_KEY_LEN];
6626 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaille0499db42023-09-07 18:43:52 +02006627 const unsigned char *sec = global.cluster_secret;
6628 size_t seclen = sizeof global.cluster_secret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006629 EVP_CIPHER_CTX *ctx = NULL;
6630 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Emeric Brun7875f122023-07-11 16:13:19 +02006631 uint32_t timestamp = (uint32_t)date.tv_sec;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006632
6633 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6634
Frédéric Lécaille6d6ddb22023-05-15 17:40:00 +02006635 /* The token is made of the token format byte, the ODCID prefixed by its one byte
6636 * length, the creation timestamp, an AEAD TAG, and finally
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006637 * the random bytes used to derive the secret to encrypt the token.
6638 */
Frédéric Lécaille6d6ddb22023-05-15 17:40:00 +02006639 if (1 + odcid->len + 1 + sizeof(timestamp) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN > len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006640 goto err;
6641
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006642 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006643 /* TODO: RAND_bytes() should be replaced */
6644 if (RAND_bytes(salt, sizeof salt) != 1) {
6645 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
6646 goto err;
6647 }
6648
6649 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6650 salt, sizeof salt, sec, seclen)) {
6651 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
6652 goto err;
6653 }
6654
6655 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
6656 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
6657 goto err;
6658 }
6659
6660 /* Token build */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006661 p = token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006662 *p++ = QUIC_TOKEN_FMT_RETRY,
6663 *p++ = odcid->len;
6664 memcpy(p, odcid->data, odcid->len);
6665 p += odcid->len;
6666 write_u32(p, htonl(timestamp));
6667 p += sizeof timestamp;
6668
6669 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Emeric Brune0190c62023-07-11 14:53:41 +02006670 if (!quic_tls_encrypt(token + 1, p - token - 1, aad, aadlen, ctx, aead, iv)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006671 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
6672 goto err;
6673 }
6674
6675 p += QUIC_TLS_TAG_LEN;
6676 memcpy(p, salt, sizeof salt);
6677 p += sizeof salt;
6678 EVP_CIPHER_CTX_free(ctx);
6679
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006680 ret = p - token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006681 leave:
6682 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6683 return ret;
6684
6685 err:
6686 if (ctx)
6687 EVP_CIPHER_CTX_free(ctx);
6688 goto leave;
6689}
6690
6691/* QUIC server only function.
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006692 *
6693 * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
6694 * the UDP datagram containing <pkt> and <l> is the listener instance on which
6695 * it was received. If the token is valid, the ODCID of <qc> QUIC connection
6696 * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
6697 * to validate the token but it can be NULL : in this case the version will be
6698 * retrieved from the packet.
6699 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006700 * Return 1 if succeeded, 0 if not.
6701 */
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006702
6703static int quic_retry_token_check(struct quic_rx_packet *pkt,
6704 struct quic_dgram *dgram,
6705 struct listener *l,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006706 struct quic_conn *qc,
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006707 struct quic_cid *odcid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006708{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006709 struct proxy *prx;
6710 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006711 int ret = 0;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006712 unsigned char *token = pkt->token;
6713 const uint64_t tokenlen = pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006714 unsigned char buf[128];
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006715 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
6716 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006717 size_t aadlen;
6718 const unsigned char *salt;
6719 unsigned char key[QUIC_TLS_KEY_LEN];
6720 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaille0499db42023-09-07 18:43:52 +02006721 const unsigned char *sec = global.cluster_secret;
6722 size_t seclen = sizeof global.cluster_secret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006723 EVP_CIPHER_CTX *ctx = NULL;
6724 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006725 const struct quic_version *qv = qc ? qc->original_version :
6726 pkt->version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006727
6728 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6729
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006730 /* The caller must ensure this. */
Frédéric Lécaille0499db42023-09-07 18:43:52 +02006731 BUG_ON(!pkt->token_len);
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006732
6733 prx = l->bind_conf->frontend;
6734 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6735
6736 if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
6737 /* TODO: New token check */
6738 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6739 goto leave;
6740 }
6741
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006742 if (sizeof buf < tokenlen) {
6743 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
6744 goto err;
6745 }
6746
Frédéric Lécaille35b63962023-05-15 18:11:21 +02006747 /* The token is made of the token format byte, the ODCID prefixed by its one byte
6748 * length, the creation timestamp, an AEAD TAG, and finally
6749 * the random bytes used to derive the secret to encrypt the token.
6750 */
6751 if (tokenlen < 2 + QUIC_ODCID_MINLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN ||
6752 tokenlen > 2 + QUIC_CID_MAXLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN) {
6753 TRACE_ERROR("invalid token length", QUIC_EV_CONN_LPKT, qc);
6754 goto err;
6755 }
6756
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006757 aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006758 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
6759 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6760 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
6761 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
6762 goto err;
6763 }
6764
6765 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
6766 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
6767 goto err;
6768 }
6769
Frédéric Lécaille35b63962023-05-15 18:11:21 +02006770 /* The token is prefixed by a one-byte length format which is not ciphered. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006771 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
6772 ctx, aead, key, iv)) {
6773 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
6774 goto err;
6775 }
6776
6777 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
6778 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
6779 goto err;
6780 }
6781
6782 EVP_CIPHER_CTX_free(ctx);
6783
6784 ret = 1;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006785 HA_ATOMIC_INC(&prx_counters->retry_validated);
6786
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006787 leave:
6788 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6789 return ret;
6790
6791 err:
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006792 HA_ATOMIC_INC(&prx_counters->retry_error);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006793 if (ctx)
6794 EVP_CIPHER_CTX_free(ctx);
6795 goto leave;
6796}
6797
6798/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
6799 * the Initial <pkt> packet.
6800 *
6801 * Returns 0 on success else non-zero.
6802 */
6803static int send_retry(int fd, struct sockaddr_storage *addr,
6804 struct quic_rx_packet *pkt, const struct quic_version *qv)
6805{
6806 int ret = 0;
6807 unsigned char buf[128];
6808 int i = 0, token_len;
6809 const socklen_t addrlen = get_addr_len(addr);
6810 struct quic_cid scid;
6811
6812 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6813
Frédéric Lécaille2b220542023-06-30 12:17:36 +02006814 /* long header(1) | fixed bit(1) | packet type QUIC_PACKET_TYPE_RETRY(2) | unused random bits(4)*/
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006815 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
Frédéric Lécaille2b220542023-06-30 12:17:36 +02006816 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT) |
6817 statistical_prng_range(16);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006818 /* version */
Emeric Brun2b6d45e2023-07-17 18:33:44 +02006819 write_n32(&buf[i], qv->num);
Frédéric Lécaille966e4682023-06-30 14:41:31 +02006820 i += sizeof(uint32_t);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006821
6822 /* Use the SCID from <pkt> for Retry DCID. */
6823 buf[i++] = pkt->scid.len;
6824 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6825 i += pkt->scid.len;
6826
6827 /* Generate a new CID to be used as SCID for the Retry packet. */
6828 scid.len = QUIC_HAP_CID_LEN;
6829 /* TODO: RAND_bytes() should be replaced */
6830 if (RAND_bytes(scid.data, scid.len) != 1) {
6831 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
6832 goto out;
6833 }
6834
6835 buf[i++] = scid.len;
6836 memcpy(&buf[i], scid.data, scid.len);
6837 i += scid.len;
6838
6839 /* token */
6840 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006841 &pkt->dcid, &pkt->scid, addr))) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006842 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
6843 goto out;
6844 }
6845
6846 i += token_len;
6847
6848 /* token integrity tag */
Emeric Bruna47f5cd2023-06-27 15:24:05 +02006849 if ((sizeof(buf) - i < QUIC_TLS_TAG_LEN) ||
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006850 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
6851 pkt->dcid.len, buf, i, qv)) {
6852 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6853 goto out;
6854 }
6855
6856 i += QUIC_TLS_TAG_LEN;
6857
6858 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
6859 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6860 goto out;
6861 }
6862
6863 ret = 1;
6864 out:
6865 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6866 return !ret;
6867}
6868
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006869/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
6870 * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
6871 * is used.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006872 *
6873 * Returns the instance or NULL if not found.
6874 */
6875static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
6876 struct listener *l,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006877 struct sockaddr_storage *saddr,
6878 int *new_tid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006879{
6880 struct quic_conn *qc = NULL;
6881 struct ebmb_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006882 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006883 struct quic_cid_tree *tree;
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006884 uint conn_id_tid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006885
6886 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006887 *new_tid = -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006888
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006889 /* First look into DCID tree. */
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006890 tree = &quic_cid_trees[_quic_cid_tree_idx(pkt->dcid.data)];
6891 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6892 node = ebmb_lookup(&tree->root, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006893
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006894 /* If not found on an Initial/0-RTT packet, it could be because an
6895 * ODCID is reused by the client. Calculate the derived CID value to
6896 * retrieve it from the DCID tree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006897 */
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006898 if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
6899 pkt->type == QUIC_PACKET_TYPE_0RTT)) {
Amaury Denoyellec2a92642023-04-13 15:26:18 +02006900 const struct quic_cid derive_cid = quic_derive_cid(&pkt->dcid, saddr);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006901
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006902 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
6903
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006904 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
6905 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6906 node = ebmb_lookup(&tree->root, derive_cid.data, derive_cid.len);
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006907 }
6908
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006909 if (!node)
6910 goto end;
6911
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006912 conn_id = ebmb_entry(node, struct quic_connection_id, node);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006913 conn_id_tid = HA_ATOMIC_LOAD(&conn_id->tid);
6914 if (conn_id_tid != tid) {
6915 *new_tid = conn_id_tid;
6916 goto end;
6917 }
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006918 qc = conn_id->qc;
Amaury Denoyelle806c5c52024-06-27 18:52:23 +02006919 TRACE_DEVEL("found connection", QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006920
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006921 end:
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006922 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle806c5c52024-06-27 18:52:23 +02006923 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006924 return qc;
6925}
6926
6927/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
6928 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
6929 * parameters of this session.
6930 * This is the responsibility of the caller to check the validity of all the
6931 * pointers passed as parameter to this function.
6932 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
6933 * CO_ER_SSL_NO_MEM.
6934 */
6935static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
6936 unsigned char *params, size_t params_len)
6937{
6938 int retry, ret = -1;
6939
6940 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6941
6942 retry = 1;
6943 retry:
6944 *ssl = SSL_new(ssl_ctx);
6945 if (!*ssl) {
6946 if (!retry--)
Frédéric Lécaillee4185272023-06-02 16:56:16 +02006947 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006948
6949 pool_gc(NULL);
6950 goto retry;
6951 }
6952
Frédéric Lécailleca87a622023-06-02 17:00:04 +02006953 if (!SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
6954 !SSL_set_quic_method(*ssl, &ha_quic_method)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006955 SSL_free(*ssl);
6956 *ssl = NULL;
6957 if (!retry--)
Frédéric Lécaillee4185272023-06-02 16:56:16 +02006958 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006959
6960 pool_gc(NULL);
6961 goto retry;
6962 }
6963
6964 ret = 0;
6965 leave:
6966 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6967 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006968}
6969
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006970/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
6971 * used to process <qc> received packets. The allocated context is stored in
6972 * <qc.xprt_ctx>.
6973 *
6974 * Returns 0 on success else non-zero.
6975 */
6976static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
6977{
6978 int ret = 0;
6979 struct bind_conf *bc = qc->li->bind_conf;
6980 struct ssl_sock_ctx *ctx = NULL;
6981
6982 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6983
6984 ctx = pool_zalloc(pool_head_quic_conn_ctx);
6985 if (!ctx) {
6986 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
6987 goto err;
6988 }
6989
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006990 ctx->subs = NULL;
6991 ctx->xprt_ctx = NULL;
6992 ctx->qc = qc;
6993
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006994 if (qc_is_listener(qc)) {
6995 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
6996 qc->enc_params, qc->enc_params_len) == -1) {
6997 goto err;
6998 }
6999#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Frédéric Lécaille65a8b9a2023-06-02 17:05:38 +02007000#ifndef USE_QUIC_OPENSSL_COMPAT
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007001 /* Enabling 0-RTT */
7002 if (bc->ssl_conf.early_data)
7003 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
7004#endif
Frédéric Lécaille65a8b9a2023-06-02 17:05:38 +02007005#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007006
7007 SSL_set_accept_state(ctx->ssl);
7008 }
7009
7010 ctx->xprt = xprt_get(XPRT_QUIC);
7011
7012 /* Store the allocated context in <qc>. */
7013 qc->xprt_ctx = ctx;
7014
Amaury Denoyelleaff66bb2023-10-25 15:38:50 +02007015 /* global.sslconns is already incremented on INITIAL packet parsing. */
7016 _HA_ATOMIC_INC(&global.totalsslconns);
7017
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007018 ret = 1;
7019 leave:
7020 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
7021 return !ret;
7022
7023 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007024 pool_free(pool_head_quic_conn_ctx, ctx);
7025 goto leave;
7026}
7027
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007028/* Check that all the bytes between <pos> included and <end> address
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007029 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007030 * check that there is at least one byte between <pos> end <end>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007031 * Return 1 if this all the bytes are null, 0 if not.
7032 */
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007033static inline int quic_padding_check(const unsigned char *pos,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007034 const unsigned char *end)
7035{
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007036 while (pos < end && !*pos)
7037 pos++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007038
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007039 return pos == end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007040}
7041
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007042/* Find the associated connection to the packet <pkt> or create a new one if
7043 * this is an Initial packet. <dgram> is the datagram containing the packet and
7044 * <l> is the listener instance on which it was received.
7045 *
Amaury Denoyelle25174d52023-04-05 17:52:05 +02007046 * By default, <new_tid> is set to -1. However, if thread affinity has been
7047 * chanbed, it will be set to its new thread ID.
7048 *
7049 * Returns the quic-conn instance or NULL if not found or thread affinity
7050 * changed.
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007051 */
7052static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
7053 struct quic_dgram *dgram,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007054 struct listener *l,
7055 int *new_tid)
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007056{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02007057 struct quic_cid token_odcid = { .len = 0 };
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007058 struct quic_conn *qc = NULL;
7059 struct proxy *prx;
7060 struct quic_counters *prx_counters;
7061
7062 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7063
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007064 *new_tid = -1;
7065
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007066 prx = l->bind_conf->frontend;
7067 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
7068
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007069 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr, new_tid);
7070
Amaury Denoyelle25174d52023-04-05 17:52:05 +02007071 /* If connection already created or rebinded on another thread. */
Frédéric Lécailleab3aa0f2023-05-24 09:06:06 +02007072 if (!qc && *new_tid != -1 && tid != *new_tid)
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007073 goto out;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007074
7075 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7076 BUG_ON(!pkt->version); /* This must not happen. */
7077
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007078 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007079 struct quic_cid_tree *tree;
7080 struct ebmb_node *node;
7081 struct quic_connection_id *conn_id;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007082 int ipv4;
7083
Amaury Denoyelle6b240e42023-11-09 16:44:50 +01007084 if (pkt->token_len) {
7085 /* Validate the token only when connection is unknown. */
7086 if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
7087 goto err;
7088 }
7089 else if (!(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
7090 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007091 TRACE_PROTO("Initial without token, sending retry",
7092 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7093 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
7094 TRACE_ERROR("Error during Retry generation",
7095 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7096 goto out;
7097 }
7098
7099 HA_ATOMIC_INC(&prx_counters->retry_sent);
7100 goto out;
7101 }
7102
7103 /* RFC 9000 7.2. Negotiating Connection IDs:
7104 * When an Initial packet is sent by a client that has not previously
7105 * received an Initial or Retry packet from the server, the client
7106 * populates the Destination Connection ID field with an unpredictable
7107 * value. This Destination Connection ID MUST be at least 8 bytes in length.
7108 */
7109 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
7110 TRACE_PROTO("dropped packet",
7111 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7112 goto err;
7113 }
7114
7115 pkt->saddr = dgram->saddr;
7116 ipv4 = dgram->saddr.ss_family == AF_INET;
7117
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007118 /* Generate the first connection CID. This is derived from the client
7119 * ODCID and address. This allows to retrieve the connection from the
7120 * ODCID without storing it in the CID tree. This is an interesting
7121 * optimization as the client is expected to stop using its ODCID in
7122 * favor of our generated value.
7123 */
7124 conn_id = new_quic_cid(NULL, NULL, &pkt->dcid, &pkt->saddr);
7125 if (!conn_id)
7126 goto err;
7127
Frédéric Lécaillefd212a72023-06-16 16:10:58 +02007128 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
7129 conn_id, &dgram->daddr, &pkt->saddr, 1,
7130 !!pkt->token_len, l);
7131 if (qc == NULL) {
Frédéric Lécaillefd212a72023-06-16 16:10:58 +02007132 pool_free(pool_head_quic_connection_id, conn_id);
7133 goto err;
7134 }
7135
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007136 tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)];
7137 HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock);
7138 node = ebmb_insert(&tree->root, &conn_id->node, conn_id->cid.len);
7139 if (node != &conn_id->node) {
7140 pool_free(pool_head_quic_connection_id, conn_id);
7141
7142 conn_id = ebmb_entry(node, struct quic_connection_id, node);
7143 *new_tid = HA_ATOMIC_LOAD(&conn_id->tid);
Frédéric Lécaillefd212a72023-06-16 16:10:58 +02007144 quic_conn_release(qc);
7145 qc = NULL;
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007146 }
Frédéric Lécaille8a586582023-06-26 10:39:56 +02007147 else {
7148 /* From here, <qc> is the correct connection for this <pkt> Initial
7149 * packet. <conn_id> must be inserted in the CIDs tree for this
7150 * connection.
7151 */
7152 eb64_insert(&qc->cids, &conn_id->seq_num);
7153 /* Initialize the next CID sequence number to be used for this connection. */
7154 qc->next_cid_seq_num = 1;
7155 }
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007156 HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock);
7157
7158 if (*new_tid != -1)
7159 goto out;
7160
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007161 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007162 }
7163 }
7164 else if (!qc) {
Amaury Denoyelle53b7b8e2024-05-22 15:55:58 +02007165 /* Stateless Reset sent even for Long header packets as haproxy
7166 * emits stateless_reset_token in its TPs.
7167 */
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007168 TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Frédéric Lécaille0499db42023-09-07 18:43:52 +02007169 if (!send_stateless_reset(l, &dgram->saddr, pkt))
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007170 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
7171 goto err;
7172 }
7173
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007174 out:
7175 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7176 return qc;
7177
7178 err:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007179 if (qc)
7180 qc->cntrs.dropped_pkt++;
7181 else
7182 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle331b8b12023-10-25 10:52:23 +02007183
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007184 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7185 return NULL;
7186}
7187
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007188/* Parse a QUIC packet starting at <pos>. Data won't be read after <end> even
Amaury Denoyelle98289692022-10-19 15:37:44 +02007189 * if the packet is incomplete. This function will populate fields of <pkt>
7190 * instance, most notably its length. <dgram> is the UDP datagram which
7191 * contains the parsed packet. <l> is the listener instance on which it was
7192 * received.
7193 *
7194 * Returns 0 on success else non-zero. Packet length is guaranteed to be set to
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007195 * the real packet value or to cover all data between <pos> and <end> : this is
Amaury Denoyelle98289692022-10-19 15:37:44 +02007196 * useful to reject a whole datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007197 */
Amaury Denoyelle98289692022-10-19 15:37:44 +02007198static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007199 unsigned char *pos, const unsigned char *end,
Amaury Denoyelle98289692022-10-19 15:37:44 +02007200 struct quic_dgram *dgram, struct listener *l)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007201{
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007202 const unsigned char *beg = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007203 struct proxy *prx;
7204 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007205
7206 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7207
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007208 prx = l->bind_conf->frontend;
7209 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
7210 /* This ist only to please to traces and distinguish the
7211 * packet with parsed packet number from others.
7212 */
7213 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007214 if (end <= pos) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007215 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7216 goto drop;
7217 }
7218
7219 /* Fixed bit */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007220 if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007221 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007222 quic_padding_check(pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007223 /* Some browsers may pad the remaining datagram space with null bytes.
7224 * That is what we called add padding out of QUIC packets. Such
7225 * datagrams must be considered as valid. But we can only consume
7226 * the remaining space.
7227 */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007228 pkt->len = end - pos;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007229 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007230 }
7231
7232 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7233 goto drop;
7234 }
7235
7236 /* Header form */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007237 if (!qc_parse_hd_form(pkt, &pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007238 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7239 goto drop;
7240 }
7241
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007242 if (pkt->type != QUIC_PACKET_TYPE_SHORT) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007243 uint64_t len;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007244 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007245
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007246 if (!quic_packet_read_long_header(&pos, end, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007247 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7248 goto drop;
7249 }
7250
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007251 /* When multiple QUIC packets are coalesced on the same UDP datagram,
7252 * they must have the same DCID.
7253 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007254 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007255 (pkt->dcid.len != dgram->dcid_len ||
7256 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007257 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007258 goto drop;
7259 }
7260
7261 /* Retry of Version Negotiation packets are only sent by servers */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007262 if (pkt->type == QUIC_PACKET_TYPE_RETRY ||
7263 (pkt->version && !pkt->version->num)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007264 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7265 goto drop;
7266 }
7267
7268 /* RFC9000 6. Version Negotiation */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007269 if (!pkt->version) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007270 /* unsupported version, send Negotiation packet */
7271 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
7272 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007273 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007274 }
7275
7276 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007277 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007278 }
7279
7280 /* For Initial packets, and for servers (QUIC clients connections),
7281 * there is no Initial connection IDs storage.
7282 */
7283 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7284 uint64_t token_len;
7285
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007286 if (!quic_dec_int(&token_len, (const unsigned char **)&pos, end) ||
7287 end - pos < token_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007288 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007289 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007290 goto drop;
7291 }
7292
7293 /* TODO Retry should be automatically activated if
7294 * suspect network usage is detected.
7295 */
Frédéric Lécaille0499db42023-09-07 18:43:52 +02007296 if (!token_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007297 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
7298 TRACE_PROTO("Initial without token, sending retry",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007299 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7300 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007301 TRACE_PROTO("Error during Retry generation",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007302 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007303 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007304 }
7305
7306 HA_ATOMIC_INC(&prx_counters->retry_sent);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007307 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007308 }
7309 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007310
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007311 pkt->token = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007312 pkt->token_len = token_len;
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007313 pos += pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007314 }
7315 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
7316 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
7317 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007318 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007319 goto drop;
7320 }
7321 }
7322
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007323 if (!quic_dec_int(&len, (const unsigned char **)&pos, end) ||
7324 end - pos < len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007325 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007326 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007327 goto drop;
7328 }
7329
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007330 /* Packet Number is stored here. Packet Length totalizes the
7331 * rest of the content.
7332 */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007333 pkt->pn_offset = pos - beg;
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007334 pkt->len = pkt->pn_offset + len;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007335
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007336 /* RFC 9000. Initial Datagram Size
7337 *
7338 * A server MUST discard an Initial packet that is carried in a UDP datagram
7339 * with a payload that is smaller than the smallest allowed maximum datagram
7340 * size of 1200 bytes.
7341 */
7342 if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
7343 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007344 TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007345 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
7346 goto drop;
7347 }
7348
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007349 /* Interrupt parsing after packet length retrieval : this
7350 * ensures that only the packet is dropped but not the whole
7351 * datagram.
7352 */
7353 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007354 TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007355 goto drop;
7356 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007357 }
7358 else {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007359 TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007360 if (end - pos < QUIC_HAP_CID_LEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007361 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007362 goto drop;
7363 }
7364
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007365 memcpy(pkt->dcid.data, pos, QUIC_HAP_CID_LEN);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007366 pkt->dcid.len = QUIC_HAP_CID_LEN;
7367
7368 /* When multiple QUIC packets are coalesced on the same UDP datagram,
7369 * they must have the same DCID.
7370 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007371 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007372 (pkt->dcid.len != dgram->dcid_len ||
7373 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007374 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007375 goto drop;
7376 }
7377
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007378 pos += QUIC_HAP_CID_LEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007379
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007380 pkt->pn_offset = pos - beg;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007381 /* A short packet is the last one of a UDP datagram. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007382 pkt->len = end - beg;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007383 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007384
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007385 TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007386 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007387 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007388
Amaury Denoyelle98289692022-10-19 15:37:44 +02007389 drop:
7390 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007391 drop_silent:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007392 if (!pkt->len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007393 pkt->len = end - beg;
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007394 TRACE_PROTO("RX pkt parsing failed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007395 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007396 return -1;
7397}
7398
7399/* Check if received packet <pkt> should be drop due to <qc> already in closing
7400 * state. This can be true if a CONNECTION_CLOSE has already been emitted for
7401 * this connection.
7402 *
7403 * Returns false if connection is not in closing state else true. The caller
7404 * should drop the whole datagram in the last case to not mess up <qc>
7405 * CONNECTION_CLOSE rate limit counter.
7406 */
7407static int qc_rx_check_closing(struct quic_conn *qc,
7408 struct quic_rx_packet *pkt)
7409{
7410 if (!(qc->flags & QUIC_FL_CONN_CLOSING))
7411 return 0;
7412
7413 TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
7414
7415 /* Check if CONNECTION_CLOSE rate reemission is reached. */
7416 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
7417 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
7418 qc->nb_pkt_for_cc++;
7419 qc->nb_pkt_since_cc = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007420 }
7421
Amaury Denoyelle98289692022-10-19 15:37:44 +02007422 return 1;
7423}
7424
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007425/* React to a connection migration initiated on <qc> by a client with the new
7426 * path addresses <peer_addr>/<local_addr>.
7427 *
7428 * Returns 0 on success else non-zero.
7429 */
7430static int qc_handle_conn_migration(struct quic_conn *qc,
7431 const struct sockaddr_storage *peer_addr,
7432 const struct sockaddr_storage *local_addr)
7433{
7434 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7435
Frédéric Lécaille6fc86972023-01-12 08:29:23 +01007436 /* RFC 9000. Connection Migration
7437 *
7438 * If the peer sent the disable_active_migration transport parameter,
7439 * an endpoint also MUST NOT send packets (including probing packets;
7440 * see Section 9.1) from a different local address to the address the peer
7441 * used during the handshake, unless the endpoint has acted on a
7442 * preferred_address transport parameter from the peer.
7443 */
7444 if (qc->li->bind_conf->quic_params.disable_active_migration) {
7445 TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
7446 goto err;
7447 }
7448
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007449 /* RFC 9000 9. Connection Migration
7450 *
Amaury Denoyelleeb6be982022-11-21 11:14:45 +01007451 * The design of QUIC relies on endpoints retaining a stable address for
7452 * the duration of the handshake. An endpoint MUST NOT initiate
7453 * connection migration before the handshake is confirmed, as defined in
7454 * Section 4.1.2 of [QUIC-TLS].
7455 */
7456 if (qc->state < QUIC_HS_ST_COMPLETE) {
7457 TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
7458 goto err;
7459 }
7460
7461 /* RFC 9000 9. Connection Migration
7462 *
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007463 * TODO
7464 * An endpoint MUST
7465 * perform path validation (Section 8.2) if it detects any change to a
7466 * peer's address, unless it has previously validated that address.
7467 */
7468
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007469 /* Update quic-conn owned socket if in used.
7470 * TODO try to reuse it instead of closing and opening a new one.
7471 */
7472 if (qc_test_fd(qc)) {
7473 /* TODO try to reuse socket instead of closing it and opening a new one. */
7474 TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
7475 qc_release_fd(qc, 1);
Amaury Denoyellefb375572023-02-01 09:28:32 +01007476 /* TODO need to adjust <jobs> on socket allocation failure. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007477 qc_alloc_fd(qc, local_addr, peer_addr);
7478 }
7479
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007480 qc->local_addr = *local_addr;
7481 qc->peer_addr = *peer_addr;
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007482 qc->cntrs.conn_migration_done++;
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007483
7484 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7485 return 0;
7486
7487 err:
7488 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7489 return 1;
7490}
7491
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007492/* Release the memory for the RX packets which are no more referenced
7493 * and consume their payloads which have been copied to the RX buffer
7494 * for the connection.
7495 * Always succeeds.
7496 */
7497static inline void quic_rx_pkts_del(struct quic_conn *qc)
7498{
7499 struct quic_rx_packet *pkt, *pktback;
7500
7501 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
7502 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007503 "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007504 (long long)pkt->pn_node.key,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007505 pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007506 (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
7507 if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
7508 size_t cdata;
7509
7510 cdata = b_contig_data(&qc->rx.buf, 0);
7511 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007512 "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007513 if (cdata && !*b_head(&qc->rx.buf)) {
7514 /* Consume the remaining data */
7515 b_del(&qc->rx.buf, cdata);
7516 }
7517 break;
7518 }
7519
7520 if (pkt->refcnt)
7521 break;
7522
7523 b_del(&qc->rx.buf, pkt->raw_len);
7524 LIST_DELETE(&pkt->qc_rx_pkt_list);
7525 pool_free(pool_head_quic_rx_packet, pkt);
7526 }
7527
7528 /* In frequent cases the buffer will be emptied at this stage. */
7529 b_realign_if_empty(&qc->rx.buf);
7530}
7531
Amaury Denoyelle98289692022-10-19 15:37:44 +02007532/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
7533 * into <qc> receive buffer after header protection removal procedure.
7534 *
7535 * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
7536 * must point to packet buffer first byte.
7537 *
7538 * <tasklist_head> may be non-NULL when the caller treat several datagrams for
7539 * different quic-conn. In this case, each quic-conn tasklet will be appended
7540 * to it in order to be woken up after the current task.
7541 *
7542 * The caller can safely removed the packet data. If packet refcount was not
7543 * incremented by this function, it means that the connection did not handled
7544 * it and it should be freed by the caller.
7545 */
7546static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
7547 struct quic_dgram *dgram, unsigned char *beg,
7548 struct list **tasklist_head)
7549{
7550 const struct quic_version *qv = pkt->version;
7551 struct quic_enc_level *qel = NULL;
7552 size_t b_cspace;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007553
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007554 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7555 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Amaury Denoyelle3f474e62022-11-24 17:15:08 +01007556
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007557 if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007558 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
7559 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
7560 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007561 TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
7562 QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007563 /* Reset the anti-amplification bit. It will be set again
7564 * when sending the next packet if reached again.
7565 */
7566 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007567 qc_set_timer(qc);
7568 if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
7569 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007570 }
7571
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007572 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
7573 TRACE_PROTO("Connection error",
7574 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7575 goto out;
7576 }
7577
7578 pkt->raw_len = pkt->len;
7579 quic_rx_pkts_del(qc);
7580 b_cspace = b_contig_space(&qc->rx.buf);
7581 if (b_cspace < pkt->len) {
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007582 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007583 "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007584 /* Do not consume buf if space not at the end. */
7585 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
7586 TRACE_PROTO("Packet dropped",
7587 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007588 qc->cntrs.dropped_pkt_bufoverrun++;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007589 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007590 }
7591
7592 /* Let us consume the remaining contiguous space. */
7593 if (b_cspace) {
7594 b_putchr(&qc->rx.buf, 0x00);
7595 b_cspace--;
7596 }
7597 b_add(&qc->rx.buf, b_cspace);
7598 if (b_contig_space(&qc->rx.buf) < pkt->len) {
7599 TRACE_PROTO("Too big packet",
7600 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007601 qc->cntrs.dropped_pkt_bufoverrun++;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007602 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007603 }
7604 }
7605
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007606 if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007607 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7608 goto drop;
7609 }
7610
7611 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7612 if (pkt->aad_len)
7613 qc_pkt_insert(qc, pkt, qel);
7614 out:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02007615 *tasklist_head = tasklet_wakeup_after(*tasklist_head,
7616 qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007617
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007618 drop_silent:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007619 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7620 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007621 return;
7622
7623 drop:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007624 qc->cntrs.dropped_pkt++;
Frédéric Lécaille464281a2023-05-24 10:24:42 +02007625 TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7626 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007627}
7628
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007629/* This function builds into a buffer at <pos> position a QUIC long packet header,
7630 * <end> being one byte past the end of this buffer.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007631 * Return 1 if enough room to build this header, 0 if not.
7632 */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007633static int quic_build_packet_long_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007634 int type, size_t pn_len,
7635 struct quic_conn *qc, const struct quic_version *ver)
7636{
7637 int ret = 0;
7638
7639 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7640
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007641 if (end - *pos < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007642 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7643 goto leave;
7644 }
7645
7646 type = quic_pkt_type(type, ver->num);
7647 /* #0 byte flags */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007648 *(*pos)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007649 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
7650 /* Version */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007651 quic_write_uint32(pos, end, ver->num);
7652 *(*pos)++ = qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007653 /* Destination connection ID */
7654 if (qc->dcid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007655 memcpy(*pos, qc->dcid.data, qc->dcid.len);
7656 *pos += qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007657 }
7658 /* Source connection ID */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007659 *(*pos)++ = qc->scid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007660 if (qc->scid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007661 memcpy(*pos, qc->scid.data, qc->scid.len);
7662 *pos += qc->scid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007663 }
7664
7665 ret = 1;
7666 leave:
7667 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7668 return ret;
7669}
7670
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007671/* This function builds into a buffer at <pos> position a QUIC short packet header,
7672 * <end> being one byte past the end of this buffer.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007673 * Return 1 if enough room to build this header, 0 if not.
7674 */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007675static int quic_build_packet_short_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007676 size_t pn_len, struct quic_conn *qc,
7677 unsigned char tls_flags)
7678{
7679 int ret = 0;
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007680 unsigned char spin_bit =
7681 (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007682
7683 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7684
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007685 if (end - *pos < 1 + qc->dcid.len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007686 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7687 goto leave;
7688 }
7689
7690 /* #0 byte flags */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007691 *(*pos)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007692 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
7693 /* Destination connection ID */
7694 if (qc->dcid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007695 memcpy(*pos, qc->dcid.data, qc->dcid.len);
7696 *pos += qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007697 }
7698
7699 ret = 1;
7700 leave:
7701 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7702 return ret;
7703}
7704
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007705/* Apply QUIC header protection to the packet with <pos> as first byte address,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007706 * <pn> as address of the Packet number field, <pnlen> being this field length
7707 * with <aead> as AEAD cipher and <key> as secret key.
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007708 *
7709 * TODO no error is expected as encryption is done in place but encryption
7710 * manual is unclear. <fail> will be set to true if an error is detected.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007711 */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007712void quic_apply_header_protection(struct quic_conn *qc, unsigned char *pos,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007713 unsigned char *pn, size_t pnlen,
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007714 struct quic_tls_ctx *tls_ctx, int *fail)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007715
7716{
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007717 int i;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007718 /* We need an IV of at least 5 bytes: one byte for bytes #0
7719 * and at most 4 bytes for the packet number
7720 */
7721 unsigned char mask[5] = {0};
7722 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
7723
7724 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7725
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007726 *fail = 0;
7727
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007728 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
7729 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007730 *fail = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007731 goto out;
7732 }
7733
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007734 *pos ^= mask[0] & (*pos & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007735 for (i = 0; i < pnlen; i++)
7736 pn[i] ^= mask[i + 1];
7737
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007738 out:
7739 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007740}
7741
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007742/* Prepare into <outlist> as most as possible ack-eliciting frame from their
7743 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
7744 * with <room> as available room, and <*len> the packet Length field initialized
7745 * with the number of bytes already present in this buffer which must be taken
7746 * into an account for the Length packet field value. <headlen> is the number of
7747 * bytes already present in this packet before building frames.
7748 *
7749 * Update consequently <*len> to reflect the size of these frames built
7750 * by this function. Also attach these frames to <l> frame list.
7751 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
7752 */
7753static inline int qc_build_frms(struct list *outlist, struct list *inlist,
7754 size_t room, size_t *len, size_t headlen,
7755 struct quic_enc_level *qel,
7756 struct quic_conn *qc)
7757{
7758 int ret;
7759 struct quic_frame *cf, *cfbak;
7760
7761 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
7762
7763 ret = 0;
7764 if (*len > room)
7765 goto leave;
Amaury Denoyelle8c94dfc2024-06-05 11:37:44 +02007766 room -= *len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007767
7768 /* If we are not probing we must take into an account the congestion
7769 * control window.
7770 */
7771 if (!qel->pktns->tx.pto_probe) {
7772 size_t remain = quic_path_prep_data(qc->path);
7773
7774 if (headlen > remain)
7775 goto leave;
7776
7777 room = QUIC_MIN(room, remain - headlen);
7778 }
7779
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007780 TRACE_PROTO("TX frms build (headlen)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007781 QUIC_EV_CONN_BCFRMS, qc, &headlen);
7782
7783 /* NOTE: switch/case block inside a loop, a successful status must be
7784 * returned by this function only if at least one frame could be built
7785 * in the switch/case block.
7786 */
7787 list_for_each_entry_safe(cf, cfbak, inlist, list) {
7788 /* header length, data length, frame length. */
7789 size_t hlen, dlen, dlen_sz, avail_room, flen;
7790
7791 if (!room)
7792 break;
7793
7794 switch (cf->type) {
7795 case QUIC_FT_CRYPTO:
7796 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
7797 QUIC_EV_CONN_BCFRMS, qc, &room, len);
7798 /* Compute the length of this CRYPTO frame header */
7799 hlen = 1 + quic_int_getsize(cf->crypto.offset);
Amaury Denoyelle8c94dfc2024-06-05 11:37:44 +02007800 /* Compute the data length of this CRYPTO frame. */
7801 dlen = max_stream_data_size(room, hlen, cf->crypto.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007802 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
7803 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
7804 if (!dlen)
7805 continue;
7806
7807 /* CRYPTO frame length. */
7808 flen = hlen + quic_int_getsize(dlen) + dlen;
7809 TRACE_DEVEL(" CRYPTO frame length (flen)",
7810 QUIC_EV_CONN_BCFRMS, qc, &flen);
7811 /* Add the CRYPTO data length and its encoded length to the packet
7812 * length and the length of this length.
7813 */
7814 *len += flen;
7815 room -= flen;
7816 if (dlen == cf->crypto.len) {
7817 /* <cf> CRYPTO data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007818 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007819 LIST_APPEND(outlist, &cf->list);
7820 }
7821 else {
7822 struct quic_frame *new_cf;
7823
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007824 new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007825 if (!new_cf) {
7826 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
7827 continue;
7828 }
7829
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007830 new_cf->crypto.len = dlen;
7831 new_cf->crypto.offset = cf->crypto.offset;
7832 new_cf->crypto.qel = qel;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007833 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007834 if (cf->origin) {
7835 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7836 /* This <cf> frame was duplicated */
7837 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7838 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007839 /* Detach the remaining CRYPTO frame from its original frame */
7840 LIST_DEL_INIT(&cf->ref);
7841 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007842 }
7843 LIST_APPEND(outlist, &new_cf->list);
7844 /* Consume <dlen> bytes of the current frame. */
7845 cf->crypto.len -= dlen;
7846 cf->crypto.offset += dlen;
7847 }
7848 break;
7849
7850 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007851 if (cf->stream.dup) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007852 struct eb64_node *node = NULL;
7853 struct qc_stream_desc *stream_desc = NULL;
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007854 struct qf_stream *strm_frm = &cf->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007855
7856 /* As this frame has been already lost, ensure the stream is always
7857 * available or the range of this frame is not consumed before
7858 * resending it.
7859 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007860 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007861 if (!node) {
7862 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007863 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007864 continue;
7865 }
7866
7867 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007868 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007869 TRACE_DEVEL("ignored frame frame in already acked range",
7870 QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007871 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007872 continue;
7873 }
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007874 else if (strm_frm->offset.key < stream_desc->ack_offset) {
7875 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
Frédéric Lécailleca079792023-03-17 08:56:50 +01007876
Frédéric Lécaillec425e032023-03-20 14:32:59 +01007877 qc_stream_frm_mv_fwd(cf, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007878 TRACE_DEVEL("updated partially acked frame",
7879 QUIC_EV_CONN_PRSAFRM, qc, cf);
7880 }
7881 }
7882 /* Note that these frames are accepted in short packets only without
7883 * "Length" packet field. Here, <*len> is used only to compute the
7884 * sum of the lengths of the already built frames for this packet.
7885 *
7886 * Compute the length of this STREAM frame "header" made a all the field
7887 * excepting the variable ones. Note that +1 is for the type of this frame.
7888 */
7889 hlen = 1 + quic_int_getsize(cf->stream.id) +
7890 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
7891 /* Compute the data length of this STREAM frame. */
Amaury Denoyelle8c94dfc2024-06-05 11:37:44 +02007892 avail_room = room - hlen;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007893 if ((ssize_t)avail_room <= 0)
7894 continue;
7895
7896 TRACE_DEVEL(" New STREAM frame build (room, len)",
7897 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007898
7899 /* hlen contains STREAM id and offset. Ensure there is
7900 * enough room for length field.
7901 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007902 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007903 dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
7904 cf->stream.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007905 dlen_sz = quic_int_getsize(dlen);
7906 flen = hlen + dlen_sz + dlen;
7907 }
7908 else {
7909 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
7910 flen = hlen + dlen;
7911 }
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007912
7913 if (cf->stream.len && !dlen) {
7914 /* Only a small gap is left on buffer, not
7915 * enough to encode the STREAM data length.
7916 */
7917 continue;
7918 }
7919
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007920 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
7921 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
7922 TRACE_DEVEL(" STREAM frame length (flen)",
7923 QUIC_EV_CONN_BCFRMS, qc, &flen);
7924 /* Add the STREAM data length and its encoded length to the packet
7925 * length and the length of this length.
7926 */
7927 *len += flen;
7928 room -= flen;
7929 if (dlen == cf->stream.len) {
7930 /* <cf> STREAM data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007931 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007932 LIST_APPEND(outlist, &cf->list);
7933
7934 /* Do not notify MUX on retransmission. */
7935 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7936 qcc_streams_sent_done(cf->stream.stream->ctx,
7937 cf->stream.len,
7938 cf->stream.offset.key);
7939 }
7940 }
7941 else {
7942 struct quic_frame *new_cf;
7943 struct buffer cf_buf;
7944
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007945 new_cf = qc_frm_alloc(cf->type);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007946 if (!new_cf) {
7947 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
7948 continue;
7949 }
7950
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007951 new_cf->stream.stream = cf->stream.stream;
7952 new_cf->stream.buf = cf->stream.buf;
7953 new_cf->stream.id = cf->stream.id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01007954 new_cf->stream.offset = cf->stream.offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007955 new_cf->stream.len = dlen;
7956 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
7957 /* FIN bit reset */
7958 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
7959 new_cf->stream.data = cf->stream.data;
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007960 new_cf->stream.dup = cf->stream.dup;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007961 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007962 if (cf->origin) {
7963 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7964 /* This <cf> frame was duplicated */
7965 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7966 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007967 /* Detach this STREAM frame from its origin */
7968 LIST_DEL_INIT(&cf->ref);
7969 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007970 }
7971 LIST_APPEND(outlist, &new_cf->list);
7972 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
7973 /* Consume <dlen> bytes of the current frame. */
7974 cf_buf = b_make(b_orig(cf->stream.buf),
7975 b_size(cf->stream.buf),
7976 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
7977 cf->stream.len -= dlen;
7978 cf->stream.offset.key += dlen;
7979 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
7980
7981 /* Do not notify MUX on retransmission. */
7982 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7983 qcc_streams_sent_done(new_cf->stream.stream->ctx,
7984 new_cf->stream.len,
7985 new_cf->stream.offset.key);
7986 }
7987 }
7988
7989 /* TODO the MUX is notified about the frame sending via
7990 * previous qcc_streams_sent_done call. However, the
7991 * sending can fail later, for example if the sendto
7992 * system call returns an error. As the MUX has been
7993 * notified, the transport layer is responsible to
7994 * bufferize and resent the announced data later.
7995 */
7996
7997 break;
7998
7999 default:
8000 flen = qc_frm_len(cf);
8001 BUG_ON(!flen);
8002 if (flen > room)
8003 continue;
8004
8005 *len += flen;
8006 room -= flen;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01008007 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008008 LIST_APPEND(outlist, &cf->list);
8009 break;
8010 }
8011
8012 /* Successful status as soon as a frame could be built */
8013 ret = 1;
8014 }
8015
8016 leave:
8017 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
8018 return ret;
8019}
8020
8021/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
8022 * is used as return parameter and should be zero'ed by the caller.
8023 */
8024static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
8025 struct quic_frame *out)
8026{
8027 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
8028 *
8029 * A CONNECTION_CLOSE frame should be sent in several packets with
8030 * different encryption levels depending on the client context. This is
8031 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
8032 * more details on how to implement it.
8033 */
8034 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
8035
8036
8037 if (qc->err.app) {
8038 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
8039 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
8040 /* RFC 9000 10.2.3. Immediate Close during the Handshake
8041 *
8042 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
8043 * packet could expose application state or be used to alter application
8044 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
8045 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
8046 * Handshake packets. Otherwise, information about the application
8047 * state might be revealed. Endpoints MUST clear the value of the
8048 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
8049 * converting to a CONNECTION_CLOSE of type 0x1c.
8050 */
8051 out->type = QUIC_FT_CONNECTION_CLOSE;
8052 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
8053 out->connection_close.reason_phrase_len = 0;
8054 }
8055 else {
8056 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
Amaury Denoyelle1d40fc92023-11-28 11:23:41 +01008057 out->connection_close_app.error_code = qc->err.code;
8058 out->connection_close_app.reason_phrase_len = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008059 }
8060 }
8061 else {
8062 out->type = QUIC_FT_CONNECTION_CLOSE;
8063 out->connection_close.error_code = qc->err.code;
8064 }
8065 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
8066
8067}
8068
8069/* This function builds a clear packet from <pkt> information (its type)
8070 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
8071 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
8072 * filling the buffer with as much frames as possible from <frms> list of
8073 * prebuilt frames.
8074 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
8075 * reserved so that to ensure there is enough room to build this AEAD TAG after
8076 * having returned from this function.
8077 * This function also updates the value of <buf_pn> pointer to point to the packet
8078 * number field in this packet. <pn_len> will also have the packet number
8079 * length as value.
8080 *
8081 * Return 1 if succeeded (enough room to buile this packet), O if not.
8082 */
8083static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
8084 size_t dglen, struct quic_tx_packet *pkt,
8085 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008086 int must_ack, int padding, int cc, int probe,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008087 struct quic_enc_level *qel, struct quic_conn *qc,
8088 const struct quic_version *ver, struct list *frms)
8089{
8090 unsigned char *beg, *payload;
8091 size_t len, len_sz, len_frms, padding_len;
8092 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
8093 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
8094 struct quic_frame cc_frm = { };
8095 size_t ack_frm_len, head_len;
8096 int64_t rx_largest_acked_pn;
8097 int add_ping_frm;
8098 struct list frm_list = LIST_HEAD_INIT(frm_list);
8099 struct quic_frame *cf;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008100 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008101
8102 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
8103
8104 /* Length field value with CRYPTO frames if present. */
8105 len_frms = 0;
8106 beg = pos;
8107 /* When not probing, and no immediate close is required, reduce the size of this
8108 * buffer to respect the congestion controller window.
8109 * This size will be limited if we have ack-eliciting frames to send from <frms>.
8110 */
8111 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
8112 size_t path_room;
8113
8114 path_room = quic_path_prep_data(qc->path);
8115 if (end - beg > path_room)
8116 end = beg + path_room;
8117 }
8118
8119 /* Ensure there is enough room for the TLS encryption tag and a zero token
8120 * length field if any.
8121 */
8122 if (end - pos < QUIC_TLS_TAG_LEN +
8123 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
8124 goto no_room;
8125
8126 end -= QUIC_TLS_TAG_LEN;
8127 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
8128 /* packet number length */
8129 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
8130 /* Build the header */
8131 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
8132 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
8133 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
8134 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
8135 goto no_room;
8136
8137 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02008138 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
8139 if (end <= pos)
8140 goto no_room;
8141
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008142 *pos++ = 0;
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02008143 }
8144
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008145 head_len = pos - beg;
8146 /* Build an ACK frame if required. */
8147 ack_frm_len = 0;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008148 /* Do not ack and probe at the same time. */
8149 if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008150 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
8151 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
8152 ack_frm.tx_ack.arngs = arngs;
8153 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
8154 qel->pktns->tx.ack_delay =
8155 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
8156 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
8157 }
8158 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
8159 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
8160 * smallest frame (PING) and <*pn_len> more for the packet number. Note
8161 * that from here, we do not know if we will have to send a PING frame.
8162 * This will be decided after having computed the ack-eliciting frames
8163 * to be added to this packet.
8164 */
Frédéric Lécaille9d68c6a2023-04-12 20:49:29 +02008165 if (end - pos <= 1 + *pn_len)
8166 goto no_room;
8167
Frédéric Lécaille4b2627b2023-04-17 13:42:42 +02008168 ack_frm_len = qc_frm_len(&ack_frm);
8169 if (ack_frm_len > end - 1 - *pn_len - pos)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008170 goto no_room;
8171 }
8172
8173 /* Length field value without the ack-eliciting frames. */
8174 len = ack_frm_len + *pn_len;
8175 len_frms = 0;
8176 if (!cc && !LIST_ISEMPTY(frms)) {
8177 ssize_t room = end - pos;
8178
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008179 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008180 /* Initialize the length of the frames built below to <len>.
8181 * If any frame could be successfully built by qc_build_frms(),
8182 * we will have len_frms > len.
8183 */
8184 len_frms = len;
8185 if (!qc_build_frms(&frm_list, frms,
8186 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008187 TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008188 qc, NULL, NULL, &room);
Frédéric Lécaille30cc6652023-11-07 18:29:28 +01008189 if (padding) {
8190 len_frms = 0;
8191 goto comp_pkt_len;
8192 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008193 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
8194 goto no_room;
8195 }
8196 }
8197
Frédéric Lécaille30cc6652023-11-07 18:29:28 +01008198comp_pkt_len:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008199 /* Length (of the remaining data). Must not fail because, the buffer size
8200 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
8201 * for the encryption tag. It must be taken into an account for the length
8202 * of this packet.
8203 */
8204 if (len_frms)
8205 len = len_frms + QUIC_TLS_TAG_LEN;
8206 else
8207 len += QUIC_TLS_TAG_LEN;
8208 /* CONNECTION_CLOSE frame */
8209 if (cc) {
8210 qc_build_cc_frm(qc, qel, &cc_frm);
8211 len += qc_frm_len(&cc_frm);
8212 }
8213 add_ping_frm = 0;
8214 padding_len = 0;
8215 len_sz = quic_int_getsize(len);
8216 /* Add this packet size to <dglen> */
8217 dglen += head_len + len_sz + len;
Frédéric Lécailleec937212023-03-03 17:34:41 +01008218 /* Note that <padding> is true only when building an Handshake packet
8219 * coalesced to an Initial packet.
8220 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008221 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
8222 /* This is a maximum padding size */
8223 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
8224 /* The length field value is of this packet is <len> + <padding_len>
8225 * the size of which may be greater than the initial computed size
8226 * <len_sz>. So, let's deduce the difference between these to packet
8227 * sizes from <padding_len>.
8228 */
8229 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
8230 len += padding_len;
8231 }
Frédéric Lécaille5faf5772023-02-16 17:30:53 +01008232 else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
8233 len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
8234 }
8235 else if (LIST_ISEMPTY(&frm_list)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008236 if (qel->pktns->tx.pto_probe) {
8237 /* If we cannot send a frame, we send a PING frame. */
8238 add_ping_frm = 1;
8239 len += 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01008240 dglen += 1;
8241 /* Note that only we are in the case where this Initial packet
8242 * is not coalesced to an Handshake packet. We must directly
8243 * pad the datragram.
8244 */
Frédéric Lécaille9c317b12023-03-28 15:39:11 +02008245 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
8246 if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
8247 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
8248 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
8249 len += padding_len;
8250 }
8251 }
8252 else {
8253 /* Note that +1 is for the PING frame */
8254 if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
8255 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01008256 }
8257 }
8258 else {
8259 /* If there is no frame at all to follow, add at least a PADDING frame. */
8260 if (!ack_frm_len && !cc)
8261 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008262 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008263 }
8264
8265 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
8266 goto no_room;
8267
8268 /* Packet number field address. */
8269 *buf_pn = pos;
8270
8271 /* Packet number encoding. */
8272 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
8273 goto no_room;
8274
8275 /* payload building (ack-eliciting or not frames) */
8276 payload = pos;
8277 if (ack_frm_len) {
8278 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
8279 goto no_room;
8280
8281 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
8282 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
8283 }
8284
8285 /* Ack-eliciting frames */
8286 if (!LIST_ISEMPTY(&frm_list)) {
8287 struct quic_frame *tmp_cf;
8288 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
8289 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
8290 ssize_t room = end - pos;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008291 TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008292 qc, NULL, NULL, &room);
8293 /* Note that <cf> was added from <frms> to <frm_list> list by
8294 * qc_build_frms().
8295 */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01008296 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008297 LIST_INSERT(frms, &cf->list);
8298 continue;
8299 }
8300
8301 quic_tx_packet_refinc(pkt);
8302 cf->pkt = pkt;
8303 }
8304 }
8305
8306 /* Build a PING frame if needed. */
8307 if (add_ping_frm) {
8308 frm.type = QUIC_FT_PING;
8309 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
8310 goto no_room;
8311 }
8312
8313 /* Build a CONNECTION_CLOSE frame if needed. */
8314 if (cc) {
8315 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
8316 goto no_room;
8317
8318 pkt->flags |= QUIC_FL_TX_PACKET_CC;
8319 }
8320
8321 /* Build a PADDING frame if needed. */
8322 if (padding_len) {
8323 frm.type = QUIC_FT_PADDING;
8324 frm.padding.len = padding_len;
8325 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
8326 goto no_room;
8327 }
8328
8329 if (pos == payload) {
8330 /* No payload was built because of congestion control */
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008331 TRACE_PROTO("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008332 goto no_room;
8333 }
8334
8335 /* If this packet is ack-eliciting and we are probing let's
8336 * decrement the PTO probe counter.
8337 */
Frédéric Lécaille0e7f9da2023-07-20 15:45:41 +02008338 if ((pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008339 qel->pktns->tx.pto_probe)
8340 qel->pktns->tx.pto_probe--;
8341
8342 pkt->len = pos - beg;
8343 LIST_SPLICE(&pkt->frms, &frm_list);
8344
8345 ret = 1;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008346 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008347 leave:
8348 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
8349 return ret;
8350
8351 no_room:
8352 /* Replace the pre-built frames which could not be add to this packet */
8353 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008354 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008355 goto leave;
8356}
8357
8358static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
8359{
8360 pkt->type = type;
8361 pkt->len = 0;
8362 pkt->in_flight_len = 0;
8363 pkt->pn_node.key = (uint64_t)-1;
8364 LIST_INIT(&pkt->frms);
8365 pkt->time_sent = TICK_ETERNITY;
8366 pkt->next = NULL;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01008367 pkt->prev = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008368 pkt->largest_acked_pn = -1;
8369 pkt->flags = 0;
8370 pkt->refcnt = 0;
8371}
8372
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008373/* Build a packet into a buffer at <pos> position, <end> pointing to one byte past
8374 * the end of this buffer, with <pkt_type> as packet type for <qc> QUIC connection
8375 * at <qel> encryption level with <frms> list of prebuilt frames.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008376 *
Frédéric Lécaille21017002023-11-08 11:31:21 +01008377 + * Return -3 if the packet could not be allocated, -2 if could not be encrypted for
8378 + * any reason, -1 if there was not enough room to build a packet.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008379 * XXX NOTE XXX
8380 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
8381 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
8382 * control window limitation.
8383 */
8384static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008385 const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008386 struct quic_enc_level *qel,
8387 struct quic_tls_ctx *tls_ctx, struct list *frms,
8388 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008389 size_t dglen, int pkt_type, int must_ack,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008390 int padding, int probe, int cc, int *err)
8391{
8392 struct quic_tx_packet *ret_pkt = NULL;
8393 /* The pointer to the packet number field. */
8394 unsigned char *buf_pn;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008395 unsigned char *first_byte, *last_byte, *payload;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008396 int64_t pn;
8397 size_t pn_len, payload_len, aad_len;
8398 struct quic_tx_packet *pkt;
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008399 int encrypt_failure = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008400
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008401 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
8402 TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008403 *err = 0;
8404 pkt = pool_alloc(pool_head_quic_tx_packet);
8405 if (!pkt) {
8406 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille21017002023-11-08 11:31:21 +01008407 *err = -3;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008408 goto err;
8409 }
8410
8411 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008412 first_byte = *pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008413 pn_len = 0;
8414 buf_pn = NULL;
8415
8416 pn = qel->pktns->tx.next_pn + 1;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008417 if (!qc_do_build_pkt(*pos, end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008418 must_ack, padding, cc, probe, qel, qc, ver, frms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008419 // trace already emitted by function above
8420 *err = -1;
8421 goto err;
8422 }
8423
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008424 last_byte = first_byte + pkt->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008425 payload = buf_pn + pn_len;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008426 payload_len = last_byte - payload;
8427 aad_len = payload - first_byte;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008428
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008429 quic_packet_encrypt(payload, payload_len, first_byte, aad_len, pn, tls_ctx, qc, &encrypt_failure);
8430 if (encrypt_failure) {
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02008431 /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008432 WARN_ON("quic_packet_encrypt failure");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008433 *err = -2;
8434 goto err;
8435 }
8436
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008437 last_byte += QUIC_TLS_TAG_LEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008438 pkt->len += QUIC_TLS_TAG_LEN;
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008439 quic_apply_header_protection(qc, first_byte, buf_pn, pn_len, tls_ctx, &encrypt_failure);
8440 if (encrypt_failure) {
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02008441 /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008442 WARN_ON("quic_apply_header_protection failure");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008443 *err = -2;
8444 goto err;
8445 }
8446
8447 /* Consume a packet number */
8448 qel->pktns->tx.next_pn++;
8449 qc->tx.prep_bytes += pkt->len;
8450 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
8451 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
8452 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
8453 }
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008454
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008455 /* Now that a correct packet is built, let us consume <*pos> buffer. */
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008456 *pos = last_byte;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008457 /* Attach the built packet to its tree. */
8458 pkt->pn_node.key = pn;
8459 /* Set the packet in fligth length for in flight packet only. */
8460 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
8461 pkt->in_flight_len = pkt->len;
8462 qc->path->prep_in_flight += pkt->len;
8463 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01008464 /* Always reset this flag */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008465 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
8466 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
8467 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
8468 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
Frédéric Lécailled7215712023-03-24 18:13:37 +01008469 qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
8470 if (tick_isset(qc->ack_expire)) {
8471 qc->ack_expire = TICK_ETERNITY;
8472 qc->idle_timer_task->expire = qc->idle_expire;
8473 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02008474 TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01008475 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008476 }
8477
8478 pkt->pktns = qel->pktns;
8479
8480 ret_pkt = pkt;
8481 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008482 TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
8483 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008484 return ret_pkt;
8485
8486 err:
8487 /* TODO: what about the frames which have been built
8488 * for this packet.
8489 */
8490 free_quic_tx_packet(qc, pkt);
8491 goto leave;
8492}
8493
8494
8495static void __quic_conn_init(void)
8496{
8497 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
8498}
8499INITCALL0(STG_REGISTER, __quic_conn_init);
8500
8501static void __quic_conn_deinit(void)
8502{
8503 BIO_meth_free(ha_quic_meth);
8504}
8505REGISTER_POST_DEINIT(__quic_conn_deinit);
8506
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008507/* Handle a new <dgram> received. Parse each QUIC packets and copied their
8508 * content to a quic-conn instance. The datagram content can be released after
8509 * this function.
8510 *
8511 * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
8512 * to the connection instance. <li> is the attached listener. The caller is
8513 * responsible to ensure that the first packet is destined to this connection
8514 * by comparing CIDs.
8515 *
8516 * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
8517 * function will thus retrieve the connection from the CID tree or allocate a
8518 * new one if possible. <li> is the listener attached to the receiver.
8519 *
8520 * Returns 0 on success else non-zero. If an error happens, some packets from
8521 * the datagram may not have been parsed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008522 */
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008523int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
8524 struct listener *li)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008525{
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008526 struct quic_rx_packet *pkt;
8527 struct quic_conn *qc = NULL;
8528 unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008529 struct list *tasklist_head = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008530
8531 TRACE_ENTER(QUIC_EV_CONN_LPKT);
8532
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008533 pos = dgram->buf;
8534 end = pos + dgram->len;
8535 do {
8536 /* TODO replace zalloc -> alloc. */
8537 pkt = pool_zalloc(pool_head_quic_rx_packet);
8538 if (!pkt) {
8539 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
8540 goto err;
8541 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008542
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008543 pkt->version = NULL;
8544 pkt->pn_offset = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008545
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008546 /* Set flag if pkt is the first one in dgram. */
8547 if (pos == dgram->buf)
8548 pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008549
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008550 LIST_INIT(&pkt->qc_rx_pkt_list);
8551 pkt->time_received = now_ms;
8552 quic_rx_packet_refinc(pkt);
8553 if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
8554 goto next;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008555
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008556 /* Search quic-conn instance for first packet of the datagram.
8557 * quic_rx_packet_parse() is responsible to discard packets
8558 * with different DCID as the first one in the same datagram.
8559 */
8560 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008561 int new_tid = -1;
8562
8563 qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li, &new_tid);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008564 /* qc is NULL if receiving a non Initial packet for an
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008565 * unknown connection or on connection affinity rebind.
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008566 */
8567 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008568 if (new_tid >= 0) {
8569 MT_LIST_APPEND(&quic_dghdlrs[new_tid].dgrams,
8570 &dgram->handler_list);
8571 tasklet_wakeup(quic_dghdlrs[new_tid].task);
Frédéric Lécaille4f401132023-11-22 16:29:08 +01008572 pool_free(pool_head_quic_rx_packet, pkt);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008573 goto out;
8574 }
8575
Amaury Denoyelle98289692022-10-19 15:37:44 +02008576 /* Skip the entire datagram. */
8577 pkt->len = end - pos;
8578 goto next;
8579 }
8580
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008581 dgram->qc = qc;
8582 }
Amaury Denoyelle98289692022-10-19 15:37:44 +02008583
Amaury Denoyelle8fc267b2023-11-20 14:56:49 +01008584 /* Ensure thread connection migration is finalized ASAP. */
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008585 if (qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED)
8586 qc_finalize_affinity_rebind(qc);
8587
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008588 if (qc_rx_check_closing(qc, pkt)) {
8589 /* Skip the entire datagram. */
8590 pkt->len = end - pos;
8591 goto next;
8592 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008593
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008594 /* Detect QUIC connection migration. */
Frédéric Lécaillef6769542023-01-12 10:36:26 +01008595 if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008596 if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
8597 /* Skip the entire datagram. */
8598 TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
8599 pkt->len = end - pos;
8600 goto next;
8601 }
8602 }
8603
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008604 qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008605
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008606 next:
8607 pos += pkt->len;
8608 quic_rx_packet_refdec(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008609
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008610 /* Free rejected packets */
8611 if (!pkt->refcnt) {
8612 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
8613 pool_free(pool_head_quic_rx_packet, pkt);
8614 }
8615 } while (pos < end);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008616
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008617 /* Increasing the received bytes counter by the UDP datagram length
8618 * if this datagram could be associated to a connection.
8619 */
8620 if (dgram->qc)
8621 dgram->qc->rx.bytes += dgram->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008622
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008623 /* This must never happen. */
8624 BUG_ON(pos > end);
8625 BUG_ON(pos < end || pos > dgram->buf + dgram->len);
8626 /* Mark this datagram as consumed */
8627 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008628
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008629 out:
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008630 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
8631 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008632
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008633 err:
Amaury Denoyellea65dd3a2023-04-19 14:26:16 +02008634 /* Mark this datagram as consumed as maybe at least some packets were parsed. */
8635 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008636 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008637 return -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008638}
8639
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008640/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
8641 * CIDs. This can be used to determine if a datagram is addressed to the right
8642 * connection instance.
8643 *
8644 * Returns a boolean value.
8645 */
8646int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len)
8647{
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008648 const uchar idx = _quic_cid_tree_idx(dcid);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008649 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008650 struct ebmb_node *node = NULL;
8651 struct quic_cid_tree *tree = &quic_cid_trees[idx];
Willy Tarreau92033572024-06-30 06:23:30 +02008652 int ret;
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008653
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008654 /* Test against our default CID or client ODCID. */
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008655 if ((qc->scid.len == dcid_len &&
8656 memcmp(qc->scid.data, dcid, dcid_len) == 0) ||
8657 (qc->odcid.len == dcid_len &&
Frédéric Lécaille07846cb2023-02-13 16:14:24 +01008658 memcmp(qc->odcid.data, dcid, dcid_len) == 0)) {
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008659 return 1;
8660 }
8661
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008662 /* Test against our other CIDs. This can happen if the client has
8663 * decided to switch to a new one.
8664 *
8665 * TODO to avoid locking, loop through qc.cids as an alternative.
8666 *
8667 * TODO set it to our default CID to avoid this operation next time.
8668 */
Willy Tarreau92033572024-06-30 06:23:30 +02008669 ret = 0;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008670 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
8671 node = ebmb_lookup(&tree->root, dcid, dcid_len);
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008672 if (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008673 conn_id = ebmb_entry(node, struct quic_connection_id, node);
8674 if (qc == conn_id->qc)
Willy Tarreau92033572024-06-30 06:23:30 +02008675 ret = 1;
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008676 }
Amaury Denoyelle60938b72024-06-27 18:15:08 +02008677 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008678
Willy Tarreau92033572024-06-30 06:23:30 +02008679 return ret;
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008680}
8681
Willy Tarreaudd9f9212023-05-07 07:07:44 +02008682/* Retrieve the DCID from a QUIC datagram or packet at <pos> position,
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008683 * <end> being at one byte past the end of this datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008684 * Returns 1 if succeeded, 0 if not.
8685 */
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008686int quic_get_dgram_dcid(unsigned char *pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008687 unsigned char **dcid, size_t *dcid_len)
8688{
8689 int ret = 0, long_header;
8690 size_t minlen, skip;
8691
8692 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
8693
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008694 if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008695 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
8696 goto err;
8697 }
8698
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008699 long_header = *pos & QUIC_PACKET_LONG_HEADER_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008700 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
8701 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
8702 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008703 if (end - pos < minlen)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008704 goto err;
8705
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008706 pos += skip;
8707 *dcid_len = long_header ? *pos++ : QUIC_HAP_CID_LEN;
8708 if (*dcid_len > QUIC_CID_MAXLEN || end - pos <= *dcid_len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008709 goto err;
8710
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008711 *dcid = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008712
8713 ret = 1;
8714 leave:
8715 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
8716 return ret;
8717
8718 err:
8719 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
8720 goto leave;
8721}
8722
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008723/* Notify upper layer of a fatal error which forces to close the connection. */
8724void qc_notify_err(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008725{
8726 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
8727
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008728 if (qc->mux_state == QC_MUX_READY) {
8729 TRACE_STATE("error notified to mux", QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008730
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008731 /* Mark socket as closed. */
8732 qc->conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
8733
8734 /* TODO quic-conn layer must stay active until MUX is released.
8735 * Thus, we have to wake up directly to ensure upper stream
8736 * layer will be notified of the error. If a proper separation
8737 * is made between MUX and quic-conn layer, wake up could be
8738 * conducted only with qc.subs.
8739 */
8740 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008741 }
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008742
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008743 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
8744}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008745
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008746/* Wake-up upper layer for sending if all conditions are met :
8747 * - room in congestion window or probe packet to sent
8748 * - socket FD ready to sent or listener socket used
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008749 *
8750 * Returns 1 if upper layer has been woken up else 0.
8751 */
8752int qc_notify_send(struct quic_conn *qc)
8753{
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008754 const struct quic_pktns *pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8755
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008756 if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008757 /* RFC 9002 7.5. Probe Timeout
8758 *
8759 * Probe packets MUST NOT be blocked by the congestion controller.
8760 */
8761 if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
Amaury Denoyellecaa16542023-02-28 15:11:26 +01008762 (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008763 tasklet_wakeup(qc->subs->tasklet);
8764 qc->subs->events &= ~SUB_RETRY_SEND;
8765 if (!qc->subs->events)
8766 qc->subs = NULL;
8767
8768 return 1;
8769 }
8770 }
8771
8772 return 0;
8773}
8774
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008775/* Move a <qc> QUIC connection and its resources from the current thread to the
Willy Tarreau77d37b02023-04-20 19:03:49 +02008776 * new one <new_tid> optionally in association with <new_li> (since it may need
8777 * to change when migrating to a thread from a different group, otherwise leave
8778 * it NULL). After this call, the connection cannot be dereferenced anymore on
8779 * the current thread.
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008780 *
8781 * Returns 0 on success else non-zero.
8782 */
Willy Tarreau77d37b02023-04-20 19:03:49 +02008783int qc_set_tid_affinity(struct quic_conn *qc, uint new_tid, struct listener *new_li)
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008784{
8785 struct task *t1 = NULL, *t2 = NULL;
8786 struct tasklet *t3 = NULL;
8787
8788 struct quic_connection_id *conn_id;
8789 struct eb64_node *node;
8790
8791 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8792
8793 /* Pre-allocate all required resources. This ensures we do not left a
8794 * connection with only some of its field rebinded.
8795 */
8796 if (((t1 = task_new_on(new_tid)) == NULL) ||
8797 (qc->timer_task && (t2 = task_new_on(new_tid)) == NULL) ||
8798 (t3 = tasklet_new()) == NULL) {
8799 goto err;
8800 }
8801
8802 /* Reinit idle timer task. */
8803 task_kill(qc->idle_timer_task);
8804 t1->expire = qc->idle_timer_task->expire;
8805 qc->idle_timer_task = t1;
8806 qc->idle_timer_task->process = qc_idle_timer_task;
8807 qc->idle_timer_task->context = qc;
8808
8809 /* Reinit timer task if allocated. */
8810 if (qc->timer_task) {
8811 task_kill(qc->timer_task);
8812 qc->timer_task = t2;
8813 qc->timer_task->process = qc_process_timer;
8814 qc->timer_task->context = qc;
8815 }
8816
8817 /* Reinit IO tasklet. */
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008818 if (qc->wait_event.tasklet->state & TASK_IN_LIST)
8819 qc->flags |= QUIC_FL_CONN_IO_TO_REQUEUE;
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008820 tasklet_kill(qc->wait_event.tasklet);
8821 /* In most cases quic_conn_app_io_cb is used but for 0-RTT quic_conn_io_cb can be still activated. */
8822 t3->process = qc->wait_event.tasklet->process;
8823 qc->wait_event.tasklet = t3;
8824 qc->wait_event.tasklet->tid = new_tid;
8825 qc->wait_event.tasklet->context = qc;
8826 qc->wait_event.events = 0;
8827
8828 /* Rebind the connection FD. */
8829 if (qc_test_fd(qc)) {
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008830 /* Reading is reactivated by the new thread. */
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008831 fd_migrate_on(qc->fd, new_tid);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008832 }
8833
Amaury Denoyelle7b516d32023-04-26 16:12:12 +02008834 /* Remove conn from per-thread list instance. It will be hidden from
8835 * "show quic" until rebinding is completed.
8836 */
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008837 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008838
8839 node = eb64_first(&qc->cids);
Amaury Denoyelleb75338e2024-03-04 18:41:39 +01008840 /* One and only one CID must be present before affinity rebind.
8841 *
8842 * This could be triggered fairly easily if tasklet is scheduled just
8843 * before thread migration for post-handshake state to generate new
8844 * CIDs. In this case, QUIC_FL_CONN_IO_TO_REQUEUE should be used
8845 * instead of tasklet_wakeup().
8846 */
8847 BUG_ON(!node || eb64_next(node));
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008848 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
Willy Tarreau77d37b02023-04-20 19:03:49 +02008849
8850 /* At this point no connection was accounted for yet on this
8851 * listener so it's OK to just swap the pointer.
8852 */
8853 if (new_li && new_li != qc->li)
8854 qc->li = new_li;
8855
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008856 /* Rebinding is considered done when CID points to the new thread. No
8857 * access should be done to quic-conn instance after it.
8858 */
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008859 qc->flags |= QUIC_FL_CONN_AFFINITY_CHANGED;
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008860 HA_ATOMIC_STORE(&conn_id->tid, new_tid);
8861 qc = NULL;
8862
8863 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, NULL);
8864 return 0;
8865
8866 err:
8867 task_destroy(t1);
8868 task_destroy(t2);
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02008869 tasklet_free(t3);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008870
8871 TRACE_DEVEL("leaving on error", QUIC_EV_CONN_SET_AFFINITY, qc);
8872 return 1;
8873}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008874
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008875/* Must be called after qc_set_tid_affinity() on the new thread. */
8876void qc_finalize_affinity_rebind(struct quic_conn *qc)
8877{
8878 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8879
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008880 /* This function must not be called twice after an affinity rebind. */
8881 BUG_ON(!(qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED));
8882 qc->flags &= ~QUIC_FL_CONN_AFFINITY_CHANGED;
8883
Amaury Denoyelle7b516d32023-04-26 16:12:12 +02008884 /* A connection must not pass to closing state until affinity rebind
8885 * is completed. Else quic_handle_stopping() may miss it during process
8886 * stopping cleanup.
8887 */
8888 BUG_ON(qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING));
8889
8890 /* Reinsert connection in ha_thread_ctx global list. */
8891 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
8892 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
8893
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008894 /* Reactivate FD polling if connection socket is active. */
8895 qc_want_recv(qc);
8896
8897 /* Reactivate timer task if needed. */
8898 qc_set_timer(qc);
8899
8900 /* Idle timer task is always active. */
8901 task_queue(qc->idle_timer_task);
8902
8903 /* Reactivate IO tasklet if needed. */
8904 if (qc->flags & QUIC_FL_CONN_IO_TO_REQUEUE) {
8905 tasklet_wakeup(qc->wait_event.tasklet);
8906 qc->flags &= ~QUIC_FL_CONN_IO_TO_REQUEUE;
8907 }
8908
8909 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, qc);
8910}
8911
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008912enum quic_dump_format {
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008913 QUIC_DUMP_FMT_ONELINE,
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008914 QUIC_DUMP_FMT_FULL,
8915};
8916
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008917/* appctx context used by "show quic" command */
8918struct show_quic_ctx {
8919 unsigned int epoch;
8920 struct bref bref; /* back-reference to the quic-conn being dumped */
8921 unsigned int thr;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008922 int flags;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008923 enum quic_dump_format format;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008924};
8925
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008926#define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */
8927
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008928static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private)
8929{
8930 struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008931 int argc = 2;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008932
8933 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
8934 return 1;
8935
8936 ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1);
8937 ctx->thr = 0;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008938 ctx->flags = 0;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008939 ctx->format = QUIC_DUMP_FMT_ONELINE;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008940
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008941 if (strcmp(args[argc], "oneline") == 0) {
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008942 /* format already used as default value */
8943 ++argc;
8944 }
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008945 else if (strcmp(args[argc], "full") == 0) {
8946 ctx->format = QUIC_DUMP_FMT_FULL;
8947 ++argc;
8948 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008949
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008950 while (*args[argc]) {
8951 if (strcmp(args[argc], "all") == 0)
8952 ctx->flags |= QC_CLI_FL_SHOW_ALL;
8953
8954 ++argc;
8955 }
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01008956
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008957 LIST_INIT(&ctx->bref.users);
8958
8959 return 0;
8960}
8961
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008962/* Dump for "show quic" with "oneline" format. */
8963static void dump_quic_oneline(struct show_quic_ctx *ctx, struct quic_conn *qc)
8964{
8965 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008966 int ret;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008967 unsigned char cid_len;
8968
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008969 ret = chunk_appendf(&trash, "%p[%02u]/%-.12s ", qc, ctx->thr,
8970 qc->li->bind_conf->frontend->id);
8971 chunk_appendf(&trash, "%*s", 36 - ret, " "); /* align output */
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008972
8973 /* State */
8974 if (qc->flags & QUIC_FL_CONN_CLOSING)
8975 chunk_appendf(&trash, "CLOSE ");
8976 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8977 chunk_appendf(&trash, "DRAIN ");
8978 else if (qc->state < QUIC_HS_ST_COMPLETE)
8979 chunk_appendf(&trash, "HDSHK ");
8980 else
8981 chunk_appendf(&trash, "ESTAB ");
8982
8983 /* Bytes in flight / Lost packets */
8984 chunk_appendf(&trash, "%9llu %6llu %6llu ",
8985 (ullong)qc->path->in_flight,
8986 (ullong)qc->path->ifae_pkts,
8987 (ullong)qc->path->loss.nb_lost_pkt);
8988
8989 /* Socket */
8990 if (qc->local_addr.ss_family == AF_INET ||
8991 qc->local_addr.ss_family == AF_INET6) {
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02008992 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8993 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
8994 chunk_appendf(&trash, "%15s:%-5s ", bufaddr, bufport);
8995
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008996 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8997 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008998 chunk_appendf(&trash, "%15s:%-5s ", bufaddr, bufport);
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008999
Amaury Denoyelle2273af12023-05-05 16:08:34 +02009000 }
9001
9002 /* CIDs */
9003 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
9004 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
9005
9006 chunk_appendf(&trash, " ");
9007 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
9008 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
9009
9010 chunk_appendf(&trash, "\n");
9011}
9012
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009013/* Dump for "show quic" with "full" format. */
9014static void dump_quic_full(struct show_quic_ctx *ctx, struct quic_conn *qc)
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009015{
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01009016 struct quic_pktns *pktns;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01009017 struct eb64_node *node;
9018 struct qc_stream_desc *stream;
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01009019 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02009020 int expire, i, addnl;
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01009021 unsigned char cid_len;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009022
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02009023 addnl = 0;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009024 /* CIDs */
9025 chunk_appendf(&trash, "* %p[%02u]: scid=", qc, ctx->thr);
9026 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
9027 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
9028 while (cid_len++ < 20)
9029 chunk_appendf(&trash, "..");
9030
9031 chunk_appendf(&trash, " dcid=");
9032 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
9033 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
9034 while (cid_len++ < 20)
9035 chunk_appendf(&trash, "..");
9036
9037 chunk_appendf(&trash, "\n");
9038
9039 chunk_appendf(&trash, " loc. TPs:");
9040 quic_transport_params_dump(&trash, qc, &qc->rx.params);
9041 chunk_appendf(&trash, "\n");
9042 chunk_appendf(&trash, " rem. TPs:");
9043 quic_transport_params_dump(&trash, qc, &qc->tx.params);
9044 chunk_appendf(&trash, "\n");
9045
9046 /* Connection state */
9047 if (qc->flags & QUIC_FL_CONN_CLOSING)
9048 chunk_appendf(&trash, " st=closing ");
9049 else if (qc->flags & QUIC_FL_CONN_DRAINING)
9050 chunk_appendf(&trash, " st=draining ");
9051 else if (qc->state < QUIC_HS_ST_CONFIRMED)
9052 chunk_appendf(&trash, " st=handshake ");
9053 else
9054 chunk_appendf(&trash, " st=opened ");
9055
9056 if (qc->mux_state == QC_MUX_NULL)
9057 chunk_appendf(&trash, "mux=null ");
9058 else if (qc->mux_state == QC_MUX_READY)
9059 chunk_appendf(&trash, "mux=ready ");
9060 else
9061 chunk_appendf(&trash, "mux=released ");
9062
9063 expire = qc->idle_expire;
9064 chunk_appendf(&trash, "expire=%02ds ",
9065 TICKS_TO_MS(tick_remain(now_ms, expire)) / 1000);
9066
9067 chunk_appendf(&trash, "\n");
9068
9069 /* Socket */
9070 chunk_appendf(&trash, " fd=%d", qc->fd);
9071 if (qc->local_addr.ss_family == AF_INET ||
9072 qc->local_addr.ss_family == AF_INET6) {
9073 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
9074 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02009075 chunk_appendf(&trash, " local_addr=%s:%s", bufaddr, bufport);
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009076
9077 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
9078 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02009079 chunk_appendf(&trash, " foreign_addr=%s:%s", bufaddr, bufport);
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009080 }
9081
9082 chunk_appendf(&trash, "\n");
9083
9084 /* Packet number spaces information */
9085 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
9086 chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu",
9087 pktns->rx.arngs.sz, pktns->tx.in_flight);
9088 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
9089 chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
9090 pktns->rx.arngs.sz, pktns->tx.in_flight);
9091 pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
9092 chunk_appendf(&trash, " [01rtt] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
9093 pktns->rx.arngs.sz, pktns->tx.in_flight);
9094
9095 chunk_appendf(&trash, " srtt=%-4u rttvar=%-4u rttmin=%-4u ptoc=%-4u cwnd=%-6llu"
Amaury Denoyelle64f67412024-02-23 17:28:49 +01009096 " mcwnd=%-6llu sentpkts=%-6llu lostpkts=%-6llu reorderedpkts=%-6llu\n",
Frédéric Lécaille35fb5932023-09-05 15:24:11 +02009097 qc->path->loss.srtt, qc->path->loss.rtt_var,
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009098 qc->path->loss.rtt_min, qc->path->loss.pto_count, (ullong)qc->path->cwnd,
Frederic Lecailledfeda3a2024-02-13 21:24:40 +01009099 (ullong)qc->path->mcwnd, (ullong)qc->cntrs.sent_pkt, (ullong)qc->path->loss.nb_lost_pkt, (ullong)qc->path->loss.nb_reordered_pkt);
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009100
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02009101 if (qc->cntrs.dropped_pkt) {
9102 chunk_appendf(&trash, " droppkts=%-6llu", qc->cntrs.dropped_pkt);
9103 addnl = 1;
9104 }
9105 if (qc->cntrs.dropped_pkt_bufoverrun) {
9106 chunk_appendf(&trash, " dropbuff=%-6llu", qc->cntrs.dropped_pkt_bufoverrun);
9107 addnl = 1;
9108 }
9109 if (qc->cntrs.dropped_parsing) {
9110 chunk_appendf(&trash, " droppars=%-6llu", qc->cntrs.dropped_parsing);
9111 addnl = 1;
9112 }
9113 if (qc->cntrs.socket_full) {
9114 chunk_appendf(&trash, " sockfull=%-6llu", qc->cntrs.socket_full);
9115 addnl = 1;
9116 }
9117 if (qc->cntrs.sendto_err) {
9118 chunk_appendf(&trash, " sendtoerr=%-6llu", qc->cntrs.sendto_err);
9119 addnl = 1;
9120 }
9121 if (qc->cntrs.sendto_err_unknown) {
9122 chunk_appendf(&trash, " sendtounknerr=%-6llu", qc->cntrs.sendto_err);
9123 addnl = 1;
9124 }
9125 if (qc->cntrs.conn_migration_done) {
9126 chunk_appendf(&trash, " migrdone=%-6llu", qc->cntrs.conn_migration_done);
9127 addnl = 1;
9128 }
9129 if (qc->cntrs.data_blocked) {
9130 chunk_appendf(&trash, " datablocked=%-6llu", qc->cntrs.data_blocked);
9131 addnl = 1;
9132 }
9133 if (qc->cntrs.stream_data_blocked) {
9134 chunk_appendf(&trash, " sdatablocked=%-6llu", qc->cntrs.stream_data_blocked);
9135 addnl = 1;
9136 }
9137 if (qc->cntrs.streams_blocked_bidi) {
9138 chunk_appendf(&trash, " sblockebidi=%-6llu", qc->cntrs.streams_blocked_bidi);
9139 addnl = 1;
9140 }
9141 if (qc->cntrs.streams_blocked_uni) {
9142 chunk_appendf(&trash, " sblockeduni=%-6llu", qc->cntrs.streams_blocked_uni);
9143 addnl = 1;
9144 }
9145 if (addnl)
9146 chunk_appendf(&trash, "\n");
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009147
9148 /* Streams */
9149 node = eb64_first(&qc->streams_by_id);
9150 i = 0;
9151 while (node) {
9152 stream = eb64_entry(node, struct qc_stream_desc, by_id);
9153 node = eb64_next(node);
9154
9155 chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key);
9156 chunk_appendf(&trash, " off=%-8llu ack=%-8llu",
9157 (unsigned long long)stream->buf_offset,
9158 (unsigned long long)stream->ack_offset);
9159
9160 if (!(++i % 3)) {
9161 chunk_appendf(&trash, "\n");
9162 i = 0;
9163 }
9164 }
9165
9166 chunk_appendf(&trash, "\n");
9167}
9168
9169static int cli_io_handler_dump_quic(struct appctx *appctx)
9170{
9171 struct show_quic_ctx *ctx = appctx->svcctx;
9172 struct stconn *sc = appctx_sc(appctx);
9173 struct quic_conn *qc;
9174
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009175 thread_isolate();
9176
9177 if (ctx->thr >= global.nbthread)
9178 goto done;
9179
Christopher Faulet87633c32023-04-03 18:32:50 +02009180 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02009181 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009182 /* If we're forced to shut down, we might have to remove our
9183 * reference to the last stream being dumped.
9184 */
9185 if (!LIST_ISEMPTY(&ctx->bref.users))
9186 LIST_DEL_INIT(&ctx->bref.users);
9187 goto done;
9188 }
9189
9190 chunk_reset(&trash);
9191
9192 if (!LIST_ISEMPTY(&ctx->bref.users)) {
9193 /* Remove show_quic_ctx from previous quic_conn instance. */
9194 LIST_DEL_INIT(&ctx->bref.users);
9195 }
9196 else if (!ctx->bref.ref) {
9197 /* First invocation. */
9198 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02009199
9200 /* Print legend for oneline format. */
9201 if (ctx->format == QUIC_DUMP_FMT_ONELINE) {
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02009202 chunk_appendf(&trash, "# conn/frontend state "
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02009203 "in_flight infl_p lost_p "
9204 "Local Address Foreign Address "
Amaury Denoyelle2273af12023-05-05 16:08:34 +02009205 "local & remote CIDs\n");
9206 applet_putchk(appctx, &trash);
9207 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009208 }
9209
9210 while (1) {
9211 int done = 0;
9212
9213 if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01009214 /* If closing connections requested through "all", move
9215 * to quic_conns_clo list after browsing quic_conns.
9216 * Else move directly to the next quic_conns thread.
9217 */
9218 if (ctx->flags & QC_CLI_FL_SHOW_ALL) {
9219 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns_clo.n;
9220 continue;
9221 }
9222
9223 done = 1;
9224 }
9225 else if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns_clo) {
9226 /* Closing list entirely browsed, go to next quic_conns
9227 * thread.
9228 */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009229 done = 1;
9230 }
9231 else {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01009232 /* Retrieve next element of the current list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009233 qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx);
9234 if ((int)(qc->qc_epoch - ctx->epoch) > 0)
9235 done = 1;
9236 }
9237
9238 if (done) {
9239 ++ctx->thr;
9240 if (ctx->thr >= global.nbthread)
9241 break;
Amaury Denoyelle2d376292023-03-08 09:42:31 +01009242 /* Switch to next thread quic_conns list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009243 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
9244 continue;
9245 }
9246
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009247 switch (ctx->format) {
9248 case QUIC_DUMP_FMT_FULL:
9249 dump_quic_full(ctx, qc);
9250 break;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02009251 case QUIC_DUMP_FMT_ONELINE:
9252 dump_quic_oneline(ctx, qc);
9253 break;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01009254 }
9255
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009256 if (applet_putchk(appctx, &trash) == -1) {
9257 /* Register show_quic_ctx to quic_conn instance. */
9258 LIST_APPEND(&qc->back_refs, &ctx->bref.users);
9259 goto full;
9260 }
9261
9262 ctx->bref.ref = qc->el_th_ctx.n;
9263 }
9264
9265 done:
9266 thread_release();
9267 return 1;
9268
9269 full:
9270 thread_release();
9271 return 0;
9272}
9273
9274static void cli_release_show_quic(struct appctx *appctx)
9275{
9276 struct show_quic_ctx *ctx = appctx->svcctx;
9277
9278 if (ctx->thr < global.nbthread) {
9279 thread_isolate();
9280 if (!LIST_ISEMPTY(&ctx->bref.users))
9281 LIST_DEL_INIT(&ctx->bref.users);
9282 thread_release();
9283 }
9284}
9285
9286static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreau6ccc8622023-05-31 15:54:48 +02009287 { { "show", "quic", NULL }, "show quic [oneline|full] [all] : 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 +01009288 {{},}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009289}};
9290
9291INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
9292
9293static void init_quic()
9294{
9295 int thr;
9296
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01009297 for (thr = 0; thr < MAX_THREADS; ++thr) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009298 LIST_INIT(&ha_thread_ctx[thr].quic_conns);
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01009299 LIST_INIT(&ha_thread_ctx[thr].quic_conns_clo);
9300 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009301}
9302INITCALL0(STG_INIT, init_quic);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02009303
9304/*
9305 * Local variables:
9306 * c-indent-level: 8
9307 * c-basic-offset: 8
9308 * End:
9309 */