blob: 5a5885e59e1eaffd7633295efc1e48dd8a052b4c [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 }
4603 LIST_DELETE(&pqpkt->list);
4604 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 }
5271 }
5272
5273 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
5274 * be considered.
5275 */
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005276 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005277 goto out;
5278
5279 if (!qc_need_sending(qc, qel) &&
5280 (!next_qel || !qc_need_sending(qc, next_qel))) {
5281 goto skip_send;
5282 }
5283
5284 buf = qc_txb_alloc(qc);
5285 if (!buf)
5286 goto out;
5287
Amaury Denoyelle147862d2023-02-28 15:10:00 +01005288 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
5289 goto skip_send;
5290
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005291 /* Currently buf cannot be non-empty at this stage. Even if a previous
5292 * sendto() has failed it is emptied to simulate packet emission and
5293 * rely on QUIC lost detection to try to emit it.
5294 */
5295 BUG_ON_HOT(b_data(buf));
5296 b_reset(buf);
5297
5298 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
5299 next_tel, &qc->els[next_tel].pktns->tx.frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01005300 if (ret == -1) {
5301 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005302 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005303 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005304
Amaury Denoyelle37333862023-02-28 11:53:48 +01005305 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01005306 if (qc->flags & QUIC_FL_CONN_TO_KILL)
5307 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005308 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005309 }
5310
5311 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005312
5313 skip_send:
5314 /* Check if there is something to do for the next level.
5315 */
5316 if (next_qel && next_qel != qel &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005317 quic_tls_has_rx_sec(next_qel) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005318 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
5319 qel = next_qel;
5320 next_qel = NULL;
5321 goto next_level;
5322 }
5323
5324 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005325 TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
5326 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005327 return t;
5328}
5329
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005330/* Release the memory allocated for <cs> CRYPTO stream */
5331void quic_cstream_free(struct quic_cstream *cs)
5332{
5333 if (!cs) {
5334 /* This is the case for ORTT encryption level */
5335 return;
5336 }
5337
Amaury Denoyellebc174b22022-11-17 10:12:52 +01005338 quic_free_ncbuf(&cs->rx.ncbuf);
5339
Amaury Denoyellebbb18202024-01-26 14:41:04 +01005340 qc_stream_desc_release(cs->desc, 0);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005341 pool_free(pool_head_quic_cstream, cs);
5342}
5343
5344/* Allocate a new QUIC stream for <qc>.
5345 * Return it if succeeded, NULL if not.
5346 */
5347struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
5348{
5349 struct quic_cstream *cs, *ret_cs = NULL;
5350
5351 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5352 cs = pool_alloc(pool_head_quic_cstream);
5353 if (!cs) {
5354 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5355 goto leave;
5356 }
5357
5358 cs->rx.offset = 0;
5359 cs->rx.ncbuf = NCBUF_NULL;
5360 cs->rx.offset = 0;
5361
5362 cs->tx.offset = 0;
5363 cs->tx.sent_offset = 0;
5364 cs->tx.buf = BUF_NULL;
5365 cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
5366 if (!cs->desc) {
5367 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5368 goto err;
5369 }
5370
5371 ret_cs = cs;
5372 leave:
5373 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5374 return ret_cs;
5375
5376 err:
5377 pool_free(pool_head_quic_cstream, cs);
5378 goto leave;
5379}
5380
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005381/* Uninitialize <qel> QUIC encryption level. Never fails. */
5382static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
5383{
5384 int i;
5385
5386 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5387
5388 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
5389 if (qel->tx.crypto.bufs[i]) {
5390 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
5391 qel->tx.crypto.bufs[i] = NULL;
5392 }
5393 }
5394 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005395 quic_cstream_free(qel->cstream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005396
5397 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5398}
5399
5400/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
5401 * connection allocating everything needed.
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005402 *
5403 * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use
5404 * quic_conn_enc_level_uninit() to cleanup partially allocated content.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005405 */
5406static int quic_conn_enc_level_init(struct quic_conn *qc,
5407 enum quic_tls_enc_level level)
5408{
5409 int ret = 0;
5410 struct quic_enc_level *qel;
5411
5412 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5413
5414 qel = &qc->els[level];
5415 qel->level = quic_to_ssl_enc_level(level);
5416 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
5417 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
5418 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
5419 qel->tls_ctx.flags = 0;
5420
5421 qel->rx.pkts = EB_ROOT;
5422 LIST_INIT(&qel->rx.pqpkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005423
5424 /* Allocate only one buffer. */
5425 /* TODO: use a pool */
5426 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
5427 if (!qel->tx.crypto.bufs)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005428 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005429
5430 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
5431 if (!qel->tx.crypto.bufs[0])
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005432 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005433
5434 qel->tx.crypto.bufs[0]->sz = 0;
5435 qel->tx.crypto.nb_buf = 1;
5436
5437 qel->tx.crypto.sz = 0;
5438 qel->tx.crypto.offset = 0;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005439 /* No CRYPTO data for early data TLS encryption level */
5440 if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA)
5441 qel->cstream = NULL;
5442 else {
5443 qel->cstream = quic_cstream_new(qc);
5444 if (!qel->cstream)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005445 goto leave;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005446 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005447
5448 ret = 1;
5449 leave:
5450 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5451 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005452}
5453
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005454/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
5455 * This is not the case if the remote peer address is not validated and if
5456 * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
5457 */
5458static int qc_may_probe_ipktns(struct quic_conn *qc)
5459{
5460 return quic_peer_validated_addr(qc) ||
5461 (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
5462}
5463
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005464/* Callback called upon loss detection and PTO timer expirations. */
5465struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
5466{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005467 struct quic_conn *qc = ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005468 struct quic_pktns *pktns;
5469
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005470 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
5471 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005472 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005473
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005474 task->expire = TICK_ETERNITY;
5475 pktns = quic_loss_pktns(qc);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005476
5477 if (qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL)) {
5478 TRACE_PROTO("cancelled action (draining state)", QUIC_EV_CONN_PTIMER, qc);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005479 goto out;
5480 }
5481
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005482 if (tick_isset(pktns->tx.loss_time)) {
5483 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
5484
5485 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
5486 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005487 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01005488 if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
5489 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005490 goto out;
5491 }
5492
5493 if (qc->path->in_flight) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +01005494 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
Frédéric Lécaille68737312023-04-07 16:28:46 +02005495 if (!pktns->tx.in_flight) {
5496 TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
5497 goto out;
5498 }
5499
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005500 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
5501 if (qc_may_probe_ipktns(qc)) {
5502 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5503 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5504 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5505 }
5506 else {
5507 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5508 }
5509 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
5510 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5511 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5512 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5513 }
Frédéric Lécaillee25fce02023-03-20 17:23:19 +01005514 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005515 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
5516 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5517 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5518 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5519 if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005520 if (qc_may_probe_ipktns(qc)) {
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005521 qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005522 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5523 }
5524 else {
5525 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5526 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005527 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005528 }
5529 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01005530 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005531 /* Wake up upper layer if waiting to send new data. */
5532 if (!qc_notify_send(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005533 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005534 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5535 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005536 }
5537 }
5538 }
5539 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
5540 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
5541 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
5542
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005543 if (quic_tls_has_tx_sec(hel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005544 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005545 if (quic_tls_has_tx_sec(iel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005546 iel->pktns->tx.pto_probe = 1;
5547 }
5548
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005549 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005550 qc->path->loss.pto_count++;
5551
5552 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005553 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
5554 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005555
5556 return task;
5557}
5558
5559/* Parse the Retry token from buffer <token> with <end> a pointer to
5560 * one byte past the end of this buffer. This will extract the ODCID
5561 * which will be stored into <odcid>
5562 *
5563 * Returns 0 on success else non-zero.
5564 */
5565static int parse_retry_token(struct quic_conn *qc,
5566 const unsigned char *token, const unsigned char *end,
5567 struct quic_cid *odcid)
5568{
5569 int ret = 0;
5570 uint64_t odcid_len;
5571 uint32_t timestamp;
Emeric Brun7875f122023-07-11 16:13:19 +02005572 uint32_t now_sec = (uint32_t)date.tv_sec;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005573
5574 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5575
5576 if (!quic_dec_int(&odcid_len, &token, end)) {
5577 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
5578 goto leave;
5579 }
5580
5581 /* RFC 9000 7.2. Negotiating Connection IDs:
5582 * When an Initial packet is sent by a client that has not previously
5583 * received an Initial or Retry packet from the server, the client
5584 * populates the Destination Connection ID field with an unpredictable
5585 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5586 */
5587 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
5588 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
5589 goto leave;
5590 }
5591
5592 if (end - token < odcid_len + sizeof timestamp) {
5593 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
5594 goto leave;
5595 }
5596
5597 timestamp = ntohl(read_u32(token + odcid_len));
Emeric Brun7875f122023-07-11 16:13:19 +02005598 /* check if elapsed time is +/- QUIC_RETRY_DURATION_SEC
5599 * to tolerate token generator is not perfectly time synced
5600 */
5601 if ((uint32_t)(now_sec - timestamp) > QUIC_RETRY_DURATION_SEC &&
5602 (uint32_t)(timestamp - now_sec) > QUIC_RETRY_DURATION_SEC) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005603 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
5604 goto leave;
5605 }
5606
5607 ret = 1;
5608 memcpy(odcid->data, token, odcid_len);
5609 odcid->len = odcid_len;
5610 leave:
5611 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5612 return !ret;
5613}
5614
5615/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
5616 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
5617 * for QUIC servers (or haproxy listeners).
Frédéric Lécaille8a586582023-06-26 10:39:56 +02005618 * <dcid> is the destination connection ID, <scid> is the source connection ID.
5619 * This latter <scid> CID as the same value on the wire as the one for <conn_id>
5620 * which is the first CID of this connection but a different internal representation used to build
5621 * NEW_CONNECTION_ID frames. This is the responsability of the caller to insert
5622 * <conn_id> in the CIDs tree for this connection (qc->cids).
5623 * <token> is the token found to be used for this connection with <token_len> as
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005624 * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005625 * Returns the connection if succeeded, NULL if not.
5626 */
5627static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
5628 struct quic_cid *dcid, struct quic_cid *scid,
5629 const struct quic_cid *token_odcid,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005630 struct quic_connection_id *conn_id,
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005631 struct sockaddr_storage *local_addr,
5632 struct sockaddr_storage *peer_addr,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005633 int server, int token, void *owner)
5634{
5635 int i;
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005636 struct quic_conn *qc = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005637 /* Initial CID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005638 char *buf_area = NULL;
5639 struct listener *l = NULL;
5640 struct quic_cc_algo *cc_algo = NULL;
5641 struct quic_tls_ctx *ictx;
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01005642 unsigned int next_actconn = 0, next_sslconn = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005643 TRACE_ENTER(QUIC_EV_CONN_INIT);
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005644
5645 next_actconn = increment_actconn();
5646 if (!next_actconn) {
5647 _HA_ATOMIC_INC(&maxconn_reached);
5648 TRACE_STATE("maxconn reached", QUIC_EV_CONN_INIT);
5649 goto err;
5650 }
5651
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01005652 next_sslconn = increment_sslconn();
5653 if (!next_sslconn) {
5654 TRACE_STATE("sslconn reached", QUIC_EV_CONN_INIT);
5655 goto err;
5656 }
5657
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005658 /* TODO replace pool_zalloc by pool_alloc(). This requires special care
5659 * to properly initialized internal quic_conn members to safely use
5660 * quic_conn_release() on alloc failure.
5661 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005662 qc = pool_zalloc(pool_head_quic_conn);
5663 if (!qc) {
5664 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
5665 goto err;
5666 }
5667
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005668 /* Now that quic_conn instance is allocated, quic_conn_release() will
5669 * ensure global accounting is decremented.
5670 */
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01005671 next_sslconn = next_actconn = 0;
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005672
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005673 /* Initialize in priority qc members required for a safe dealloc. */
5674
5675 /* required to use MTLIST_IN_LIST */
5676 MT_LIST_INIT(&qc->accept_list);
5677
5678 LIST_INIT(&qc->rx.pkt_list);
5679
Amaury Denoyelle42448332022-12-12 11:24:05 +01005680 qc_init_fd(qc);
5681
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005682 LIST_INIT(&qc->back_refs);
Amaury Denoyelled537ca72023-04-19 10:45:40 +02005683 LIST_INIT(&qc->el_th_ctx);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005684
Frédéric Lécaille1b0a5a02023-12-05 20:12:51 +01005685 /* Packet number spaces initialization. */
5686 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
5687 quic_pktns_init(&qc->pktns[i]);
5688
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005689 /* Now proceeds to allocation of qc members. */
5690
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005691 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
5692 if (!buf_area) {
5693 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
5694 goto err;
5695 }
5696
5697 qc->cids = EB_ROOT;
5698 /* QUIC Server (or listener). */
5699 if (server) {
5700 struct proxy *prx;
5701
5702 l = owner;
5703 prx = l->bind_conf->frontend;
5704 cc_algo = l->bind_conf->quic_cc_algo;
5705
5706 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
5707 &quic_stats_module);
5708 qc->flags |= QUIC_FL_CONN_LISTENER;
5709 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005710 /* Copy the client original DCID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005711 qc->odcid.len = dcid->len;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005712 memcpy(qc->odcid.data, dcid->data, dcid->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005713
5714 /* copy the packet SCID to reuse it as DCID for sending */
5715 if (scid->len)
5716 memcpy(qc->dcid.data, scid->data, scid->len);
5717 qc->dcid.len = scid->len;
5718 qc->tx.buf = BUF_NULL;
5719 qc->li = l;
5720 }
5721 /* QUIC Client (outgoing connection to servers) */
5722 else {
5723 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
5724 if (dcid->len)
5725 memcpy(qc->dcid.data, dcid->data, dcid->len);
5726 qc->dcid.len = dcid->len;
5727 }
5728 qc->mux_state = QC_MUX_NULL;
5729 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
5730
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005731 conn_id->qc = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005732
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005733 if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
5734 is_addr(local_addr)) {
5735 TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
5736 qc_alloc_fd(qc, local_addr, peer_addr);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005737
5738 /* haproxy soft-stop is supported only for QUIC connections
5739 * with their owned socket.
5740 */
5741 if (qc_test_fd(qc))
5742 _HA_ATOMIC_INC(&jobs);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005743 }
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005744
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005745 /* Select our SCID which is the first CID with 0 as sequence number. */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005746 qc->scid = conn_id->cid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005747
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005748 /* QUIC encryption level context initialization. */
5749 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5750 if (!quic_conn_enc_level_init(qc, i)) {
5751 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
5752 goto err;
5753 }
5754 /* Initialize the packet number space. */
5755 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
5756 }
5757
5758 qc->original_version = qv;
5759 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
5760 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
5761 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
5762 /* TX part. */
5763 LIST_INIT(&qc->tx.frms_to_send);
5764 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
5765 qc->tx.wbuf = qc->tx.rbuf = 0;
5766 qc->tx.bytes = 0;
5767 qc->tx.buf = BUF_NULL;
5768 /* RX part. */
5769 qc->rx.bytes = 0;
5770 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
5771 for (i = 0; i < QCS_MAX_TYPES; i++)
5772 qc->rx.strms[i].nb_streams = 0;
5773
5774 qc->nb_pkt_for_cc = 1;
5775 qc->nb_pkt_since_cc = 0;
5776
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005777 if (!quic_tls_ku_init(qc)) {
5778 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
5779 goto err;
5780 }
5781
5782 /* XXX TO DO: Only one path at this time. */
5783 qc->path = &qc->paths[0];
5784 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
5785
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005786 qc->streams_by_id = EB_ROOT_UNIQUE;
5787 qc->stream_buf_count = 0;
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005788 memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
5789 memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005790
5791 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005792 conn_id->stateless_reset_token,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005793 dcid->data, dcid->len,
5794 qc->scid.data, qc->scid.len, token_odcid))
5795 goto err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005796
Frédéric Lécailledeb97812023-03-22 11:29:45 +01005797 /* Initialize the idle timeout of the connection at the "max_idle_timeout"
5798 * value from local transport parameters.
5799 */
5800 qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005801 qc->wait_event.tasklet = tasklet_new();
5802 if (!qc->wait_event.tasklet) {
5803 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
5804 goto err;
5805 }
5806 qc->wait_event.tasklet->process = quic_conn_io_cb;
5807 qc->wait_event.tasklet->context = qc;
5808 qc->wait_event.events = 0;
Amaury Denoyellebbb1c682022-09-28 15:15:51 +02005809 qc->subs = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005810
5811 if (qc_conn_alloc_ssl_ctx(qc) ||
5812 !quic_conn_init_timer(qc) ||
5813 !quic_conn_init_idle_timer_task(qc))
5814 goto err;
5815
5816 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
5817 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
5818 goto err;
5819
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005820 /* Counters initialization */
5821 memset(&qc->cntrs, 0, sizeof qc->cntrs);
5822
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005823 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
5824 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
5825
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005826 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
5827
5828 return qc;
5829
5830 err:
5831 pool_free(pool_head_quic_conn_rxbuf, buf_area);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005832 if (qc) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005833 qc->rx.buf.area = NULL;
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005834 quic_conn_release(qc);
5835 }
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005836
5837 /* Decrement global counters. Done only for errors happening before or
5838 * on pool_head_quic_conn alloc. All other cases are covered by
5839 * quic_conn_release().
5840 */
5841 if (next_actconn)
5842 _HA_ATOMIC_DEC(&actconn);
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01005843 if (next_sslconn)
5844 _HA_ATOMIC_DEC(&global.sslconns);
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005845
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005846 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005847 return NULL;
5848}
5849
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005850/* Update the proxy counters of <qc> QUIC connection from its counters */
5851static inline void quic_conn_prx_cntrs_update(struct quic_conn *qc)
5852{
Frédéric Lécaille557d30a2023-06-14 18:09:54 +02005853 if (!qc->prx_counters)
5854 return;
5855
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005856 HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt, qc->cntrs.dropped_pkt);
5857 HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt_bufoverrun, qc->cntrs.dropped_pkt_bufoverrun);
5858 HA_ATOMIC_ADD(&qc->prx_counters->dropped_parsing, qc->cntrs.dropped_parsing);
5859 HA_ATOMIC_ADD(&qc->prx_counters->socket_full, qc->cntrs.socket_full);
5860 HA_ATOMIC_ADD(&qc->prx_counters->sendto_err, qc->cntrs.sendto_err);
5861 HA_ATOMIC_ADD(&qc->prx_counters->sendto_err_unknown, qc->cntrs.sendto_err_unknown);
Frédéric Lécaille12a815a2023-05-24 15:55:14 +02005862 HA_ATOMIC_ADD(&qc->prx_counters->sent_pkt, qc->cntrs.sent_pkt);
Frédéric Lécaillea216e062023-07-03 10:40:32 +02005863 /* It is possible that ->path was not initialized. For instance if a
5864 * QUIC connection allocation has failed.
5865 */
5866 if (qc->path)
5867 HA_ATOMIC_ADD(&qc->prx_counters->lost_pkt, qc->path->loss.nb_lost_pkt);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005868 HA_ATOMIC_ADD(&qc->prx_counters->conn_migration_done, qc->cntrs.conn_migration_done);
5869 /* Stream related counters */
5870 HA_ATOMIC_ADD(&qc->prx_counters->data_blocked, qc->cntrs.data_blocked);
5871 HA_ATOMIC_ADD(&qc->prx_counters->stream_data_blocked, qc->cntrs.stream_data_blocked);
Amaury Denoyelle6d6ee0d2023-05-25 10:36:04 +02005872 HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_bidi, qc->cntrs.streams_blocked_bidi);
5873 HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_uni, qc->cntrs.streams_blocked_uni);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005874}
5875
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005876/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
5877 * The connection tasklet is killed.
5878 *
5879 * This function must only be called by the thread responsible of the quic_conn
5880 * tasklet.
5881 */
5882void quic_conn_release(struct quic_conn *qc)
5883{
5884 int i;
5885 struct ssl_sock_ctx *conn_ctx;
5886 struct eb64_node *node;
5887 struct quic_tls_ctx *app_tls_ctx;
5888 struct quic_rx_packet *pkt, *pktback;
5889
5890 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5891
5892 /* We must not free the quic-conn if the MUX is still allocated. */
5893 BUG_ON(qc->mux_state == QC_MUX_READY);
5894
Amaury Denoyellefb375572023-02-01 09:28:32 +01005895 if (qc_test_fd(qc))
5896 _HA_ATOMIC_DEC(&jobs);
5897
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005898 /* Close quic-conn socket fd. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01005899 qc_release_fd(qc, 0);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005900
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005901 /* in the unlikely (but possible) case the connection was just added to
5902 * the accept_list we must delete it from there.
5903 */
5904 MT_LIST_DELETE(&qc->accept_list);
5905
5906 /* free remaining stream descriptors */
5907 node = eb64_first(&qc->streams_by_id);
5908 while (node) {
5909 struct qc_stream_desc *stream;
5910
5911 stream = eb64_entry(node, struct qc_stream_desc, by_id);
5912 node = eb64_next(node);
5913
5914 /* all streams attached to the quic-conn are released, so
5915 * qc_stream_desc_free will liberate the stream instance.
5916 */
5917 BUG_ON(!stream->release);
5918 qc_stream_desc_free(stream, 1);
5919 }
5920
5921 /* Purge Rx packet list. */
5922 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
5923 LIST_DELETE(&pkt->qc_rx_pkt_list);
5924 pool_free(pool_head_quic_rx_packet, pkt);
5925 }
5926
5927 if (qc->idle_timer_task) {
5928 task_destroy(qc->idle_timer_task);
5929 qc->idle_timer_task = NULL;
5930 }
5931
5932 if (qc->timer_task) {
5933 task_destroy(qc->timer_task);
5934 qc->timer_task = NULL;
5935 }
5936
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02005937 tasklet_free(qc->wait_event.tasklet);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005938
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005939 /* remove the connection from receiver cids trees */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005940 free_quic_conn_cids(qc);
5941
5942 conn_ctx = qc->xprt_ctx;
5943 if (conn_ctx) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005944 SSL_free(conn_ctx->ssl);
5945 pool_free(pool_head_quic_conn_ctx, conn_ctx);
5946 }
5947
5948 quic_tls_ku_free(qc);
5949 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5950 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
5951 quic_conn_enc_level_uninit(qc, &qc->els[i]);
5952 }
5953 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
5954
5955 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
5956 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
5957 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
5958
5959 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
5960 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
5961 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
Frédéric Lécaille73e29442023-09-13 09:28:10 +02005962 qc_release_pktns_frms(qc, &qc->pktns[i]);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005963 }
5964
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01005965 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005966
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005967 quic_conn_prx_cntrs_update(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005968 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
5969 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +02005970 qc = NULL;
Amaury Denoyellefb375572023-02-01 09:28:32 +01005971
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005972 /* Decrement global counters when quic_conn is deallocated.
5973 * quic_cc_conn instances are not accounted as they run for a short
5974 * time with limited ressources.
5975 */
5976 _HA_ATOMIC_DEC(&actconn);
Amaury Denoyelle62b950b2023-11-06 17:47:17 +01005977 _HA_ATOMIC_DEC(&global.sslconns);
Amaury Denoyelledc654e82023-11-06 17:45:14 +01005978
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005979 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
5980
5981 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5982}
5983
5984/* Initialize the timer task of <qc> QUIC connection.
5985 * Returns 1 if succeeded, 0 if not.
5986 */
5987static int quic_conn_init_timer(struct quic_conn *qc)
5988{
5989 int ret = 0;
5990 /* Attach this task to the same thread ID used for the connection */
5991 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5992
Amaury Denoyelle66947282023-04-13 11:48:38 +02005993 qc->timer_task = task_new_here();
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005994 if (!qc->timer_task) {
5995 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5996 goto leave;
5997 }
5998
5999 qc->timer = TICK_ETERNITY;
6000 qc->timer_task->process = qc_process_timer;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02006001 qc->timer_task->context = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006002
6003 ret = 1;
6004 leave:
6005 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6006 return ret;
6007}
6008
Frédéric Lécailled7215712023-03-24 18:13:37 +01006009/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
6010 * connection. */
6011static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006012{
6013 unsigned int expire;
6014
Frédéric Lécaille11ffacf2023-11-06 14:16:10 +01006015 /* It is possible the idle timer task has been already released. */
6016 if (!qc->idle_timer_task)
6017 return;
6018
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01006019 if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille495968e2023-04-03 17:42:05 +02006020 TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01006021 qc->ack_expire = TICK_ETERNITY;
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01006022 task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
6023 }
6024 else {
Amaury Denoyelle10197d62023-10-25 14:45:53 +02006025 if (qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
6026 /* RFC 9000 10.2. Immediate Close
6027 *
6028 * The closing and draining connection states exist to ensure that
6029 * connections close cleanly and that delayed or reordered packets are
6030 * properly discarded. These states SHOULD persist for at least three
6031 * times the current PTO interval as defined in [QUIC-RECOVERY].
6032 */
6033
6034 /* Delay is limited to 1s which should cover most of
6035 * network conditions. The process should not be
6036 * impacted by a connection with a high RTT.
6037 */
6038 expire = MIN(3 * quic_pto(qc), 1000);
6039 }
6040 else {
6041 /* RFC 9000 10.1. Idle Timeout
6042 *
6043 * To avoid excessively small idle timeout periods, endpoints MUST
6044 * increase the idle timeout period to be at least three times the
6045 * current Probe Timeout (PTO). This allows for multiple PTOs to expire,
6046 * and therefore multiple probes to be sent and lost, prior to idle
6047 * timeout.
6048 */
6049 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
6050 }
6051
Frédéric Lécailled7215712023-03-24 18:13:37 +01006052 qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
6053 if (arm_ack) {
6054 /* Arm the ack timer only if not already armed. */
6055 if (!tick_isset(qc->ack_expire)) {
6056 qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
6057 qc->idle_timer_task->expire = qc->ack_expire;
6058 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02006059 TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01006060 }
6061 }
6062 else {
6063 qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
6064 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02006065 TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01006066 }
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01006067 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006068}
6069
Frédéric Lécailled7215712023-03-24 18:13:37 +01006070/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
6071 * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
6072 * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
6073 * which must be rearmed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006074 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01006075static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006076{
6077 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
6078
6079 if (read) {
6080 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
6081 }
6082 else {
6083 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
6084 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01006085 qc_idle_timer_do_rearm(qc, arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006086
6087 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
6088}
6089
6090/* The task handling the idle timeout */
6091struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
6092{
6093 struct quic_conn *qc = ctx;
6094 struct quic_counters *prx_counters = qc->prx_counters;
6095 unsigned int qc_flags = qc->flags;
6096
6097 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
6098
Frédéric Lécaille12eca3a2023-04-04 10:46:54 +02006099 if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
6100 goto requeue;
6101
Frédéric Lécailled7215712023-03-24 18:13:37 +01006102 if (tick_is_expired(qc->ack_expire, now_ms)) {
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006103 TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01006104 qc->ack_expire = TICK_ETERNITY;
6105 /* Note that ->idle_expire is always set. */
6106 t->expire = qc->idle_expire;
Frédéric Lécailleb73762a2023-04-24 11:32:22 +02006107 /* Do not wakeup the I/O handler in DRAINING state or if the
6108 * connection must be killed as soon as possible.
6109 */
6110 if (!(qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL))) {
6111 qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
6112 tasklet_wakeup(qc->wait_event.tasklet);
6113 }
6114
Frédéric Lécailled7215712023-03-24 18:13:37 +01006115 goto requeue;
6116 }
6117
Frédéric Lécaille495968e2023-04-03 17:42:05 +02006118 TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006119 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
6120 * might free the quic-conn too early via quic_close().
6121 */
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02006122 qc_notify_err(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006123
6124 /* If the MUX is still alive, keep the quic-conn. The MUX is
6125 * responsible to call quic_close to release it.
6126 */
6127 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006128 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006129 quic_conn_release(qc);
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006130 qc = NULL;
Frédéric Lécaille11ffacf2023-11-06 14:16:10 +01006131 }
6132 else {
6133 task_destroy(t);
6134 qc->idle_timer_task = NULL;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006135 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006136
Frédéric Lécaille11ffacf2023-11-06 14:16:10 +01006137 t = NULL;
6138
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006139 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
6140 * least clean some parts of it such as the tasklet.
6141 */
6142
6143 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
6144 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02006145 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006146 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
6147 }
6148
Frédéric Lécailled7215712023-03-24 18:13:37 +01006149 requeue:
6150 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
6151 return t;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006152}
6153
6154/* Initialize the idle timeout task for <qc>.
6155 * Returns 1 if succeeded, 0 if not.
6156 */
6157static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
6158{
6159 int ret = 0;
6160
6161 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6162
6163 qc->idle_timer_task = task_new_here();
6164 if (!qc->idle_timer_task) {
6165 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
6166 goto leave;
6167 }
6168
6169 qc->idle_timer_task->process = qc_idle_timer_task;
6170 qc->idle_timer_task->context = qc;
Frédéric Lécailled7215712023-03-24 18:13:37 +01006171 qc->ack_expire = TICK_ETERNITY;
6172 qc_idle_timer_rearm(qc, 1, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006173 task_queue(qc->idle_timer_task);
6174
6175 ret = 1;
6176 leave:
6177 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6178 return ret;
6179}
6180
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006181/* Parse into <pkt> a long header located at <*pos> position, <end> begin a pointer to the end
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006182 * past one byte of this buffer.
6183 */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006184static inline int quic_packet_read_long_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006185 struct quic_rx_packet *pkt)
6186{
6187 int ret = 0;
6188 unsigned char dcid_len, scid_len;
6189
6190 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
6191
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006192 if (end == *pos) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006193 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
6194 goto leave;
6195 }
6196
6197 /* Destination Connection ID Length */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006198 dcid_len = *(*pos)++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006199 /* 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 +02006200 if (dcid_len > QUIC_CID_MAXLEN || end - *pos < dcid_len + 1) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006201 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
6202 goto leave;
6203 }
6204
6205 if (dcid_len) {
6206 /* Check that the length of this received DCID matches the CID lengths
6207 * of our implementation for non Initials packets only.
6208 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006209 if (pkt->version && pkt->version->num &&
6210 pkt->type != QUIC_PACKET_TYPE_INITIAL &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006211 pkt->type != QUIC_PACKET_TYPE_0RTT &&
6212 dcid_len != QUIC_HAP_CID_LEN) {
6213 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
6214 goto leave;
6215 }
6216
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006217 memcpy(pkt->dcid.data, *pos, dcid_len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006218 }
6219
6220 pkt->dcid.len = dcid_len;
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006221 *pos += dcid_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006222
6223 /* Source Connection ID Length */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006224 scid_len = *(*pos)++;
6225 if (scid_len > QUIC_CID_MAXLEN || end - *pos < scid_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006226 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
6227 goto leave;
6228 }
6229
6230 if (scid_len)
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006231 memcpy(pkt->scid.data, *pos, scid_len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006232 pkt->scid.len = scid_len;
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006233 *pos += scid_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006234
6235 ret = 1;
6236 leave:
6237 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6238 return ret;
6239}
6240
6241/* Insert <pkt> RX packet in its <qel> RX packets tree */
6242static void qc_pkt_insert(struct quic_conn *qc,
6243 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
6244{
6245 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
6246
6247 pkt->pn_node.key = pkt->pn;
6248 quic_rx_packet_refinc(pkt);
6249 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
6250
6251 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6252}
6253
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006254/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
6255 * address of the packet first byte, using the keys from encryption level <el>.
6256 *
6257 * If header protection has been successfully removed, packet data are copied
6258 * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
6259 * proceeded, and the packet is inserted into <qc> protected packets tree. In
6260 * both cases, packet can now be considered handled by the <qc> connection.
6261 *
6262 * If header protection cannot be removed due to <el> secrets already
6263 * discarded, no operation is conducted.
6264 *
6265 * Returns 1 on success : packet data is now handled by the connection. On
6266 * error 0 is returned : packet should be dropped by the caller.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006267 */
6268static inline int qc_try_rm_hp(struct quic_conn *qc,
6269 struct quic_rx_packet *pkt,
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006270 unsigned char *beg,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006271 struct quic_enc_level **el)
6272{
6273 int ret = 0;
6274 unsigned char *pn = NULL; /* Packet number field */
6275 enum quic_tls_enc_level tel;
6276 struct quic_enc_level *qel;
6277 /* Only for traces. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006278
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006279 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006280 BUG_ON(!pkt->pn_offset);
6281
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006282 /* The packet number is here. This is also the start minus
6283 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
6284 * protection.
6285 */
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006286 pn = beg + pkt->pn_offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006287
6288 tel = quic_packet_type_enc_level(pkt->type);
6289 qel = &qc->els[tel];
6290
6291 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01006292 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
6293
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006294 /* Note that the following function enables us to unprotect the packet
6295 * number and its length subsequently used to decrypt the entire
6296 * packets.
6297 */
Frédéric Lécaille72027782023-02-22 16:20:09 +01006298 if (!qc_do_rm_hp(qc, pkt, tls_ctx,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006299 qel->pktns->rx.largest_pn, pn, beg)) {
6300 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
6301 goto out;
6302 }
6303
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006304 qc_handle_spin_bit(qc, pkt, qel);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006305 /* The AAD includes the packet number field. */
6306 pkt->aad_len = pkt->pn_offset + pkt->pnl;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006307 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
6308 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
6309 goto out;
6310 }
6311
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006312 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006313 }
6314 else {
6315 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
6316 /* If the packet number space has been discarded, this packet
6317 * will be not parsed.
6318 */
6319 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
6320 goto out;
6321 }
6322
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006323 TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006324 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
6325 quic_rx_packet_refinc(pkt);
6326 }
6327
6328 *el = qel;
6329 /* No reference counter incrementation here!!! */
6330 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
6331 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
6332 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
6333 b_add(&qc->rx.buf, pkt->len);
Amaury Denoyelledc654e82023-11-06 17:45:14 +01006334
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006335 ret = 1;
6336 out:
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006337 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006338 return ret;
6339}
6340
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006341/* Return the QUIC version (quic_version struct) with <version> as version number
6342 * if supported or NULL if not.
6343 */
6344static inline const struct quic_version *qc_supported_version(uint32_t version)
6345{
6346 int i;
6347
6348 if (unlikely(!version))
6349 return &quic_version_VN_reserved;
6350
6351 for (i = 0; i < quic_versions_nb; i++)
6352 if (quic_versions[i].num == version)
6353 return &quic_versions[i];
6354
6355 return NULL;
6356}
6357
Willy Tarreaudd9f9212023-05-07 07:07:44 +02006358/* Parse a QUIC packet header starting at <pos> position without exceeding <end>.
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006359 * Version and type are stored in <pkt> packet instance. Type is set to unknown
6360 * on two occasions : for unsupported version, in this case version field is
6361 * set to NULL; for Version Negotiation packet with version number set to 0.
6362 *
6363 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006364 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006365int qc_parse_hd_form(struct quic_rx_packet *pkt,
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006366 unsigned char **pos, const unsigned char *end)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006367{
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006368 uint32_t version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006369 int ret = 0;
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006370 const unsigned char byte0 = **pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006371
6372 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006373 pkt->version = NULL;
6374 pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006375
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006376 (*pos)++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006377 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
6378 unsigned char type =
6379 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
6380
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006381 /* Version */
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006382 if (!quic_read_uint32(&version, (const unsigned char **)pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006383 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
6384 goto out;
6385 }
6386
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006387 pkt->version = qc_supported_version(version);
6388 if (version && pkt->version) {
6389 if (version != QUIC_PROTOCOL_VERSION_2) {
6390 pkt->type = type;
6391 }
6392 else {
6393 switch (type) {
6394 case 0:
6395 pkt->type = QUIC_PACKET_TYPE_RETRY;
6396 break;
6397 case 1:
6398 pkt->type = QUIC_PACKET_TYPE_INITIAL;
6399 break;
6400 case 2:
6401 pkt->type = QUIC_PACKET_TYPE_0RTT;
6402 break;
6403 case 3:
6404 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
6405 break;
6406 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006407 }
6408 }
6409 }
6410 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006411 if (byte0 & QUIC_PACKET_SPIN_BIT)
6412 pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006413 pkt->type = QUIC_PACKET_TYPE_SHORT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006414 }
6415
6416 ret = 1;
6417 out:
6418 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6419 return ret;
6420}
6421
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006422/*
6423 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
6424 * address <addr>.
6425 * Implementation of RFC9000 6. Version Negotiation
6426 *
6427 * TODO implement a rate-limiting sending of Version Negotiation packets
6428 *
6429 * Returns 0 on success else non-zero
6430 */
6431static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
6432 struct quic_rx_packet *pkt)
6433{
6434 char buf[256];
6435 int ret = 0, i = 0, j;
6436 uint32_t version;
6437 const socklen_t addrlen = get_addr_len(addr);
6438
6439 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6440 /*
6441 * header form
6442 * long header, fixed bit to 0 for Version Negotiation
6443 */
6444 /* TODO: RAND_bytes() should be replaced? */
6445 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
6446 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
6447 goto out;
6448 }
6449
6450 buf[i++] |= '\x80';
6451 /* null version for Version Negotiation */
6452 buf[i++] = '\x00';
6453 buf[i++] = '\x00';
6454 buf[i++] = '\x00';
6455 buf[i++] = '\x00';
6456
6457 /* source connection id */
6458 buf[i++] = pkt->scid.len;
6459 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6460 i += pkt->scid.len;
6461
6462 /* destination connection id */
6463 buf[i++] = pkt->dcid.len;
6464 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
6465 i += pkt->dcid.len;
6466
6467 /* supported version */
6468 for (j = 0; j < quic_versions_nb; j++) {
6469 version = htonl(quic_versions[j].num);
6470 memcpy(&buf[i], &version, sizeof(version));
6471 i += sizeof(version);
6472 }
6473
6474 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
6475 goto out;
6476
6477 ret = 1;
6478 out:
6479 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6480 return !ret;
6481}
6482
6483/* Send a stateless reset packet depending on <pkt> RX packet information
6484 * from <fd> UDP socket to <dst>
6485 * Return 1 if succeeded, 0 if not.
6486 */
6487static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
6488 struct quic_rx_packet *rxpkt)
6489{
6490 int ret = 0, pktlen, rndlen;
6491 unsigned char pkt[64];
6492 const socklen_t addrlen = get_addr_len(dstaddr);
6493 struct proxy *prx;
6494 struct quic_counters *prx_counters;
6495
6496 TRACE_ENTER(QUIC_EV_STATELESS_RST);
6497
Amaury Denoyelle53b7b8e2024-05-22 15:55:58 +02006498 /* RFC 9000 10.3. Stateless Reset
6499 *
6500 * Endpoints MUST discard packets that are too small to be valid QUIC
6501 * packets. To give an example, with the set of AEAD functions defined
6502 * in [QUIC-TLS], short header packets that are smaller than 21 bytes
6503 * are never valid.
6504 *
6505 * [...]
6506 *
6507 * RFC 9000 10.3.3. Looping
6508 *
6509 * An endpoint MUST ensure that every Stateless Reset that it sends is
6510 * smaller than the packet that triggered it, unless it maintains state
6511 * sufficient to prevent looping. In the event of a loop, this results
6512 * in packets eventually being too small to trigger a response.
6513 */
6514 if (rxpkt->len <= QUIC_STATELESS_RESET_PACKET_MINLEN) {
6515 TRACE_DEVEL("rxpkt too short", QUIC_EV_STATELESS_RST);
6516 goto leave;
6517 }
6518
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006519 prx = l->bind_conf->frontend;
6520 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Amaury Denoyelle53b7b8e2024-05-22 15:55:58 +02006521
6522 /* RFC 9000 10.3. Stateless Reset
6523 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006524 * An endpoint that sends a Stateless Reset in response to a packet that is
6525 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
6526 * than the packet it responds to.
6527 */
Amaury Denoyelle53b7b8e2024-05-22 15:55:58 +02006528 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 :
6529 QUIC_STATELESS_RESET_PACKET_MINLEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006530 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
6531
6532 /* Put a header of random bytes */
6533 /* TODO: RAND_bytes() should be replaced */
6534 if (RAND_bytes(pkt, rndlen) != 1) {
6535 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
6536 goto leave;
6537 }
6538
6539 /* Clear the most significant bit, and set the second one */
6540 *pkt = (*pkt & ~0x80) | 0x40;
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02006541 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006542 rxpkt->dcid.data, rxpkt->dcid.len))
6543 goto leave;
6544
6545 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
6546 goto leave;
6547
6548 ret = 1;
6549 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
6550 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
6551 leave:
6552 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
6553 return ret;
6554}
6555
6556/* QUIC server only function.
6557 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
6558 * This is the responsibility of the caller to check <aad> size is big enough
6559 * to contain these data.
6560 * Return the number of bytes copied to <aad>.
6561 */
6562static int quic_generate_retry_token_aad(unsigned char *aad,
6563 uint32_t version,
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006564 const struct quic_cid *cid,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006565 const struct sockaddr_storage *addr)
6566{
6567 unsigned char *p;
6568
6569 p = aad;
Willy Tarreaub4cf4ba2024-04-05 23:54:17 +02006570 write_u32(p, htonl(version));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006571 p += sizeof version;
6572 p += quic_saddr_cpy(p, addr);
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006573 memcpy(p, cid->data, cid->len);
6574 p += cid->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006575
6576 return p - aad;
6577}
6578
6579/* QUIC server only function.
6580 * Generate the token to be used in Retry packets. The token is written to
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006581 * <token> with <len> as length. <odcid> is the original destination connection
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006582 * ID and <dcid> is our side destination connection ID (or client source
6583 * connection ID).
6584 * Returns the length of the encoded token or 0 on error.
6585 */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006586static int quic_generate_retry_token(unsigned char *token, size_t len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006587 const uint32_t version,
6588 const struct quic_cid *odcid,
6589 const struct quic_cid *dcid,
6590 struct sockaddr_storage *addr)
6591{
6592 int ret = 0;
6593 unsigned char *p;
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006594 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
6595 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006596 size_t aadlen;
6597 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
6598 unsigned char key[QUIC_TLS_KEY_LEN];
6599 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaille0499db42023-09-07 18:43:52 +02006600 const unsigned char *sec = global.cluster_secret;
6601 size_t seclen = sizeof global.cluster_secret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006602 EVP_CIPHER_CTX *ctx = NULL;
6603 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Emeric Brun7875f122023-07-11 16:13:19 +02006604 uint32_t timestamp = (uint32_t)date.tv_sec;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006605
6606 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6607
Frédéric Lécaille6d6ddb22023-05-15 17:40:00 +02006608 /* The token is made of the token format byte, the ODCID prefixed by its one byte
6609 * length, the creation timestamp, an AEAD TAG, and finally
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006610 * the random bytes used to derive the secret to encrypt the token.
6611 */
Frédéric Lécaille6d6ddb22023-05-15 17:40:00 +02006612 if (1 + odcid->len + 1 + sizeof(timestamp) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN > len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006613 goto err;
6614
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006615 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006616 /* TODO: RAND_bytes() should be replaced */
6617 if (RAND_bytes(salt, sizeof salt) != 1) {
6618 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
6619 goto err;
6620 }
6621
6622 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6623 salt, sizeof salt, sec, seclen)) {
6624 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
6625 goto err;
6626 }
6627
6628 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
6629 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
6630 goto err;
6631 }
6632
6633 /* Token build */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006634 p = token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006635 *p++ = QUIC_TOKEN_FMT_RETRY,
6636 *p++ = odcid->len;
6637 memcpy(p, odcid->data, odcid->len);
6638 p += odcid->len;
6639 write_u32(p, htonl(timestamp));
6640 p += sizeof timestamp;
6641
6642 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Emeric Brune0190c62023-07-11 14:53:41 +02006643 if (!quic_tls_encrypt(token + 1, p - token - 1, aad, aadlen, ctx, aead, iv)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006644 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
6645 goto err;
6646 }
6647
6648 p += QUIC_TLS_TAG_LEN;
6649 memcpy(p, salt, sizeof salt);
6650 p += sizeof salt;
6651 EVP_CIPHER_CTX_free(ctx);
6652
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006653 ret = p - token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006654 leave:
6655 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6656 return ret;
6657
6658 err:
6659 if (ctx)
6660 EVP_CIPHER_CTX_free(ctx);
6661 goto leave;
6662}
6663
6664/* QUIC server only function.
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006665 *
6666 * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
6667 * the UDP datagram containing <pkt> and <l> is the listener instance on which
6668 * it was received. If the token is valid, the ODCID of <qc> QUIC connection
6669 * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
6670 * to validate the token but it can be NULL : in this case the version will be
6671 * retrieved from the packet.
6672 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006673 * Return 1 if succeeded, 0 if not.
6674 */
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006675
6676static int quic_retry_token_check(struct quic_rx_packet *pkt,
6677 struct quic_dgram *dgram,
6678 struct listener *l,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006679 struct quic_conn *qc,
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006680 struct quic_cid *odcid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006681{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006682 struct proxy *prx;
6683 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006684 int ret = 0;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006685 unsigned char *token = pkt->token;
6686 const uint64_t tokenlen = pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006687 unsigned char buf[128];
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006688 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
6689 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006690 size_t aadlen;
6691 const unsigned char *salt;
6692 unsigned char key[QUIC_TLS_KEY_LEN];
6693 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaille0499db42023-09-07 18:43:52 +02006694 const unsigned char *sec = global.cluster_secret;
6695 size_t seclen = sizeof global.cluster_secret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006696 EVP_CIPHER_CTX *ctx = NULL;
6697 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006698 const struct quic_version *qv = qc ? qc->original_version :
6699 pkt->version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006700
6701 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6702
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006703 /* The caller must ensure this. */
Frédéric Lécaille0499db42023-09-07 18:43:52 +02006704 BUG_ON(!pkt->token_len);
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006705
6706 prx = l->bind_conf->frontend;
6707 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6708
6709 if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
6710 /* TODO: New token check */
6711 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6712 goto leave;
6713 }
6714
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006715 if (sizeof buf < tokenlen) {
6716 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
6717 goto err;
6718 }
6719
Frédéric Lécaille35b63962023-05-15 18:11:21 +02006720 /* The token is made of the token format byte, the ODCID prefixed by its one byte
6721 * length, the creation timestamp, an AEAD TAG, and finally
6722 * the random bytes used to derive the secret to encrypt the token.
6723 */
6724 if (tokenlen < 2 + QUIC_ODCID_MINLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN ||
6725 tokenlen > 2 + QUIC_CID_MAXLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN) {
6726 TRACE_ERROR("invalid token length", QUIC_EV_CONN_LPKT, qc);
6727 goto err;
6728 }
6729
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006730 aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006731 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
6732 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6733 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
6734 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
6735 goto err;
6736 }
6737
6738 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
6739 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
6740 goto err;
6741 }
6742
Frédéric Lécaille35b63962023-05-15 18:11:21 +02006743 /* The token is prefixed by a one-byte length format which is not ciphered. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006744 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
6745 ctx, aead, key, iv)) {
6746 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
6747 goto err;
6748 }
6749
6750 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
6751 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
6752 goto err;
6753 }
6754
6755 EVP_CIPHER_CTX_free(ctx);
6756
6757 ret = 1;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006758 HA_ATOMIC_INC(&prx_counters->retry_validated);
6759
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006760 leave:
6761 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6762 return ret;
6763
6764 err:
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006765 HA_ATOMIC_INC(&prx_counters->retry_error);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006766 if (ctx)
6767 EVP_CIPHER_CTX_free(ctx);
6768 goto leave;
6769}
6770
6771/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
6772 * the Initial <pkt> packet.
6773 *
6774 * Returns 0 on success else non-zero.
6775 */
6776static int send_retry(int fd, struct sockaddr_storage *addr,
6777 struct quic_rx_packet *pkt, const struct quic_version *qv)
6778{
6779 int ret = 0;
6780 unsigned char buf[128];
6781 int i = 0, token_len;
6782 const socklen_t addrlen = get_addr_len(addr);
6783 struct quic_cid scid;
6784
6785 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6786
Frédéric Lécaille2b220542023-06-30 12:17:36 +02006787 /* long header(1) | fixed bit(1) | packet type QUIC_PACKET_TYPE_RETRY(2) | unused random bits(4)*/
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006788 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
Frédéric Lécaille2b220542023-06-30 12:17:36 +02006789 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT) |
6790 statistical_prng_range(16);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006791 /* version */
Emeric Brun2b6d45e2023-07-17 18:33:44 +02006792 write_n32(&buf[i], qv->num);
Frédéric Lécaille966e4682023-06-30 14:41:31 +02006793 i += sizeof(uint32_t);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006794
6795 /* Use the SCID from <pkt> for Retry DCID. */
6796 buf[i++] = pkt->scid.len;
6797 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6798 i += pkt->scid.len;
6799
6800 /* Generate a new CID to be used as SCID for the Retry packet. */
6801 scid.len = QUIC_HAP_CID_LEN;
6802 /* TODO: RAND_bytes() should be replaced */
6803 if (RAND_bytes(scid.data, scid.len) != 1) {
6804 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
6805 goto out;
6806 }
6807
6808 buf[i++] = scid.len;
6809 memcpy(&buf[i], scid.data, scid.len);
6810 i += scid.len;
6811
6812 /* token */
6813 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Emeric Bruna0dda4b2023-09-28 15:29:53 +02006814 &pkt->dcid, &pkt->scid, addr))) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006815 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
6816 goto out;
6817 }
6818
6819 i += token_len;
6820
6821 /* token integrity tag */
Emeric Bruna47f5cd2023-06-27 15:24:05 +02006822 if ((sizeof(buf) - i < QUIC_TLS_TAG_LEN) ||
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006823 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
6824 pkt->dcid.len, buf, i, qv)) {
6825 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6826 goto out;
6827 }
6828
6829 i += QUIC_TLS_TAG_LEN;
6830
6831 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
6832 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6833 goto out;
6834 }
6835
6836 ret = 1;
6837 out:
6838 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6839 return !ret;
6840}
6841
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006842/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
6843 * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
6844 * is used.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006845 *
6846 * Returns the instance or NULL if not found.
6847 */
6848static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
6849 struct listener *l,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006850 struct sockaddr_storage *saddr,
6851 int *new_tid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006852{
6853 struct quic_conn *qc = NULL;
6854 struct ebmb_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006855 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006856 struct quic_cid_tree *tree;
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006857 uint conn_id_tid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006858
6859 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006860 *new_tid = -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006861
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006862 /* First look into DCID tree. */
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006863 tree = &quic_cid_trees[_quic_cid_tree_idx(pkt->dcid.data)];
6864 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6865 node = ebmb_lookup(&tree->root, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006866
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006867 /* If not found on an Initial/0-RTT packet, it could be because an
6868 * ODCID is reused by the client. Calculate the derived CID value to
6869 * retrieve it from the DCID tree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006870 */
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006871 if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
6872 pkt->type == QUIC_PACKET_TYPE_0RTT)) {
Amaury Denoyellec2a92642023-04-13 15:26:18 +02006873 const struct quic_cid derive_cid = quic_derive_cid(&pkt->dcid, saddr);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006874
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006875 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
6876
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006877 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
6878 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6879 node = ebmb_lookup(&tree->root, derive_cid.data, derive_cid.len);
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006880 }
6881
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006882 if (!node)
6883 goto end;
6884
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006885 conn_id = ebmb_entry(node, struct quic_connection_id, node);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006886 conn_id_tid = HA_ATOMIC_LOAD(&conn_id->tid);
6887 if (conn_id_tid != tid) {
6888 *new_tid = conn_id_tid;
6889 goto end;
6890 }
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006891 qc = conn_id->qc;
Amaury Denoyelle806c5c52024-06-27 18:52:23 +02006892 TRACE_DEVEL("found connection", QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006893
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006894 end:
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006895 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle806c5c52024-06-27 18:52:23 +02006896 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006897 return qc;
6898}
6899
6900/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
6901 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
6902 * parameters of this session.
6903 * This is the responsibility of the caller to check the validity of all the
6904 * pointers passed as parameter to this function.
6905 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
6906 * CO_ER_SSL_NO_MEM.
6907 */
6908static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
6909 unsigned char *params, size_t params_len)
6910{
6911 int retry, ret = -1;
6912
6913 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6914
6915 retry = 1;
6916 retry:
6917 *ssl = SSL_new(ssl_ctx);
6918 if (!*ssl) {
6919 if (!retry--)
Frédéric Lécaillee4185272023-06-02 16:56:16 +02006920 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006921
6922 pool_gc(NULL);
6923 goto retry;
6924 }
6925
Frédéric Lécailleca87a622023-06-02 17:00:04 +02006926 if (!SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
6927 !SSL_set_quic_method(*ssl, &ha_quic_method)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006928 SSL_free(*ssl);
6929 *ssl = NULL;
6930 if (!retry--)
Frédéric Lécaillee4185272023-06-02 16:56:16 +02006931 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006932
6933 pool_gc(NULL);
6934 goto retry;
6935 }
6936
6937 ret = 0;
6938 leave:
6939 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6940 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006941}
6942
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006943/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
6944 * used to process <qc> received packets. The allocated context is stored in
6945 * <qc.xprt_ctx>.
6946 *
6947 * Returns 0 on success else non-zero.
6948 */
6949static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
6950{
6951 int ret = 0;
6952 struct bind_conf *bc = qc->li->bind_conf;
6953 struct ssl_sock_ctx *ctx = NULL;
6954
6955 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6956
6957 ctx = pool_zalloc(pool_head_quic_conn_ctx);
6958 if (!ctx) {
6959 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
6960 goto err;
6961 }
6962
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006963 ctx->subs = NULL;
6964 ctx->xprt_ctx = NULL;
6965 ctx->qc = qc;
6966
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006967 if (qc_is_listener(qc)) {
6968 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
6969 qc->enc_params, qc->enc_params_len) == -1) {
6970 goto err;
6971 }
6972#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Frédéric Lécaille65a8b9a2023-06-02 17:05:38 +02006973#ifndef USE_QUIC_OPENSSL_COMPAT
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006974 /* Enabling 0-RTT */
6975 if (bc->ssl_conf.early_data)
6976 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
6977#endif
Frédéric Lécaille65a8b9a2023-06-02 17:05:38 +02006978#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006979
6980 SSL_set_accept_state(ctx->ssl);
6981 }
6982
6983 ctx->xprt = xprt_get(XPRT_QUIC);
6984
6985 /* Store the allocated context in <qc>. */
6986 qc->xprt_ctx = ctx;
6987
Amaury Denoyelleaff66bb2023-10-25 15:38:50 +02006988 /* global.sslconns is already incremented on INITIAL packet parsing. */
6989 _HA_ATOMIC_INC(&global.totalsslconns);
6990
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006991 ret = 1;
6992 leave:
6993 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6994 return !ret;
6995
6996 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006997 pool_free(pool_head_quic_conn_ctx, ctx);
6998 goto leave;
6999}
7000
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007001/* Check that all the bytes between <pos> included and <end> address
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007002 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007003 * check that there is at least one byte between <pos> end <end>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007004 * Return 1 if this all the bytes are null, 0 if not.
7005 */
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007006static inline int quic_padding_check(const unsigned char *pos,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007007 const unsigned char *end)
7008{
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007009 while (pos < end && !*pos)
7010 pos++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007011
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02007012 return pos == end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007013}
7014
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007015/* Find the associated connection to the packet <pkt> or create a new one if
7016 * this is an Initial packet. <dgram> is the datagram containing the packet and
7017 * <l> is the listener instance on which it was received.
7018 *
Amaury Denoyelle25174d52023-04-05 17:52:05 +02007019 * By default, <new_tid> is set to -1. However, if thread affinity has been
7020 * chanbed, it will be set to its new thread ID.
7021 *
7022 * Returns the quic-conn instance or NULL if not found or thread affinity
7023 * changed.
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007024 */
7025static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
7026 struct quic_dgram *dgram,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007027 struct listener *l,
7028 int *new_tid)
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007029{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02007030 struct quic_cid token_odcid = { .len = 0 };
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007031 struct quic_conn *qc = NULL;
7032 struct proxy *prx;
7033 struct quic_counters *prx_counters;
7034
7035 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7036
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007037 *new_tid = -1;
7038
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007039 prx = l->bind_conf->frontend;
7040 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
7041
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007042 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr, new_tid);
7043
Amaury Denoyelle25174d52023-04-05 17:52:05 +02007044 /* If connection already created or rebinded on another thread. */
Frédéric Lécailleab3aa0f2023-05-24 09:06:06 +02007045 if (!qc && *new_tid != -1 && tid != *new_tid)
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007046 goto out;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007047
7048 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7049 BUG_ON(!pkt->version); /* This must not happen. */
7050
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007051 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007052 struct quic_cid_tree *tree;
7053 struct ebmb_node *node;
7054 struct quic_connection_id *conn_id;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007055 int ipv4;
7056
Amaury Denoyelle6b240e42023-11-09 16:44:50 +01007057 if (pkt->token_len) {
7058 /* Validate the token only when connection is unknown. */
7059 if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
7060 goto err;
7061 }
7062 else if (!(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
7063 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007064 TRACE_PROTO("Initial without token, sending retry",
7065 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7066 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
7067 TRACE_ERROR("Error during Retry generation",
7068 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7069 goto out;
7070 }
7071
7072 HA_ATOMIC_INC(&prx_counters->retry_sent);
7073 goto out;
7074 }
7075
7076 /* RFC 9000 7.2. Negotiating Connection IDs:
7077 * When an Initial packet is sent by a client that has not previously
7078 * received an Initial or Retry packet from the server, the client
7079 * populates the Destination Connection ID field with an unpredictable
7080 * value. This Destination Connection ID MUST be at least 8 bytes in length.
7081 */
7082 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
7083 TRACE_PROTO("dropped packet",
7084 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7085 goto err;
7086 }
7087
7088 pkt->saddr = dgram->saddr;
7089 ipv4 = dgram->saddr.ss_family == AF_INET;
7090
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007091 /* Generate the first connection CID. This is derived from the client
7092 * ODCID and address. This allows to retrieve the connection from the
7093 * ODCID without storing it in the CID tree. This is an interesting
7094 * optimization as the client is expected to stop using its ODCID in
7095 * favor of our generated value.
7096 */
7097 conn_id = new_quic_cid(NULL, NULL, &pkt->dcid, &pkt->saddr);
7098 if (!conn_id)
7099 goto err;
7100
Frédéric Lécaillefd212a72023-06-16 16:10:58 +02007101 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
7102 conn_id, &dgram->daddr, &pkt->saddr, 1,
7103 !!pkt->token_len, l);
7104 if (qc == NULL) {
Frédéric Lécaillefd212a72023-06-16 16:10:58 +02007105 pool_free(pool_head_quic_connection_id, conn_id);
7106 goto err;
7107 }
7108
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007109 tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)];
7110 HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock);
7111 node = ebmb_insert(&tree->root, &conn_id->node, conn_id->cid.len);
7112 if (node != &conn_id->node) {
7113 pool_free(pool_head_quic_connection_id, conn_id);
7114
7115 conn_id = ebmb_entry(node, struct quic_connection_id, node);
7116 *new_tid = HA_ATOMIC_LOAD(&conn_id->tid);
Frédéric Lécaillefd212a72023-06-16 16:10:58 +02007117 quic_conn_release(qc);
7118 qc = NULL;
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007119 }
Frédéric Lécaille8a586582023-06-26 10:39:56 +02007120 else {
7121 /* From here, <qc> is the correct connection for this <pkt> Initial
7122 * packet. <conn_id> must be inserted in the CIDs tree for this
7123 * connection.
7124 */
7125 eb64_insert(&qc->cids, &conn_id->seq_num);
7126 /* Initialize the next CID sequence number to be used for this connection. */
7127 qc->next_cid_seq_num = 1;
7128 }
Amaury Denoyellef16ec342023-04-13 17:42:34 +02007129 HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock);
7130
7131 if (*new_tid != -1)
7132 goto out;
7133
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007134 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007135 }
7136 }
7137 else if (!qc) {
Amaury Denoyelle53b7b8e2024-05-22 15:55:58 +02007138 /* Stateless Reset sent even for Long header packets as haproxy
7139 * emits stateless_reset_token in its TPs.
7140 */
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007141 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 +02007142 if (!send_stateless_reset(l, &dgram->saddr, pkt))
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007143 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
7144 goto err;
7145 }
7146
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007147 out:
7148 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7149 return qc;
7150
7151 err:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007152 if (qc)
7153 qc->cntrs.dropped_pkt++;
7154 else
7155 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle331b8b12023-10-25 10:52:23 +02007156
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007157 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7158 return NULL;
7159}
7160
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007161/* Parse a QUIC packet starting at <pos>. Data won't be read after <end> even
Amaury Denoyelle98289692022-10-19 15:37:44 +02007162 * if the packet is incomplete. This function will populate fields of <pkt>
7163 * instance, most notably its length. <dgram> is the UDP datagram which
7164 * contains the parsed packet. <l> is the listener instance on which it was
7165 * received.
7166 *
7167 * 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 +02007168 * the real packet value or to cover all data between <pos> and <end> : this is
Amaury Denoyelle98289692022-10-19 15:37:44 +02007169 * useful to reject a whole datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007170 */
Amaury Denoyelle98289692022-10-19 15:37:44 +02007171static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007172 unsigned char *pos, const unsigned char *end,
Amaury Denoyelle98289692022-10-19 15:37:44 +02007173 struct quic_dgram *dgram, struct listener *l)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007174{
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007175 const unsigned char *beg = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007176 struct proxy *prx;
7177 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007178
7179 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7180
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007181 prx = l->bind_conf->frontend;
7182 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
7183 /* This ist only to please to traces and distinguish the
7184 * packet with parsed packet number from others.
7185 */
7186 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007187 if (end <= pos) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007188 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7189 goto drop;
7190 }
7191
7192 /* Fixed bit */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007193 if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007194 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007195 quic_padding_check(pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007196 /* Some browsers may pad the remaining datagram space with null bytes.
7197 * That is what we called add padding out of QUIC packets. Such
7198 * datagrams must be considered as valid. But we can only consume
7199 * the remaining space.
7200 */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007201 pkt->len = end - pos;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007202 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007203 }
7204
7205 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7206 goto drop;
7207 }
7208
7209 /* Header form */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007210 if (!qc_parse_hd_form(pkt, &pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007211 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7212 goto drop;
7213 }
7214
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007215 if (pkt->type != QUIC_PACKET_TYPE_SHORT) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007216 uint64_t len;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007217 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007218
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007219 if (!quic_packet_read_long_header(&pos, end, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007220 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7221 goto drop;
7222 }
7223
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007224 /* When multiple QUIC packets are coalesced on the same UDP datagram,
7225 * they must have the same DCID.
7226 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007227 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007228 (pkt->dcid.len != dgram->dcid_len ||
7229 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007230 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007231 goto drop;
7232 }
7233
7234 /* Retry of Version Negotiation packets are only sent by servers */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007235 if (pkt->type == QUIC_PACKET_TYPE_RETRY ||
7236 (pkt->version && !pkt->version->num)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007237 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7238 goto drop;
7239 }
7240
7241 /* RFC9000 6. Version Negotiation */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007242 if (!pkt->version) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007243 /* unsupported version, send Negotiation packet */
7244 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
7245 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007246 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007247 }
7248
7249 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007250 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007251 }
7252
7253 /* For Initial packets, and for servers (QUIC clients connections),
7254 * there is no Initial connection IDs storage.
7255 */
7256 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7257 uint64_t token_len;
7258
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007259 if (!quic_dec_int(&token_len, (const unsigned char **)&pos, end) ||
7260 end - pos < token_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007261 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007262 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007263 goto drop;
7264 }
7265
7266 /* TODO Retry should be automatically activated if
7267 * suspect network usage is detected.
7268 */
Frédéric Lécaille0499db42023-09-07 18:43:52 +02007269 if (!token_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007270 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
7271 TRACE_PROTO("Initial without token, sending retry",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007272 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7273 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007274 TRACE_PROTO("Error during Retry generation",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007275 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007276 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007277 }
7278
7279 HA_ATOMIC_INC(&prx_counters->retry_sent);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007280 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007281 }
7282 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007283
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007284 pkt->token = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007285 pkt->token_len = token_len;
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007286 pos += pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007287 }
7288 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
7289 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
7290 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007291 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007292 goto drop;
7293 }
7294 }
7295
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007296 if (!quic_dec_int(&len, (const unsigned char **)&pos, end) ||
7297 end - pos < len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007298 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007299 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007300 goto drop;
7301 }
7302
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007303 /* Packet Number is stored here. Packet Length totalizes the
7304 * rest of the content.
7305 */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007306 pkt->pn_offset = pos - beg;
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007307 pkt->len = pkt->pn_offset + len;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007308
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007309 /* RFC 9000. Initial Datagram Size
7310 *
7311 * A server MUST discard an Initial packet that is carried in a UDP datagram
7312 * with a payload that is smaller than the smallest allowed maximum datagram
7313 * size of 1200 bytes.
7314 */
7315 if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
7316 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007317 TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007318 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
7319 goto drop;
7320 }
7321
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007322 /* Interrupt parsing after packet length retrieval : this
7323 * ensures that only the packet is dropped but not the whole
7324 * datagram.
7325 */
7326 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007327 TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007328 goto drop;
7329 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007330 }
7331 else {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007332 TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007333 if (end - pos < QUIC_HAP_CID_LEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007334 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007335 goto drop;
7336 }
7337
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007338 memcpy(pkt->dcid.data, pos, QUIC_HAP_CID_LEN);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007339 pkt->dcid.len = QUIC_HAP_CID_LEN;
7340
7341 /* When multiple QUIC packets are coalesced on the same UDP datagram,
7342 * they must have the same DCID.
7343 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007344 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007345 (pkt->dcid.len != dgram->dcid_len ||
7346 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007347 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007348 goto drop;
7349 }
7350
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007351 pos += QUIC_HAP_CID_LEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007352
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007353 pkt->pn_offset = pos - beg;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007354 /* A short packet is the last one of a UDP datagram. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007355 pkt->len = end - beg;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007356 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007357
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007358 TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007359 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007360 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007361
Amaury Denoyelle98289692022-10-19 15:37:44 +02007362 drop:
7363 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007364 drop_silent:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007365 if (!pkt->len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007366 pkt->len = end - beg;
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007367 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 +01007368 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007369 return -1;
7370}
7371
7372/* Check if received packet <pkt> should be drop due to <qc> already in closing
7373 * state. This can be true if a CONNECTION_CLOSE has already been emitted for
7374 * this connection.
7375 *
7376 * Returns false if connection is not in closing state else true. The caller
7377 * should drop the whole datagram in the last case to not mess up <qc>
7378 * CONNECTION_CLOSE rate limit counter.
7379 */
7380static int qc_rx_check_closing(struct quic_conn *qc,
7381 struct quic_rx_packet *pkt)
7382{
7383 if (!(qc->flags & QUIC_FL_CONN_CLOSING))
7384 return 0;
7385
7386 TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
7387
7388 /* Check if CONNECTION_CLOSE rate reemission is reached. */
7389 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
7390 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
7391 qc->nb_pkt_for_cc++;
7392 qc->nb_pkt_since_cc = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007393 }
7394
Amaury Denoyelle98289692022-10-19 15:37:44 +02007395 return 1;
7396}
7397
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007398/* React to a connection migration initiated on <qc> by a client with the new
7399 * path addresses <peer_addr>/<local_addr>.
7400 *
7401 * Returns 0 on success else non-zero.
7402 */
7403static int qc_handle_conn_migration(struct quic_conn *qc,
7404 const struct sockaddr_storage *peer_addr,
7405 const struct sockaddr_storage *local_addr)
7406{
7407 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7408
Frédéric Lécaille6fc86972023-01-12 08:29:23 +01007409 /* RFC 9000. Connection Migration
7410 *
7411 * If the peer sent the disable_active_migration transport parameter,
7412 * an endpoint also MUST NOT send packets (including probing packets;
7413 * see Section 9.1) from a different local address to the address the peer
7414 * used during the handshake, unless the endpoint has acted on a
7415 * preferred_address transport parameter from the peer.
7416 */
7417 if (qc->li->bind_conf->quic_params.disable_active_migration) {
7418 TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
7419 goto err;
7420 }
7421
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007422 /* RFC 9000 9. Connection Migration
7423 *
Amaury Denoyelleeb6be982022-11-21 11:14:45 +01007424 * The design of QUIC relies on endpoints retaining a stable address for
7425 * the duration of the handshake. An endpoint MUST NOT initiate
7426 * connection migration before the handshake is confirmed, as defined in
7427 * Section 4.1.2 of [QUIC-TLS].
7428 */
7429 if (qc->state < QUIC_HS_ST_COMPLETE) {
7430 TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
7431 goto err;
7432 }
7433
7434 /* RFC 9000 9. Connection Migration
7435 *
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007436 * TODO
7437 * An endpoint MUST
7438 * perform path validation (Section 8.2) if it detects any change to a
7439 * peer's address, unless it has previously validated that address.
7440 */
7441
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007442 /* Update quic-conn owned socket if in used.
7443 * TODO try to reuse it instead of closing and opening a new one.
7444 */
7445 if (qc_test_fd(qc)) {
7446 /* TODO try to reuse socket instead of closing it and opening a new one. */
7447 TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
7448 qc_release_fd(qc, 1);
Amaury Denoyellefb375572023-02-01 09:28:32 +01007449 /* TODO need to adjust <jobs> on socket allocation failure. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007450 qc_alloc_fd(qc, local_addr, peer_addr);
7451 }
7452
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007453 qc->local_addr = *local_addr;
7454 qc->peer_addr = *peer_addr;
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007455 qc->cntrs.conn_migration_done++;
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007456
7457 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7458 return 0;
7459
7460 err:
7461 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7462 return 1;
7463}
7464
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007465/* Release the memory for the RX packets which are no more referenced
7466 * and consume their payloads which have been copied to the RX buffer
7467 * for the connection.
7468 * Always succeeds.
7469 */
7470static inline void quic_rx_pkts_del(struct quic_conn *qc)
7471{
7472 struct quic_rx_packet *pkt, *pktback;
7473
7474 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
7475 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007476 "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007477 (long long)pkt->pn_node.key,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007478 pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007479 (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
7480 if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
7481 size_t cdata;
7482
7483 cdata = b_contig_data(&qc->rx.buf, 0);
7484 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007485 "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007486 if (cdata && !*b_head(&qc->rx.buf)) {
7487 /* Consume the remaining data */
7488 b_del(&qc->rx.buf, cdata);
7489 }
7490 break;
7491 }
7492
7493 if (pkt->refcnt)
7494 break;
7495
7496 b_del(&qc->rx.buf, pkt->raw_len);
7497 LIST_DELETE(&pkt->qc_rx_pkt_list);
7498 pool_free(pool_head_quic_rx_packet, pkt);
7499 }
7500
7501 /* In frequent cases the buffer will be emptied at this stage. */
7502 b_realign_if_empty(&qc->rx.buf);
7503}
7504
Amaury Denoyelle98289692022-10-19 15:37:44 +02007505/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
7506 * into <qc> receive buffer after header protection removal procedure.
7507 *
7508 * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
7509 * must point to packet buffer first byte.
7510 *
7511 * <tasklist_head> may be non-NULL when the caller treat several datagrams for
7512 * different quic-conn. In this case, each quic-conn tasklet will be appended
7513 * to it in order to be woken up after the current task.
7514 *
7515 * The caller can safely removed the packet data. If packet refcount was not
7516 * incremented by this function, it means that the connection did not handled
7517 * it and it should be freed by the caller.
7518 */
7519static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
7520 struct quic_dgram *dgram, unsigned char *beg,
7521 struct list **tasklist_head)
7522{
7523 const struct quic_version *qv = pkt->version;
7524 struct quic_enc_level *qel = NULL;
7525 size_t b_cspace;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007526
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007527 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7528 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Amaury Denoyelle3f474e62022-11-24 17:15:08 +01007529
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007530 if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007531 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
7532 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
7533 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007534 TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
7535 QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007536 /* Reset the anti-amplification bit. It will be set again
7537 * when sending the next packet if reached again.
7538 */
7539 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007540 qc_set_timer(qc);
7541 if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
7542 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007543 }
7544
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007545 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
7546 TRACE_PROTO("Connection error",
7547 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7548 goto out;
7549 }
7550
7551 pkt->raw_len = pkt->len;
7552 quic_rx_pkts_del(qc);
7553 b_cspace = b_contig_space(&qc->rx.buf);
7554 if (b_cspace < pkt->len) {
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007555 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007556 "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007557 /* Do not consume buf if space not at the end. */
7558 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
7559 TRACE_PROTO("Packet dropped",
7560 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007561 qc->cntrs.dropped_pkt_bufoverrun++;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007562 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007563 }
7564
7565 /* Let us consume the remaining contiguous space. */
7566 if (b_cspace) {
7567 b_putchr(&qc->rx.buf, 0x00);
7568 b_cspace--;
7569 }
7570 b_add(&qc->rx.buf, b_cspace);
7571 if (b_contig_space(&qc->rx.buf) < pkt->len) {
7572 TRACE_PROTO("Too big packet",
7573 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007574 qc->cntrs.dropped_pkt_bufoverrun++;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007575 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007576 }
7577 }
7578
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007579 if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007580 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7581 goto drop;
7582 }
7583
7584 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7585 if (pkt->aad_len)
7586 qc_pkt_insert(qc, pkt, qel);
7587 out:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02007588 *tasklist_head = tasklet_wakeup_after(*tasklist_head,
7589 qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007590
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007591 drop_silent:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007592 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7593 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007594 return;
7595
7596 drop:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007597 qc->cntrs.dropped_pkt++;
Frédéric Lécaille464281a2023-05-24 10:24:42 +02007598 TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7599 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007600}
7601
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007602/* This function builds into a buffer at <pos> position a QUIC long packet header,
7603 * <end> being one byte past the end of this buffer.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007604 * Return 1 if enough room to build this header, 0 if not.
7605 */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007606static int quic_build_packet_long_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007607 int type, size_t pn_len,
7608 struct quic_conn *qc, const struct quic_version *ver)
7609{
7610 int ret = 0;
7611
7612 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7613
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007614 if (end - *pos < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007615 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7616 goto leave;
7617 }
7618
7619 type = quic_pkt_type(type, ver->num);
7620 /* #0 byte flags */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007621 *(*pos)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007622 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
7623 /* Version */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007624 quic_write_uint32(pos, end, ver->num);
7625 *(*pos)++ = qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007626 /* Destination connection ID */
7627 if (qc->dcid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007628 memcpy(*pos, qc->dcid.data, qc->dcid.len);
7629 *pos += qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007630 }
7631 /* Source connection ID */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007632 *(*pos)++ = qc->scid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007633 if (qc->scid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007634 memcpy(*pos, qc->scid.data, qc->scid.len);
7635 *pos += qc->scid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007636 }
7637
7638 ret = 1;
7639 leave:
7640 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7641 return ret;
7642}
7643
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007644/* This function builds into a buffer at <pos> position a QUIC short packet header,
7645 * <end> being one byte past the end of this buffer.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007646 * Return 1 if enough room to build this header, 0 if not.
7647 */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007648static int quic_build_packet_short_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007649 size_t pn_len, struct quic_conn *qc,
7650 unsigned char tls_flags)
7651{
7652 int ret = 0;
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007653 unsigned char spin_bit =
7654 (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007655
7656 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7657
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007658 if (end - *pos < 1 + qc->dcid.len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007659 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7660 goto leave;
7661 }
7662
7663 /* #0 byte flags */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007664 *(*pos)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007665 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
7666 /* Destination connection ID */
7667 if (qc->dcid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007668 memcpy(*pos, qc->dcid.data, qc->dcid.len);
7669 *pos += qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007670 }
7671
7672 ret = 1;
7673 leave:
7674 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7675 return ret;
7676}
7677
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007678/* Apply QUIC header protection to the packet with <pos> as first byte address,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007679 * <pn> as address of the Packet number field, <pnlen> being this field length
7680 * with <aead> as AEAD cipher and <key> as secret key.
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007681 *
7682 * TODO no error is expected as encryption is done in place but encryption
7683 * manual is unclear. <fail> will be set to true if an error is detected.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007684 */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007685void quic_apply_header_protection(struct quic_conn *qc, unsigned char *pos,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007686 unsigned char *pn, size_t pnlen,
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007687 struct quic_tls_ctx *tls_ctx, int *fail)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007688
7689{
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007690 int i;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007691 /* We need an IV of at least 5 bytes: one byte for bytes #0
7692 * and at most 4 bytes for the packet number
7693 */
7694 unsigned char mask[5] = {0};
7695 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
7696
7697 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7698
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007699 *fail = 0;
7700
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007701 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
7702 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007703 *fail = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007704 goto out;
7705 }
7706
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007707 *pos ^= mask[0] & (*pos & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007708 for (i = 0; i < pnlen; i++)
7709 pn[i] ^= mask[i + 1];
7710
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007711 out:
7712 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007713}
7714
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007715/* Prepare into <outlist> as most as possible ack-eliciting frame from their
7716 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
7717 * with <room> as available room, and <*len> the packet Length field initialized
7718 * with the number of bytes already present in this buffer which must be taken
7719 * into an account for the Length packet field value. <headlen> is the number of
7720 * bytes already present in this packet before building frames.
7721 *
7722 * Update consequently <*len> to reflect the size of these frames built
7723 * by this function. Also attach these frames to <l> frame list.
7724 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
7725 */
7726static inline int qc_build_frms(struct list *outlist, struct list *inlist,
7727 size_t room, size_t *len, size_t headlen,
7728 struct quic_enc_level *qel,
7729 struct quic_conn *qc)
7730{
7731 int ret;
7732 struct quic_frame *cf, *cfbak;
7733
7734 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
7735
7736 ret = 0;
7737 if (*len > room)
7738 goto leave;
Amaury Denoyelle8c94dfc2024-06-05 11:37:44 +02007739 room -= *len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007740
7741 /* If we are not probing we must take into an account the congestion
7742 * control window.
7743 */
7744 if (!qel->pktns->tx.pto_probe) {
7745 size_t remain = quic_path_prep_data(qc->path);
7746
7747 if (headlen > remain)
7748 goto leave;
7749
7750 room = QUIC_MIN(room, remain - headlen);
7751 }
7752
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007753 TRACE_PROTO("TX frms build (headlen)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007754 QUIC_EV_CONN_BCFRMS, qc, &headlen);
7755
7756 /* NOTE: switch/case block inside a loop, a successful status must be
7757 * returned by this function only if at least one frame could be built
7758 * in the switch/case block.
7759 */
7760 list_for_each_entry_safe(cf, cfbak, inlist, list) {
7761 /* header length, data length, frame length. */
7762 size_t hlen, dlen, dlen_sz, avail_room, flen;
7763
7764 if (!room)
7765 break;
7766
7767 switch (cf->type) {
7768 case QUIC_FT_CRYPTO:
7769 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
7770 QUIC_EV_CONN_BCFRMS, qc, &room, len);
7771 /* Compute the length of this CRYPTO frame header */
7772 hlen = 1 + quic_int_getsize(cf->crypto.offset);
Amaury Denoyelle8c94dfc2024-06-05 11:37:44 +02007773 /* Compute the data length of this CRYPTO frame. */
7774 dlen = max_stream_data_size(room, hlen, cf->crypto.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007775 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
7776 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
7777 if (!dlen)
7778 continue;
7779
7780 /* CRYPTO frame length. */
7781 flen = hlen + quic_int_getsize(dlen) + dlen;
7782 TRACE_DEVEL(" CRYPTO frame length (flen)",
7783 QUIC_EV_CONN_BCFRMS, qc, &flen);
7784 /* Add the CRYPTO data length and its encoded length to the packet
7785 * length and the length of this length.
7786 */
7787 *len += flen;
7788 room -= flen;
7789 if (dlen == cf->crypto.len) {
7790 /* <cf> CRYPTO data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007791 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007792 LIST_APPEND(outlist, &cf->list);
7793 }
7794 else {
7795 struct quic_frame *new_cf;
7796
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007797 new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007798 if (!new_cf) {
7799 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
7800 continue;
7801 }
7802
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007803 new_cf->crypto.len = dlen;
7804 new_cf->crypto.offset = cf->crypto.offset;
7805 new_cf->crypto.qel = qel;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007806 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007807 if (cf->origin) {
7808 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7809 /* This <cf> frame was duplicated */
7810 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7811 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007812 /* Detach the remaining CRYPTO frame from its original frame */
7813 LIST_DEL_INIT(&cf->ref);
7814 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007815 }
7816 LIST_APPEND(outlist, &new_cf->list);
7817 /* Consume <dlen> bytes of the current frame. */
7818 cf->crypto.len -= dlen;
7819 cf->crypto.offset += dlen;
7820 }
7821 break;
7822
7823 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007824 if (cf->stream.dup) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007825 struct eb64_node *node = NULL;
7826 struct qc_stream_desc *stream_desc = NULL;
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007827 struct qf_stream *strm_frm = &cf->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007828
7829 /* As this frame has been already lost, ensure the stream is always
7830 * available or the range of this frame is not consumed before
7831 * resending it.
7832 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007833 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007834 if (!node) {
7835 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007836 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007837 continue;
7838 }
7839
7840 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007841 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007842 TRACE_DEVEL("ignored frame frame in already acked range",
7843 QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007844 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007845 continue;
7846 }
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007847 else if (strm_frm->offset.key < stream_desc->ack_offset) {
7848 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
Frédéric Lécailleca079792023-03-17 08:56:50 +01007849
Frédéric Lécaillec425e032023-03-20 14:32:59 +01007850 qc_stream_frm_mv_fwd(cf, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007851 TRACE_DEVEL("updated partially acked frame",
7852 QUIC_EV_CONN_PRSAFRM, qc, cf);
7853 }
7854 }
7855 /* Note that these frames are accepted in short packets only without
7856 * "Length" packet field. Here, <*len> is used only to compute the
7857 * sum of the lengths of the already built frames for this packet.
7858 *
7859 * Compute the length of this STREAM frame "header" made a all the field
7860 * excepting the variable ones. Note that +1 is for the type of this frame.
7861 */
7862 hlen = 1 + quic_int_getsize(cf->stream.id) +
7863 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
7864 /* Compute the data length of this STREAM frame. */
Amaury Denoyelle8c94dfc2024-06-05 11:37:44 +02007865 avail_room = room - hlen;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007866 if ((ssize_t)avail_room <= 0)
7867 continue;
7868
7869 TRACE_DEVEL(" New STREAM frame build (room, len)",
7870 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007871
7872 /* hlen contains STREAM id and offset. Ensure there is
7873 * enough room for length field.
7874 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007875 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007876 dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
7877 cf->stream.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007878 dlen_sz = quic_int_getsize(dlen);
7879 flen = hlen + dlen_sz + dlen;
7880 }
7881 else {
7882 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
7883 flen = hlen + dlen;
7884 }
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007885
7886 if (cf->stream.len && !dlen) {
7887 /* Only a small gap is left on buffer, not
7888 * enough to encode the STREAM data length.
7889 */
7890 continue;
7891 }
7892
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007893 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
7894 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
7895 TRACE_DEVEL(" STREAM frame length (flen)",
7896 QUIC_EV_CONN_BCFRMS, qc, &flen);
7897 /* Add the STREAM data length and its encoded length to the packet
7898 * length and the length of this length.
7899 */
7900 *len += flen;
7901 room -= flen;
7902 if (dlen == cf->stream.len) {
7903 /* <cf> STREAM data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007904 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007905 LIST_APPEND(outlist, &cf->list);
7906
7907 /* Do not notify MUX on retransmission. */
7908 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7909 qcc_streams_sent_done(cf->stream.stream->ctx,
7910 cf->stream.len,
7911 cf->stream.offset.key);
7912 }
7913 }
7914 else {
7915 struct quic_frame *new_cf;
7916 struct buffer cf_buf;
7917
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007918 new_cf = qc_frm_alloc(cf->type);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007919 if (!new_cf) {
7920 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
7921 continue;
7922 }
7923
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007924 new_cf->stream.stream = cf->stream.stream;
7925 new_cf->stream.buf = cf->stream.buf;
7926 new_cf->stream.id = cf->stream.id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01007927 new_cf->stream.offset = cf->stream.offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007928 new_cf->stream.len = dlen;
7929 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
7930 /* FIN bit reset */
7931 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
7932 new_cf->stream.data = cf->stream.data;
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007933 new_cf->stream.dup = cf->stream.dup;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007934 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007935 if (cf->origin) {
7936 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7937 /* This <cf> frame was duplicated */
7938 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7939 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007940 /* Detach this STREAM frame from its origin */
7941 LIST_DEL_INIT(&cf->ref);
7942 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007943 }
7944 LIST_APPEND(outlist, &new_cf->list);
7945 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
7946 /* Consume <dlen> bytes of the current frame. */
7947 cf_buf = b_make(b_orig(cf->stream.buf),
7948 b_size(cf->stream.buf),
7949 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
7950 cf->stream.len -= dlen;
7951 cf->stream.offset.key += dlen;
7952 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
7953
7954 /* Do not notify MUX on retransmission. */
7955 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7956 qcc_streams_sent_done(new_cf->stream.stream->ctx,
7957 new_cf->stream.len,
7958 new_cf->stream.offset.key);
7959 }
7960 }
7961
7962 /* TODO the MUX is notified about the frame sending via
7963 * previous qcc_streams_sent_done call. However, the
7964 * sending can fail later, for example if the sendto
7965 * system call returns an error. As the MUX has been
7966 * notified, the transport layer is responsible to
7967 * bufferize and resent the announced data later.
7968 */
7969
7970 break;
7971
7972 default:
7973 flen = qc_frm_len(cf);
7974 BUG_ON(!flen);
7975 if (flen > room)
7976 continue;
7977
7978 *len += flen;
7979 room -= flen;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007980 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007981 LIST_APPEND(outlist, &cf->list);
7982 break;
7983 }
7984
7985 /* Successful status as soon as a frame could be built */
7986 ret = 1;
7987 }
7988
7989 leave:
7990 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
7991 return ret;
7992}
7993
7994/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
7995 * is used as return parameter and should be zero'ed by the caller.
7996 */
7997static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
7998 struct quic_frame *out)
7999{
8000 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
8001 *
8002 * A CONNECTION_CLOSE frame should be sent in several packets with
8003 * different encryption levels depending on the client context. This is
8004 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
8005 * more details on how to implement it.
8006 */
8007 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
8008
8009
8010 if (qc->err.app) {
8011 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
8012 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
8013 /* RFC 9000 10.2.3. Immediate Close during the Handshake
8014 *
8015 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
8016 * packet could expose application state or be used to alter application
8017 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
8018 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
8019 * Handshake packets. Otherwise, information about the application
8020 * state might be revealed. Endpoints MUST clear the value of the
8021 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
8022 * converting to a CONNECTION_CLOSE of type 0x1c.
8023 */
8024 out->type = QUIC_FT_CONNECTION_CLOSE;
8025 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
8026 out->connection_close.reason_phrase_len = 0;
8027 }
8028 else {
8029 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
Amaury Denoyelle1d40fc92023-11-28 11:23:41 +01008030 out->connection_close_app.error_code = qc->err.code;
8031 out->connection_close_app.reason_phrase_len = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008032 }
8033 }
8034 else {
8035 out->type = QUIC_FT_CONNECTION_CLOSE;
8036 out->connection_close.error_code = qc->err.code;
8037 }
8038 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
8039
8040}
8041
8042/* This function builds a clear packet from <pkt> information (its type)
8043 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
8044 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
8045 * filling the buffer with as much frames as possible from <frms> list of
8046 * prebuilt frames.
8047 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
8048 * reserved so that to ensure there is enough room to build this AEAD TAG after
8049 * having returned from this function.
8050 * This function also updates the value of <buf_pn> pointer to point to the packet
8051 * number field in this packet. <pn_len> will also have the packet number
8052 * length as value.
8053 *
8054 * Return 1 if succeeded (enough room to buile this packet), O if not.
8055 */
8056static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
8057 size_t dglen, struct quic_tx_packet *pkt,
8058 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008059 int must_ack, int padding, int cc, int probe,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008060 struct quic_enc_level *qel, struct quic_conn *qc,
8061 const struct quic_version *ver, struct list *frms)
8062{
8063 unsigned char *beg, *payload;
8064 size_t len, len_sz, len_frms, padding_len;
8065 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
8066 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
8067 struct quic_frame cc_frm = { };
8068 size_t ack_frm_len, head_len;
8069 int64_t rx_largest_acked_pn;
8070 int add_ping_frm;
8071 struct list frm_list = LIST_HEAD_INIT(frm_list);
8072 struct quic_frame *cf;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008073 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008074
8075 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
8076
8077 /* Length field value with CRYPTO frames if present. */
8078 len_frms = 0;
8079 beg = pos;
8080 /* When not probing, and no immediate close is required, reduce the size of this
8081 * buffer to respect the congestion controller window.
8082 * This size will be limited if we have ack-eliciting frames to send from <frms>.
8083 */
8084 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
8085 size_t path_room;
8086
8087 path_room = quic_path_prep_data(qc->path);
8088 if (end - beg > path_room)
8089 end = beg + path_room;
8090 }
8091
8092 /* Ensure there is enough room for the TLS encryption tag and a zero token
8093 * length field if any.
8094 */
8095 if (end - pos < QUIC_TLS_TAG_LEN +
8096 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
8097 goto no_room;
8098
8099 end -= QUIC_TLS_TAG_LEN;
8100 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
8101 /* packet number length */
8102 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
8103 /* Build the header */
8104 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
8105 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
8106 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
8107 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
8108 goto no_room;
8109
8110 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02008111 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
8112 if (end <= pos)
8113 goto no_room;
8114
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008115 *pos++ = 0;
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02008116 }
8117
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008118 head_len = pos - beg;
8119 /* Build an ACK frame if required. */
8120 ack_frm_len = 0;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008121 /* Do not ack and probe at the same time. */
8122 if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008123 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
8124 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
8125 ack_frm.tx_ack.arngs = arngs;
8126 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
8127 qel->pktns->tx.ack_delay =
8128 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
8129 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
8130 }
8131 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
8132 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
8133 * smallest frame (PING) and <*pn_len> more for the packet number. Note
8134 * that from here, we do not know if we will have to send a PING frame.
8135 * This will be decided after having computed the ack-eliciting frames
8136 * to be added to this packet.
8137 */
Frédéric Lécaille9d68c6a2023-04-12 20:49:29 +02008138 if (end - pos <= 1 + *pn_len)
8139 goto no_room;
8140
Frédéric Lécaille4b2627b2023-04-17 13:42:42 +02008141 ack_frm_len = qc_frm_len(&ack_frm);
8142 if (ack_frm_len > end - 1 - *pn_len - pos)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008143 goto no_room;
8144 }
8145
8146 /* Length field value without the ack-eliciting frames. */
8147 len = ack_frm_len + *pn_len;
8148 len_frms = 0;
8149 if (!cc && !LIST_ISEMPTY(frms)) {
8150 ssize_t room = end - pos;
8151
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008152 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008153 /* Initialize the length of the frames built below to <len>.
8154 * If any frame could be successfully built by qc_build_frms(),
8155 * we will have len_frms > len.
8156 */
8157 len_frms = len;
8158 if (!qc_build_frms(&frm_list, frms,
8159 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008160 TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008161 qc, NULL, NULL, &room);
Frédéric Lécaille30cc6652023-11-07 18:29:28 +01008162 if (padding) {
8163 len_frms = 0;
8164 goto comp_pkt_len;
8165 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008166 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
8167 goto no_room;
8168 }
8169 }
8170
Frédéric Lécaille30cc6652023-11-07 18:29:28 +01008171comp_pkt_len:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008172 /* Length (of the remaining data). Must not fail because, the buffer size
8173 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
8174 * for the encryption tag. It must be taken into an account for the length
8175 * of this packet.
8176 */
8177 if (len_frms)
8178 len = len_frms + QUIC_TLS_TAG_LEN;
8179 else
8180 len += QUIC_TLS_TAG_LEN;
8181 /* CONNECTION_CLOSE frame */
8182 if (cc) {
8183 qc_build_cc_frm(qc, qel, &cc_frm);
8184 len += qc_frm_len(&cc_frm);
8185 }
8186 add_ping_frm = 0;
8187 padding_len = 0;
8188 len_sz = quic_int_getsize(len);
8189 /* Add this packet size to <dglen> */
8190 dglen += head_len + len_sz + len;
Frédéric Lécailleec937212023-03-03 17:34:41 +01008191 /* Note that <padding> is true only when building an Handshake packet
8192 * coalesced to an Initial packet.
8193 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008194 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
8195 /* This is a maximum padding size */
8196 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
8197 /* The length field value is of this packet is <len> + <padding_len>
8198 * the size of which may be greater than the initial computed size
8199 * <len_sz>. So, let's deduce the difference between these to packet
8200 * sizes from <padding_len>.
8201 */
8202 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
8203 len += padding_len;
8204 }
Frédéric Lécaille5faf5772023-02-16 17:30:53 +01008205 else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
8206 len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
8207 }
8208 else if (LIST_ISEMPTY(&frm_list)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008209 if (qel->pktns->tx.pto_probe) {
8210 /* If we cannot send a frame, we send a PING frame. */
8211 add_ping_frm = 1;
8212 len += 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01008213 dglen += 1;
8214 /* Note that only we are in the case where this Initial packet
8215 * is not coalesced to an Handshake packet. We must directly
8216 * pad the datragram.
8217 */
Frédéric Lécaille9c317b12023-03-28 15:39:11 +02008218 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
8219 if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
8220 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
8221 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
8222 len += padding_len;
8223 }
8224 }
8225 else {
8226 /* Note that +1 is for the PING frame */
8227 if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
8228 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01008229 }
8230 }
8231 else {
8232 /* If there is no frame at all to follow, add at least a PADDING frame. */
8233 if (!ack_frm_len && !cc)
8234 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008235 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008236 }
8237
8238 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
8239 goto no_room;
8240
8241 /* Packet number field address. */
8242 *buf_pn = pos;
8243
8244 /* Packet number encoding. */
8245 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
8246 goto no_room;
8247
8248 /* payload building (ack-eliciting or not frames) */
8249 payload = pos;
8250 if (ack_frm_len) {
8251 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
8252 goto no_room;
8253
8254 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
8255 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
8256 }
8257
8258 /* Ack-eliciting frames */
8259 if (!LIST_ISEMPTY(&frm_list)) {
8260 struct quic_frame *tmp_cf;
8261 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
8262 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
8263 ssize_t room = end - pos;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008264 TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008265 qc, NULL, NULL, &room);
8266 /* Note that <cf> was added from <frms> to <frm_list> list by
8267 * qc_build_frms().
8268 */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01008269 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008270 LIST_INSERT(frms, &cf->list);
8271 continue;
8272 }
8273
8274 quic_tx_packet_refinc(pkt);
8275 cf->pkt = pkt;
8276 }
8277 }
8278
8279 /* Build a PING frame if needed. */
8280 if (add_ping_frm) {
8281 frm.type = QUIC_FT_PING;
8282 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
8283 goto no_room;
8284 }
8285
8286 /* Build a CONNECTION_CLOSE frame if needed. */
8287 if (cc) {
8288 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
8289 goto no_room;
8290
8291 pkt->flags |= QUIC_FL_TX_PACKET_CC;
8292 }
8293
8294 /* Build a PADDING frame if needed. */
8295 if (padding_len) {
8296 frm.type = QUIC_FT_PADDING;
8297 frm.padding.len = padding_len;
8298 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
8299 goto no_room;
8300 }
8301
8302 if (pos == payload) {
8303 /* No payload was built because of congestion control */
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008304 TRACE_PROTO("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008305 goto no_room;
8306 }
8307
8308 /* If this packet is ack-eliciting and we are probing let's
8309 * decrement the PTO probe counter.
8310 */
Frédéric Lécaille0e7f9da2023-07-20 15:45:41 +02008311 if ((pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008312 qel->pktns->tx.pto_probe)
8313 qel->pktns->tx.pto_probe--;
8314
8315 pkt->len = pos - beg;
8316 LIST_SPLICE(&pkt->frms, &frm_list);
8317
8318 ret = 1;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008319 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008320 leave:
8321 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
8322 return ret;
8323
8324 no_room:
8325 /* Replace the pre-built frames which could not be add to this packet */
8326 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008327 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008328 goto leave;
8329}
8330
8331static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
8332{
8333 pkt->type = type;
8334 pkt->len = 0;
8335 pkt->in_flight_len = 0;
8336 pkt->pn_node.key = (uint64_t)-1;
8337 LIST_INIT(&pkt->frms);
8338 pkt->time_sent = TICK_ETERNITY;
8339 pkt->next = NULL;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01008340 pkt->prev = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008341 pkt->largest_acked_pn = -1;
8342 pkt->flags = 0;
8343 pkt->refcnt = 0;
8344}
8345
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008346/* Build a packet into a buffer at <pos> position, <end> pointing to one byte past
8347 * the end of this buffer, with <pkt_type> as packet type for <qc> QUIC connection
8348 * at <qel> encryption level with <frms> list of prebuilt frames.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008349 *
Frédéric Lécaille21017002023-11-08 11:31:21 +01008350 + * Return -3 if the packet could not be allocated, -2 if could not be encrypted for
8351 + * any reason, -1 if there was not enough room to build a packet.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008352 * XXX NOTE XXX
8353 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
8354 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
8355 * control window limitation.
8356 */
8357static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008358 const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008359 struct quic_enc_level *qel,
8360 struct quic_tls_ctx *tls_ctx, struct list *frms,
8361 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008362 size_t dglen, int pkt_type, int must_ack,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008363 int padding, int probe, int cc, int *err)
8364{
8365 struct quic_tx_packet *ret_pkt = NULL;
8366 /* The pointer to the packet number field. */
8367 unsigned char *buf_pn;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008368 unsigned char *first_byte, *last_byte, *payload;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008369 int64_t pn;
8370 size_t pn_len, payload_len, aad_len;
8371 struct quic_tx_packet *pkt;
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008372 int encrypt_failure = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008373
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008374 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
8375 TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008376 *err = 0;
8377 pkt = pool_alloc(pool_head_quic_tx_packet);
8378 if (!pkt) {
8379 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille21017002023-11-08 11:31:21 +01008380 *err = -3;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008381 goto err;
8382 }
8383
8384 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008385 first_byte = *pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008386 pn_len = 0;
8387 buf_pn = NULL;
8388
8389 pn = qel->pktns->tx.next_pn + 1;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008390 if (!qc_do_build_pkt(*pos, end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008391 must_ack, padding, cc, probe, qel, qc, ver, frms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008392 // trace already emitted by function above
8393 *err = -1;
8394 goto err;
8395 }
8396
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008397 last_byte = first_byte + pkt->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008398 payload = buf_pn + pn_len;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008399 payload_len = last_byte - payload;
8400 aad_len = payload - first_byte;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008401
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008402 quic_packet_encrypt(payload, payload_len, first_byte, aad_len, pn, tls_ctx, qc, &encrypt_failure);
8403 if (encrypt_failure) {
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02008404 /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008405 WARN_ON("quic_packet_encrypt failure");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008406 *err = -2;
8407 goto err;
8408 }
8409
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008410 last_byte += QUIC_TLS_TAG_LEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008411 pkt->len += QUIC_TLS_TAG_LEN;
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008412 quic_apply_header_protection(qc, first_byte, buf_pn, pn_len, tls_ctx, &encrypt_failure);
8413 if (encrypt_failure) {
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02008414 /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008415 WARN_ON("quic_apply_header_protection failure");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008416 *err = -2;
8417 goto err;
8418 }
8419
8420 /* Consume a packet number */
8421 qel->pktns->tx.next_pn++;
8422 qc->tx.prep_bytes += pkt->len;
8423 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
8424 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
8425 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
8426 }
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008427
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008428 /* Now that a correct packet is built, let us consume <*pos> buffer. */
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008429 *pos = last_byte;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008430 /* Attach the built packet to its tree. */
8431 pkt->pn_node.key = pn;
8432 /* Set the packet in fligth length for in flight packet only. */
8433 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
8434 pkt->in_flight_len = pkt->len;
8435 qc->path->prep_in_flight += pkt->len;
8436 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01008437 /* Always reset this flag */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008438 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
8439 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
8440 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
8441 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
Frédéric Lécailled7215712023-03-24 18:13:37 +01008442 qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
8443 if (tick_isset(qc->ack_expire)) {
8444 qc->ack_expire = TICK_ETERNITY;
8445 qc->idle_timer_task->expire = qc->idle_expire;
8446 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02008447 TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01008448 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008449 }
8450
8451 pkt->pktns = qel->pktns;
8452
8453 ret_pkt = pkt;
8454 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008455 TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
8456 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008457 return ret_pkt;
8458
8459 err:
8460 /* TODO: what about the frames which have been built
8461 * for this packet.
8462 */
8463 free_quic_tx_packet(qc, pkt);
8464 goto leave;
8465}
8466
8467
8468static void __quic_conn_init(void)
8469{
8470 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
8471}
8472INITCALL0(STG_REGISTER, __quic_conn_init);
8473
8474static void __quic_conn_deinit(void)
8475{
8476 BIO_meth_free(ha_quic_meth);
8477}
8478REGISTER_POST_DEINIT(__quic_conn_deinit);
8479
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008480/* Handle a new <dgram> received. Parse each QUIC packets and copied their
8481 * content to a quic-conn instance. The datagram content can be released after
8482 * this function.
8483 *
8484 * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
8485 * to the connection instance. <li> is the attached listener. The caller is
8486 * responsible to ensure that the first packet is destined to this connection
8487 * by comparing CIDs.
8488 *
8489 * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
8490 * function will thus retrieve the connection from the CID tree or allocate a
8491 * new one if possible. <li> is the listener attached to the receiver.
8492 *
8493 * Returns 0 on success else non-zero. If an error happens, some packets from
8494 * the datagram may not have been parsed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008495 */
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008496int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
8497 struct listener *li)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008498{
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008499 struct quic_rx_packet *pkt;
8500 struct quic_conn *qc = NULL;
8501 unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008502 struct list *tasklist_head = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008503
8504 TRACE_ENTER(QUIC_EV_CONN_LPKT);
8505
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008506 pos = dgram->buf;
8507 end = pos + dgram->len;
8508 do {
8509 /* TODO replace zalloc -> alloc. */
8510 pkt = pool_zalloc(pool_head_quic_rx_packet);
8511 if (!pkt) {
8512 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
8513 goto err;
8514 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008515
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008516 pkt->version = NULL;
8517 pkt->pn_offset = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008518
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008519 /* Set flag if pkt is the first one in dgram. */
8520 if (pos == dgram->buf)
8521 pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008522
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008523 LIST_INIT(&pkt->qc_rx_pkt_list);
8524 pkt->time_received = now_ms;
8525 quic_rx_packet_refinc(pkt);
8526 if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
8527 goto next;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008528
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008529 /* Search quic-conn instance for first packet of the datagram.
8530 * quic_rx_packet_parse() is responsible to discard packets
8531 * with different DCID as the first one in the same datagram.
8532 */
8533 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008534 int new_tid = -1;
8535
8536 qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li, &new_tid);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008537 /* qc is NULL if receiving a non Initial packet for an
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008538 * unknown connection or on connection affinity rebind.
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008539 */
8540 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008541 if (new_tid >= 0) {
8542 MT_LIST_APPEND(&quic_dghdlrs[new_tid].dgrams,
8543 &dgram->handler_list);
8544 tasklet_wakeup(quic_dghdlrs[new_tid].task);
Frédéric Lécaille4f401132023-11-22 16:29:08 +01008545 pool_free(pool_head_quic_rx_packet, pkt);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008546 goto out;
8547 }
8548
Amaury Denoyelle98289692022-10-19 15:37:44 +02008549 /* Skip the entire datagram. */
8550 pkt->len = end - pos;
8551 goto next;
8552 }
8553
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008554 dgram->qc = qc;
8555 }
Amaury Denoyelle98289692022-10-19 15:37:44 +02008556
Amaury Denoyelle8fc267b2023-11-20 14:56:49 +01008557 /* Ensure thread connection migration is finalized ASAP. */
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008558 if (qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED)
8559 qc_finalize_affinity_rebind(qc);
8560
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008561 if (qc_rx_check_closing(qc, pkt)) {
8562 /* Skip the entire datagram. */
8563 pkt->len = end - pos;
8564 goto next;
8565 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008566
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008567 /* Detect QUIC connection migration. */
Frédéric Lécaillef6769542023-01-12 10:36:26 +01008568 if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008569 if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
8570 /* Skip the entire datagram. */
8571 TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
8572 pkt->len = end - pos;
8573 goto next;
8574 }
8575 }
8576
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008577 qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008578
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008579 next:
8580 pos += pkt->len;
8581 quic_rx_packet_refdec(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008582
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008583 /* Free rejected packets */
8584 if (!pkt->refcnt) {
8585 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
8586 pool_free(pool_head_quic_rx_packet, pkt);
8587 }
8588 } while (pos < end);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008589
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008590 /* Increasing the received bytes counter by the UDP datagram length
8591 * if this datagram could be associated to a connection.
8592 */
8593 if (dgram->qc)
8594 dgram->qc->rx.bytes += dgram->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008595
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008596 /* This must never happen. */
8597 BUG_ON(pos > end);
8598 BUG_ON(pos < end || pos > dgram->buf + dgram->len);
8599 /* Mark this datagram as consumed */
8600 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008601
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008602 out:
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008603 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
8604 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008605
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008606 err:
Amaury Denoyellea65dd3a2023-04-19 14:26:16 +02008607 /* Mark this datagram as consumed as maybe at least some packets were parsed. */
8608 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008609 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008610 return -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008611}
8612
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008613/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
8614 * CIDs. This can be used to determine if a datagram is addressed to the right
8615 * connection instance.
8616 *
8617 * Returns a boolean value.
8618 */
8619int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len)
8620{
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008621 const uchar idx = _quic_cid_tree_idx(dcid);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008622 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008623 struct ebmb_node *node = NULL;
8624 struct quic_cid_tree *tree = &quic_cid_trees[idx];
Willy Tarreau92033572024-06-30 06:23:30 +02008625 int ret;
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008626
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008627 /* Test against our default CID or client ODCID. */
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008628 if ((qc->scid.len == dcid_len &&
8629 memcmp(qc->scid.data, dcid, dcid_len) == 0) ||
8630 (qc->odcid.len == dcid_len &&
Frédéric Lécaille07846cb2023-02-13 16:14:24 +01008631 memcmp(qc->odcid.data, dcid, dcid_len) == 0)) {
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008632 return 1;
8633 }
8634
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008635 /* Test against our other CIDs. This can happen if the client has
8636 * decided to switch to a new one.
8637 *
8638 * TODO to avoid locking, loop through qc.cids as an alternative.
8639 *
8640 * TODO set it to our default CID to avoid this operation next time.
8641 */
Willy Tarreau92033572024-06-30 06:23:30 +02008642 ret = 0;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008643 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
8644 node = ebmb_lookup(&tree->root, dcid, dcid_len);
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008645 if (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008646 conn_id = ebmb_entry(node, struct quic_connection_id, node);
8647 if (qc == conn_id->qc)
Willy Tarreau92033572024-06-30 06:23:30 +02008648 ret = 1;
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008649 }
Amaury Denoyelle60938b72024-06-27 18:15:08 +02008650 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008651
Willy Tarreau92033572024-06-30 06:23:30 +02008652 return ret;
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008653}
8654
Willy Tarreaudd9f9212023-05-07 07:07:44 +02008655/* Retrieve the DCID from a QUIC datagram or packet at <pos> position,
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008656 * <end> being at one byte past the end of this datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008657 * Returns 1 if succeeded, 0 if not.
8658 */
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008659int quic_get_dgram_dcid(unsigned char *pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008660 unsigned char **dcid, size_t *dcid_len)
8661{
8662 int ret = 0, long_header;
8663 size_t minlen, skip;
8664
8665 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
8666
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008667 if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008668 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
8669 goto err;
8670 }
8671
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008672 long_header = *pos & QUIC_PACKET_LONG_HEADER_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008673 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
8674 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
8675 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008676 if (end - pos < minlen)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008677 goto err;
8678
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008679 pos += skip;
8680 *dcid_len = long_header ? *pos++ : QUIC_HAP_CID_LEN;
8681 if (*dcid_len > QUIC_CID_MAXLEN || end - pos <= *dcid_len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008682 goto err;
8683
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008684 *dcid = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008685
8686 ret = 1;
8687 leave:
8688 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
8689 return ret;
8690
8691 err:
8692 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
8693 goto leave;
8694}
8695
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008696/* Notify upper layer of a fatal error which forces to close the connection. */
8697void qc_notify_err(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008698{
8699 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
8700
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008701 if (qc->mux_state == QC_MUX_READY) {
8702 TRACE_STATE("error notified to mux", QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008703
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008704 /* Mark socket as closed. */
8705 qc->conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
8706
8707 /* TODO quic-conn layer must stay active until MUX is released.
8708 * Thus, we have to wake up directly to ensure upper stream
8709 * layer will be notified of the error. If a proper separation
8710 * is made between MUX and quic-conn layer, wake up could be
8711 * conducted only with qc.subs.
8712 */
8713 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008714 }
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008715
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008716 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
8717}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008718
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008719/* Wake-up upper layer for sending if all conditions are met :
8720 * - room in congestion window or probe packet to sent
8721 * - socket FD ready to sent or listener socket used
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008722 *
8723 * Returns 1 if upper layer has been woken up else 0.
8724 */
8725int qc_notify_send(struct quic_conn *qc)
8726{
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008727 const struct quic_pktns *pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8728
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008729 if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008730 /* RFC 9002 7.5. Probe Timeout
8731 *
8732 * Probe packets MUST NOT be blocked by the congestion controller.
8733 */
8734 if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
Amaury Denoyellecaa16542023-02-28 15:11:26 +01008735 (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008736 tasklet_wakeup(qc->subs->tasklet);
8737 qc->subs->events &= ~SUB_RETRY_SEND;
8738 if (!qc->subs->events)
8739 qc->subs = NULL;
8740
8741 return 1;
8742 }
8743 }
8744
8745 return 0;
8746}
8747
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008748/* Move a <qc> QUIC connection and its resources from the current thread to the
Willy Tarreau77d37b02023-04-20 19:03:49 +02008749 * new one <new_tid> optionally in association with <new_li> (since it may need
8750 * to change when migrating to a thread from a different group, otherwise leave
8751 * it NULL). After this call, the connection cannot be dereferenced anymore on
8752 * the current thread.
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008753 *
8754 * Returns 0 on success else non-zero.
8755 */
Willy Tarreau77d37b02023-04-20 19:03:49 +02008756int qc_set_tid_affinity(struct quic_conn *qc, uint new_tid, struct listener *new_li)
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008757{
8758 struct task *t1 = NULL, *t2 = NULL;
8759 struct tasklet *t3 = NULL;
8760
8761 struct quic_connection_id *conn_id;
8762 struct eb64_node *node;
8763
8764 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8765
8766 /* Pre-allocate all required resources. This ensures we do not left a
8767 * connection with only some of its field rebinded.
8768 */
8769 if (((t1 = task_new_on(new_tid)) == NULL) ||
8770 (qc->timer_task && (t2 = task_new_on(new_tid)) == NULL) ||
8771 (t3 = tasklet_new()) == NULL) {
8772 goto err;
8773 }
8774
8775 /* Reinit idle timer task. */
8776 task_kill(qc->idle_timer_task);
8777 t1->expire = qc->idle_timer_task->expire;
8778 qc->idle_timer_task = t1;
8779 qc->idle_timer_task->process = qc_idle_timer_task;
8780 qc->idle_timer_task->context = qc;
8781
8782 /* Reinit timer task if allocated. */
8783 if (qc->timer_task) {
8784 task_kill(qc->timer_task);
8785 qc->timer_task = t2;
8786 qc->timer_task->process = qc_process_timer;
8787 qc->timer_task->context = qc;
8788 }
8789
8790 /* Reinit IO tasklet. */
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008791 if (qc->wait_event.tasklet->state & TASK_IN_LIST)
8792 qc->flags |= QUIC_FL_CONN_IO_TO_REQUEUE;
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008793 tasklet_kill(qc->wait_event.tasklet);
8794 /* In most cases quic_conn_app_io_cb is used but for 0-RTT quic_conn_io_cb can be still activated. */
8795 t3->process = qc->wait_event.tasklet->process;
8796 qc->wait_event.tasklet = t3;
8797 qc->wait_event.tasklet->tid = new_tid;
8798 qc->wait_event.tasklet->context = qc;
8799 qc->wait_event.events = 0;
8800
8801 /* Rebind the connection FD. */
8802 if (qc_test_fd(qc)) {
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008803 /* Reading is reactivated by the new thread. */
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008804 fd_migrate_on(qc->fd, new_tid);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008805 }
8806
Amaury Denoyelle7b516d32023-04-26 16:12:12 +02008807 /* Remove conn from per-thread list instance. It will be hidden from
8808 * "show quic" until rebinding is completed.
8809 */
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008810 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008811
8812 node = eb64_first(&qc->cids);
Amaury Denoyelleb75338e2024-03-04 18:41:39 +01008813 /* One and only one CID must be present before affinity rebind.
8814 *
8815 * This could be triggered fairly easily if tasklet is scheduled just
8816 * before thread migration for post-handshake state to generate new
8817 * CIDs. In this case, QUIC_FL_CONN_IO_TO_REQUEUE should be used
8818 * instead of tasklet_wakeup().
8819 */
8820 BUG_ON(!node || eb64_next(node));
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008821 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
Willy Tarreau77d37b02023-04-20 19:03:49 +02008822
8823 /* At this point no connection was accounted for yet on this
8824 * listener so it's OK to just swap the pointer.
8825 */
8826 if (new_li && new_li != qc->li)
8827 qc->li = new_li;
8828
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008829 /* Rebinding is considered done when CID points to the new thread. No
8830 * access should be done to quic-conn instance after it.
8831 */
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008832 qc->flags |= QUIC_FL_CONN_AFFINITY_CHANGED;
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008833 HA_ATOMIC_STORE(&conn_id->tid, new_tid);
8834 qc = NULL;
8835
8836 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, NULL);
8837 return 0;
8838
8839 err:
8840 task_destroy(t1);
8841 task_destroy(t2);
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02008842 tasklet_free(t3);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008843
8844 TRACE_DEVEL("leaving on error", QUIC_EV_CONN_SET_AFFINITY, qc);
8845 return 1;
8846}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008847
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008848/* Must be called after qc_set_tid_affinity() on the new thread. */
8849void qc_finalize_affinity_rebind(struct quic_conn *qc)
8850{
8851 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8852
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008853 /* This function must not be called twice after an affinity rebind. */
8854 BUG_ON(!(qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED));
8855 qc->flags &= ~QUIC_FL_CONN_AFFINITY_CHANGED;
8856
Amaury Denoyelle7b516d32023-04-26 16:12:12 +02008857 /* A connection must not pass to closing state until affinity rebind
8858 * is completed. Else quic_handle_stopping() may miss it during process
8859 * stopping cleanup.
8860 */
8861 BUG_ON(qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING));
8862
8863 /* Reinsert connection in ha_thread_ctx global list. */
8864 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
8865 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
8866
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008867 /* Reactivate FD polling if connection socket is active. */
8868 qc_want_recv(qc);
8869
8870 /* Reactivate timer task if needed. */
8871 qc_set_timer(qc);
8872
8873 /* Idle timer task is always active. */
8874 task_queue(qc->idle_timer_task);
8875
8876 /* Reactivate IO tasklet if needed. */
8877 if (qc->flags & QUIC_FL_CONN_IO_TO_REQUEUE) {
8878 tasklet_wakeup(qc->wait_event.tasklet);
8879 qc->flags &= ~QUIC_FL_CONN_IO_TO_REQUEUE;
8880 }
8881
8882 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, qc);
8883}
8884
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008885enum quic_dump_format {
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008886 QUIC_DUMP_FMT_ONELINE,
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008887 QUIC_DUMP_FMT_FULL,
8888};
8889
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008890/* appctx context used by "show quic" command */
8891struct show_quic_ctx {
8892 unsigned int epoch;
8893 struct bref bref; /* back-reference to the quic-conn being dumped */
8894 unsigned int thr;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008895 int flags;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008896 enum quic_dump_format format;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008897};
8898
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008899#define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */
8900
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008901static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private)
8902{
8903 struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008904 int argc = 2;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008905
8906 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
8907 return 1;
8908
8909 ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1);
8910 ctx->thr = 0;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008911 ctx->flags = 0;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008912 ctx->format = QUIC_DUMP_FMT_ONELINE;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008913
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008914 if (strcmp(args[argc], "oneline") == 0) {
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008915 /* format already used as default value */
8916 ++argc;
8917 }
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008918 else if (strcmp(args[argc], "full") == 0) {
8919 ctx->format = QUIC_DUMP_FMT_FULL;
8920 ++argc;
8921 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008922
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008923 while (*args[argc]) {
8924 if (strcmp(args[argc], "all") == 0)
8925 ctx->flags |= QC_CLI_FL_SHOW_ALL;
8926
8927 ++argc;
8928 }
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01008929
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008930 LIST_INIT(&ctx->bref.users);
8931
8932 return 0;
8933}
8934
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008935/* Dump for "show quic" with "oneline" format. */
8936static void dump_quic_oneline(struct show_quic_ctx *ctx, struct quic_conn *qc)
8937{
8938 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008939 int ret;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008940 unsigned char cid_len;
8941
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008942 ret = chunk_appendf(&trash, "%p[%02u]/%-.12s ", qc, ctx->thr,
8943 qc->li->bind_conf->frontend->id);
8944 chunk_appendf(&trash, "%*s", 36 - ret, " "); /* align output */
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008945
8946 /* State */
8947 if (qc->flags & QUIC_FL_CONN_CLOSING)
8948 chunk_appendf(&trash, "CLOSE ");
8949 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8950 chunk_appendf(&trash, "DRAIN ");
8951 else if (qc->state < QUIC_HS_ST_COMPLETE)
8952 chunk_appendf(&trash, "HDSHK ");
8953 else
8954 chunk_appendf(&trash, "ESTAB ");
8955
8956 /* Bytes in flight / Lost packets */
8957 chunk_appendf(&trash, "%9llu %6llu %6llu ",
8958 (ullong)qc->path->in_flight,
8959 (ullong)qc->path->ifae_pkts,
8960 (ullong)qc->path->loss.nb_lost_pkt);
8961
8962 /* Socket */
8963 if (qc->local_addr.ss_family == AF_INET ||
8964 qc->local_addr.ss_family == AF_INET6) {
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02008965 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8966 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
8967 chunk_appendf(&trash, "%15s:%-5s ", bufaddr, bufport);
8968
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008969 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8970 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008971 chunk_appendf(&trash, "%15s:%-5s ", bufaddr, bufport);
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008972
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008973 }
8974
8975 /* CIDs */
8976 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
8977 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
8978
8979 chunk_appendf(&trash, " ");
8980 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
8981 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
8982
8983 chunk_appendf(&trash, "\n");
8984}
8985
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008986/* Dump for "show quic" with "full" format. */
8987static void dump_quic_full(struct show_quic_ctx *ctx, struct quic_conn *qc)
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008988{
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008989 struct quic_pktns *pktns;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008990 struct eb64_node *node;
8991 struct qc_stream_desc *stream;
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008992 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02008993 int expire, i, addnl;
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008994 unsigned char cid_len;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008995
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02008996 addnl = 0;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008997 /* CIDs */
8998 chunk_appendf(&trash, "* %p[%02u]: scid=", qc, ctx->thr);
8999 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
9000 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
9001 while (cid_len++ < 20)
9002 chunk_appendf(&trash, "..");
9003
9004 chunk_appendf(&trash, " dcid=");
9005 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
9006 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
9007 while (cid_len++ < 20)
9008 chunk_appendf(&trash, "..");
9009
9010 chunk_appendf(&trash, "\n");
9011
9012 chunk_appendf(&trash, " loc. TPs:");
9013 quic_transport_params_dump(&trash, qc, &qc->rx.params);
9014 chunk_appendf(&trash, "\n");
9015 chunk_appendf(&trash, " rem. TPs:");
9016 quic_transport_params_dump(&trash, qc, &qc->tx.params);
9017 chunk_appendf(&trash, "\n");
9018
9019 /* Connection state */
9020 if (qc->flags & QUIC_FL_CONN_CLOSING)
9021 chunk_appendf(&trash, " st=closing ");
9022 else if (qc->flags & QUIC_FL_CONN_DRAINING)
9023 chunk_appendf(&trash, " st=draining ");
9024 else if (qc->state < QUIC_HS_ST_CONFIRMED)
9025 chunk_appendf(&trash, " st=handshake ");
9026 else
9027 chunk_appendf(&trash, " st=opened ");
9028
9029 if (qc->mux_state == QC_MUX_NULL)
9030 chunk_appendf(&trash, "mux=null ");
9031 else if (qc->mux_state == QC_MUX_READY)
9032 chunk_appendf(&trash, "mux=ready ");
9033 else
9034 chunk_appendf(&trash, "mux=released ");
9035
9036 expire = qc->idle_expire;
9037 chunk_appendf(&trash, "expire=%02ds ",
9038 TICKS_TO_MS(tick_remain(now_ms, expire)) / 1000);
9039
9040 chunk_appendf(&trash, "\n");
9041
9042 /* Socket */
9043 chunk_appendf(&trash, " fd=%d", qc->fd);
9044 if (qc->local_addr.ss_family == AF_INET ||
9045 qc->local_addr.ss_family == AF_INET6) {
9046 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
9047 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02009048 chunk_appendf(&trash, " local_addr=%s:%s", bufaddr, bufport);
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009049
9050 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
9051 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02009052 chunk_appendf(&trash, " foreign_addr=%s:%s", bufaddr, bufport);
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009053 }
9054
9055 chunk_appendf(&trash, "\n");
9056
9057 /* Packet number spaces information */
9058 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
9059 chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu",
9060 pktns->rx.arngs.sz, pktns->tx.in_flight);
9061 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
9062 chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
9063 pktns->rx.arngs.sz, pktns->tx.in_flight);
9064 pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
9065 chunk_appendf(&trash, " [01rtt] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
9066 pktns->rx.arngs.sz, pktns->tx.in_flight);
9067
9068 chunk_appendf(&trash, " srtt=%-4u rttvar=%-4u rttmin=%-4u ptoc=%-4u cwnd=%-6llu"
Amaury Denoyelle64f67412024-02-23 17:28:49 +01009069 " mcwnd=%-6llu sentpkts=%-6llu lostpkts=%-6llu reorderedpkts=%-6llu\n",
Frédéric Lécaille35fb5932023-09-05 15:24:11 +02009070 qc->path->loss.srtt, qc->path->loss.rtt_var,
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009071 qc->path->loss.rtt_min, qc->path->loss.pto_count, (ullong)qc->path->cwnd,
Frederic Lecailledfeda3a2024-02-13 21:24:40 +01009072 (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 +02009073
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02009074 if (qc->cntrs.dropped_pkt) {
9075 chunk_appendf(&trash, " droppkts=%-6llu", qc->cntrs.dropped_pkt);
9076 addnl = 1;
9077 }
9078 if (qc->cntrs.dropped_pkt_bufoverrun) {
9079 chunk_appendf(&trash, " dropbuff=%-6llu", qc->cntrs.dropped_pkt_bufoverrun);
9080 addnl = 1;
9081 }
9082 if (qc->cntrs.dropped_parsing) {
9083 chunk_appendf(&trash, " droppars=%-6llu", qc->cntrs.dropped_parsing);
9084 addnl = 1;
9085 }
9086 if (qc->cntrs.socket_full) {
9087 chunk_appendf(&trash, " sockfull=%-6llu", qc->cntrs.socket_full);
9088 addnl = 1;
9089 }
9090 if (qc->cntrs.sendto_err) {
9091 chunk_appendf(&trash, " sendtoerr=%-6llu", qc->cntrs.sendto_err);
9092 addnl = 1;
9093 }
9094 if (qc->cntrs.sendto_err_unknown) {
9095 chunk_appendf(&trash, " sendtounknerr=%-6llu", qc->cntrs.sendto_err);
9096 addnl = 1;
9097 }
9098 if (qc->cntrs.conn_migration_done) {
9099 chunk_appendf(&trash, " migrdone=%-6llu", qc->cntrs.conn_migration_done);
9100 addnl = 1;
9101 }
9102 if (qc->cntrs.data_blocked) {
9103 chunk_appendf(&trash, " datablocked=%-6llu", qc->cntrs.data_blocked);
9104 addnl = 1;
9105 }
9106 if (qc->cntrs.stream_data_blocked) {
9107 chunk_appendf(&trash, " sdatablocked=%-6llu", qc->cntrs.stream_data_blocked);
9108 addnl = 1;
9109 }
9110 if (qc->cntrs.streams_blocked_bidi) {
9111 chunk_appendf(&trash, " sblockebidi=%-6llu", qc->cntrs.streams_blocked_bidi);
9112 addnl = 1;
9113 }
9114 if (qc->cntrs.streams_blocked_uni) {
9115 chunk_appendf(&trash, " sblockeduni=%-6llu", qc->cntrs.streams_blocked_uni);
9116 addnl = 1;
9117 }
9118 if (addnl)
9119 chunk_appendf(&trash, "\n");
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009120
9121 /* Streams */
9122 node = eb64_first(&qc->streams_by_id);
9123 i = 0;
9124 while (node) {
9125 stream = eb64_entry(node, struct qc_stream_desc, by_id);
9126 node = eb64_next(node);
9127
9128 chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key);
9129 chunk_appendf(&trash, " off=%-8llu ack=%-8llu",
9130 (unsigned long long)stream->buf_offset,
9131 (unsigned long long)stream->ack_offset);
9132
9133 if (!(++i % 3)) {
9134 chunk_appendf(&trash, "\n");
9135 i = 0;
9136 }
9137 }
9138
9139 chunk_appendf(&trash, "\n");
9140}
9141
9142static int cli_io_handler_dump_quic(struct appctx *appctx)
9143{
9144 struct show_quic_ctx *ctx = appctx->svcctx;
9145 struct stconn *sc = appctx_sc(appctx);
9146 struct quic_conn *qc;
9147
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009148 thread_isolate();
9149
9150 if (ctx->thr >= global.nbthread)
9151 goto done;
9152
Christopher Faulet87633c32023-04-03 18:32:50 +02009153 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02009154 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009155 /* If we're forced to shut down, we might have to remove our
9156 * reference to the last stream being dumped.
9157 */
9158 if (!LIST_ISEMPTY(&ctx->bref.users))
9159 LIST_DEL_INIT(&ctx->bref.users);
9160 goto done;
9161 }
9162
9163 chunk_reset(&trash);
9164
9165 if (!LIST_ISEMPTY(&ctx->bref.users)) {
9166 /* Remove show_quic_ctx from previous quic_conn instance. */
9167 LIST_DEL_INIT(&ctx->bref.users);
9168 }
9169 else if (!ctx->bref.ref) {
9170 /* First invocation. */
9171 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02009172
9173 /* Print legend for oneline format. */
9174 if (ctx->format == QUIC_DUMP_FMT_ONELINE) {
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02009175 chunk_appendf(&trash, "# conn/frontend state "
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02009176 "in_flight infl_p lost_p "
9177 "Local Address Foreign Address "
Amaury Denoyelle2273af12023-05-05 16:08:34 +02009178 "local & remote CIDs\n");
9179 applet_putchk(appctx, &trash);
9180 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009181 }
9182
9183 while (1) {
9184 int done = 0;
9185
9186 if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01009187 /* If closing connections requested through "all", move
9188 * to quic_conns_clo list after browsing quic_conns.
9189 * Else move directly to the next quic_conns thread.
9190 */
9191 if (ctx->flags & QC_CLI_FL_SHOW_ALL) {
9192 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns_clo.n;
9193 continue;
9194 }
9195
9196 done = 1;
9197 }
9198 else if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns_clo) {
9199 /* Closing list entirely browsed, go to next quic_conns
9200 * thread.
9201 */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009202 done = 1;
9203 }
9204 else {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01009205 /* Retrieve next element of the current list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009206 qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx);
9207 if ((int)(qc->qc_epoch - ctx->epoch) > 0)
9208 done = 1;
9209 }
9210
9211 if (done) {
9212 ++ctx->thr;
9213 if (ctx->thr >= global.nbthread)
9214 break;
Amaury Denoyelle2d376292023-03-08 09:42:31 +01009215 /* Switch to next thread quic_conns list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009216 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
9217 continue;
9218 }
9219
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02009220 switch (ctx->format) {
9221 case QUIC_DUMP_FMT_FULL:
9222 dump_quic_full(ctx, qc);
9223 break;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02009224 case QUIC_DUMP_FMT_ONELINE:
9225 dump_quic_oneline(ctx, qc);
9226 break;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01009227 }
9228
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009229 if (applet_putchk(appctx, &trash) == -1) {
9230 /* Register show_quic_ctx to quic_conn instance. */
9231 LIST_APPEND(&qc->back_refs, &ctx->bref.users);
9232 goto full;
9233 }
9234
9235 ctx->bref.ref = qc->el_th_ctx.n;
9236 }
9237
9238 done:
9239 thread_release();
9240 return 1;
9241
9242 full:
9243 thread_release();
9244 return 0;
9245}
9246
9247static void cli_release_show_quic(struct appctx *appctx)
9248{
9249 struct show_quic_ctx *ctx = appctx->svcctx;
9250
9251 if (ctx->thr < global.nbthread) {
9252 thread_isolate();
9253 if (!LIST_ISEMPTY(&ctx->bref.users))
9254 LIST_DEL_INIT(&ctx->bref.users);
9255 thread_release();
9256 }
9257}
9258
9259static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreau6ccc8622023-05-31 15:54:48 +02009260 { { "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 +01009261 {{},}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009262}};
9263
9264INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
9265
9266static void init_quic()
9267{
9268 int thr;
9269
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01009270 for (thr = 0; thr < MAX_THREADS; ++thr) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009271 LIST_INIT(&ha_thread_ctx[thr].quic_conns);
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01009272 LIST_INIT(&ha_thread_ctx[thr].quic_conns_clo);
9273 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009274}
9275INITCALL0(STG_INIT, init_quic);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02009276
9277/*
9278 * Local variables:
9279 * c-indent-level: 8
9280 * c-basic-offset: 8
9281 * End:
9282 */