blob: c3e0beccf6ec955392f0bf19d8dd8a85b3862e36 [file] [log] [blame]
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001/*
2 * QUIC protocol implementation. Lower layer with internal features implemented
3 * here such as QUIC encryption, idle timeout, acknowledgement and
4 * retransmission.
5 *
6 * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 */
14
15#include <haproxy/quic_conn.h>
16
17#define _GNU_SOURCE
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020018#include <stdio.h>
19#include <stdlib.h>
20
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include <netinet/tcp.h>
26
27#include <import/ebmbtree.h>
28
29#include <haproxy/buf-t.h>
30#include <haproxy/compat.h>
31#include <haproxy/api.h>
32#include <haproxy/debug.h>
33#include <haproxy/tools.h>
34#include <haproxy/ticks.h>
Amaury Denoyelle162baaf2023-04-03 18:49:39 +020035#include <haproxy/xxhash.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020036
Amaury Denoyelle15c74702023-02-01 10:18:26 +010037#include <haproxy/applet-t.h>
38#include <haproxy/cli.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020039#include <haproxy/connection.h>
40#include <haproxy/fd.h>
41#include <haproxy/freq_ctr.h>
42#include <haproxy/global.h>
43#include <haproxy/h3.h>
44#include <haproxy/hq_interop.h>
45#include <haproxy/log.h>
46#include <haproxy/mux_quic.h>
47#include <haproxy/ncbuf.h>
48#include <haproxy/pipe.h>
49#include <haproxy/proxy.h>
50#include <haproxy/quic_cc.h>
51#include <haproxy/quic_frame.h>
52#include <haproxy/quic_enc.h>
53#include <haproxy/quic_loss.h>
54#include <haproxy/quic_sock.h>
55#include <haproxy/quic_stats.h>
56#include <haproxy/quic_stream.h>
57#include <haproxy/quic_tp.h>
58#include <haproxy/cbuf.h>
59#include <haproxy/proto_quic.h>
60#include <haproxy/quic_tls.h>
61#include <haproxy/ssl_sock.h>
62#include <haproxy/task.h>
Amaury Denoyelle15c74702023-02-01 10:18:26 +010063#include <haproxy/thread.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020064#include <haproxy/trace.h>
65
Amaury Denoyelle15c74702023-02-01 10:18:26 +010066/* incremented by each "show quic". */
67static unsigned int qc_epoch = 0;
68
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020069/* list of supported QUIC versions by this implementation */
70const struct quic_version quic_versions[] = {
71 {
72 .num = QUIC_PROTOCOL_VERSION_DRAFT_29,
73 .initial_salt = initial_salt_draft_29,
74 .initial_salt_len = sizeof initial_salt_draft_29,
75 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
76 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
77 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
78 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
79 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
80 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
81 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
82 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
83 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT,
84 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT,
85 },
86 {
87 .num = QUIC_PROTOCOL_VERSION_1,
88 .initial_salt = initial_salt_v1,
89 .initial_salt_len = sizeof initial_salt_v1,
90 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
91 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
92 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
93 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
94 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
95 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
96 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
97 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
98 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1,
99 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1,
100 },
101 {
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +0100102 .num = QUIC_PROTOCOL_VERSION_2,
103 .initial_salt = initial_salt_v2,
104 .initial_salt_len = sizeof initial_salt_v2,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200105 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2,
106 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1,
107 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2,
108 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1,
109 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2,
110 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1,
111 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2,
112 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1,
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +0100113 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2,
114 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200115 },
116};
117
118/* The total number of supported versions */
119const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions;
120/* Listener only preferred version */
121const struct quic_version *preferred_version;
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +0200122/* RFC 8999 5.4. Version
123 * A Version field with a
124 * value of 0x00000000 is reserved for version negotiation
125 */
126const struct quic_version quic_version_VN_reserved = { .num = 0, };
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200127
128/* trace source and events */
129static void quic_trace(enum trace_level level, uint64_t mask, \
130 const struct trace_source *src,
131 const struct ist where, const struct ist func,
132 const void *a1, const void *a2, const void *a3, const void *a4);
133
134static const struct trace_event quic_trace_events[] = {
135 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
136 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
137 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
138 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
139 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
140 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
141 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
142 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
143 { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" },
144 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
145 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
146 { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" },
147 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
148 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
149 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
150 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
151 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
152 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
153 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
154 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
155 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
156 { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" },
157 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
158 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
159 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
160 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
161 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
162 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
163 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
164 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
165 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
166 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
167 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
168 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
169 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
170 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
171 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
172 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
173 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
174 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
175 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
176 { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"},
177 { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"},
178 { .mask = QUIC_EV_CONN_SUB, .name = "xprt_sub", .desc = "RX/TX subcription or unsubscription to QUIC xprt"},
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200179 { .mask = QUIC_EV_CONN_RCV, .name = "conn_recv", .desc = "RX on connection" },
Amaury Denoyelle25174d52023-04-05 17:52:05 +0200180 { .mask = QUIC_EV_CONN_SET_AFFINITY, .name = "conn_set_affinity", .desc = "set connection thread affinity" },
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200181 { /* end */ }
182};
183
184static const struct name_desc quic_trace_lockon_args[4] = {
185 /* arg1 */ { /* already used by the connection */ },
186 /* arg2 */ { .name="quic", .desc="QUIC transport" },
187 /* arg3 */ { },
188 /* arg4 */ { }
189};
190
191static const struct name_desc quic_trace_decoding[] = {
192#define QUIC_VERB_CLEAN 1
193 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
194 { /* end */ }
195};
196
197
198struct trace_source trace_quic = {
199 .name = IST("quic"),
200 .desc = "QUIC xprt",
201 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
202 .default_cb = quic_trace,
203 .known_events = quic_trace_events,
204 .lockon_args = quic_trace_lockon_args,
205 .decoding = quic_trace_decoding,
206 .report_events = ~0, /* report everything by default */
207};
208
209#define TRACE_SOURCE &trace_quic
210INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
211
212static BIO_METHOD *ha_quic_meth;
213
214DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ);
215DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
216DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
217 "quic_conn_ctx", sizeof(struct ssl_sock_ctx));
218DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
219DECLARE_POOL(pool_head_quic_connection_id,
220 "quic_connnection_id", sizeof(struct quic_connection_id));
221DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
222DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet));
223DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet));
224DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm));
225DECLARE_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 +0200226DECLARE_STATIC_POOL(pool_head_quic_cstream, "quic_cstream", sizeof(struct quic_cstream));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200227DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame));
228DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node));
229
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +0100230static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +0200231 struct quic_conn *qc,
232 const struct quic_cid *odcid,
233 const struct sockaddr_storage *saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200234static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
235 struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
236 struct list *frms, struct quic_conn *qc,
237 const struct quic_version *ver, size_t dglen, int pkt_type,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +0200238 int must_ack, int padding, int probe, int cc, int *err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200239struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécailled7215712023-03-24 18:13:37 +0100240static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack);
241static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200242static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
243static int quic_conn_init_timer(struct quic_conn *qc);
244static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
245
246/* Only for debug purpose */
247struct enc_debug_info {
248 unsigned char *payload;
249 size_t payload_len;
250 unsigned char *aad;
251 size_t aad_len;
252 uint64_t pn;
253};
254
255/* Initializes a enc_debug_info struct (only for debug purpose) */
256static inline void enc_debug_info_init(struct enc_debug_info *edi,
257 unsigned char *payload, size_t payload_len,
258 unsigned char *aad, size_t aad_len, uint64_t pn)
259{
260 edi->payload = payload;
261 edi->payload_len = payload_len;
262 edi->aad = aad;
263 edi->aad_len = aad_len;
264 edi->pn = pn;
265}
266
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100267/* Used only for QUIC TLS key phase traces */
268struct quic_kp_trace {
269 const unsigned char *rx_sec;
270 size_t rx_seclen;
271 const struct quic_tls_kp *rx;
272 const unsigned char *tx_sec;
273 size_t tx_seclen;
274 const struct quic_tls_kp *tx;
275};
276
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200277/* Trace callback for QUIC.
278 * These traces always expect that arg1, if non-null, is of type connection.
279 */
280static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
281 const struct ist where, const struct ist func,
282 const void *a1, const void *a2, const void *a3, const void *a4)
283{
284 const struct quic_conn *qc = a1;
285
286 if (qc) {
287 const struct quic_tls_ctx *tls_ctx;
288
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +0200289 chunk_appendf(&trace_buf, " : qc@%p flags=0x%x", qc, qc->flags);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200290 if (mask & QUIC_EV_CONN_INIT) {
291 chunk_appendf(&trace_buf, "\n odcid");
292 quic_cid_dump(&trace_buf, &qc->odcid);
293 chunk_appendf(&trace_buf, "\n dcid");
294 quic_cid_dump(&trace_buf, &qc->dcid);
295 chunk_appendf(&trace_buf, "\n scid");
296 quic_cid_dump(&trace_buf, &qc->scid);
297 }
298
299 if (mask & QUIC_EV_TRANSP_PARAMS) {
300 const struct quic_transport_params *p = a2;
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100301
302 if (p)
303 quic_transport_params_dump(&trace_buf, qc, p);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200304 }
305
306 if (mask & QUIC_EV_CONN_ADDDATA) {
307 const enum ssl_encryption_level_t *level = a2;
308 const size_t *len = a3;
309
310 if (level) {
311 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
312
313 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
314 }
315 if (len)
316 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
317 }
318 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
319 /* Initial read & write secrets. */
320 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
321 const unsigned char *rx_sec = a2;
322 const unsigned char *tx_sec = a3;
323
324 tls_ctx = &qc->els[level].tls_ctx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200325 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
326 if (rx_sec)
327 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
328 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
329 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
330 if (tx_sec)
331 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
332 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200333 }
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100334
335 if ((mask & QUIC_EV_CONN_KP) && qc) {
336 /* Initial read & write secrets. */
337 const struct quic_kp_trace *kp = a2;
338
339 if (kp) {
340 if (kp->rx) {
341 chunk_appendf(&trace_buf, "\n RX kp");
342 if (kp->rx_sec)
343 quic_tls_secret_hexdump(&trace_buf, kp->rx_sec, kp->rx_seclen);
344 quic_tls_kp_keys_hexdump(&trace_buf, kp->rx);
345 }
346 if (kp->tx) {
347 chunk_appendf(&trace_buf, "\n TX kp");
348 if (kp->tx_sec)
349 quic_tls_secret_hexdump(&trace_buf, kp->tx_sec, kp->tx_seclen);
350 quic_tls_kp_keys_hexdump(&trace_buf, kp->tx);
351 }
352 }
353 }
354
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200355 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
356 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200357
358 if (level) {
359 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
360
361 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200362 if (quic_tls_has_rx_sec(&qc->els[lvl])) {
363 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200364 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200365 }
366 else
367 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200368 }
369 }
370
371 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
372 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200373
374 if (level) {
375 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
376
377 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200378 if (quic_tls_has_tx_sec(&qc->els[lvl])) {
379 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200380 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200381 }
382 else
383 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200384 }
385
386 }
387
388 if (mask & QUIC_EV_CONN_FRMLIST) {
389 const struct list *l = a2;
390
391 if (l) {
392 const struct quic_frame *frm;
393 list_for_each_entry(frm, l, list) {
394 chunk_appendf(&trace_buf, " frm@%p", frm);
395 chunk_frm_appendf(&trace_buf, frm);
396 }
397 }
398 }
399
400 if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) {
401 const struct quic_tx_packet *pkt = a2;
402 const struct quic_enc_level *qel = a3;
403 const ssize_t *room = a4;
404
405 if (qel) {
406 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200407 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 +0200408 "if=%llu pp=%u",
409 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200410 qel->pktns->flags,
Frédéric Lécaille45400532023-02-13 18:39:19 +0100411 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200412 (unsigned long long)qc->path->cwnd,
413 (unsigned long long)qc->path->prep_in_flight,
414 (unsigned long long)qc->path->in_flight,
415 (unsigned long long)pktns->tx.in_flight,
416 pktns->tx.pto_probe);
417 }
418 if (pkt) {
419 const struct quic_frame *frm;
420 if (pkt->pn_node.key != (uint64_t)-1)
421 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
422 list_for_each_entry(frm, &pkt->frms, list) {
423 chunk_appendf(&trace_buf, " frm@%p", frm);
424 chunk_frm_appendf(&trace_buf, frm);
425 }
426 }
427
428 if (room) {
429 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
430 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
431 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
432 }
433 }
434
435 if (mask & QUIC_EV_CONN_IO_CB) {
436 const enum quic_handshake_state *state = a2;
437 const int *err = a3;
438
439 if (state)
440 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
441 if (err)
442 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
443 }
444
445 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
446 const struct quic_rx_packet *pkt = a2;
447 const unsigned long *pktlen = a3;
448 const SSL *ssl = a4;
449
450 if (pkt) {
451 chunk_appendf(&trace_buf, " pkt@%p", pkt);
452 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
453 chunk_appendf(&trace_buf, " kp=%d",
454 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
455 chunk_appendf(&trace_buf, " el=%c",
456 quic_packet_type_enc_level_char(pkt->type));
457 if (pkt->pnl)
458 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
459 (unsigned long long)pkt->pn);
460 if (pkt->token_len)
461 chunk_appendf(&trace_buf, " toklen=%llu",
462 (unsigned long long)pkt->token_len);
463 if (pkt->aad_len)
464 chunk_appendf(&trace_buf, " aadlen=%llu",
465 (unsigned long long)pkt->aad_len);
466 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
467 pkt->flags, (unsigned long long)pkt->len);
468 }
469 if (pktlen)
470 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
471 if (ssl) {
472 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
473 chunk_appendf(&trace_buf, " el=%c",
474 quic_enc_level_char(ssl_to_quic_enc_level(level)));
475 }
476 }
477
478 if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
479 const struct quic_rx_packet *pkt = a2;
480 const struct quic_rx_crypto_frm *cf = a3;
481 const SSL *ssl = a4;
482
483 if (pkt)
484 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
485 quic_packet_type_enc_level_char(pkt->type),
486 (unsigned long long)pkt->pn);
487 if (cf)
488 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
489 (unsigned long long)cf->offset_node.key,
490 (unsigned long long)cf->len);
491 if (ssl) {
492 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
493 chunk_appendf(&trace_buf, " rel=%c",
494 quic_enc_level_char(ssl_to_quic_enc_level(level)));
495 }
496
497 if (qc->err.code)
498 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code);
499 }
500
501 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
502 const struct quic_frame *frm = a2;
503
504 if (frm)
505 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
506 }
507
508 if (mask & QUIC_EV_CONN_PHPKTS) {
509 const struct quic_enc_level *qel = a2;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +0200510 const struct list *l = a3;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200511
512 if (qel) {
513 const struct quic_pktns *pktns = qel->pktns;
514 chunk_appendf(&trace_buf,
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200515 " 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 +0200516 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200517 qel->pktns->flags,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200518 quic_hdshk_state_str(qc->state),
519 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille45400532023-02-13 18:39:19 +0100520 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200521 (unsigned long long)qc->path->cwnd,
522 (unsigned long long)qc->path->prep_in_flight,
523 (unsigned long long)qc->path->in_flight,
524 (unsigned long long)pktns->tx.in_flight,
Amaury Denoyelle2f668f02022-11-18 15:24:08 +0100525 pktns->tx.pto_probe,
526 qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200527 }
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +0200528
529 if (l) {
530 const struct quic_frame *frm;
531 list_for_each_entry(frm, l, list) {
532 chunk_appendf(&trace_buf, " frm@%p", frm);
533 chunk_frm_appendf(&trace_buf, frm);
534 }
535 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200536 }
537
538 if (mask & QUIC_EV_CONN_ENCPKT) {
539 const struct enc_debug_info *edi = a2;
540
541 if (edi)
542 chunk_appendf(&trace_buf,
543 " payload=@%p payload_len=%llu"
544 " aad=@%p aad_len=%llu pn=%llu",
545 edi->payload, (unsigned long long)edi->payload_len,
546 edi->aad, (unsigned long long)edi->aad_len,
547 (unsigned long long)edi->pn);
548 }
549
550 if (mask & QUIC_EV_CONN_RMHP) {
551 const struct quic_rx_packet *pkt = a2;
552
553 if (pkt) {
554 const int *ret = a3;
555
556 chunk_appendf(&trace_buf, " pkt@%p", pkt);
557 if (ret && *ret)
558 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
559 pkt->pnl, (unsigned long long)pkt->pn);
560 }
561 }
562
563 if (mask & QUIC_EV_CONN_PRSAFRM) {
564 const struct quic_frame *frm = a2;
565 const unsigned long *val1 = a3;
566 const unsigned long *val2 = a4;
567
568 if (frm) {
569 chunk_appendf(&trace_buf, " frm@%p", frm);
570 chunk_frm_appendf(&trace_buf, frm);
571 }
572 if (val1)
573 chunk_appendf(&trace_buf, " %lu", *val1);
574 if (val2)
575 chunk_appendf(&trace_buf, "..%lu", *val2);
576 }
577
578 if (mask & QUIC_EV_CONN_ACKSTRM) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +0200579 const struct qf_stream *strm_frm = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200580 const struct qc_stream_desc *stream = a3;
581
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +0200582 if (strm_frm)
583 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)strm_frm->offset.key, (ull)strm_frm->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200584 if (stream)
585 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
586 }
587
588 if (mask & QUIC_EV_CONN_RTTUPDT) {
589 const unsigned int *rtt_sample = a2;
590 const unsigned int *ack_delay = a3;
591 const struct quic_loss *ql = a4;
592
593 if (rtt_sample)
594 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
595 if (ack_delay)
596 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
597 if (ql)
598 chunk_appendf(&trace_buf,
599 " srtt=%ums rttvar=%ums min_rtt=%ums",
600 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
601 }
602 if (mask & QUIC_EV_CONN_CC) {
603 const struct quic_cc_event *ev = a2;
604 const struct quic_cc *cc = a3;
605
606 if (a2)
607 quic_cc_event_trace(&trace_buf, ev);
608 if (a3)
609 quic_cc_state_trace(&trace_buf, cc);
610 }
611
612 if (mask & QUIC_EV_CONN_PKTLOSS) {
613 const struct quic_pktns *pktns = a2;
614 const struct list *lost_pkts = a3;
615
616 if (pktns) {
617 chunk_appendf(&trace_buf, " pktns=%s",
618 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
619 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
620 if (pktns->tx.loss_time)
621 chunk_appendf(&trace_buf, " loss_time=%dms",
622 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
623 }
624 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
625 struct quic_tx_packet *pkt;
626
627 chunk_appendf(&trace_buf, " lost_pkts:");
628 list_for_each_entry(pkt, lost_pkts, list)
629 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
630 }
631 }
632
633 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
634 const struct quic_pktns *pktns = a2;
635 const int *duration = a3;
636 const uint64_t *ifae_pkts = a4;
637
638 if (ifae_pkts)
639 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
640 (unsigned long long)*ifae_pkts);
641 if (pktns) {
642 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
643 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
644 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
645 pktns->tx.pto_probe);
646 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
647 if (pktns->tx.in_flight)
648 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
649 if (pktns->tx.loss_time)
650 chunk_appendf(&trace_buf, " loss_time=%dms",
651 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
652 }
653 if (mask & QUIC_EV_CONN_SPTO) {
654 if (pktns->tx.time_of_last_eliciting)
655 chunk_appendf(&trace_buf, " tole=%dms",
656 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
657 if (duration)
658 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
659 }
660 }
661
662 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
663 chunk_appendf(&trace_buf,
664 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
665 }
666 }
667
668 if (mask & QUIC_EV_CONN_SPPKTS) {
669 const struct quic_tx_packet *pkt = a2;
670
Frédéric Lécaille45400532023-02-13 18:39:19 +0100671 chunk_appendf(&trace_buf, " pto_count=%d cwnd=%llu ppif=%llu pif=%llu",
672 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200673 (unsigned long long)qc->path->cwnd,
674 (unsigned long long)qc->path->prep_in_flight,
675 (unsigned long long)qc->path->in_flight);
676 if (pkt) {
677 const struct quic_frame *frm;
Frédéric Lécaille6fd25762023-04-07 19:01:33 +0200678 if (pkt->flags & QUIC_FL_TX_PACKET_ACK)
679 chunk_appendf(&trace_buf, " ack");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200680 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
681 (unsigned long)pkt->pn_node.key,
682 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
683 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
684 (unsigned long long)pkt->in_flight_len);
685 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
686 (unsigned long long)qc->rx.bytes,
687 (unsigned long long)qc->tx.bytes);
688 list_for_each_entry(frm, &pkt->frms, list) {
689 chunk_appendf(&trace_buf, " frm@%p", frm);
690 chunk_frm_appendf(&trace_buf, frm);
691 }
Frédéric Lécaillebc09f742023-02-13 17:45:36 +0100692
693 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
694 chunk_appendf(&trace_buf, " with scid");
695 quic_cid_dump(&trace_buf, &qc->scid);
696 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200697 }
698 }
699
700 if (mask & QUIC_EV_CONN_SSLALERT) {
701 const uint8_t *alert = a2;
702 const enum ssl_encryption_level_t *level = a3;
703
704 if (alert)
705 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
706 if (level)
707 chunk_appendf(&trace_buf, " el=%c",
708 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
709 }
710
711 if (mask & QUIC_EV_CONN_BCFRMS) {
712 const size_t *sz1 = a2;
713 const size_t *sz2 = a3;
714 const size_t *sz3 = a4;
715
716 if (sz1)
717 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
718 if (sz2)
719 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
720 if (sz3)
721 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
722 }
723
724 if (mask & QUIC_EV_CONN_PSTRM) {
725 const struct quic_frame *frm = a2;
726
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100727 if (frm)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200728 chunk_frm_appendf(&trace_buf, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200729 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +0200730
731 if (mask & QUIC_EV_CONN_ELEVELSEL) {
732 const enum quic_handshake_state *state = a2;
733 const enum quic_tls_enc_level *level = a3;
734 const enum quic_tls_enc_level *next_level = a4;
735
736 if (state)
737 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state));
738 if (level)
739 chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level));
740 if (next_level)
741 chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level));
742
743 }
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200744
745 if (mask & QUIC_EV_CONN_RCV) {
746 const struct quic_dgram *dgram = a2;
747
748 if (dgram)
749 chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len);
750 }
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200751
752 if (mask & QUIC_EV_CONN_IDLE_TIMER) {
753 if (tick_isset(qc->ack_expire))
754 chunk_appendf(&trace_buf, " ack_expire=%ums",
755 TICKS_TO_MS(tick_remain(now_ms, qc->ack_expire)));
756 if (tick_isset(qc->idle_expire))
757 chunk_appendf(&trace_buf, " idle_expire=%ums",
758 TICKS_TO_MS(tick_remain(now_ms, qc->idle_expire)));
Frédéric Lécaillece5c1452023-04-05 09:44:21 +0200759 if (qc->idle_timer_task && tick_isset(qc->idle_timer_task->expire))
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200760 chunk_appendf(&trace_buf, " expire=%ums",
761 TICKS_TO_MS(tick_remain(now_ms, qc->idle_timer_task->expire)));
762 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200763 }
764 if (mask & QUIC_EV_CONN_LPKT) {
765 const struct quic_rx_packet *pkt = a2;
766 const uint64_t *len = a3;
767 const struct quic_version *ver = a4;
768
769 if (pkt) {
770 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
771 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
772 if (pkt->pn_node.key != (uint64_t)-1)
773 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
774 }
775
776 if (len)
777 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
778
779 if (ver)
780 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
781 }
782
783 if (mask & QUIC_EV_STATELESS_RST) {
784 const struct quic_cid *cid = a2;
785
786 if (cid)
787 quic_cid_dump(&trace_buf, cid);
788 }
789
790}
791
792/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
793static inline int quic_peer_validated_addr(struct quic_conn *qc)
794{
795 struct quic_pktns *hdshk_pktns, *app_pktns;
796
797 if (!qc_is_listener(qc))
798 return 1;
799
800 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
801 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
802 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
803 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
804 qc->state >= QUIC_HS_ST_COMPLETE)
805 return 1;
806
807 return 0;
808}
809
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100810/* To be called to kill a connection as soon as possible (without sending any packet). */
811void qc_kill_conn(struct quic_conn *qc)
812{
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100813 TRACE_ENTER(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200814 TRACE_PROTO("killing the connection", QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100815 qc->flags |= QUIC_FL_CONN_TO_KILL;
816 task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER);
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100817 TRACE_LEAVE(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100818}
819
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200820/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
821 * both loss detection and PTO and schedule the task assiated to this timer if needed.
822 */
823static inline void qc_set_timer(struct quic_conn *qc)
824{
825 struct quic_pktns *pktns;
826 unsigned int pto;
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100827 int handshake_confirmed;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200828
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100829 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc);
830 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200831
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100832 pktns = NULL;
833 if (!qc->timer_task) {
834 TRACE_PROTO("already released timer task", QUIC_EV_CONN_STIMER, qc);
835 goto leave;
836 }
837
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200838 pktns = quic_loss_pktns(qc);
839 if (tick_isset(pktns->tx.loss_time)) {
840 qc->timer = pktns->tx.loss_time;
841 goto out;
842 }
843
844 /* anti-amplification: the timer must be
845 * cancelled for a server which reached the anti-amplification limit.
846 */
847 if (!quic_peer_validated_addr(qc) &&
848 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
849 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
850 qc->timer = TICK_ETERNITY;
851 goto out;
852 }
853
854 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
855 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
856 /* Timer cancellation. */
857 qc->timer = TICK_ETERNITY;
858 goto out;
859 }
860
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100861 handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED;
862 pktns = quic_pto_pktns(qc, handshake_confirmed, &pto);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200863 if (tick_isset(pto))
864 qc->timer = pto;
865 out:
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100866 if (qc->timer == TICK_ETERNITY) {
867 qc->timer_task->expire = TICK_ETERNITY;
868 }
869 else if (tick_is_expired(qc->timer, now_ms)) {
870 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
871 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
872 }
873 else {
874 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
875 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200876 }
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100877 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100878 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, pktns);
879 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200880}
881
882/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
883 * connection.
884 * Return 1 if succeeded, 0 if not.
885 */
886static int quic_tls_key_update(struct quic_conn *qc)
887{
888 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 +0100889 struct quic_tls_secrets *rx = &tls_ctx->rx;
890 struct quic_tls_secrets *tx = &tls_ctx->tx;
891 /* Used only for the traces */
892 struct quic_kp_trace kp_trace = {
893 .rx_sec = rx->secret,
894 .rx_seclen = rx->secretlen,
895 .tx_sec = tx->secret,
896 .tx_seclen = tx->secretlen,
897 };
898 /* The next key phase secrets to be derived */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200899 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
900 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
901 const struct quic_version *ver =
902 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
903 int ret = 0;
904
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100905 TRACE_ENTER(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200906
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200907 nxt_rx = &qc->ku.nxt_rx;
908 nxt_tx = &qc->ku.nxt_tx;
909
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100910 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
911 "nxt_rx->secretlen=%llu rx->secretlen=%llu",
912 (ull)nxt_rx->secretlen, (ull)rx->secretlen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200913 /* Prepare new RX secrets */
914 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
915 rx->secret, rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100916 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200917 goto leave;
918 }
919
920 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
921 nxt_rx->key, nxt_rx->keylen,
922 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
923 nxt_rx->secret, nxt_rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100924 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200925 goto leave;
926 }
927
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100928 kp_trace.rx = nxt_rx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200929 /* Prepare new TX secrets */
930 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
931 tx->secret, tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100932 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200933 goto leave;
934 }
935
936 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
937 nxt_tx->key, nxt_tx->keylen,
938 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
939 nxt_tx->secret, nxt_tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100940 TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200941 goto leave;
942 }
943
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100944 kp_trace.tx = nxt_tx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200945 if (nxt_rx->ctx) {
946 EVP_CIPHER_CTX_free(nxt_rx->ctx);
947 nxt_rx->ctx = NULL;
948 }
949
950 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100951 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200952 goto leave;
953 }
954
955 if (nxt_tx->ctx) {
956 EVP_CIPHER_CTX_free(nxt_tx->ctx);
957 nxt_tx->ctx = NULL;
958 }
959
960 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100961 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200962 goto leave;
963 }
964
965 ret = 1;
966 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100967 TRACE_PROTO("key update", QUIC_EV_CONN_KP, qc, &kp_trace);
968 TRACE_LEAVE(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200969 return ret;
970}
971
972/* Rotate the Key Update information for <qc> QUIC connection.
973 * Must be used after having updated them.
974 * Always succeeds.
975 */
976static void quic_tls_rotate_keys(struct quic_conn *qc)
977{
978 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
979 unsigned char *curr_secret, *curr_iv, *curr_key;
980 EVP_CIPHER_CTX *curr_ctx;
981
982 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
983
984 /* Rotate the RX secrets */
985 curr_ctx = tls_ctx->rx.ctx;
986 curr_secret = tls_ctx->rx.secret;
987 curr_iv = tls_ctx->rx.iv;
988 curr_key = tls_ctx->rx.key;
989
990 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
991 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
992 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
993 tls_ctx->rx.key = qc->ku.nxt_rx.key;
994
995 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
996 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
997 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
998 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
999
1000 qc->ku.prv_rx.ctx = curr_ctx;
1001 qc->ku.prv_rx.secret = curr_secret;
1002 qc->ku.prv_rx.iv = curr_iv;
1003 qc->ku.prv_rx.key = curr_key;
1004 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
1005
1006 /* Update the TX secrets */
1007 curr_ctx = tls_ctx->tx.ctx;
1008 curr_secret = tls_ctx->tx.secret;
1009 curr_iv = tls_ctx->tx.iv;
1010 curr_key = tls_ctx->tx.key;
1011
1012 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
1013 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
1014 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
1015 tls_ctx->tx.key = qc->ku.nxt_tx.key;
1016
1017 qc->ku.nxt_tx.ctx = curr_ctx;
1018 qc->ku.nxt_tx.secret = curr_secret;
1019 qc->ku.nxt_tx.iv = curr_iv;
1020 qc->ku.nxt_tx.key = curr_key;
1021
1022 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1023}
1024
1025/* returns 0 on error, 1 on success */
1026int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
1027 const uint8_t *read_secret,
1028 const uint8_t *write_secret, size_t secret_len)
1029{
1030 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1031 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
1032 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001033 struct quic_tls_secrets *rx = NULL, *tx = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001034 const struct quic_version *ver =
1035 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
1036 int ret = 0;
1037
1038 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
1039 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001040
1041 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1042 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1043 goto out;
1044 }
1045
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001046 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1047 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001048 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001049 }
1050
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001051 if (!read_secret)
1052 goto write;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001053
1054 rx = &tls_ctx->rx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001055 if (!quic_tls_secrets_keys_alloc(rx)) {
1056 TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1057 goto leave;
1058 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001059
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001060 rx->aead = tls_aead(cipher);
1061 rx->md = tls_md(cipher);
1062 rx->hp = tls_hp(cipher);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001063
1064 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen,
1065 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
1066 read_secret, secret_len)) {
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001067 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001068 goto leave;
1069 }
1070
1071 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
1072 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1073 goto leave;
1074 }
1075
1076 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
1077 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1078 goto leave;
1079 }
1080
1081 /* Enqueue this connection asap if we could derive O-RTT secrets as
1082 * listener. Note that a listener derives only RX secrets for this
1083 * level.
1084 */
1085 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
1086 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
1087 quic_accept_push_qc(qc);
1088 }
1089
1090write:
1091
1092 if (!write_secret)
1093 goto out;
1094
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001095 tx = &tls_ctx->tx;
1096 if (!quic_tls_secrets_keys_alloc(tx)) {
1097 TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1098 goto leave;
1099 }
1100
1101 tx->aead = tls_aead(cipher);
1102 tx->md = tls_md(cipher);
1103 tx->hp = tls_hp(cipher);
1104
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001105 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen,
1106 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
1107 write_secret, secret_len)) {
1108 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
1109 goto leave;
1110 }
1111
1112 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
1113 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1114 goto leave;
1115 }
1116
1117 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
1118 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1119 goto leave;
1120 }
1121
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01001122 if (level == ssl_encryption_handshake && qc_is_listener(qc)) {
1123 qc->enc_params_len =
1124 quic_transport_params_encode(qc->enc_params,
1125 qc->enc_params + sizeof qc->enc_params,
1126 &qc->rx.params, ver, 1);
1127 if (!qc->enc_params_len) {
1128 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC);
1129 goto leave;
1130 }
1131
1132 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
1133 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC);
1134 goto leave;
1135 }
1136 }
1137
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001138 if (level == ssl_encryption_application) {
1139 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
1140 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
1141 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
1142
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001143 if (rx) {
1144 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1145 TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1146 goto leave;
1147 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001148
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001149 memcpy(rx->secret, read_secret, secret_len);
1150 rx->secretlen = secret_len;
1151 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001152
1153 if (tx) {
1154 if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1155 TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1156 goto leave;
1157 }
1158
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001159 memcpy(tx->secret, write_secret, secret_len);
1160 tx->secretlen = secret_len;
1161 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001162
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001163 /* Initialize all the secret keys lengths */
1164 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001165 }
1166
1167 out:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001168 ret = 1;
1169 leave:
1170 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
1171 return ret;
1172}
1173
1174/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1175 * with <len> as size in CRYPTO buffers dedicated to store the information about
1176 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1177 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
1178 * buffers to store all the data.
1179 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1180 */
1181static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
1182 const unsigned char *data, size_t len)
1183{
1184 struct quic_crypto_buf **qcb;
1185 /* The remaining byte to store in CRYPTO buffers. */
1186 size_t cf_offset, cf_len, *nb_buf;
1187 unsigned char *pos;
1188 int ret = 0;
1189
1190 nb_buf = &qel->tx.crypto.nb_buf;
1191 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1192 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1193 cf_len = len;
1194
1195 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1196
1197 while (len) {
1198 size_t to_copy, room;
1199
1200 pos = (*qcb)->data + (*qcb)->sz;
1201 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1202 to_copy = len > room ? room : len;
1203 if (to_copy) {
1204 memcpy(pos, data, to_copy);
1205 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1206 qel->tx.crypto.sz += to_copy;
1207 (*qcb)->sz += to_copy;
1208 len -= to_copy;
1209 data += to_copy;
1210 }
1211 else {
1212 struct quic_crypto_buf **tmp;
1213
1214 // FIXME: realloc!
1215 tmp = realloc(qel->tx.crypto.bufs,
1216 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1217 if (tmp) {
1218 qel->tx.crypto.bufs = tmp;
1219 qcb = &qel->tx.crypto.bufs[*nb_buf];
1220 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1221 if (!*qcb) {
1222 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1223 goto leave;
1224 }
1225
1226 (*qcb)->sz = 0;
1227 ++*nb_buf;
1228 }
1229 else {
1230 break;
1231 }
1232 }
1233 }
1234
1235 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1236 * have been buffered.
1237 */
1238 if (!len) {
1239 struct quic_frame *frm;
1240 struct quic_frame *found = NULL;
1241
1242 /* There is at most one CRYPTO frame in this packet number
1243 * space. Let's look for it.
1244 */
1245 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
1246 if (frm->type != QUIC_FT_CRYPTO)
1247 continue;
1248
1249 /* Found */
1250 found = frm;
1251 break;
1252 }
1253
1254 if (found) {
1255 found->crypto.len += cf_len;
1256 }
1257 else {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001258 frm = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001259 if (!frm) {
1260 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1261 goto leave;
1262 }
1263
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001264 frm->crypto.offset = cf_offset;
1265 frm->crypto.len = cf_len;
1266 frm->crypto.qel = qel;
1267 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
1268 }
1269 }
1270 ret = len == 0;
1271 leave:
1272 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1273 return ret;
1274}
1275
1276/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1277 * activity for <qc> will be interrupted.
1278 */
1279void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
1280{
1281 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1282 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
1283 goto leave;
1284
1285 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
1286 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
1287 qc->err.code = err.code;
1288 qc->err.app = err.app;
1289 leave:
1290 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1291}
1292
1293/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1294void quic_set_tls_alert(struct quic_conn *qc, int alert)
1295{
1296 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1297
1298 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1299 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
1300 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
1301 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1302 }
1303 quic_set_connection_close(qc, quic_err_tls(alert));
1304 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
1305 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1306
1307 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1308}
1309
1310/* Set the application for <qc> QUIC connection.
1311 * Return 1 if succeeded, 0 if not.
1312 */
1313int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1314{
1315 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1316 qc->app_ops = &h3_ops;
1317 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1318 qc->app_ops = &hq_interop_ops;
1319 else
1320 return 0;
1321
1322 return 1;
1323}
1324
1325/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1326 * wants to provide the QUIC layer with CRYPTO data.
1327 * Returns 1 if succeeded, 0 if not.
1328 */
1329int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1330 const uint8_t *data, size_t len)
1331{
1332 struct quic_conn *qc;
1333 enum quic_tls_enc_level tel;
1334 struct quic_enc_level *qel;
1335 int ret = 0;
1336
1337 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1338 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1339
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001340 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1341 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1342 goto out;
1343 }
1344
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001345 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1346 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
1347 goto out;
1348 }
1349
1350 tel = ssl_to_quic_enc_level(level);
1351 if (tel == -1) {
1352 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1353 goto leave;
1354 }
1355
1356 qel = &qc->els[tel];
1357 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1358 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1359 goto leave;
1360 }
1361
1362 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
1363 qc, &level, &len);
1364 out:
1365 ret = 1;
1366 leave:
1367 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1368 return ret;
1369}
1370
1371int ha_quic_flush_flight(SSL *ssl)
1372{
1373 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1374
1375 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1376 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
1377
1378 return 1;
1379}
1380
1381int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1382{
1383 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1384
1385 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1386
1387 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1388
1389 quic_set_tls_alert(qc, alert);
1390 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1391 return 1;
1392}
1393
1394/* QUIC TLS methods */
1395static SSL_QUIC_METHOD ha_quic_method = {
1396 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1397 .add_handshake_data = ha_quic_add_handshake_data,
1398 .flush_flight = ha_quic_flush_flight,
1399 .send_alert = ha_quic_send_alert,
1400};
1401
1402/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1403 * Returns an error count.
1404 */
1405int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1406{
1407 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1408 int cfgerr = 0;
1409
1410 long options =
1411 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1412 SSL_OP_SINGLE_ECDH_USE |
1413 SSL_OP_CIPHER_SERVER_PREFERENCE;
1414 SSL_CTX *ctx;
1415
1416 ctx = SSL_CTX_new(TLS_server_method());
1417 bind_conf->initial_ctx = ctx;
1418
1419 SSL_CTX_set_options(ctx, options);
1420 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1421 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1422 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1423
1424#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1425# if defined(HAVE_SSL_CLIENT_HELLO_CB)
1426# if defined(SSL_OP_NO_ANTI_REPLAY)
1427 if (bind_conf->ssl_conf.early_data) {
1428 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
1429 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
1430 }
1431# endif /* !SSL_OP_NO_ANTI_REPLAY */
1432 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1433 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1434# else /* ! HAVE_SSL_CLIENT_HELLO_CB */
1435 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1436# endif
1437 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1438#endif
1439 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1440
1441 return cfgerr;
1442}
1443
1444/* Decode an expected packet number from <truncated_on> its truncated value,
1445 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1446 * the number of bits used to encode this packet number (its length in bytes * 8).
1447 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1448 */
1449static uint64_t decode_packet_number(uint64_t largest_pn,
1450 uint32_t truncated_pn, unsigned int pn_nbits)
1451{
1452 uint64_t expected_pn = largest_pn + 1;
1453 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1454 uint64_t pn_hwin = pn_win / 2;
1455 uint64_t pn_mask = pn_win - 1;
1456 uint64_t candidate_pn;
1457
1458
1459 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1460 /* Note that <pn_win> > <pn_hwin>. */
1461 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1462 candidate_pn + pn_hwin <= expected_pn)
1463 return candidate_pn + pn_win;
1464
1465 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1466 return candidate_pn - pn_win;
1467
1468 return candidate_pn;
1469}
1470
1471/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1472 * cryptographic context.
1473 * <largest_pn> is the largest received packet number and <pn> the address of
1474 * the packet number field for this packet with <byte0> address of its first byte.
1475 * <end> points to one byte past the end of this packet.
1476 * Returns 1 if succeeded, 0 if not.
1477 */
1478static int qc_do_rm_hp(struct quic_conn *qc,
1479 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1480 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
1481{
1482 int ret, i, pnlen;
1483 uint64_t packet_number;
1484 uint32_t truncated_pn = 0;
1485 unsigned char mask[5] = {0};
1486 unsigned char *sample;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001487
1488 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1489
1490 ret = 0;
1491
1492 /* Check there is enough data in this packet. */
1493 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1494 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1495 goto leave;
1496 }
1497
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001498 sample = pn + QUIC_PACKET_PN_MAXLEN;
1499
1500 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1501 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
1502 goto leave;
1503 }
1504
1505 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1506 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1507 for (i = 0; i < pnlen; i++) {
1508 pn[i] ^= mask[i + 1];
1509 truncated_pn = (truncated_pn << 8) | pn[i];
1510 }
1511
1512 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1513 /* Store remaining information for this unprotected header */
1514 pkt->pn = packet_number;
1515 pkt->pnl = pnlen;
1516
1517 ret = 1;
1518 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001519 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
1520 return ret;
1521}
1522
1523/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1524 * address, with <payload_len> as payload length, <aad> as address of
1525 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1526 * context.
1527 * Returns 1 if succeeded, 0 if not.
1528 */
1529static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1530 unsigned char *aad, size_t aad_len, uint64_t pn,
1531 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
1532{
1533 int ret = 0;
1534 unsigned char iv[QUIC_TLS_IV_LEN];
1535 unsigned char *tx_iv = tls_ctx->tx.iv;
1536 size_t tx_iv_sz = tls_ctx->tx.ivlen;
1537 struct enc_debug_info edi;
1538
1539 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1540
1541 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1542 TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc);
1543 goto err;
1544 }
1545
1546 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1547 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1548 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
1549 goto err;
1550 }
1551
1552 ret = 1;
1553 leave:
1554 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1555 return ret;
1556
1557 err:
1558 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1559 goto leave;
1560}
1561
Frédéric Lécaille72027782023-02-22 16:20:09 +01001562/* Select the correct TLS cipher context to used to decipher <pkt> packet
1563 * attached to <qc> connection from <qel> encryption level.
1564 */
1565static inline struct quic_tls_ctx *qc_select_tls_ctx(struct quic_conn *qc,
1566 struct quic_enc_level *qel,
1567 struct quic_rx_packet *pkt)
1568{
1569 return pkt->type != QUIC_PACKET_TYPE_INITIAL ? &qel->tls_ctx :
1570 pkt->version == qc->negotiated_version ? &qc->negotiated_ictx : &qel->tls_ctx;
1571}
1572
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001573/* Decrypt <pkt> packet using encryption level <qel> for <qc> connection.
1574 * Decryption is done in place in packet buffer.
1575 *
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +05001576 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001577 */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001578static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel,
1579 struct quic_rx_packet *pkt)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001580{
1581 int ret, kp_changed;
1582 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaille72027782023-02-22 16:20:09 +01001583 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001584 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
1585 unsigned char *rx_iv = tls_ctx->rx.iv;
1586 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1587 unsigned char *rx_key = tls_ctx->rx.key;
1588
1589 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1590
1591 ret = 0;
1592 kp_changed = 0;
1593
1594 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1595 /* The two tested bits are not at the same position,
1596 * this is why they are first both inversed.
1597 */
1598 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1599 if (pkt->pn < tls_ctx->rx.pn) {
1600 /* The lowest packet number of a previous key phase
1601 * cannot be null if it really stores previous key phase
1602 * secrets.
1603 */
1604 // TODO: check if BUG_ON() more suitable
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001605 if (!qc->ku.prv_rx.pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001606 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1607 goto leave;
1608 }
1609
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001610 rx_ctx = qc->ku.prv_rx.ctx;
1611 rx_iv = qc->ku.prv_rx.iv;
1612 rx_key = qc->ku.prv_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001613 }
1614 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1615 /* Next key phase */
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +01001616 TRACE_PROTO("Key phase changed", QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001617 kp_changed = 1;
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001618 rx_ctx = qc->ku.nxt_rx.ctx;
1619 rx_iv = qc->ku.nxt_rx.iv;
1620 rx_key = qc->ku.nxt_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001621 }
1622 }
1623 }
1624
1625 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1626 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1627 goto leave;
1628 }
1629
1630 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1631 pkt->data, pkt->aad_len,
1632 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
1633 if (!ret) {
1634 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1635 goto leave;
1636 }
1637
1638 /* Update the keys only if the packet decryption succeeded. */
1639 if (kp_changed) {
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001640 quic_tls_rotate_keys(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001641 /* Toggle the Key Phase bit */
1642 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1643 /* Store the lowest packet number received for the current key phase */
1644 tls_ctx->rx.pn = pkt->pn;
1645 /* Prepare the next key update */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001646 if (!quic_tls_key_update(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001647 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1648 goto leave;
1649 }
1650 }
1651
1652 /* Update the packet length (required to parse the frames). */
1653 pkt->len -= QUIC_TLS_TAG_LEN;
1654 ret = 1;
1655 leave:
1656 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1657 return ret;
1658}
1659
1660
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001661/* Release <frm> frame and mark its copies as acknowledged */
1662void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
1663{
1664 uint64_t pn;
1665 struct quic_frame *origin, *f, *tmp;
1666
1667 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1668
1669 /* Identify this frame: a frame copy or one of its copies */
1670 origin = frm->origin ? frm->origin : frm;
1671 /* Ensure the source of the copies is flagged as acked, <frm> being
1672 * possibly a copy of <origin>
1673 */
1674 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1675 /* Mark all the copy of <origin> as acknowledged. We must
1676 * not release the packets (releasing the frames) at this time as
1677 * they are possibly also to be acknowledged alongside the
1678 * the current one.
1679 */
1680 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1681 if (f->pkt) {
1682 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1683 f->origin = NULL;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001684 LIST_DEL_INIT(&f->ref);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001685 pn = f->pkt->pn_node.key;
1686 TRACE_DEVEL("mark frame as acked from packet",
1687 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1688 }
1689 else {
1690 TRACE_DEVEL("freeing unsent frame",
1691 QUIC_EV_CONN_PRSAFRM, qc, f);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001692 LIST_DEL_INIT(&f->ref);
1693 qc_frm_free(&f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001694 }
1695 }
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001696 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001697 pn = frm->pkt->pn_node.key;
1698 quic_tx_packet_refdec(frm->pkt);
1699 TRACE_DEVEL("freeing frame from packet",
1700 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001701 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001702
1703 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1704}
1705
1706/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1707 * and all STREAM data are acknowledged. The MUX is responsible to have set
1708 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1709 *
1710 * TODO this should also be called on lost packet detection
1711 */
1712void qc_check_close_on_released_mux(struct quic_conn *qc)
1713{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001714 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1715
1716 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1717 /* Reuse errcode which should have been previously set by the MUX on release. */
1718 quic_set_connection_close(qc, qc->err);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02001719 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001720 }
1721
1722 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1723}
1724
1725/* Remove from <stream> the acknowledged frames.
1726 *
1727 * Returns 1 if at least one frame was removed else 0.
1728 */
1729static int quic_stream_try_to_consume(struct quic_conn *qc,
1730 struct qc_stream_desc *stream)
1731{
1732 int ret;
1733 struct eb64_node *frm_node;
1734
1735 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1736
1737 ret = 0;
1738 frm_node = eb64_first(&stream->acked_frms);
1739 while (frm_node) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001740 struct qf_stream *strm_frm;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001741 struct quic_frame *frm;
1742 size_t offset, len;
1743
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001744 strm_frm = eb64_entry(frm_node, struct qf_stream, offset);
1745 offset = strm_frm->offset.key;
1746 len = strm_frm->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001747
1748 if (offset > stream->ack_offset)
1749 break;
1750
1751 if (qc_stream_desc_ack(&stream, offset, len)) {
1752 /* cf. next comment : frame may be freed at this stage. */
1753 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001754 qc, stream ? strm_frm : NULL, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001755 ret = 1;
1756 }
1757
1758 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1759 * has been freed. with the stream frames tree. Nothing to do
1760 * anymore in here.
1761 */
1762 if (!stream) {
1763 qc_check_close_on_released_mux(qc);
1764 ret = 1;
1765 goto leave;
1766 }
1767
1768 frm_node = eb64_next(frm_node);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001769 eb64_delete(&strm_frm->offset);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001770
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001771 frm = container_of(strm_frm, struct quic_frame, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001772 qc_release_frm(qc, frm);
1773 }
1774
1775 leave:
1776 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
1777 return ret;
1778}
1779
1780/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1781static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1782 struct quic_frame *frm)
1783{
Frédéric Lécaille8f991942023-03-24 15:14:45 +01001784 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1785 TRACE_PROTO("RX ack TX frm", QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001786
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001787 switch (frm->type) {
1788 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1789 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02001790 struct qf_stream *strm_frm = &frm->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001791 struct eb64_node *node = NULL;
1792 struct qc_stream_desc *stream = NULL;
1793 const size_t offset = strm_frm->offset.key;
1794 const size_t len = strm_frm->len;
1795
1796 /* do not use strm_frm->stream as the qc_stream_desc instance
1797 * might be freed at this stage. Use the id to do a proper
1798 * lookup.
1799 *
1800 * TODO if lookup operation impact on the perf is noticeable,
1801 * implement a refcount on qc_stream_desc instances.
1802 */
1803 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1804 if (!node) {
1805 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
1806 qc_release_frm(qc, frm);
1807 /* early return */
1808 goto leave;
1809 }
1810 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1811
1812 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
1813 if (offset <= stream->ack_offset) {
1814 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001815 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1816 qc, strm_frm, stream);
1817 }
1818
1819 if (!stream) {
1820 /* no need to continue if stream freed. */
1821 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
1822 qc_release_frm(qc, frm);
1823 qc_check_close_on_released_mux(qc);
1824 break;
1825 }
1826
1827 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1828 qc, strm_frm, stream);
1829 qc_release_frm(qc, frm);
1830 }
1831 else {
1832 eb64_insert(&stream->acked_frms, &strm_frm->offset);
1833 }
1834
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01001835 quic_stream_try_to_consume(qc, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001836 }
1837 break;
1838 default:
1839 qc_release_frm(qc, frm);
1840 }
1841
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001842 leave:
1843 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1844}
1845
1846/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1847 * deallocating them, and their TX frames.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001848 * May be NULL if <largest> node could not be found.
1849 */
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001850static inline void qc_ackrng_pkts(struct quic_conn *qc,
1851 struct eb_root *pkts,
1852 unsigned int *pkt_flags,
1853 struct list *newly_acked_pkts,
1854 struct eb64_node *largest_node,
1855 uint64_t largest, uint64_t smallest)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001856{
1857 struct eb64_node *node;
1858 struct quic_tx_packet *pkt;
1859
1860 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1861
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001862 node = eb64_lookup_ge(pkts, smallest);
1863 if (!node)
1864 goto leave;
1865
1866 largest_node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
1867 if (!largest_node)
1868 goto leave;
1869
1870 while (node && node->key <= largest_node->key) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001871 struct quic_frame *frm, *frmbak;
1872
1873 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
1874 *pkt_flags |= pkt->flags;
1875 LIST_INSERT(newly_acked_pkts, &pkt->list);
1876 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
1877 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1878 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaille814645f2022-11-18 18:15:28 +01001879 /* If there are others packet in the same datagram <pkt> is attached to,
1880 * detach the previous one and the next one from <pkt>.
1881 */
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01001882 quic_tx_packet_dgram_detach(pkt);
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001883 node = eb64_next(node);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001884 eb64_delete(&pkt->pn_node);
1885 }
1886
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001887 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001888 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001889}
1890
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001891/* Remove all frames from <pkt_frm_list> and reinsert them in the same order
1892 * they have been sent into <pktns_frm_list>. The loss counter of each frame is
1893 * incremented and checked if it does not exceed retransmission limit.
1894 *
1895 * Returns 1 on success, 0 if a frame loss limit is exceeded. A
1896 * CONNECTION_CLOSE is scheduled in this case.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001897 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001898static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1899 struct quic_tx_packet *pkt,
1900 struct list *pktns_frm_list)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001901{
1902 struct quic_frame *frm, *frmbak;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001903 struct list *pkt_frm_list = &pkt->frms;
1904 uint64_t pn = pkt->pn_node.key;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001905 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001906
1907 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1908
1909 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1910 /* First remove this frame from the packet it was attached to */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001911 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001912 quic_tx_packet_refdec(pkt);
1913 /* At this time, this frame is not freed but removed from its packet */
1914 frm->pkt = NULL;
1915 /* Remove any reference to this frame */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001916 qc_frm_unref(frm, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001917 switch (frm->type) {
1918 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1919 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02001920 struct qf_stream *strm_frm = &frm->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001921 struct eb64_node *node = NULL;
1922 struct qc_stream_desc *stream_desc;
1923
1924 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1925 if (!node) {
1926 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1927 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1928 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001929 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001930 continue;
1931 }
1932
1933 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1934 /* Do not resend this frame if in the "already acked range" */
1935 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1936 TRACE_DEVEL("ignored frame in already acked range",
1937 QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001938 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001939 continue;
1940 }
1941 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01001942 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
1943
Frédéric Lécaillec425e032023-03-20 14:32:59 +01001944 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001945 TRACE_DEVEL("updated partially acked frame",
1946 QUIC_EV_CONN_PRSAFRM, qc, frm);
1947 }
1948 break;
1949 }
1950
1951 default:
1952 break;
1953 }
1954
1955 /* Do not resend probing packet with old data */
1956 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1957 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1958 qc, frm, &pn);
1959 if (frm->origin)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001960 LIST_DEL_INIT(&frm->ref);
1961 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001962 continue;
1963 }
1964
1965 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1966 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1967 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1968 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001969 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001970 }
1971 else {
Amaury Denoyelle24d5b722023-01-31 11:44:50 +01001972 if (++frm->loss_count >= global.tune.quic_max_frame_loss) {
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001973 TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc);
1974 quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR));
1975 close = 1;
1976 }
1977
Frédéric Lécaillebe795ce2023-03-08 18:23:13 +01001978 LIST_APPEND(pktns_frm_list, &frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001979 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
1980 }
1981 }
1982
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001983 end:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001984 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001985 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001986}
1987
1988/* Free <pkt> TX packet and its attached frames.
1989 * This is the responsibility of the caller to remove this packet of
1990 * any data structure it was possibly attached to.
1991 */
1992static inline void free_quic_tx_packet(struct quic_conn *qc,
1993 struct quic_tx_packet *pkt)
1994{
1995 struct quic_frame *frm, *frmbak;
1996
1997 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1998
1999 if (!pkt)
2000 goto leave;
2001
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002002 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
2003 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002004 pool_free(pool_head_quic_tx_packet, pkt);
2005
2006 leave:
2007 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
2008}
2009
2010/* Free the TX packets of <pkts> list */
2011static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
2012{
2013 struct quic_tx_packet *pkt, *tmp;
2014
2015 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
2016
2017 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2018 LIST_DELETE(&pkt->list);
2019 eb64_delete(&pkt->pn_node);
2020 free_quic_tx_packet(qc, pkt);
2021 }
2022
2023 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
2024}
2025
2026/* Remove already sent ranges of acknowledged packet numbers from
2027 * <pktns> packet number space tree below <largest_acked_pn> possibly
2028 * updating the range which contains <largest_acked_pn>.
2029 * Never fails.
2030 */
2031static void qc_treat_ack_of_ack(struct quic_conn *qc,
2032 struct quic_pktns *pktns,
2033 int64_t largest_acked_pn)
2034{
2035 struct eb64_node *ar, *next_ar;
2036 struct quic_arngs *arngs = &pktns->rx.arngs;
2037
2038 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2039
2040 ar = eb64_first(&arngs->root);
2041 while (ar) {
2042 struct quic_arng_node *ar_node;
2043
2044 next_ar = eb64_next(ar);
2045 ar_node = eb64_entry(ar, struct quic_arng_node, first);
2046
2047 if ((int64_t)ar_node->first.key > largest_acked_pn) {
2048 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
2049 break;
2050 }
2051
2052 if (largest_acked_pn < ar_node->last) {
2053 eb64_delete(ar);
2054 ar_node->first.key = largest_acked_pn + 1;
2055 eb64_insert(&arngs->root, ar);
2056 break;
2057 }
2058
2059 eb64_delete(ar);
2060 pool_free(pool_head_quic_arng, ar_node);
2061 arngs->sz--;
2062 ar = next_ar;
2063 }
2064
2065 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2066}
2067
2068/* Send a packet ack event nofication for each newly acked packet of
2069 * <newly_acked_pkts> list and free them.
2070 * Always succeeds.
2071 */
2072static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
2073 struct list *newly_acked_pkts)
2074{
2075 struct quic_tx_packet *pkt, *tmp;
2076 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
2077
2078 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2079
2080 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
2081 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2082 qc->path->prep_in_flight -= pkt->in_flight_len;
2083 qc->path->in_flight -= pkt->in_flight_len;
2084 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2085 qc->path->ifae_pkts--;
2086 /* If this packet contained an ACK frame, proceed to the
2087 * acknowledging of range of acks from the largest acknowledged
2088 * packet number which was sent in an ACK frame by this packet.
2089 */
2090 if (pkt->largest_acked_pn != -1)
2091 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
2092 ev.ack.acked = pkt->in_flight_len;
2093 ev.ack.time_sent = pkt->time_sent;
2094 quic_cc_event(&qc->path->cc, &ev);
2095 LIST_DELETE(&pkt->list);
2096 eb64_delete(&pkt->pn_node);
2097 quic_tx_packet_refdec(pkt);
2098 }
2099
2100 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2101
2102}
2103
2104/* Release all the frames attached to <pktns> packet number space */
2105static inline void qc_release_pktns_frms(struct quic_conn *qc,
2106 struct quic_pktns *pktns)
2107{
2108 struct quic_frame *frm, *frmbak;
2109
2110 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2111
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002112 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list)
2113 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002114
2115 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2116}
2117
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002118/* Handle <pkts> list of lost packets detected at <now_us> handling their TX
2119 * frames. Send a packet loss event to the congestion controller if in flight
2120 * packet have been lost. Also frees the packet in <pkts> list.
2121 *
2122 * Returns 1 on success else 0 if loss limit has been exceeded. A
2123 * CONNECTION_CLOSE was prepared to close the connection ASAP.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002124 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002125static inline int qc_release_lost_pkts(struct quic_conn *qc,
2126 struct quic_pktns *pktns,
2127 struct list *pkts,
2128 uint64_t now_us)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002129{
2130 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002131 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002132
2133 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2134
2135 if (LIST_ISEMPTY(pkts))
2136 goto leave;
2137
2138 oldest_lost = newest_lost = NULL;
2139 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2140 struct list tmp = LIST_HEAD_INIT(tmp);
2141
2142 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2143 qc->path->prep_in_flight -= pkt->in_flight_len;
2144 qc->path->in_flight -= pkt->in_flight_len;
2145 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2146 qc->path->ifae_pkts--;
2147 /* Treat the frames of this lost packet. */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002148 if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms))
2149 close = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002150 LIST_DELETE(&pkt->list);
2151 if (!oldest_lost) {
2152 oldest_lost = newest_lost = pkt;
2153 }
2154 else {
2155 if (newest_lost != oldest_lost)
2156 quic_tx_packet_refdec(newest_lost);
2157 newest_lost = pkt;
2158 }
2159 }
2160
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002161 if (!close) {
2162 if (newest_lost) {
2163 /* Sent a congestion event to the controller */
2164 struct quic_cc_event ev = { };
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002165
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002166 ev.type = QUIC_CC_EVT_LOSS;
2167 ev.loss.time_sent = newest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002168
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002169 quic_cc_event(&qc->path->cc, &ev);
2170 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002171
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002172 /* If an RTT have been already sampled, <rtt_min> has been set.
2173 * We must check if we are experiencing a persistent congestion.
2174 * If this is the case, the congestion controller must re-enter
2175 * slow start state.
2176 */
2177 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2178 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002179
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002180 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2181 now_ms, qc->max_ack_delay))
2182 qc->path->cc.algo->slow_start(&qc->path->cc);
2183 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002184 }
2185
Amaury Denoyelle3a72ba22022-11-14 11:41:34 +01002186 /* <oldest_lost> cannot be NULL at this stage because we have ensured
2187 * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a
2188 * possible overflow on a 0 byte region with O2 optimization.
2189 */
2190 ALREADY_CHECKED(oldest_lost);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002191 quic_tx_packet_refdec(oldest_lost);
2192 if (newest_lost != oldest_lost)
2193 quic_tx_packet_refdec(newest_lost);
2194
2195 leave:
2196 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002197 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002198}
2199
2200/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2201 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
2202 * if the largest acked packet was newly acked and if there was at least one newly
2203 * acked ack-eliciting packet.
2204 * Return 1, if succeeded, 0 if not.
2205 */
2206static inline int qc_parse_ack_frm(struct quic_conn *qc,
2207 struct quic_frame *frm,
2208 struct quic_enc_level *qel,
2209 unsigned int *rtt_sample,
2210 const unsigned char **pos, const unsigned char *end)
2211{
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002212 struct qf_ack *ack_frm = &frm->ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002213 uint64_t smallest, largest;
2214 struct eb_root *pkts;
2215 struct eb64_node *largest_node;
2216 unsigned int time_sent, pkt_flags;
2217 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2218 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2219 int ret = 0;
2220
2221 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2222
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002223 if (ack_frm->largest_ack > qel->pktns->tx.next_pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002224 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002225 qc, NULL, &ack_frm->largest_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002226 goto err;
2227 }
2228
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002229 if (ack_frm->first_ack_range > ack_frm->largest_ack) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002230 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002231 qc, NULL, &ack_frm->first_ack_range);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002232 goto err;
2233 }
2234
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002235 largest = ack_frm->largest_ack;
2236 smallest = largest - ack_frm->first_ack_range;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002237 pkts = &qel->pktns->tx.pkts;
2238 pkt_flags = 0;
2239 largest_node = NULL;
2240 time_sent = 0;
2241
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002242 if ((int64_t)ack_frm->largest_ack > qel->pktns->rx.largest_acked_pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002243 largest_node = eb64_lookup(pkts, largest);
2244 if (!largest_node) {
2245 TRACE_DEVEL("Largest acked packet not found",
2246 QUIC_EV_CONN_PRSAFRM, qc);
2247 }
2248 else {
2249 time_sent = eb64_entry(largest_node,
2250 struct quic_tx_packet, pn_node)->time_sent;
2251 }
2252 }
2253
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002254 TRACE_PROTO("RX ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002255 qc, NULL, &largest, &smallest);
2256 do {
2257 uint64_t gap, ack_range;
2258
2259 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2260 largest_node, largest, smallest);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002261 if (!ack_frm->ack_range_num--)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002262 break;
2263
2264 if (!quic_dec_int(&gap, pos, end)) {
2265 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
2266 goto err;
2267 }
2268
2269 if (smallest < gap + 2) {
2270 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
2271 qc, NULL, &gap, &smallest);
2272 goto err;
2273 }
2274
2275 largest = smallest - gap - 2;
2276 if (!quic_dec_int(&ack_range, pos, end)) {
2277 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
2278 goto err;
2279 }
2280
2281 if (largest < ack_range) {
2282 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
2283 qc, NULL, &largest, &ack_range);
2284 goto err;
2285 }
2286
2287 /* Do not use this node anymore. */
2288 largest_node = NULL;
2289 /* Next range */
2290 smallest = largest - ack_range;
2291
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002292 TRACE_PROTO("RX next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002293 qc, NULL, &largest, &smallest);
2294 } while (1);
2295
2296 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2297 *rtt_sample = tick_remain(time_sent, now_ms);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002298 qel->pktns->rx.largest_acked_pn = ack_frm->largest_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002299 }
2300
2301 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2302 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
2303 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002304 if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms))
2305 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002306 }
2307 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2308 if (quic_peer_validated_addr(qc))
2309 qc->path->loss.pto_count = 0;
2310 qc_set_timer(qc);
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01002311 qc_notify_send(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002312 }
2313
2314 ret = 1;
2315 leave:
2316 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2317 return ret;
2318
2319 err:
2320 free_quic_tx_pkts(qc, &newly_acked_pkts);
2321 goto leave;
2322}
2323
2324/* This function gives the detail of the SSL error. It is used only
2325 * if the debug mode and the verbose mode are activated. It dump all
2326 * the SSL error until the stack was empty.
2327 */
2328static forceinline void qc_ssl_dump_errors(struct connection *conn)
2329{
2330 if (unlikely(global.mode & MODE_DEBUG)) {
2331 while (1) {
2332 const char *func = NULL;
2333 unsigned long ret;
2334
2335 ERR_peek_error_func(&func);
2336 ret = ERR_get_error();
2337 if (!ret)
2338 return;
2339
2340 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
2341 func, ERR_reason_error_string(ret));
2342 }
2343 }
2344}
2345
2346int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2347 const char **str, int *len);
2348
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002349/* Finalize <qc> QUIC connection:
2350 * - initialize the Initial QUIC TLS context for negotiated version,
2351 * - derive the secrets for this context,
2352 * - set them into the TLS stack,
2353 *
2354 * MUST be called after having received the remote transport parameters which
2355 * are parsed when the TLS callback for the ClientHello message is called upon
2356 * SSL_do_handshake() calls, not necessarily at the first time as this TLS
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002357 * message may be split between packets
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002358 * Return 1 if succeeded, 0 if not.
2359 */
2360static int qc_conn_finalize(struct quic_conn *qc, int server)
2361{
2362 int ret = 0;
2363
2364 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
2365
2366 if (qc->flags & QUIC_FL_CONN_FINALIZED)
2367 goto finalized;
2368
2369 if (qc->negotiated_version &&
2370 !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
2371 qc->odcid.data, qc->odcid.len, server))
2372 goto out;
2373
2374 /* This connection is functional (ready to send/receive) */
2375 qc->flags |= QUIC_FL_CONN_FINALIZED;
2376
2377 finalized:
2378 ret = 1;
2379 out:
2380 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
2381 return ret;
2382}
2383
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002384/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2385 * from <qel> encryption level with <ctx> as QUIC connection context.
2386 * Remaining parameter are there for debugging purposes.
2387 * Return 1 if succeeded, 0 if not.
2388 */
2389static inline int qc_provide_cdata(struct quic_enc_level *el,
2390 struct ssl_sock_ctx *ctx,
2391 const unsigned char *data, size_t len,
2392 struct quic_rx_packet *pkt,
2393 struct quic_rx_crypto_frm *cf)
2394{
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002395#ifdef DEBUG_STRICT
2396 enum ncb_ret ncb_ret;
2397#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002398 int ssl_err, state;
2399 struct quic_conn *qc;
2400 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002401 struct ncbuf *ncbuf = &el->cstream->rx.ncbuf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002402
2403 ssl_err = SSL_ERROR_NONE;
2404 qc = ctx->qc;
2405
2406 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2407
2408 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2409 TRACE_ERROR("SSL_provide_quic_data() error",
2410 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
2411 goto leave;
2412 }
2413
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002414 TRACE_PROTO("in order CRYPTO data",
2415 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
2416
2417 state = qc->state;
2418 if (state < QUIC_HS_ST_COMPLETE) {
2419 ssl_err = SSL_do_handshake(ctx->ssl);
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002420
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01002421 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
2422 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
2423 goto leave;
2424 }
2425
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002426 /* Finalize the connection as soon as possible if the peer transport parameters
2427 * have been received. This may be useful to send packets even if this
2428 * handshake fails.
2429 */
2430 if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) {
2431 TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state);
2432 goto leave;
2433 }
2434
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002435 if (ssl_err != 1) {
2436 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2437 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2438 TRACE_PROTO("SSL handshake in progress",
2439 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2440 goto out;
2441 }
2442
2443 /* TODO: Should close the connection asap */
2444 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2445 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2446 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2447 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2448 }
2449 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2450 qc_ssl_dump_errors(ctx->conn);
2451 ERR_clear_error();
2452 goto leave;
2453 }
2454
2455 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
2456
2457 /* Check the alpn could be negotiated */
2458 if (!qc->app_ops) {
2459 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2460 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2461 goto leave;
2462 }
2463
2464 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2465 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
2466 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2467 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2468 }
2469 /* I/O callback switch */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02002470 qc->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002471 if (qc_is_listener(ctx->qc)) {
Amaury Denoyelle1304d192023-04-11 16:46:03 +02002472 qc->flags |= QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002473 qc->state = QUIC_HS_ST_CONFIRMED;
2474 /* The connection is ready to be accepted. */
2475 quic_accept_push_qc(qc);
2476 }
2477 else {
2478 qc->state = QUIC_HS_ST_COMPLETE;
2479 }
2480
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02002481 /* Prepare the next key update */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002482 if (!quic_tls_key_update(qc)) {
2483 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc);
2484 goto leave;
2485 }
2486 } else {
2487 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2488 if (ssl_err != 1) {
2489 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2490 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2491 TRACE_PROTO("SSL post handshake in progress",
2492 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2493 goto out;
2494 }
2495
2496 TRACE_ERROR("SSL post handshake error",
2497 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2498 goto leave;
2499 }
2500
2501 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
2502 }
2503
2504 out:
2505 ret = 1;
2506 leave:
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002507 /* The CRYPTO data are consumed even in case of an error to release
2508 * the memory asap.
2509 */
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002510 if (!ncb_is_null(ncbuf)) {
2511#ifdef DEBUG_STRICT
2512 ncb_ret = ncb_advance(ncbuf, len);
2513 /* ncb_advance() must always succeed. This is guaranteed as
2514 * this is only done inside a data block. If false, this will
2515 * lead to handshake failure with quic_enc_level offset shifted
2516 * from buffer data.
2517 */
2518 BUG_ON(ncb_ret != NCB_RET_OK);
2519#else
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002520 ncb_advance(ncbuf, len);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002521#endif
2522 }
2523
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002524 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
2525 return ret;
2526}
2527
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002528/* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc>
2529 * connection. <fin> is true if FIN bit is set on frame type.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002530 *
2531 * Return 1 on success. On error, 0 is returned. In this case, the packet
2532 * containing the frame must not be acknowledged.
2533 */
2534static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02002535 struct qf_stream *strm_frm,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002536 struct quic_conn *qc, char fin)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002537{
2538 int ret;
2539
2540 /* RFC9000 13.1. Packet Processing
2541 *
2542 * A packet MUST NOT be acknowledged until packet protection has been
2543 * successfully removed and all frames contained in the packet have
2544 * been processed. For STREAM frames, this means the data has been
2545 * enqueued in preparation to be received by the application protocol,
2546 * but it does not require that data be delivered and consumed.
2547 */
2548 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2549
2550 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002551 strm_frm->offset.key, fin, (char *)strm_frm->data);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002552
2553 /* frame rejected - packet must not be acknowledeged */
2554 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2555 return !ret;
2556}
2557
2558/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2559 * for <qc> QUIC connection.
2560 * This is a best effort function which never fails even if no memory could be
2561 * allocated to duplicate these frames.
2562 */
2563static void qc_dup_pkt_frms(struct quic_conn *qc,
2564 struct list *pkt_frm_list, struct list *out_frm_list)
2565{
2566 struct quic_frame *frm, *frmbak;
2567 struct list tmp = LIST_HEAD_INIT(tmp);
2568
2569 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2570
2571 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2572 struct quic_frame *dup_frm, *origin;
2573
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002574 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
2575 TRACE_DEVEL("already acknowledged frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2576 continue;
2577 }
2578
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002579 switch (frm->type) {
2580 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2581 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02002582 struct qf_stream *strm_frm = &frm->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002583 struct eb64_node *node = NULL;
2584 struct qc_stream_desc *stream_desc;
2585
2586 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2587 if (!node) {
2588 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
2589 continue;
2590 }
2591
2592 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2593 /* Do not resend this frame if in the "already acked range" */
2594 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2595 TRACE_DEVEL("ignored frame in already acked range",
2596 QUIC_EV_CONN_PRSAFRM, qc, frm);
2597 continue;
2598 }
2599 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01002600 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
2601
Frédéric Lécaillec425e032023-03-20 14:32:59 +01002602 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002603 TRACE_DEVEL("updated partially acked frame",
2604 QUIC_EV_CONN_PRSAFRM, qc, frm);
2605 }
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01002606
2607 strm_frm->dup = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002608 break;
2609 }
2610
2611 default:
2612 break;
2613 }
2614
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002615 /* If <frm> is already a copy of another frame, we must take
2616 * its original frame as source for the copy.
2617 */
2618 origin = frm->origin ? frm->origin : frm;
2619 dup_frm = qc_frm_dup(origin);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002620 if (!dup_frm) {
2621 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2622 break;
2623 }
2624
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002625 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002626 if (origin->pkt) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002627 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
2628 qc, NULL, &origin->pkt->pn_node.key);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002629 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002630 else {
2631 /* <origin> is a frame which was sent from a packet detected as lost. */
2632 TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
2633 }
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002634
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002635 LIST_APPEND(&tmp, &dup_frm->list);
2636 }
2637
2638 LIST_SPLICE(out_frm_list, &tmp);
2639
2640 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2641}
2642
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002643/* Boolean function which return 1 if <pkt> TX packet is only made of
2644 * already acknowledged frame.
2645 */
2646static inline int qc_pkt_with_only_acked_frms(struct quic_tx_packet *pkt)
2647{
2648 struct quic_frame *frm;
2649
2650 list_for_each_entry(frm, &pkt->frms, list)
2651 if (!(frm->flags & QUIC_FL_TX_FRAME_ACKED))
2652 return 0;
2653
2654 return 1;
2655}
2656
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002657/* Prepare a fast retransmission from <qel> encryption level */
2658static void qc_prep_fast_retrans(struct quic_conn *qc,
2659 struct quic_enc_level *qel,
2660 struct list *frms1, struct list *frms2)
2661{
2662 struct eb_root *pkts = &qel->pktns->tx.pkts;
2663 struct list *frms = frms1;
2664 struct eb64_node *node;
2665 struct quic_tx_packet *pkt;
2666
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002667 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002668
2669 BUG_ON(frms1 == frms2);
2670
2671 pkt = NULL;
2672 node = eb64_first(pkts);
2673 start:
2674 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002675 struct quic_tx_packet *p;
2676
2677 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002678 node = eb64_next(node);
2679 /* Skip the empty and coalesced packets */
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002680 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002681 "--> pn=%llu (%d %d %d)", (ull)p->pn_node.key,
2682 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED),
2683 qc_pkt_with_only_acked_frms(p));
2684 if (!LIST_ISEMPTY(&p->frms) && !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002685 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002686 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002687 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002688 }
2689
2690 if (!pkt)
2691 goto leave;
2692
2693 /* When building a packet from another one, the field which may increase the
2694 * packet size is the packet number. And the maximum increase is 4 bytes.
2695 */
2696 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2697 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002698 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002699 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2700 goto leave;
2701 }
2702
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002703 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002704 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2705 if (frms == frms1 && frms2) {
2706 frms = frms2;
2707 goto start;
2708 }
2709 leave:
2710 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
2711}
2712
2713/* Prepare a fast retransmission during a handshake after a client
2714 * has resent Initial packets. According to the RFC a server may retransmit
2715 * Initial packets send them coalescing with others (Handshake here).
2716 * (Listener only function).
2717 */
2718static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2719 struct list *ifrms, struct list *hfrms)
2720{
2721 struct list itmp = LIST_HEAD_INIT(itmp);
2722 struct list htmp = LIST_HEAD_INIT(htmp);
2723
2724 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2725 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2726 struct quic_enc_level *qel = iqel;
2727 struct eb_root *pkts;
2728 struct eb64_node *node;
2729 struct quic_tx_packet *pkt;
2730 struct list *tmp = &itmp;
2731
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002732 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002733 start:
2734 pkt = NULL;
2735 pkts = &qel->pktns->tx.pkts;
2736 node = eb64_first(pkts);
2737 /* Skip the empty packet (they have already been retransmitted) */
2738 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002739 struct quic_tx_packet *p;
2740
2741 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002742 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002743 "--> pn=%llu (%d %d)", (ull)p->pn_node.key,
2744 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED));
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002745 if (!LIST_ISEMPTY(&p->frms) && !(p->flags & QUIC_FL_TX_PACKET_COALESCED) &&
2746 !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002747 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002748 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002749 }
2750
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002751 node = eb64_next(node);
2752 }
2753
2754 if (!pkt)
2755 goto end;
2756
2757 /* When building a packet from another one, the field which may increase the
2758 * packet size is the packet number. And the maximum increase is 4 bytes.
2759 */
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002760 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2761 size_t dglen = pkt->len + 4;
2762
2763 dglen += pkt->next ? pkt->next->len + 4 : 0;
2764 if (dglen > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002765 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002766 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2767 if (pkt->next)
2768 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt->next);
2769 goto end;
2770 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002771 }
2772
2773 qel->pktns->tx.pto_probe += 1;
2774
2775 /* No risk to loop here, #packet per datagram is bounded */
2776 requeue:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002777 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002778 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
2779 if (qel == iqel) {
2780 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2781 pkt = pkt->next;
2782 tmp = &htmp;
2783 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002784 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002785 goto requeue;
2786 }
2787 }
2788
2789 end:
2790 LIST_SPLICE(ifrms, &itmp);
2791 LIST_SPLICE(hfrms, &htmp);
2792
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002793 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002794}
2795
2796static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
2797{
2798 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2799
2800 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2801 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2802 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2803 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
2804 goto out;
2805
2806 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2807 }
2808
2809 out:
2810 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
2811}
2812
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002813/* Cancel a request on connection <qc> for stream id <id>. This is useful when
2814 * the client opens a new stream but the MUX has already been released. A
Amaury Denoyelle75463012023-02-20 10:31:27 +01002815 * STOP_SENDING + RESET_STREAM frames are prepared for emission.
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002816 *
2817 * TODO this function is closely related to H3. Its place should be in H3 layer
2818 * instead of quic-conn but this requires an architecture adjustment.
2819 *
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002820 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002821 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002822static int qc_h3_request_reject(struct quic_conn *qc, uint64_t id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002823{
2824 int ret = 0;
Amaury Denoyelle75463012023-02-20 10:31:27 +01002825 struct quic_frame *ss, *rs;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002826 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002827 const uint64_t app_error_code = H3_REQUEST_REJECTED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002828
2829 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2830
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002831 /* Do not emit rejection for unknown unidirectional stream as it is
2832 * forbidden to close some of them (H3 control stream and QPACK
2833 * encoder/decoder streams).
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002834 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002835 if (quic_stream_is_uni(id)) {
2836 ret = 1;
2837 goto out;
2838 }
2839
Amaury Denoyelle75463012023-02-20 10:31:27 +01002840 ss = qc_frm_alloc(QUIC_FT_STOP_SENDING);
2841 if (!ss) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002842 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2843 goto out;
2844 }
2845
Amaury Denoyelle75463012023-02-20 10:31:27 +01002846 ss->stop_sending.id = id;
2847 ss->stop_sending.app_error_code = app_error_code;
2848
2849 rs = qc_frm_alloc(QUIC_FT_RESET_STREAM);
2850 if (!rs) {
2851 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2852 qc_frm_free(&ss);
2853 goto out;
2854 }
2855
2856 rs->reset_stream.id = id;
2857 rs->reset_stream.app_error_code = app_error_code;
2858 rs->reset_stream.final_size = 0;
2859
2860 LIST_APPEND(&qel->pktns->tx.frms, &ss->list);
2861 LIST_APPEND(&qel->pktns->tx.frms, &rs->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002862 ret = 1;
2863 out:
2864 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2865 return ret;
2866}
2867
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002868/* Release the underlying memory use by <ncbuf> non-contiguous buffer */
2869static void quic_free_ncbuf(struct ncbuf *ncbuf)
2870{
2871 struct buffer buf;
2872
2873 if (ncb_is_null(ncbuf))
2874 return;
2875
2876 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
2877 b_free(&buf);
2878 offer_buffers(NULL, 1);
2879
2880 *ncbuf = NCBUF_NULL;
2881}
2882
2883/* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */
2884static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf)
2885{
2886 struct buffer buf = BUF_NULL;
2887
2888 if (!ncb_is_null(ncbuf))
2889 return ncbuf;
2890
2891 b_alloc(&buf);
2892 BUG_ON(b_is_null(&buf));
2893
2894 *ncbuf = ncb_make(buf.area, buf.size, 0);
2895 ncb_init(ncbuf, 0);
2896
2897 return ncbuf;
2898}
2899
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002900/* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn.
2901 * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the
2902 * speed up handshake completion may be run after having received duplicated
2903 * CRYPTO data.
2904 */
2905static int qc_handle_crypto_frm(struct quic_conn *qc,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002906 struct qf_crypto *crypto_frm, struct quic_rx_packet *pkt,
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002907 struct quic_enc_level *qel, int *fast_retrans)
2908{
2909 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002910 enum ncb_ret ncb_ret;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002911 /* XXX TO DO: <cfdebug> is used only for the traces. */
2912 struct quic_rx_crypto_frm cfdebug = {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002913 .offset_node.key = crypto_frm->offset,
2914 .len = crypto_frm->len,
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002915 };
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002916 struct quic_cstream *cstream = qel->cstream;
2917 struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002918
2919 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2920 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2921 TRACE_PROTO("CRYPTO data discarded",
2922 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2923 goto done;
2924 }
2925
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002926 if (unlikely(crypto_frm->offset < cstream->rx.offset)) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002927 size_t diff;
2928
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002929 if (crypto_frm->offset + crypto_frm->len <= cstream->rx.offset) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002930 /* Nothing to do */
2931 TRACE_PROTO("Already received CRYPTO data",
2932 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2933 if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
2934 !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP))
2935 *fast_retrans = 1;
2936 goto done;
2937 }
2938
2939 TRACE_PROTO("Partially already received CRYPTO data",
2940 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2941
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002942 diff = cstream->rx.offset - crypto_frm->offset;
2943 crypto_frm->len -= diff;
2944 crypto_frm->data += diff;
2945 crypto_frm->offset = cstream->rx.offset;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002946 }
2947
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002948 if (crypto_frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) {
2949 if (!qc_provide_cdata(qel, qc->xprt_ctx, crypto_frm->data, crypto_frm->len,
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002950 pkt, &cfdebug)) {
2951 // trace already emitted by function above
2952 goto leave;
2953 }
2954
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002955 cstream->rx.offset += crypto_frm->len;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002956 TRACE_DEVEL("increment crypto level offset", QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002957 goto done;
2958 }
2959
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002960 if (!quic_get_ncbuf(ncbuf) ||
2961 ncb_is_null(ncbuf)) {
2962 TRACE_ERROR("CRYPTO ncbuf allocation failed", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002963 goto leave;
2964 }
2965
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002966 /* crypto_frm->offset > cstream-trx.offset */
2967 ncb_ret = ncb_add(ncbuf, crypto_frm->offset - cstream->rx.offset,
2968 (const char *)crypto_frm->data, crypto_frm->len, NCB_ADD_COMPARE);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002969 if (ncb_ret != NCB_RET_OK) {
2970 if (ncb_ret == NCB_RET_DATA_REJ) {
2971 TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc);
2972 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
2973 }
2974 else if (ncb_ret == NCB_RET_GAP_SIZE) {
2975 TRACE_ERROR("cannot bufferize frame due to gap size limit",
2976 QUIC_EV_CONN_PRSHPKT, qc);
2977 }
2978 goto leave;
2979 }
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002980
2981 done:
2982 ret = 1;
2983 leave:
2984 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2985 return ret;
2986}
2987
Amaury Denoyelle591e7982023-04-12 10:04:49 +02002988/* Build a NEW_CONNECTION_ID frame for <conn_id> CID of <qc> connection.
2989 *
2990 * Returns 1 on success else 0.
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01002991 */
2992static int qc_build_new_connection_id_frm(struct quic_conn *qc,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02002993 struct quic_connection_id *conn_id)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01002994{
2995 int ret = 0;
2996 struct quic_frame *frm;
2997 struct quic_enc_level *qel;
2998
2999 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3000
3001 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3002 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
3003 if (!frm) {
3004 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3005 goto leave;
3006 }
3007
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003008 quic_connection_id_to_frm_cpy(frm, conn_id);
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003009 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
3010 ret = 1;
3011 leave:
3012 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3013 return ret;
3014}
3015
3016
3017/* Handle RETIRE_CONNECTION_ID frame from <frm> frame.
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003018 * Return 1 if succeeded, 0 if not. If succeeded, also set <to_retire>
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003019 * to the CID to be retired if not already retired.
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003020 */
3021static int qc_handle_retire_connection_id_frm(struct quic_conn *qc,
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003022 struct quic_frame *frm,
3023 struct quic_cid *dcid,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003024 struct quic_connection_id **to_retire)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003025{
3026 int ret = 0;
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003027 struct qf_retire_connection_id *rcid_frm = &frm->retire_connection_id;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003028 struct eb64_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003029 struct quic_connection_id *conn_id;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003030
3031 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3032
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003033 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3034 * Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number greater
3035 * than any previously sent to the peer MUST be treated as a connection error
3036 * of type PROTOCOL_VIOLATION.
3037 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003038 if (rcid_frm->seq_num >= qc->next_cid_seq_num) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003039 TRACE_PROTO("CID seq. number too big", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003040 goto protocol_violation;
3041 }
3042
3043 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3044 * The sequence number specified in a RETIRE_CONNECTION_ID frame MUST NOT refer to
3045 * the Destination Connection ID field of the packet in which the frame is contained.
3046 * The peer MAY treat this as a connection error of type PROTOCOL_VIOLATION.
3047 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003048 node = eb64_lookup(&qc->cids, rcid_frm->seq_num);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003049 if (!node) {
3050 TRACE_PROTO("CID already retired", QUIC_EV_CONN_PSTRM, qc, frm);
3051 goto out;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003052 }
3053
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003054 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003055 /* Note that the length of <dcid> has already been checked. It must match the
3056 * length of the CIDs which have been provided to the peer.
3057 */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003058 if (!memcmp(dcid->data, conn_id->cid.data, QUIC_HAP_CID_LEN)) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003059 TRACE_PROTO("cannot retire the current CID", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003060 goto protocol_violation;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003061 }
3062
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003063 *to_retire = conn_id;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003064 out:
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003065 ret = 1;
3066 leave:
3067 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3068 return ret;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003069 protocol_violation:
3070 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
3071 goto leave;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003072}
3073
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003074/* Remove a <qc> quic-conn from its ha_thread_ctx list. If <closing> is true,
3075 * it will immediately be reinserted in the ha_thread_ctx quic_conns_clo list.
3076 */
3077static void qc_detach_th_ctx_list(struct quic_conn *qc, int closing)
3078{
3079 struct bref *bref, *back;
3080
3081 /* Detach CLI context watchers currently dumping this connection.
3082 * Reattach them to the next quic_conn instance.
3083 */
3084 list_for_each_entry_safe(bref, back, &qc->back_refs, users) {
3085 /* Remove watcher from this quic_conn instance. */
3086 LIST_DEL_INIT(&bref->users);
3087
3088 /* Attach it to next instance unless it was the last list element. */
3089 if (qc->el_th_ctx.n != &th_ctx->quic_conns &&
3090 qc->el_th_ctx.n != &th_ctx->quic_conns_clo) {
3091 struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx,
3092 struct quic_conn *,
3093 el_th_ctx);
3094 LIST_APPEND(&next->back_refs, &bref->users);
3095 }
3096 bref->ref = qc->el_th_ctx.n;
3097 __ha_barrier_store();
3098 }
3099
3100 /* Remove quic_conn from global ha_thread_ctx list. */
3101 LIST_DEL_INIT(&qc->el_th_ctx);
3102
3103 if (closing)
3104 LIST_APPEND(&th_ctx->quic_conns_clo, &qc->el_th_ctx);
3105}
3106
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003107/* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel>
3108 * as encryption level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003109 * Returns 1 if succeeded, 0 if failed.
3110 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003111static int qc_parse_pkt_frms(struct quic_conn *qc, struct quic_rx_packet *pkt,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003112 struct quic_enc_level *qel)
3113{
3114 struct quic_frame frm;
3115 const unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003116 int fast_retrans = 0, ret = 0;
3117
3118 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3119 /* Skip the AAD */
3120 pos = pkt->data + pkt->aad_len;
3121 end = pkt->data + pkt->len;
3122
3123 while (pos < end) {
3124 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
3125 // trace already emitted by function above
3126 goto leave;
3127 }
3128
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003129 switch (frm.type) {
3130 case QUIC_FT_PADDING:
3131 break;
3132 case QUIC_FT_PING:
3133 break;
3134 case QUIC_FT_ACK:
3135 {
3136 unsigned int rtt_sample;
3137
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003138 rtt_sample = UINT_MAX;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003139 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
3140 // trace already emitted by function above
3141 goto leave;
3142 }
3143
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003144 if (rtt_sample != UINT_MAX) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003145 unsigned int ack_delay;
3146
3147 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
3148 qc->state >= QUIC_HS_ST_CONFIRMED ?
3149 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
3150 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
3151 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
3152 }
3153 break;
3154 }
3155 case QUIC_FT_RESET_STREAM:
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01003156 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003157 struct qf_reset_stream *rs_frm = &frm.reset_stream;
3158 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 +01003159 }
3160 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003161 case QUIC_FT_STOP_SENDING:
3162 {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003163 struct qf_stop_sending *ss_frm = &frm.stop_sending;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003164 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003165 if (qcc_recv_stop_sending(qc->qcc, ss_frm->id,
3166 ss_frm->app_error_code)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003167 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
3168 goto leave;
3169 }
3170 }
3171 break;
3172 }
3173 case QUIC_FT_CRYPTO:
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003174 if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003175 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003176 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003177 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
3178 {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003179 struct qf_stream *strm_frm = &frm.stream;
3180 unsigned nb_streams = qc->rx.strms[qcs_id_type(strm_frm->id)].nb_streams;
Amaury Denoyelle2216b082023-02-02 14:59:36 +01003181 const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003182
3183 /* The upper layer may not be allocated. */
3184 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003185 if ((strm_frm->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003186 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003187 }
3188 else {
3189 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle38836b62023-02-07 14:24:54 +01003190 if (qc->app_ops == &h3_ops) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003191 if (!qc_h3_request_reject(qc, strm_frm->id)) {
Amaury Denoyelle156a89a2023-02-20 10:32:16 +01003192 TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc);
3193 /* This packet will not be acknowledged */
3194 goto leave;
3195 }
3196 }
3197 else {
3198 /* This packet will not be acknowledged */
3199 goto leave;
Frédéric Lécailled18025e2023-01-20 15:33:50 +01003200 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003201 }
Amaury Denoyelle315a4f62023-03-06 09:10:53 +01003202
3203 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003204 }
3205
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003206 if (!qc_handle_strm_frm(pkt, strm_frm, qc, fin)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003207 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
3208 goto leave;
3209 }
3210
3211 break;
3212 }
3213 case QUIC_FT_MAX_DATA:
3214 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003215 struct qf_max_data *md_frm = &frm.max_data;
3216 qcc_recv_max_data(qc->qcc, md_frm->max_data);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003217 }
3218 break;
3219 case QUIC_FT_MAX_STREAM_DATA:
3220 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003221 struct qf_max_stream_data *msd_frm = &frm.max_stream_data;
3222 if (qcc_recv_max_stream_data(qc->qcc, msd_frm->id,
3223 msd_frm->max_stream_data)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003224 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
3225 goto leave;
3226 }
3227 }
3228 break;
3229 case QUIC_FT_MAX_STREAMS_BIDI:
3230 case QUIC_FT_MAX_STREAMS_UNI:
3231 break;
3232 case QUIC_FT_DATA_BLOCKED:
3233 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
3234 break;
3235 case QUIC_FT_STREAM_DATA_BLOCKED:
3236 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
3237 break;
3238 case QUIC_FT_STREAMS_BLOCKED_BIDI:
3239 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
3240 break;
3241 case QUIC_FT_STREAMS_BLOCKED_UNI:
3242 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
3243 break;
3244 case QUIC_FT_NEW_CONNECTION_ID:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003245 /* XXX TO DO XXX */
3246 break;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003247 case QUIC_FT_RETIRE_CONNECTION_ID:
3248 {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003249 struct quic_connection_id *conn_id = NULL;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003250
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003251 if (!qc_handle_retire_connection_id_frm(qc, &frm, &pkt->dcid, &conn_id))
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003252 goto leave;
3253
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003254 if (!conn_id)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003255 break;
3256
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003257 ebmb_delete(&conn_id->node);
3258 eb64_delete(&conn_id->seq_num);
3259 pool_free(pool_head_quic_connection_id, conn_id);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003260 TRACE_PROTO("CID retired", QUIC_EV_CONN_PSTRM, qc);
3261
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003262 conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
3263 if (!conn_id) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003264 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
3265 }
3266 else {
Amaury Denoyellee83f9372023-04-18 11:10:54 +02003267 quic_cid_insert(conn_id);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003268 qc_build_new_connection_id_frm(qc, conn_id);
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003269 }
3270 break;
3271 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003272 case QUIC_FT_CONNECTION_CLOSE:
3273 case QUIC_FT_CONNECTION_CLOSE_APP:
3274 /* Increment the error counters */
3275 qc_cc_err_count_inc(qc, &frm);
3276 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
3277 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
3278 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
3279 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
3280 }
3281 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
3282 /* RFC 9000 10.2. Immediate Close:
3283 * The closing and draining connection states exist to ensure
3284 * that connections close cleanly and that delayed or reordered
3285 * packets are properly discarded. These states SHOULD persist
3286 * for at least three times the current PTO interval...
3287 *
3288 * Rearm the idle timeout only one time when entering draining
3289 * state.
3290 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003291 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003292 qc_detach_th_ctx_list(qc, 1);
Frédéric Lécailled7215712023-03-24 18:13:37 +01003293 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003294 qc_notify_close(qc);
3295 }
3296 break;
3297 case QUIC_FT_HANDSHAKE_DONE:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003298 if (qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003299 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
3300 QUIC_EV_CONN_PRSHPKT, qc);
3301 goto leave;
3302 }
3303
3304 qc->state = QUIC_HS_ST_CONFIRMED;
3305 break;
3306 default:
3307 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
3308 goto leave;
3309 }
3310 }
3311
3312 /* Flag this packet number space as having received a packet. */
3313 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
3314
3315 if (fast_retrans) {
3316 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3317 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3318
3319 TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc);
3320 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
3321 qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP;
3322 }
3323
3324 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
3325 * has successfully parse a Handshake packet. The Initial encryption must also
3326 * be discarded.
3327 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003328 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003329 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
3330 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
3331 QUIC_FL_TLS_SECRETS_DCD)) {
3332 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3333 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
3334 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003335 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003336 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3337 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3338 }
3339 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
3340 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
3341 }
3342 }
3343
3344 ret = 1;
3345 leave:
3346 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3347 return ret;
3348}
3349
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003350
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003351/* Allocate Tx buffer from <qc> quic-conn if needed.
3352 *
3353 * Returns allocated buffer or NULL on error.
3354 */
3355static struct buffer *qc_txb_alloc(struct quic_conn *qc)
3356{
3357 struct buffer *buf = &qc->tx.buf;
3358 if (!b_alloc(buf))
3359 return NULL;
3360
3361 return buf;
3362}
3363
3364/* Free Tx buffer from <qc> if it is empty. */
3365static void qc_txb_release(struct quic_conn *qc)
3366{
3367 struct buffer *buf = &qc->tx.buf;
3368
3369 /* For the moment sending function is responsible to purge the buffer
3370 * entirely. It may change in the future but this requires to be able
3371 * to reuse old data.
Frédéric Lécaillebbf86be2023-02-20 09:28:58 +01003372 * For the momemt we do not care to leave data in the buffer for
3373 * a connection which is supposed to be killed asap.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003374 */
3375 BUG_ON_HOT(buf && b_data(buf));
3376
3377 if (!b_data(buf)) {
3378 b_free(buf);
3379 offer_buffers(NULL, 1);
3380 }
3381}
3382
3383/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
3384 * must contains the address of the first packet stored in the payload.
3385 *
3386 * Caller is responsible that there is enough space in the buffer.
3387 */
3388static void qc_txb_store(struct buffer *buf, uint16_t length,
3389 struct quic_tx_packet *first_pkt)
3390{
3391 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
3392 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
3393
3394 write_u16(b_tail(buf), length);
3395 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
3396 b_add(buf, hdlen + length);
3397}
3398
3399/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
3400 * with <frms> as ack-eliciting frame list to send, 0 if not.
3401 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
3402 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003403 * Also set <*must_ack> to inform the caller if an acknowledgement should be sent.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003404 */
3405static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003406 struct quic_enc_level *qel, int cc, int probe,
3407 int *must_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003408{
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003409 int force_ack =
3410 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3411 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3412 int nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
3413
3414 /* An acknowledgement must be sent if this has been forced by the caller,
3415 * typically during the handshake when the packets must be acknowledged as
3416 * soon as possible. This is also the case when the ack delay timer has been
3417 * triggered, or at least every QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK packets.
3418 */
3419 *must_ack = (qc->flags & QUIC_FL_CONN_ACK_TIMER_FIRED) ||
3420 ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3421 (force_ack || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003422
3423 /* Do not build any more packet if the TX secrets are not available or
3424 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
3425 * and if there is no more packets to send upon PTO expiration
3426 * and if there is no more ack-eliciting frames to send or in flight
3427 * congestion control limit is reached for prepared data
3428 */
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02003429 if (!quic_tls_has_tx_sec(qel) ||
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003430 (!cc && !probe && !*must_ack &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003431 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
3432 return 0;
3433 }
3434
3435 return 1;
3436}
3437
3438/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3439 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
3440 *
3441 * Each datagram is prepended by a two fields header : the datagram length and
3442 * the address of the packet contained in the datagram.
3443 *
3444 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3445 * -1 if something wrong happened.
3446 */
3447static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
3448 struct list *frms)
3449{
3450 int ret = -1;
3451 struct quic_enc_level *qel;
3452 unsigned char *end, *pos;
3453 struct quic_tx_packet *pkt;
3454 size_t total;
3455 /* Each datagram is prepended with its length followed by the address
3456 * of the first packet in the datagram.
3457 */
3458 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
3459
3460 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3461
3462 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3463 total = 0;
3464 pos = (unsigned char *)b_tail(buf);
3465 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003466 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003467
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02003468 TRACE_PROTO("TX prep app pkts", QUIC_EV_CONN_PHPKTS, qc, qel, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003469 probe = 0;
3470 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3471 /* We do not probe if an immediate close was asked */
3472 if (!cc)
3473 probe = qel->pktns->tx.pto_probe;
3474
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003475 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003476 break;
3477
3478 /* Leave room for the datagram header */
3479 pos += dg_headlen;
3480 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3481 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3482 }
3483 else {
3484 end = pos + qc->path->mtu;
3485 }
3486
3487 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003488 QUIC_PACKET_TYPE_SHORT, must_ack, 0, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003489 switch (err) {
3490 case -2:
3491 // trace already emitted by function above
3492 goto leave;
3493 case -1:
3494 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
3495 * MTU, we are here because of the congestion control window. There is
3496 * no need to try to reuse this buffer.
3497 */
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003498 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003499 goto out;
3500 default:
3501 break;
3502 }
3503
3504 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
3505 BUG_ON(!pkt);
3506
3507 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3508 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3509
3510 total += pkt->len;
3511
3512 /* Write datagram header. */
3513 qc_txb_store(buf, pkt->len, pkt);
3514 }
3515
3516 out:
3517 ret = total;
3518 leave:
3519 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3520 return ret;
3521}
3522
3523/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3524 * <frms>. Several packets can be regrouped in a single datagram. The result is
3525 * written into <buf>.
3526 *
3527 * Each datagram is prepended by a two fields header : the datagram length and
3528 * the address of first packet in the datagram.
3529 *
3530 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3531 * -1 if something wrong happened.
3532 */
3533static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
3534 enum quic_tls_enc_level tel, struct list *tel_frms,
3535 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3536{
3537 struct quic_enc_level *qel;
3538 unsigned char *end, *pos;
3539 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3540 /* length of datagrams */
3541 uint16_t dglen;
3542 size_t total;
3543 int ret = -1, padding;
3544 /* Each datagram is prepended with its length followed by the address
3545 * of the first packet in the datagram.
3546 */
3547 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
3548 struct list *frms;
3549
3550 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3551
3552 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05003553 * caller must ensure that buf is reset.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003554 */
3555 BUG_ON_HOT(buf->head || buf->data);
3556
3557 total = 0;
3558 qel = &qc->els[tel];
3559 frms = tel_frms;
3560 dglen = 0;
3561 padding = 0;
3562 pos = (unsigned char *)b_head(buf);
3563 first_pkt = prv_pkt = NULL;
3564 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003565 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003566 enum quic_pkt_type pkt_type;
3567 struct quic_tls_ctx *tls_ctx;
3568 const struct quic_version *ver;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003569
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003570 TRACE_PROTO("TX prep pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003571 probe = 0;
3572 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3573 /* We do not probe if an immediate close was asked */
3574 if (!cc)
3575 probe = qel->pktns->tx.pto_probe;
3576
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003577 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003578 if (prv_pkt)
3579 qc_txb_store(buf, dglen, first_pkt);
3580 /* Let's select the next encryption level */
3581 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3582 tel = next_tel;
3583 frms = next_tel_frms;
3584 qel = &qc->els[tel];
3585 /* Build a new datagram */
3586 prv_pkt = NULL;
3587 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
3588 continue;
3589 }
3590 break;
3591 }
3592
3593 pkt_type = quic_tls_level_pkt_type(tel);
3594 if (!prv_pkt) {
3595 /* Leave room for the datagram header */
3596 pos += dg_headlen;
3597 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3598 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3599 }
3600 else {
3601 end = pos + qc->path->mtu;
3602 }
3603 }
3604
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003605 /* RFC 9000 14.1 Initial datagram size
3606 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3607 * Initial packets to at least the smallest allowed maximum datagram size of
3608 * 1200 bytes.
3609 *
3610 * Ensure that no ack-eliciting packets are sent into too small datagrams
3611 */
3612 if (pkt_type == QUIC_PACKET_TYPE_INITIAL && !LIST_ISEMPTY(tel_frms)) {
3613 if (end - pos < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01003614 TRACE_PROTO("No more enough room to build an Initial packet",
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003615 QUIC_EV_CONN_PHPKTS, qc);
3616 goto out;
3617 }
3618
3619 /* Pad this Initial packet if there is no ack-eliciting frames to send from
3620 * the next packet number space.
3621 */
Frédéric Lécailleec937212023-03-03 17:34:41 +01003622 if (!next_tel_frms || LIST_ISEMPTY(next_tel_frms))
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003623 padding = 1;
3624 }
3625
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003626 if (qc->negotiated_version) {
3627 ver = qc->negotiated_version;
3628 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3629 tls_ctx = &qc->negotiated_ictx;
3630 else
3631 tls_ctx = &qel->tls_ctx;
3632 }
3633 else {
3634 ver = qc->original_version;
3635 tls_ctx = &qel->tls_ctx;
3636 }
3637
3638 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
3639 qc, ver, dglen, pkt_type,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003640 must_ack, padding, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003641 switch (err) {
3642 case -2:
3643 // trace already emitted by function above
3644 goto leave;
3645 case -1:
3646 /* If there was already a correct packet present, set the
3647 * current datagram as prepared into <cbuf>.
3648 */
3649 if (prv_pkt)
3650 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003651 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003652 goto out;
3653 default:
3654 break;
3655 }
3656
3657 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
3658 BUG_ON(!cur_pkt);
3659
3660 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3661 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3662
3663 total += cur_pkt->len;
3664 /* keep trace of the first packet in the datagram */
3665 if (!first_pkt)
3666 first_pkt = cur_pkt;
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01003667 /* Attach the current one to the previous one and vice versa */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003668 if (prv_pkt) {
3669 prv_pkt->next = cur_pkt;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01003670 cur_pkt->prev = prv_pkt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003671 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3672 }
3673 /* Let's say we have to build a new dgram */
3674 prv_pkt = NULL;
3675 dglen += cur_pkt->len;
3676 /* Client: discard the Initial encryption keys as soon as
3677 * a handshake packet could be built.
3678 */
3679 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
3680 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3681 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3682 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3683 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3684 qc_set_timer(qc);
3685 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3686 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3687 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
3688 }
3689 /* If the data for the current encryption level have all been sent,
3690 * select the next level.
3691 */
3692 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillea576c1b2023-04-13 15:59:02 +02003693 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms))) {
Frédéric Lécaille895700b2023-04-13 18:30:16 +02003694 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try
3695 * QUIC_TLS_ENC_LEVEL_APP except if the connection was probing.
3696 */
3697 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) {
3698 if ((qc->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED)) {
3699 TRACE_PROTO("skip APP enc. level", QUIC_EV_CONN_PHPKTS, qc);
3700 qc_txb_store(buf, dglen, first_pkt);
3701 goto out;
3702 }
3703
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003704 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaille895700b2023-04-13 18:30:16 +02003705 }
3706
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003707 tel = next_tel;
3708 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3709 frms = &qc->els[tel].pktns->tx.frms;
3710 else
3711 frms = next_tel_frms;
3712 qel = &qc->els[tel];
3713 if (!LIST_ISEMPTY(frms)) {
3714 /* If there is data for the next level, do not
3715 * consume a datagram.
3716 */
3717 prv_pkt = cur_pkt;
3718 }
3719 }
3720
3721 /* If we have to build a new datagram, set the current datagram as
3722 * prepared into <cbuf>.
3723 */
3724 if (!prv_pkt) {
3725 qc_txb_store(buf, dglen, first_pkt);
3726 first_pkt = NULL;
3727 dglen = 0;
3728 padding = 0;
3729 }
3730 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
3731 (!qc_is_listener(qc) ||
3732 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3733 padding = 1;
3734 }
3735 }
3736
3737 out:
3738 ret = total;
3739 leave:
3740 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3741 return ret;
3742}
3743
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003744/* Free all frames in <l> list. In addition also remove all these frames
3745 * from the original ones if they are the results of duplications.
3746 */
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003747static inline void qc_free_frm_list(struct list *l)
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003748{
3749 struct quic_frame *frm, *frmbak;
3750
3751 list_for_each_entry_safe(frm, frmbak, l, list) {
3752 LIST_DEL_INIT(&frm->ref);
3753 qc_frm_free(&frm);
3754 }
3755}
3756
3757/* Free <pkt> TX packet and all the packets coalesced to it. */
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003758static inline void qc_free_tx_coalesced_pkts(struct quic_tx_packet *p)
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003759{
3760 struct quic_tx_packet *pkt, *nxt_pkt;
3761
3762 for (pkt = p; pkt; pkt = nxt_pkt) {
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003763 qc_free_frm_list(&pkt->frms);
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003764 nxt_pkt = pkt->next;
3765 pool_free(pool_head_quic_tx_packet, pkt);
3766 }
3767}
3768
3769/* Purge <buf> TX buffer from its prepare packets. */
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003770static void qc_purge_tx_buf(struct buffer *buf)
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003771{
3772 while (b_contig_data(buf, 0)) {
3773 uint16_t dglen;
3774 struct quic_tx_packet *pkt;
3775 size_t headlen = sizeof dglen + sizeof pkt;
3776
3777 dglen = read_u16(b_head(buf));
3778 pkt = read_ptr(b_head(buf) + sizeof dglen);
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003779 qc_free_tx_coalesced_pkts(pkt);
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003780 b_del(buf, dglen + headlen);
3781 }
3782
3783 BUG_ON(b_data(buf));
3784}
3785
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003786/* Send datagrams stored in <buf>.
3787 *
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003788 * This function returns 1 for success. On error, there is several behavior
3789 * depending on underlying sendto() error :
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003790 * - for an unrecoverable error, 0 is returned and connection is killed.
3791 * - a transient error is handled differently if connection has its owned
3792 * socket. If this is the case, 0 is returned and socket is subscribed on the
3793 * poller. The other case is assimilated to a success case with 1 returned.
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003794 * Remaining data are purged from the buffer and will eventually be detected
3795 * as lost which gives the opportunity to retry sending.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003796 */
3797int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
3798{
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003799 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003800 struct quic_conn *qc;
3801 char skip_sendto = 0;
3802
3803 qc = ctx->qc;
3804 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
3805 while (b_contig_data(buf, 0)) {
3806 unsigned char *pos;
3807 struct buffer tmpbuf = { };
3808 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3809 uint16_t dglen;
3810 size_t headlen = sizeof dglen + sizeof first_pkt;
3811 unsigned int time_sent;
3812
3813 pos = (unsigned char *)b_head(buf);
3814 dglen = read_u16(pos);
3815 BUG_ON_HOT(!dglen); /* this should not happen */
3816
3817 pos += sizeof dglen;
3818 first_pkt = read_ptr(pos);
3819 pos += sizeof first_pkt;
3820 tmpbuf.area = (char *)pos;
3821 tmpbuf.size = tmpbuf.data = dglen;
3822
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003823 TRACE_PROTO("TX dgram", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003824 /* If sendto is on error just skip the call to it for the rest
3825 * of the loop but continue to purge the buffer. Data will be
3826 * transmitted when QUIC packets are detected as lost on our
3827 * side.
3828 *
3829 * TODO use fd-monitoring to detect when send operation can be
3830 * retry. This should improve the bandwidth without relying on
3831 * retransmission timer. However, it requires a major rework on
3832 * quic-conn fd management.
3833 */
3834 if (!skip_sendto) {
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003835 int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0);
3836 if (ret < 0) {
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003837 TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc, first_pkt);
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003838 qc_kill_conn(qc);
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003839 qc_free_tx_coalesced_pkts(first_pkt);
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003840 b_del(buf, dglen + headlen);
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003841 qc_purge_tx_buf(buf);
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003842 goto leave;
3843 }
3844 else if (!ret) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003845 /* Connection owned socket : poller will wake us up when transient error is cleared. */
3846 if (qc_test_fd(qc)) {
3847 TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc);
3848 goto leave;
3849 }
3850
3851 /* No connection owned-socket : rely on retransmission to retry sending. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003852 skip_sendto = 1;
3853 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
3854 }
3855 }
3856
3857 b_del(buf, dglen + headlen);
3858 qc->tx.bytes += tmpbuf.data;
3859 time_sent = now_ms;
3860
3861 for (pkt = first_pkt; pkt; pkt = next_pkt) {
Frédéric Lécailleceb88b82023-02-20 14:43:55 +01003862 /* RFC 9000 14.1 Initial datagram size
3863 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3864 * Initial packets to at least the smallest allowed maximum datagram size of
3865 * 1200 bytes.
3866 */
3867 BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3868 (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
3869 dglen < QUIC_INITIAL_PACKET_MINLEN);
3870
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003871 pkt->time_sent = time_sent;
3872 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3873 pkt->pktns->tx.time_of_last_eliciting = time_sent;
3874 qc->path->ifae_pkts++;
3875 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
Frédéric Lécailled7215712023-03-24 18:13:37 +01003876 qc_idle_timer_rearm(qc, 0, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003877 }
3878 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3879 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3880 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003881 qc_detach_th_ctx_list(qc, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003882 qc_notify_close(qc);
3883
3884 /* RFC 9000 10.2. Immediate Close:
3885 * The closing and draining connection states exist to ensure
3886 * that connections close cleanly and that delayed or reordered
3887 * packets are properly discarded. These states SHOULD persist
3888 * for at least three times the current PTO interval...
3889 *
3890 * Rearm the idle timeout only one time when entering closing
3891 * state.
3892 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01003893 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003894 if (qc->timer_task) {
3895 task_destroy(qc->timer_task);
3896 qc->timer_task = NULL;
3897 }
3898 }
3899 qc->path->in_flight += pkt->in_flight_len;
3900 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3901 if (pkt->in_flight_len)
3902 qc_set_timer(qc);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003903 TRACE_PROTO("TX pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003904 next_pkt = pkt->next;
3905 quic_tx_packet_refinc(pkt);
3906 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
3907 }
3908 }
3909
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003910 ret = 1;
3911leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003912 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3913
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003914 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003915}
3916
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003917/* Copy at <pos> position a stateless reset token depending on the
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003918 * <salt> salt input. This is the cluster secret which will be derived
3919 * as HKDF input secret to generate this token.
3920 * Return 1 if succeeded, 0 if not.
3921 */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003922static int quic_stateless_reset_token_cpy(unsigned char *pos, size_t len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003923 const unsigned char *salt, size_t saltlen)
3924{
3925 /* Input secret */
3926 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3927 size_t keylen = strlen(global.cluster_secret);
3928 /* Info */
3929 const unsigned char label[] = "stateless token";
3930 size_t labellen = sizeof label - 1;
3931 int ret;
3932
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003933 ret = quic_hkdf_extract_and_expand(EVP_sha256(), pos, len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003934 key, keylen, salt, saltlen, label, labellen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003935 return ret;
3936}
3937
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003938/* Initialize the stateless reset token attached to <conn_id> connection ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003939 * Returns 1 if succeeded, 0 if not.
3940 */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003941static int quic_stateless_reset_token_init(struct quic_connection_id *conn_id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003942{
3943 int ret;
3944
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003945 if (global.cluster_secret) {
3946 /* Output secret */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003947 unsigned char *token = conn_id->stateless_reset_token;
3948 size_t tokenlen = sizeof conn_id->stateless_reset_token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003949 /* Salt */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003950 const unsigned char *cid = conn_id->cid.data;
3951 size_t cidlen = conn_id->cid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003952
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02003953 ret = quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003954 }
3955 else {
3956 /* TODO: RAND_bytes() should be replaced */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003957 ret = RAND_bytes(conn_id->stateless_reset_token,
3958 sizeof conn_id->stateless_reset_token) == 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003959 }
3960
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003961 return ret;
3962}
3963
Amaury Denoyelle1e959ad2023-04-13 17:34:56 +02003964/* Generate a CID directly derived from <orig> CID and <addr> address.
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003965 *
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003966 * Returns the derived CID.
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003967 */
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003968struct quic_cid quic_derive_cid(const struct quic_cid *orig,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003969 const struct sockaddr_storage *addr)
3970{
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003971 struct quic_cid cid;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003972 const struct sockaddr_in *in;
3973 const struct sockaddr_in6 *in6;
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003974 char *pos = trash.area;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003975 size_t idx = 0;
3976 uint64_t hash;
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003977 int i;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003978
3979 /* Prepare buffer for hash using original CID first. */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003980 memcpy(pos, orig->data, orig->len);
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003981 idx += orig->len;
3982
3983 /* Concatenate client address. */
3984 switch (addr->ss_family) {
3985 case AF_INET:
3986 in = (struct sockaddr_in *)addr;
3987
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003988 memcpy(&pos[idx], &in->sin_addr, sizeof(in->sin_addr));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003989 idx += sizeof(in->sin_addr);
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003990 memcpy(&pos[idx], &in->sin_port, sizeof(in->sin_port));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003991 idx += sizeof(in->sin_port);
3992 break;
3993
3994 case AF_INET6:
3995 in6 = (struct sockaddr_in6 *)addr;
3996
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003997 memcpy(&pos[idx], &in6->sin6_addr, sizeof(in6->sin6_addr));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003998 idx += sizeof(in6->sin6_addr);
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003999 memcpy(&pos[idx], &in6->sin6_port, sizeof(in6->sin6_port));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004000 idx += sizeof(in6->sin6_port);
4001 break;
4002
4003 default:
4004 /* TODO to implement */
4005 ABORT_NOW();
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004006 }
4007
4008 /* Avoid similar values between multiple haproxy process. */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004009 memcpy(&pos[idx], boot_seed, sizeof(boot_seed));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004010 idx += sizeof(boot_seed);
4011
4012 /* Hash the final buffer content. */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004013 hash = XXH64(pos, idx, 0);
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004014
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004015 for (i = 0; i < sizeof(hash); ++i)
4016 cid.data[i] = hash >> ((sizeof(hash) * 7) - (8 * i));
4017 cid.len = sizeof(hash);
4018
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004019 return cid;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004020}
4021
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004022/* Retrieve the thread ID associated to QUIC connection ID <cid> of length
4023 * <cid_len>. CID may be not found on the CID tree because it is an ODCID. In
4024 * this case, it will derived using client address <cli_addr> as hash
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004025 * parameter. However, this is done only if <pos> points to an INITIAL or 0RTT
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004026 * packet of length <len>.
4027 *
4028 * Returns the thread ID or a negative error code.
4029 */
4030int quic_get_cid_tid(const unsigned char *cid, size_t cid_len,
4031 const struct sockaddr_storage *cli_addr,
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004032 unsigned char *pos, size_t len)
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004033{
4034 struct quic_cid_tree *tree;
4035 struct quic_connection_id *conn_id;
4036 struct ebmb_node *node;
4037
4038 tree = &quic_cid_trees[_quic_cid_tree_idx(cid)];
4039 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
4040 node = ebmb_lookup(&tree->root, cid, cid_len);
4041 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
4042
4043 if (!node) {
4044 struct quic_cid orig, derive_cid;
4045 struct quic_rx_packet pkt;
4046
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004047 if (!qc_parse_hd_form(&pkt, &pos, pos + len))
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004048 goto not_found;
4049
4050 if (pkt.type != QUIC_PACKET_TYPE_INITIAL &&
4051 pkt.type != QUIC_PACKET_TYPE_0RTT) {
4052 goto not_found;
4053 }
4054
4055 memcpy(orig.data, cid, cid_len);
4056 orig.len = cid_len;
4057 derive_cid = quic_derive_cid(&orig, cli_addr);
4058
4059 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
4060 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
4061 node = ebmb_lookup(&tree->root, cid, cid_len);
4062 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
4063 }
4064
4065 if (!node)
4066 goto not_found;
4067
4068 conn_id = ebmb_entry(node, struct quic_connection_id, node);
4069 return HA_ATOMIC_LOAD(&conn_id->tid);
4070
4071 not_found:
4072 return -1;
4073}
4074
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004075/* Allocate a new CID and attach it to <root> ebtree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004076 *
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004077 * If <orig> and <addr> params are non null, the new CID value is directly
4078 * derived from them. Else a random value is generated. The CID is then marked
4079 * with the current thread ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004080 *
4081 * Returns the new CID if succeeded, NULL if not.
4082 */
4083static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004084 struct quic_conn *qc,
4085 const struct quic_cid *orig,
4086 const struct sockaddr_storage *addr)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004087{
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004088 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004089
4090 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4091
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004092 /* Caller must set either none or both values. */
4093 BUG_ON(!!orig != !!addr);
4094
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004095 conn_id = pool_alloc(pool_head_quic_connection_id);
4096 if (!conn_id) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004097 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
4098 goto err;
4099 }
4100
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004101 conn_id->cid.len = QUIC_HAP_CID_LEN;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004102
4103 if (!orig) {
4104 /* TODO: RAND_bytes() should be replaced */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004105 if (RAND_bytes(conn_id->cid.data, conn_id->cid.len) != 1) {
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004106 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
4107 goto err;
4108 }
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004109 }
4110 else {
4111 /* Derive the new CID value from original CID. */
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004112 conn_id->cid = quic_derive_cid(orig, addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004113 }
4114
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004115 if (quic_stateless_reset_token_init(conn_id) != 1) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004116 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
4117 goto err;
4118 }
4119
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004120 conn_id->qc = qc;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004121 HA_ATOMIC_STORE(&conn_id->tid, tid);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004122
Amaury Denoyellef16ec342023-04-13 17:42:34 +02004123 conn_id->seq_num.key = qc ? qc->next_cid_seq_num++ : 0;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004124 conn_id->retire_prior_to = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004125 /* insert the allocated CID in the quic_conn tree */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02004126 if (root)
4127 eb64_insert(root, &conn_id->seq_num);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004128
4129 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004130 return conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004131
4132 err:
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004133 pool_free(pool_head_quic_connection_id, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004134 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4135 return NULL;
4136}
4137
4138/* Build all the frames which must be sent just after the handshake have succeeded.
4139 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
4140 * a HANDSHAKE_DONE frame.
4141 * Return 1 if succeeded, 0 if not.
4142 */
4143static int quic_build_post_handshake_frames(struct quic_conn *qc)
4144{
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004145 int ret = 0, max;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004146 struct quic_enc_level *qel;
4147 struct quic_frame *frm, *frmbak;
4148 struct list frm_list = LIST_HEAD_INIT(frm_list);
4149 struct eb64_node *node;
4150
4151 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4152
4153 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4154 /* Only servers must send a HANDSHAKE_DONE frame. */
4155 if (qc_is_listener(qc)) {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004156 frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004157 if (!frm) {
4158 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4159 goto leave;
4160 }
4161
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004162 LIST_APPEND(&frm_list, &frm->list);
4163 }
4164
4165 /* Initialize <max> connection IDs minus one: there is
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004166 * already one connection ID used for the current connection. Also limit
4167 * the number of connection IDs sent to the peer to 4 (3 from this function
4168 * plus 1 for the current connection.
4169 * Note that active_connection_id_limit >= 2: this has been already checked
4170 * when receiving this parameter.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004171 */
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004172 max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3);
4173 while (max--) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004174 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004175
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004176 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004177 if (!frm) {
4178 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4179 goto err;
4180 }
4181
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004182 conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
4183 if (!conn_id) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004184 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004185 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
4186 goto err;
4187 }
4188
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004189 /* TODO To prevent CID tree locking, all CIDs created here
4190 * could be allocated at the same time as the first one.
4191 */
4192 quic_cid_insert(conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004193
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004194 quic_connection_id_to_frm_cpy(frm, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004195 LIST_APPEND(&frm_list, &frm->list);
4196 }
4197
4198 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004199 qc->flags &= ~QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004200
4201 ret = 1;
4202 leave:
4203 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4204 return ret;
4205
4206 err:
4207 /* free the frames */
4208 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004209 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004210
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004211 /* The first CID sequence number value used to allocated CIDs by this function is 1,
4212 * 0 being the sequence number of the CID for this connection.
4213 */
4214 node = eb64_lookup_ge(&qc->cids, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004215 while (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004216 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004217
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004218 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
4219 if (conn_id->seq_num.key >= max)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004220 break;
4221
4222 node = eb64_next(node);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004223 quic_cid_delete(conn_id);
4224
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004225 eb64_delete(&conn_id->seq_num);
4226 pool_free(pool_head_quic_connection_id, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004227 }
4228 goto leave;
4229}
4230
4231/* Deallocate <l> list of ACK ranges. */
4232void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
4233{
4234 struct eb64_node *n;
4235 struct quic_arng_node *ar;
4236
4237 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4238
4239 n = eb64_first(&arngs->root);
4240 while (n) {
4241 struct eb64_node *next;
4242
4243 ar = eb64_entry(n, struct quic_arng_node, first);
4244 next = eb64_next(n);
4245 eb64_delete(n);
4246 pool_free(pool_head_quic_arng, ar);
4247 n = next;
4248 }
4249
4250 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4251}
4252
4253/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
4254 * descending order.
4255 */
4256static inline size_t sack_gap(struct quic_arng_node *p,
4257 struct quic_arng_node *q)
4258{
4259 return p->first.key - q->last - 2;
4260}
4261
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004262/* Set the encoded size of <arngs> QUIC ack ranges. */
4263static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
4264{
4265 struct eb64_node *node, *next;
4266 struct quic_arng_node *ar, *ar_next;
4267
4268 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4269
4270 node = eb64_last(&arngs->root);
4271 if (!node)
4272 goto leave;
4273
4274 ar = eb64_entry(node, struct quic_arng_node, first);
4275 arngs->enc_sz = quic_int_getsize(ar->last) +
4276 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
4277
4278 while ((next = eb64_prev(node))) {
4279 ar_next = eb64_entry(next, struct quic_arng_node, first);
4280 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
4281 quic_int_getsize(ar_next->last - ar_next->first.key);
4282 node = next;
4283 ar = eb64_entry(node, struct quic_arng_node, first);
4284 }
4285
4286 leave:
4287 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4288}
4289
4290/* Insert <ar> ack range into <argns> tree of ack ranges.
4291 * Returns the ack range node which has been inserted if succeeded, NULL if not.
4292 */
4293static inline
4294struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
4295 struct quic_arngs *arngs,
4296 struct quic_arng *ar)
4297{
4298 struct quic_arng_node *new_ar;
4299
4300 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4301
Frédéric Lécaille0ed94032023-04-17 14:10:14 +02004302 if (arngs->sz >= QUIC_MAX_ACK_RANGES) {
4303 struct eb64_node *last;
4304
4305 last = eb64_last(&arngs->root);
4306 BUG_ON(last == NULL);
4307 eb64_delete(last);
4308 pool_free(pool_head_quic_arng, last);
4309 arngs->sz--;
4310 }
4311
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004312 new_ar = pool_alloc(pool_head_quic_arng);
4313 if (!new_ar) {
4314 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
4315 goto leave;
4316 }
4317
4318 new_ar->first.key = ar->first;
4319 new_ar->last = ar->last;
4320 eb64_insert(&arngs->root, &new_ar->first);
4321 arngs->sz++;
4322
4323 leave:
4324 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4325 return new_ar;
4326}
4327
4328/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
4329 * Note that this function computes the number of bytes required to encode
4330 * this tree of ACK ranges in descending order.
4331 *
4332 * Descending order
4333 * ------------->
4334 * range1 range2
4335 * ..........|--------|..............|--------|
4336 * ^ ^ ^ ^
4337 * | | | |
4338 * last1 first1 last2 first2
4339 * ..........+--------+--------------+--------+......
4340 * diff1 gap12 diff2
4341 *
4342 * To encode the previous list of ranges we must encode integers as follows in
4343 * descending order:
4344 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
4345 * with diff1 = last1 - first1
4346 * diff2 = last2 - first2
4347 * gap12 = first1 - last2 - 2 (>= 0)
4348 *
4349
4350returns 0 on error
4351
4352 */
4353int quic_update_ack_ranges_list(struct quic_conn *qc,
4354 struct quic_arngs *arngs,
4355 struct quic_arng *ar)
4356{
4357 int ret = 0;
4358 struct eb64_node *le;
4359 struct quic_arng_node *new_node;
4360 struct eb64_node *new;
4361
4362 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4363
4364 new = NULL;
4365 if (eb_is_empty(&arngs->root)) {
4366 new_node = quic_insert_new_range(qc, arngs, ar);
4367 if (new_node)
4368 ret = 1;
4369
4370 goto leave;
4371 }
4372
4373 le = eb64_lookup_le(&arngs->root, ar->first);
4374 if (!le) {
4375 new_node = quic_insert_new_range(qc, arngs, ar);
4376 if (!new_node)
4377 goto leave;
4378
4379 new = &new_node->first;
4380 }
4381 else {
4382 struct quic_arng_node *le_ar =
4383 eb64_entry(le, struct quic_arng_node, first);
4384
4385 /* Already existing range */
4386 if (le_ar->last >= ar->last) {
4387 ret = 1;
4388 }
4389 else if (le_ar->last + 1 >= ar->first) {
4390 le_ar->last = ar->last;
4391 new = le;
4392 new_node = le_ar;
4393 }
4394 else {
4395 new_node = quic_insert_new_range(qc, arngs, ar);
4396 if (!new_node)
4397 goto leave;
4398
4399 new = &new_node->first;
4400 }
4401 }
4402
4403 /* Verify that the new inserted node does not overlap the nodes
4404 * which follow it.
4405 */
4406 if (new) {
4407 struct eb64_node *next;
4408 struct quic_arng_node *next_node;
4409
4410 while ((next = eb64_next(new))) {
4411 next_node =
4412 eb64_entry(next, struct quic_arng_node, first);
4413 if (new_node->last + 1 < next_node->first.key)
4414 break;
4415
4416 if (next_node->last > new_node->last)
4417 new_node->last = next_node->last;
4418 eb64_delete(next);
4419 pool_free(pool_head_quic_arng, next_node);
4420 /* Decrement the size of these ranges. */
4421 arngs->sz--;
4422 }
4423 }
4424
4425 ret = 1;
4426 leave:
4427 quic_arngs_set_enc_sz(qc, arngs);
4428 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4429 return ret;
4430}
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004431
4432/* Detect the value of the spin bit to be used. */
4433static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt,
4434 struct quic_enc_level *qel)
4435{
4436 uint64_t largest_pn = qel->pktns->rx.largest_pn;
4437
4438 if (qel != &qc->els[QUIC_TLS_ENC_LEVEL_APP] || largest_pn == -1 ||
4439 pkt->pn <= largest_pn)
4440 return;
4441
4442 if (qc_is_listener(qc)) {
4443 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4444 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4445 else
4446 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4447 }
4448 else {
4449 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4450 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4451 else
4452 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4453 }
4454}
4455
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004456/* Remove the header protection of packets at <el> encryption level.
4457 * Always succeeds.
4458 */
4459static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
4460{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004461 struct quic_rx_packet *pqpkt, *pkttmp;
4462 struct quic_enc_level *app_qel;
4463
4464 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
4465 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4466 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
4467 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004468 TRACE_PROTO("RX hp not removed (handshake not completed)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004469 QUIC_EV_CONN_ELRMHP, qc);
4470 goto out;
4471 }
Frédéric Lécaille72027782023-02-22 16:20:09 +01004472
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004473 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01004474 struct quic_tls_ctx *tls_ctx;
4475
4476 tls_ctx = qc_select_tls_ctx(qc, el, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004477 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
4478 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004479 TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004480 }
4481 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004482 qc_handle_spin_bit(qc, pqpkt, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004483 /* The AAD includes the packet number field */
4484 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
4485 /* Store the packet into the tree of packets to decrypt. */
4486 pqpkt->pn_node.key = pqpkt->pn;
4487 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
4488 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004489 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004490 }
4491 LIST_DELETE(&pqpkt->list);
4492 quic_rx_packet_refdec(pqpkt);
4493 }
4494
4495 out:
4496 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
4497}
4498
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004499/* Process all the CRYPTO frame at <el> encryption level. This is the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004500 * responsibility of the called to ensure there exists a CRYPTO data
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004501 * stream for this level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004502 * Return 1 if succeeded, 0 if not.
4503 */
4504static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
4505 struct quic_enc_level *el,
4506 struct ssl_sock_ctx *ctx)
4507{
4508 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004509 struct ncbuf *ncbuf;
4510 struct quic_cstream *cstream = el->cstream;
4511 ncb_sz_t data;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004512
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004513 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004514
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004515 BUG_ON(!cstream);
4516 ncbuf = &cstream->rx.ncbuf;
4517 if (ncb_is_null(ncbuf))
4518 goto done;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004519
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004520 /* TODO not working if buffer is wrapping */
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004521 while ((data = ncb_data(ncbuf, 0))) {
4522 const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004523
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004524 if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004525 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004526
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004527 cstream->rx.offset += data;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004528 TRACE_DEVEL("buffered crypto data were provided to TLS stack",
4529 QUIC_EV_CONN_PHPKTS, qc, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004530 }
4531
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004532 done:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004533 ret = 1;
4534 leave:
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004535 if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
4536 TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004537 quic_free_ncbuf(ncbuf);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004538 }
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004539 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004540 return ret;
4541}
4542
4543/* Process all the packets at <el> and <next_el> encryption level.
4544 * This is the caller responsibility to check that <cur_el> is different of <next_el>
4545 * as pointer value.
4546 * Return 1 if succeeded, 0 if not.
4547 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004548int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el,
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004549 struct quic_enc_level *next_el)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004550{
4551 int ret = 0;
4552 struct eb64_node *node;
4553 int64_t largest_pn = -1;
4554 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004555 struct quic_enc_level *qel = cur_el;
4556
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004557 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004558 qel = cur_el;
4559 next_tel:
4560 if (!qel)
4561 goto out;
4562
4563 node = eb64_first(&qel->rx.pkts);
4564 while (node) {
4565 struct quic_rx_packet *pkt;
4566
4567 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
4568 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004569 qc, pkt, NULL, qc->xprt_ctx->ssl);
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01004570 if (!qc_pkt_decrypt(qc, qel, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004571 /* Drop the packet */
4572 TRACE_ERROR("packet decryption failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004573 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004574 }
4575 else {
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004576 if (!qc_parse_pkt_frms(qc, pkt, qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004577 /* Drop the packet */
4578 TRACE_ERROR("packet parsing failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004579 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004580 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
4581 }
4582 else {
4583 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
4584
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004585 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
Frédéric Lécailleb5efe792023-04-14 09:56:17 +02004586 int arm_ack_timer =
4587 qc->state >= QUIC_HS_ST_COMPLETE &&
4588 qel->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT];
4589
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004590 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
4591 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécailleb5efe792023-04-14 09:56:17 +02004592 qc_idle_timer_rearm(qc, 1, arm_ack_timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004593 }
4594 if (pkt->pn > largest_pn) {
4595 largest_pn = pkt->pn;
4596 largest_pn_time_received = pkt->time_received;
4597 }
4598 /* Update the list of ranges to acknowledge. */
4599 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
4600 TRACE_ERROR("Could not update ack range list",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004601 QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004602 }
4603 }
4604 node = eb64_next(node);
4605 eb64_delete(&pkt->pn_node);
4606 quic_rx_packet_refdec(pkt);
4607 }
4608
4609 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
4610 /* Update the largest packet number. */
4611 qel->pktns->rx.largest_pn = largest_pn;
4612 /* Update the largest acknowledged packet timestamps */
4613 qel->pktns->rx.largest_time_received = largest_pn_time_received;
4614 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
4615 }
4616
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004617 if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004618 // trace already emitted by function above
4619 goto leave;
4620 }
4621
4622 if (qel == cur_el) {
4623 BUG_ON(qel == next_el);
4624 qel = next_el;
4625 largest_pn = -1;
4626 goto next_tel;
4627 }
4628
4629 out:
4630 ret = 1;
4631 leave:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004632 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004633 return ret;
4634}
4635
4636/* Check if it's possible to remove header protection for packets related to
4637 * encryption level <qel>. If <qel> is NULL, assume it's false.
4638 *
4639 * Return true if the operation is possible else false.
4640 */
4641static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
4642{
4643 int ret = 0;
4644 enum quic_tls_enc_level tel;
4645
4646 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
4647
4648 if (!qel)
4649 goto cant_rm_hp;
4650
4651 tel = ssl_to_quic_enc_level(qel->level);
4652
4653 /* check if tls secrets are available */
4654 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004655 TRACE_PROTO("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004656 goto cant_rm_hp;
4657 }
4658
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004659 if (!quic_tls_has_rx_sec(qel)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004660 TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004661 goto cant_rm_hp;
4662 }
4663
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004664 if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004665 TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004666 goto cant_rm_hp;
4667 }
4668
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004669 /* check if the connection layer is ready before using app level */
4670 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
4671 qc->mux_state == QC_MUX_NULL) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004672 TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004673 goto cant_rm_hp;
4674 }
4675
4676 ret = 1;
4677 cant_rm_hp:
4678 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
4679 return ret;
4680}
4681
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004682/* Flush txbuf for <qc> connection. This must be called prior to a packet
4683 * preparation when txbuf contains older data. A send will be conducted for
4684 * these data.
4685 *
4686 * Returns 1 on success : buffer is empty and can be use for packet
4687 * preparation. On error 0 is returned.
4688 */
4689static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
4690{
4691 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4692
4693 /* This operation can only be conducted if txbuf is not empty. This
4694 * case only happens for connection with their owned socket due to an
4695 * older transient sendto() error.
4696 */
4697 BUG_ON(!qc_test_fd(qc));
4698
4699 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4700 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4701 qc_txb_release(qc);
4702 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4703 return 0;
4704 }
4705
4706 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4707 return 1;
4708}
4709
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004710/* Try to send application frames from list <frms> on connection <qc>.
4711 *
4712 * Use qc_send_app_probing wrapper when probing with old data.
4713 *
4714 * Returns 1 on success. Some data might not have been sent due to congestion,
4715 * in this case they are left in <frms> input list. The caller may subscribe on
4716 * quic-conn to retry later.
4717 *
4718 * Returns 0 on critical error.
4719 * TODO review and classify more distinctly transient from definitive errors to
4720 * allow callers to properly handle it.
4721 */
4722static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
4723{
4724 int status = 0;
4725 struct buffer *buf;
4726
4727 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4728
4729 buf = qc_txb_alloc(qc);
4730 if (!buf) {
4731 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004732 goto err;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004733 }
4734
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004735 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4736 goto err;
4737
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004738 /* Prepare and send packets until we could not further prepare packets. */
4739 while (1) {
4740 int ret;
4741 /* Currently buf cannot be non-empty at this stage. Even if a
4742 * previous sendto() has failed it is emptied to simulate
4743 * packet emission and rely on QUIC lost detection to try to
4744 * emit it.
4745 */
4746 BUG_ON_HOT(b_data(buf));
4747 b_reset(buf);
4748
4749 ret = qc_prep_app_pkts(qc, buf, frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004750 if (ret == -1) {
4751 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004752 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004753 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004754
Amaury Denoyelle37333862023-02-28 11:53:48 +01004755 if (!ret)
4756 break;
4757
4758 if (!qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004759 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4760 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004761 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004762 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004763 }
4764
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004765 status = 1;
4766 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004767 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4768 return status;
4769
4770 err:
Amaury Denoyelle37333862023-02-28 11:53:48 +01004771 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4772 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004773}
4774
4775/* Try to send application frames from list <frms> on connection <qc>. Use this
4776 * function when probing is required.
4777 *
4778 * Returns the result from qc_send_app_pkts function.
4779 */
4780static forceinline int qc_send_app_probing(struct quic_conn *qc,
4781 struct list *frms)
4782{
4783 int ret;
4784
4785 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4786
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02004787 TRACE_PROTO("preparing old data (probing)", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004788 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4789 ret = qc_send_app_pkts(qc, frms);
4790 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
4791
4792 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4793 return ret;
4794}
4795
4796/* Try to send application frames from list <frms> on connection <qc>. This
4797 * function is provided for MUX upper layer usage only.
4798 *
4799 * Returns the result from qc_send_app_pkts function.
4800 */
4801int qc_send_mux(struct quic_conn *qc, struct list *frms)
4802{
4803 int ret;
4804
4805 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4806 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
4807
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004808 /* Try to send post handshake frames first unless on 0-RTT. */
4809 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) &&
4810 qc->state >= QUIC_HS_ST_COMPLETE) {
4811 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4812 quic_build_post_handshake_frames(qc);
4813 qc_send_app_pkts(qc, &qel->pktns->tx.frms);
4814 }
4815
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004816 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4817 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
4818 ret = qc_send_app_pkts(qc, frms);
4819 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
4820
4821 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4822 return ret;
4823}
4824
4825/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4826 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
4827 * QUIC connection. <old_data> is used as boolean to send data already sent but
4828 * not already acknowledged (in flight).
4829 * Returns 1 if succeeded, 0 if not.
4830 */
4831int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4832 enum quic_tls_enc_level tel, struct list *tel_frms,
4833 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4834{
4835 int ret, status = 0;
4836 struct buffer *buf = qc_txb_alloc(qc);
4837
4838 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4839
4840 if (!buf) {
4841 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4842 goto leave;
4843 }
4844
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004845 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4846 goto out;
4847
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004848 /* Currently buf cannot be non-empty at this stage. Even if a previous
4849 * sendto() has failed it is emptied to simulate packet emission and
4850 * rely on QUIC lost detection to try to emit it.
4851 */
4852 BUG_ON_HOT(b_data(buf));
4853 b_reset(buf);
4854
4855 if (old_data) {
4856 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
4857 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4858 }
4859
4860 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004861 if (ret == -1) {
4862 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004863 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004864 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004865
Amaury Denoyelle37333862023-02-28 11:53:48 +01004866 if (ret && !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 out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004870 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004871
Amaury Denoyelle37333862023-02-28 11:53:48 +01004872 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004873 status = 1;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004874
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004875 out:
4876 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
4877 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004878 leave:
4879 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4880 return status;
4881}
4882
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004883/* Retransmit up to two datagrams depending on packet number space.
4884 * Return 0 when failed, 0 if not.
4885 */
4886static int qc_dgrams_retransmit(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004887{
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004888 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004889 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4890 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4891 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4892
4893 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4894
4895 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004896 int i;
4897
4898 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4899 struct list ifrms = LIST_HEAD_INIT(ifrms);
4900 struct list hfrms = LIST_HEAD_INIT(hfrms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004901
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004902 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
4903 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4904 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
4905 if (!LIST_ISEMPTY(&ifrms)) {
4906 iqel->pktns->tx.pto_probe = 1;
4907 if (!LIST_ISEMPTY(&hfrms))
4908 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004909 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4910 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms))
4911 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004912 /* Put back unsent frames in their packet number spaces */
4913 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4914 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
4915 }
Frédéric Lécailleec937212023-03-03 17:34:41 +01004916 else {
4917 if (!(qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
4918 iqel->pktns->tx.pto_probe = 1;
4919 if (!qc_send_hdshk_pkts(qc, 0, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4920 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4921 goto leave;
4922 }
4923 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004924 }
4925 TRACE_STATE("no more need to probe Initial packet number space",
4926 QUIC_EV_CONN_TXPKT, qc);
4927 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004928 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004929 }
4930 else {
4931 int i;
4932
4933 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004934 hqel->pktns->tx.pto_probe = 0;
4935 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
Frédéric Lécaille7b5d9b12022-11-28 17:21:45 +01004936 struct list frms1 = LIST_HEAD_INIT(frms1);
4937
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004938 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
4939 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4940 if (!LIST_ISEMPTY(&frms1)) {
4941 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004942 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4943 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4944 goto leave;
4945
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004946 /* Put back unsent frames into their packet number spaces */
4947 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
4948 }
4949 }
4950 TRACE_STATE("no more need to probe Handshake packet number space",
4951 QUIC_EV_CONN_TXPKT, qc);
4952 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4953 }
4954 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4955 struct list frms2 = LIST_HEAD_INIT(frms2);
4956 struct list frms1 = LIST_HEAD_INIT(frms1);
4957
4958 aqel->pktns->tx.pto_probe = 0;
4959 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4960 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4961 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4962 if (!LIST_ISEMPTY(&frms1)) {
4963 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillec6bec2a2023-04-24 11:20:32 +02004964 if (!qc_send_app_probing(qc, &frms1)) {
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02004965 qc_free_frm_list(&frms2);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004966 goto leave;
Frédéric Lécaillec6bec2a2023-04-24 11:20:32 +02004967 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004968
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004969 /* Put back unsent frames into their packet number spaces */
4970 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
4971 }
4972 if (!LIST_ISEMPTY(&frms2)) {
4973 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004974 if (!qc_send_app_probing(qc, &frms2))
4975 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004976 /* Put back unsent frames into their packet number spaces */
4977 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
4978 }
4979 TRACE_STATE("no more need to probe 01RTT packet number space",
4980 QUIC_EV_CONN_TXPKT, qc);
4981 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4982 }
4983 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004984
4985 ret = 1;
4986 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004987 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004988 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004989}
4990
4991/* QUIC connection packet handler task (post handshake) */
4992struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4993{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004994 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004995 struct quic_enc_level *qel;
4996
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004997 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02004998
4999 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005000 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
5001
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01005002 if (qc_test_fd(qc))
5003 qc_rcv_buf(qc);
5004
Amaury Denoyelle1304d192023-04-11 16:46:03 +02005005 /* Prepare post-handshake frames
5006 * - after connection is instantiated (accept is done)
5007 * - handshake state is completed (may not be the case here in 0-RTT)
5008 */
5009 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) && qc->conn &&
5010 qc->state >= QUIC_HS_ST_COMPLETE) {
5011 quic_build_post_handshake_frames(qc);
5012 }
5013
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005014 /* Retranmissions */
5015 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
5016 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
5017 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005018 if (!qc_dgrams_retransmit(qc))
5019 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005020 }
5021
5022 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
5023 qc_rm_hp_pkts(qc, qel);
5024
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005025 if (!qc_treat_rx_pkts(qc, qel, NULL)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005026 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
5027 goto out;
5028 }
5029
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005030 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5031 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
5032 goto out;
5033 }
5034
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005035 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5036 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
5037 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
5038 goto out;
5039 }
5040
5041 /* XXX TODO: how to limit the list frames to send */
5042 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
5043 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
5044 goto out;
5045 }
5046
5047 out:
5048 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
5049 return t;
5050}
5051
5052/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
5053static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
5054{
5055 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
5056 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
5057 qel->pktns->tx.pto_probe ||
5058 !LIST_ISEMPTY(&qel->pktns->tx.frms);
5059}
5060
5061/* QUIC connection packet handler task. */
5062struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
5063{
5064 int ret, ssl_err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005065 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005066 enum quic_tls_enc_level tel, next_tel;
5067 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005068 /* Early-data encryption level */
5069 struct quic_enc_level *eqel;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005070 struct buffer *buf = NULL;
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005071 int st, zero_rtt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005072
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005073 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02005074
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005075 eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005076 st = qc->state;
5077 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
5078
5079 /* Retranmissions */
5080 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
5081 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
5082 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005083 if (!qc_dgrams_retransmit(qc))
5084 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005085 }
5086
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005087 ssl_err = SSL_ERROR_NONE;
5088 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005089 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005090 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01005091
5092 if (qc_test_fd(qc))
5093 qc_rcv_buf(qc);
5094
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005095 if (st >= QUIC_HS_ST_COMPLETE &&
5096 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
5097 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
5098 /* There may be remaining Handshake packets to treat and acknowledge. */
5099 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
5100 next_tel = QUIC_TLS_ENC_LEVEL_APP;
5101 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005102 else if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, zero_rtt))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005103 goto out;
5104
5105 qel = &qc->els[tel];
5106 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
5107
5108 next_level:
5109 /* Treat packets waiting for header packet protection decryption */
5110 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
5111 qc_rm_hp_pkts(qc, qel);
5112
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005113 if (!qc_treat_rx_pkts(qc, qel, next_qel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005114 goto out;
5115
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005116 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5117 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
5118 goto out;
5119 }
5120
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005121 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5122 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
5123 goto out;
5124
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005125 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005126 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005127 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
5128 if (next_qel && next_qel == eqel && zero_rtt) {
5129 TRACE_DEVEL("select 0RTT as next encryption level",
5130 QUIC_EV_CONN_PHPKTS, qc);
5131 qel = next_qel;
5132 next_qel = NULL;
5133 goto next_level;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005134 }
5135
5136 st = qc->state;
5137 if (st >= QUIC_HS_ST_COMPLETE) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005138 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
5139 QUIC_FL_TLS_SECRETS_DCD)) {
5140 /* Discard the Handshake keys. */
5141 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5142 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
5143 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
5144 qc_set_timer(qc);
5145 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5146 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
5147 }
5148
5149 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
5150 /* There may be remaining handshake to build (acks) */
5151 st = QUIC_HS_ST_SERVER_HANDSHAKE;
5152 }
5153 }
5154
5155 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
5156 * be considered.
5157 */
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005158 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005159 goto out;
5160
5161 if (!qc_need_sending(qc, qel) &&
5162 (!next_qel || !qc_need_sending(qc, next_qel))) {
5163 goto skip_send;
5164 }
5165
5166 buf = qc_txb_alloc(qc);
5167 if (!buf)
5168 goto out;
5169
Amaury Denoyelle147862d2023-02-28 15:10:00 +01005170 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
5171 goto skip_send;
5172
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005173 /* Currently buf cannot be non-empty at this stage. Even if a previous
5174 * sendto() has failed it is emptied to simulate packet emission and
5175 * rely on QUIC lost detection to try to emit it.
5176 */
5177 BUG_ON_HOT(b_data(buf));
5178 b_reset(buf);
5179
5180 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
5181 next_tel, &qc->els[next_tel].pktns->tx.frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01005182 if (ret == -1) {
5183 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005184 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005185 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005186
Amaury Denoyelle37333862023-02-28 11:53:48 +01005187 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01005188 if (qc->flags & QUIC_FL_CONN_TO_KILL)
5189 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005190 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005191 }
5192
5193 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005194
5195 skip_send:
5196 /* Check if there is something to do for the next level.
5197 */
5198 if (next_qel && next_qel != qel &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005199 quic_tls_has_rx_sec(next_qel) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005200 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
5201 qel = next_qel;
5202 next_qel = NULL;
5203 goto next_level;
5204 }
5205
5206 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005207 TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
5208 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005209 return t;
5210}
5211
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005212/* Release the memory allocated for <cs> CRYPTO stream */
5213void quic_cstream_free(struct quic_cstream *cs)
5214{
5215 if (!cs) {
5216 /* This is the case for ORTT encryption level */
5217 return;
5218 }
5219
Amaury Denoyellebc174b22022-11-17 10:12:52 +01005220 quic_free_ncbuf(&cs->rx.ncbuf);
5221
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005222 qc_stream_desc_release(cs->desc);
5223 pool_free(pool_head_quic_cstream, cs);
5224}
5225
5226/* Allocate a new QUIC stream for <qc>.
5227 * Return it if succeeded, NULL if not.
5228 */
5229struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
5230{
5231 struct quic_cstream *cs, *ret_cs = NULL;
5232
5233 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5234 cs = pool_alloc(pool_head_quic_cstream);
5235 if (!cs) {
5236 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5237 goto leave;
5238 }
5239
5240 cs->rx.offset = 0;
5241 cs->rx.ncbuf = NCBUF_NULL;
5242 cs->rx.offset = 0;
5243
5244 cs->tx.offset = 0;
5245 cs->tx.sent_offset = 0;
5246 cs->tx.buf = BUF_NULL;
5247 cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
5248 if (!cs->desc) {
5249 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5250 goto err;
5251 }
5252
5253 ret_cs = cs;
5254 leave:
5255 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5256 return ret_cs;
5257
5258 err:
5259 pool_free(pool_head_quic_cstream, cs);
5260 goto leave;
5261}
5262
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005263/* Uninitialize <qel> QUIC encryption level. Never fails. */
5264static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
5265{
5266 int i;
5267
5268 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5269
5270 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
5271 if (qel->tx.crypto.bufs[i]) {
5272 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
5273 qel->tx.crypto.bufs[i] = NULL;
5274 }
5275 }
5276 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005277 quic_cstream_free(qel->cstream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005278
5279 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5280}
5281
5282/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
5283 * connection allocating everything needed.
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005284 *
5285 * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use
5286 * quic_conn_enc_level_uninit() to cleanup partially allocated content.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005287 */
5288static int quic_conn_enc_level_init(struct quic_conn *qc,
5289 enum quic_tls_enc_level level)
5290{
5291 int ret = 0;
5292 struct quic_enc_level *qel;
5293
5294 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5295
5296 qel = &qc->els[level];
5297 qel->level = quic_to_ssl_enc_level(level);
5298 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
5299 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
5300 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
5301 qel->tls_ctx.flags = 0;
5302
5303 qel->rx.pkts = EB_ROOT;
5304 LIST_INIT(&qel->rx.pqpkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005305
5306 /* Allocate only one buffer. */
5307 /* TODO: use a pool */
5308 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
5309 if (!qel->tx.crypto.bufs)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005310 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005311
5312 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
5313 if (!qel->tx.crypto.bufs[0])
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005314 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005315
5316 qel->tx.crypto.bufs[0]->sz = 0;
5317 qel->tx.crypto.nb_buf = 1;
5318
5319 qel->tx.crypto.sz = 0;
5320 qel->tx.crypto.offset = 0;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005321 /* No CRYPTO data for early data TLS encryption level */
5322 if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA)
5323 qel->cstream = NULL;
5324 else {
5325 qel->cstream = quic_cstream_new(qc);
5326 if (!qel->cstream)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005327 goto leave;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005328 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005329
5330 ret = 1;
5331 leave:
5332 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5333 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005334}
5335
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005336/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
5337 * This is not the case if the remote peer address is not validated and if
5338 * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
5339 */
5340static int qc_may_probe_ipktns(struct quic_conn *qc)
5341{
5342 return quic_peer_validated_addr(qc) ||
5343 (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
5344}
5345
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005346/* Callback called upon loss detection and PTO timer expirations. */
5347struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
5348{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005349 struct quic_conn *qc = ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005350 struct quic_pktns *pktns;
5351
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005352 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
5353 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005354 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005355
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005356 task->expire = TICK_ETERNITY;
5357 pktns = quic_loss_pktns(qc);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005358
5359 if (qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL)) {
5360 TRACE_PROTO("cancelled action (draining state)", QUIC_EV_CONN_PTIMER, qc);
5361 task = NULL;
5362 goto out;
5363 }
5364
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005365 if (tick_isset(pktns->tx.loss_time)) {
5366 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
5367
5368 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
5369 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005370 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01005371 if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
5372 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005373 goto out;
5374 }
5375
5376 if (qc->path->in_flight) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +01005377 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
Frédéric Lécaille68737312023-04-07 16:28:46 +02005378 if (!pktns->tx.in_flight) {
5379 TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
5380 goto out;
5381 }
5382
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005383 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
5384 if (qc_may_probe_ipktns(qc)) {
5385 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5386 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5387 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5388 }
5389 else {
5390 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5391 }
5392 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
5393 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5394 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5395 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5396 }
Frédéric Lécaillee25fce02023-03-20 17:23:19 +01005397 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005398 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
5399 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5400 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5401 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5402 if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005403 if (qc_may_probe_ipktns(qc)) {
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005404 qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005405 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5406 }
5407 else {
5408 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5409 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005410 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005411 }
5412 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01005413 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005414 /* Wake up upper layer if waiting to send new data. */
5415 if (!qc_notify_send(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005416 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005417 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5418 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005419 }
5420 }
5421 }
5422 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
5423 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
5424 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
5425
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005426 if (quic_tls_has_tx_sec(hel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005427 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005428 if (quic_tls_has_tx_sec(iel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005429 iel->pktns->tx.pto_probe = 1;
5430 }
5431
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005432 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005433 qc->path->loss.pto_count++;
5434
5435 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005436 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
5437 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005438
5439 return task;
5440}
5441
5442/* Parse the Retry token from buffer <token> with <end> a pointer to
5443 * one byte past the end of this buffer. This will extract the ODCID
5444 * which will be stored into <odcid>
5445 *
5446 * Returns 0 on success else non-zero.
5447 */
5448static int parse_retry_token(struct quic_conn *qc,
5449 const unsigned char *token, const unsigned char *end,
5450 struct quic_cid *odcid)
5451{
5452 int ret = 0;
5453 uint64_t odcid_len;
5454 uint32_t timestamp;
5455
5456 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5457
5458 if (!quic_dec_int(&odcid_len, &token, end)) {
5459 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
5460 goto leave;
5461 }
5462
5463 /* RFC 9000 7.2. Negotiating Connection IDs:
5464 * When an Initial packet is sent by a client that has not previously
5465 * received an Initial or Retry packet from the server, the client
5466 * populates the Destination Connection ID field with an unpredictable
5467 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5468 */
5469 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
5470 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
5471 goto leave;
5472 }
5473
5474 if (end - token < odcid_len + sizeof timestamp) {
5475 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
5476 goto leave;
5477 }
5478
5479 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécailled0742132023-04-19 17:31:28 +02005480 if (tick_is_expired(tick_add(timestamp, MS_TO_TICKS(QUIC_RETRY_DURATION_MS)), now_ms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005481 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
5482 goto leave;
5483 }
5484
5485 ret = 1;
5486 memcpy(odcid->data, token, odcid_len);
5487 odcid->len = odcid_len;
5488 leave:
5489 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5490 return !ret;
5491}
5492
5493/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
5494 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
5495 * for QUIC servers (or haproxy listeners).
5496 * <dcid> is the destination connection ID, <scid> is the source connection ID,
5497 * <token> the token found to be used for this connection with <token_len> as
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005498 * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005499 * Returns the connection if succeeded, NULL if not.
5500 */
5501static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
5502 struct quic_cid *dcid, struct quic_cid *scid,
5503 const struct quic_cid *token_odcid,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005504 struct quic_connection_id *conn_id,
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005505 struct sockaddr_storage *local_addr,
5506 struct sockaddr_storage *peer_addr,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005507 int server, int token, void *owner)
5508{
5509 int i;
5510 struct quic_conn *qc;
5511 /* Initial CID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005512 char *buf_area = NULL;
5513 struct listener *l = NULL;
5514 struct quic_cc_algo *cc_algo = NULL;
5515 struct quic_tls_ctx *ictx;
5516 TRACE_ENTER(QUIC_EV_CONN_INIT);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005517 /* TODO replace pool_zalloc by pool_alloc(). This requires special care
5518 * to properly initialized internal quic_conn members to safely use
5519 * quic_conn_release() on alloc failure.
5520 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005521 qc = pool_zalloc(pool_head_quic_conn);
5522 if (!qc) {
5523 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
5524 goto err;
5525 }
5526
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005527 /* Initialize in priority qc members required for a safe dealloc. */
5528
5529 /* required to use MTLIST_IN_LIST */
5530 MT_LIST_INIT(&qc->accept_list);
5531
5532 LIST_INIT(&qc->rx.pkt_list);
5533
Amaury Denoyelle42448332022-12-12 11:24:05 +01005534 qc_init_fd(qc);
5535
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005536 LIST_INIT(&qc->back_refs);
Amaury Denoyelled537ca72023-04-19 10:45:40 +02005537 LIST_INIT(&qc->el_th_ctx);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005538
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005539 /* Now proceeds to allocation of qc members. */
5540
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005541 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
5542 if (!buf_area) {
5543 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
5544 goto err;
5545 }
5546
5547 qc->cids = EB_ROOT;
5548 /* QUIC Server (or listener). */
5549 if (server) {
5550 struct proxy *prx;
5551
5552 l = owner;
5553 prx = l->bind_conf->frontend;
5554 cc_algo = l->bind_conf->quic_cc_algo;
5555
5556 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
5557 &quic_stats_module);
5558 qc->flags |= QUIC_FL_CONN_LISTENER;
5559 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005560 /* Copy the client original DCID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005561 qc->odcid.len = dcid->len;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005562 memcpy(qc->odcid.data, dcid->data, dcid->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005563
5564 /* copy the packet SCID to reuse it as DCID for sending */
5565 if (scid->len)
5566 memcpy(qc->dcid.data, scid->data, scid->len);
5567 qc->dcid.len = scid->len;
5568 qc->tx.buf = BUF_NULL;
5569 qc->li = l;
5570 }
5571 /* QUIC Client (outgoing connection to servers) */
5572 else {
5573 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
5574 if (dcid->len)
5575 memcpy(qc->dcid.data, dcid->data, dcid->len);
5576 qc->dcid.len = dcid->len;
5577 }
5578 qc->mux_state = QC_MUX_NULL;
5579 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
5580
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005581 conn_id->qc = qc;
5582 eb64_insert(&qc->cids, &conn_id->seq_num);
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01005583 /* Initialize the next CID sequence number to be used for this connection. */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005584 qc->next_cid_seq_num = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005585
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005586 if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
5587 is_addr(local_addr)) {
5588 TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
5589 qc_alloc_fd(qc, local_addr, peer_addr);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005590
5591 /* haproxy soft-stop is supported only for QUIC connections
5592 * with their owned socket.
5593 */
5594 if (qc_test_fd(qc))
5595 _HA_ATOMIC_INC(&jobs);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005596 }
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005597
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005598 /* Select our SCID which is the first CID with 0 as sequence number. */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005599 qc->scid = conn_id->cid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005600
5601 /* Packet number spaces initialization. */
5602 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
5603 quic_pktns_init(&qc->pktns[i]);
5604 /* QUIC encryption level context initialization. */
5605 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5606 if (!quic_conn_enc_level_init(qc, i)) {
5607 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
5608 goto err;
5609 }
5610 /* Initialize the packet number space. */
5611 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
5612 }
5613
5614 qc->original_version = qv;
5615 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
5616 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
5617 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
5618 /* TX part. */
5619 LIST_INIT(&qc->tx.frms_to_send);
5620 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
5621 qc->tx.wbuf = qc->tx.rbuf = 0;
5622 qc->tx.bytes = 0;
5623 qc->tx.buf = BUF_NULL;
5624 /* RX part. */
5625 qc->rx.bytes = 0;
5626 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
5627 for (i = 0; i < QCS_MAX_TYPES; i++)
5628 qc->rx.strms[i].nb_streams = 0;
5629
5630 qc->nb_pkt_for_cc = 1;
5631 qc->nb_pkt_since_cc = 0;
5632
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005633 if (!quic_tls_ku_init(qc)) {
5634 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
5635 goto err;
5636 }
5637
5638 /* XXX TO DO: Only one path at this time. */
5639 qc->path = &qc->paths[0];
5640 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
5641
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005642 qc->streams_by_id = EB_ROOT_UNIQUE;
5643 qc->stream_buf_count = 0;
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005644 memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
5645 memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005646
5647 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005648 conn_id->stateless_reset_token,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005649 dcid->data, dcid->len,
5650 qc->scid.data, qc->scid.len, token_odcid))
5651 goto err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005652
Frédéric Lécailledeb97812023-03-22 11:29:45 +01005653 /* Initialize the idle timeout of the connection at the "max_idle_timeout"
5654 * value from local transport parameters.
5655 */
5656 qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005657 qc->wait_event.tasklet = tasklet_new();
5658 if (!qc->wait_event.tasklet) {
5659 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
5660 goto err;
5661 }
5662 qc->wait_event.tasklet->process = quic_conn_io_cb;
5663 qc->wait_event.tasklet->context = qc;
5664 qc->wait_event.events = 0;
Amaury Denoyellebbb1c682022-09-28 15:15:51 +02005665 qc->subs = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005666
5667 if (qc_conn_alloc_ssl_ctx(qc) ||
5668 !quic_conn_init_timer(qc) ||
5669 !quic_conn_init_idle_timer_task(qc))
5670 goto err;
5671
5672 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
5673 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
5674 goto err;
5675
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005676 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
5677 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
5678
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005679 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
5680
5681 return qc;
5682
5683 err:
5684 pool_free(pool_head_quic_conn_rxbuf, buf_area);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005685 if (qc) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005686 qc->rx.buf.area = NULL;
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005687 quic_conn_release(qc);
5688 }
5689 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005690 return NULL;
5691}
5692
5693/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
5694 * The connection tasklet is killed.
5695 *
5696 * This function must only be called by the thread responsible of the quic_conn
5697 * tasklet.
5698 */
5699void quic_conn_release(struct quic_conn *qc)
5700{
5701 int i;
5702 struct ssl_sock_ctx *conn_ctx;
5703 struct eb64_node *node;
5704 struct quic_tls_ctx *app_tls_ctx;
5705 struct quic_rx_packet *pkt, *pktback;
5706
5707 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5708
5709 /* We must not free the quic-conn if the MUX is still allocated. */
5710 BUG_ON(qc->mux_state == QC_MUX_READY);
5711
Amaury Denoyellefb375572023-02-01 09:28:32 +01005712 if (qc_test_fd(qc))
5713 _HA_ATOMIC_DEC(&jobs);
5714
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005715 /* Close quic-conn socket fd. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01005716 qc_release_fd(qc, 0);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005717
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005718 /* in the unlikely (but possible) case the connection was just added to
5719 * the accept_list we must delete it from there.
5720 */
5721 MT_LIST_DELETE(&qc->accept_list);
5722
5723 /* free remaining stream descriptors */
5724 node = eb64_first(&qc->streams_by_id);
5725 while (node) {
5726 struct qc_stream_desc *stream;
5727
5728 stream = eb64_entry(node, struct qc_stream_desc, by_id);
5729 node = eb64_next(node);
5730
5731 /* all streams attached to the quic-conn are released, so
5732 * qc_stream_desc_free will liberate the stream instance.
5733 */
5734 BUG_ON(!stream->release);
5735 qc_stream_desc_free(stream, 1);
5736 }
5737
5738 /* Purge Rx packet list. */
5739 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
5740 LIST_DELETE(&pkt->qc_rx_pkt_list);
5741 pool_free(pool_head_quic_rx_packet, pkt);
5742 }
5743
5744 if (qc->idle_timer_task) {
5745 task_destroy(qc->idle_timer_task);
5746 qc->idle_timer_task = NULL;
5747 }
5748
5749 if (qc->timer_task) {
5750 task_destroy(qc->timer_task);
5751 qc->timer_task = NULL;
5752 }
5753
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02005754 tasklet_free(qc->wait_event.tasklet);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005755
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005756 /* remove the connection from receiver cids trees */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005757 free_quic_conn_cids(qc);
5758
5759 conn_ctx = qc->xprt_ctx;
5760 if (conn_ctx) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005761 SSL_free(conn_ctx->ssl);
5762 pool_free(pool_head_quic_conn_ctx, conn_ctx);
5763 }
5764
5765 quic_tls_ku_free(qc);
5766 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5767 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
5768 quic_conn_enc_level_uninit(qc, &qc->els[i]);
5769 }
5770 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
5771
5772 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
5773 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
5774 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
5775
5776 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
5777 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
5778 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
5779 }
5780
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01005781 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005782
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005783 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
5784 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +02005785 qc = NULL;
Amaury Denoyellefb375572023-02-01 09:28:32 +01005786
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005787 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
5788
5789 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5790}
5791
5792/* Initialize the timer task of <qc> QUIC connection.
5793 * Returns 1 if succeeded, 0 if not.
5794 */
5795static int quic_conn_init_timer(struct quic_conn *qc)
5796{
5797 int ret = 0;
5798 /* Attach this task to the same thread ID used for the connection */
5799 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5800
Amaury Denoyelle66947282023-04-13 11:48:38 +02005801 qc->timer_task = task_new_here();
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005802 if (!qc->timer_task) {
5803 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5804 goto leave;
5805 }
5806
5807 qc->timer = TICK_ETERNITY;
5808 qc->timer_task->process = qc_process_timer;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005809 qc->timer_task->context = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005810
5811 ret = 1;
5812 leave:
5813 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5814 return ret;
5815}
5816
Frédéric Lécailled7215712023-03-24 18:13:37 +01005817/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
5818 * connection. */
5819static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005820{
5821 unsigned int expire;
5822
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005823 if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005824 TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005825 qc->ack_expire = TICK_ETERNITY;
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005826 task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
5827 }
5828 else {
5829 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005830 qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
5831 if (arm_ack) {
5832 /* Arm the ack timer only if not already armed. */
5833 if (!tick_isset(qc->ack_expire)) {
5834 qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
5835 qc->idle_timer_task->expire = qc->ack_expire;
5836 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005837 TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005838 }
5839 }
5840 else {
5841 qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
5842 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005843 TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005844 }
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005845 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005846}
5847
Frédéric Lécailled7215712023-03-24 18:13:37 +01005848/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
5849 * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
5850 * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
5851 * which must be rearmed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005852 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01005853static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005854{
5855 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5856
5857 if (read) {
5858 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5859 }
5860 else {
5861 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5862 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01005863 qc_idle_timer_do_rearm(qc, arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005864
5865 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5866}
5867
5868/* The task handling the idle timeout */
5869struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
5870{
5871 struct quic_conn *qc = ctx;
5872 struct quic_counters *prx_counters = qc->prx_counters;
5873 unsigned int qc_flags = qc->flags;
5874
5875 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5876
Frédéric Lécaille12eca3a2023-04-04 10:46:54 +02005877 if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
5878 goto requeue;
5879
Frédéric Lécailled7215712023-03-24 18:13:37 +01005880 if (tick_is_expired(qc->ack_expire, now_ms)) {
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005881 TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005882 qc->ack_expire = TICK_ETERNITY;
5883 /* Note that ->idle_expire is always set. */
5884 t->expire = qc->idle_expire;
Frédéric Lécailleb73762a2023-04-24 11:32:22 +02005885 /* Do not wakeup the I/O handler in DRAINING state or if the
5886 * connection must be killed as soon as possible.
5887 */
5888 if (!(qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL))) {
5889 qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
5890 tasklet_wakeup(qc->wait_event.tasklet);
5891 }
5892
Frédéric Lécailled7215712023-03-24 18:13:37 +01005893 goto requeue;
5894 }
5895
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005896 TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005897 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
5898 * might free the quic-conn too early via quic_close().
5899 */
5900 qc_notify_close(qc);
5901
5902 /* If the MUX is still alive, keep the quic-conn. The MUX is
5903 * responsible to call quic_close to release it.
5904 */
5905 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005906 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005907 quic_conn_release(qc);
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005908 qc = NULL;
5909 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005910
5911 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
5912 * least clean some parts of it such as the tasklet.
5913 */
5914
5915 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
5916 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005917 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005918 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
5919 }
5920
Frédéric Lécailled7215712023-03-24 18:13:37 +01005921 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005922 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5923 return NULL;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005924
5925 requeue:
5926 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5927 return t;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005928}
5929
5930/* Initialize the idle timeout task for <qc>.
5931 * Returns 1 if succeeded, 0 if not.
5932 */
5933static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
5934{
5935 int ret = 0;
5936
5937 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5938
5939 qc->idle_timer_task = task_new_here();
5940 if (!qc->idle_timer_task) {
5941 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5942 goto leave;
5943 }
5944
5945 qc->idle_timer_task->process = qc_idle_timer_task;
5946 qc->idle_timer_task->context = qc;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005947 qc->ack_expire = TICK_ETERNITY;
5948 qc_idle_timer_rearm(qc, 1, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005949 task_queue(qc->idle_timer_task);
5950
5951 ret = 1;
5952 leave:
5953 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5954 return ret;
5955}
5956
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02005957/* Parse into <pkt> a long header located at <*pos> position, <end> begin a pointer to the end
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005958 * past one byte of this buffer.
5959 */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02005960static inline int quic_packet_read_long_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005961 struct quic_rx_packet *pkt)
5962{
5963 int ret = 0;
5964 unsigned char dcid_len, scid_len;
5965
5966 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5967
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02005968 if (end == *pos) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005969 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
5970 goto leave;
5971 }
5972
5973 /* Destination Connection ID Length */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02005974 dcid_len = *(*pos)++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005975 /* 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 +02005976 if (dcid_len > QUIC_CID_MAXLEN || end - *pos < dcid_len + 1) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005977 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
5978 goto leave;
5979 }
5980
5981 if (dcid_len) {
5982 /* Check that the length of this received DCID matches the CID lengths
5983 * of our implementation for non Initials packets only.
5984 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02005985 if (pkt->version && pkt->version->num &&
5986 pkt->type != QUIC_PACKET_TYPE_INITIAL &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005987 pkt->type != QUIC_PACKET_TYPE_0RTT &&
5988 dcid_len != QUIC_HAP_CID_LEN) {
5989 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
5990 goto leave;
5991 }
5992
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02005993 memcpy(pkt->dcid.data, *pos, dcid_len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005994 }
5995
5996 pkt->dcid.len = dcid_len;
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02005997 *pos += dcid_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005998
5999 /* Source Connection ID Length */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006000 scid_len = *(*pos)++;
6001 if (scid_len > QUIC_CID_MAXLEN || end - *pos < scid_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006002 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
6003 goto leave;
6004 }
6005
6006 if (scid_len)
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006007 memcpy(pkt->scid.data, *pos, scid_len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006008 pkt->scid.len = scid_len;
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006009 *pos += scid_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006010
6011 ret = 1;
6012 leave:
6013 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6014 return ret;
6015}
6016
6017/* Insert <pkt> RX packet in its <qel> RX packets tree */
6018static void qc_pkt_insert(struct quic_conn *qc,
6019 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
6020{
6021 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
6022
6023 pkt->pn_node.key = pkt->pn;
6024 quic_rx_packet_refinc(pkt);
6025 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
6026
6027 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6028}
6029
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006030/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
6031 * address of the packet first byte, using the keys from encryption level <el>.
6032 *
6033 * If header protection has been successfully removed, packet data are copied
6034 * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
6035 * proceeded, and the packet is inserted into <qc> protected packets tree. In
6036 * both cases, packet can now be considered handled by the <qc> connection.
6037 *
6038 * If header protection cannot be removed due to <el> secrets already
6039 * discarded, no operation is conducted.
6040 *
6041 * Returns 1 on success : packet data is now handled by the connection. On
6042 * error 0 is returned : packet should be dropped by the caller.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006043 */
6044static inline int qc_try_rm_hp(struct quic_conn *qc,
6045 struct quic_rx_packet *pkt,
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006046 unsigned char *beg,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006047 struct quic_enc_level **el)
6048{
6049 int ret = 0;
6050 unsigned char *pn = NULL; /* Packet number field */
6051 enum quic_tls_enc_level tel;
6052 struct quic_enc_level *qel;
6053 /* Only for traces. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006054
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006055 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006056 BUG_ON(!pkt->pn_offset);
6057
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006058 /* The packet number is here. This is also the start minus
6059 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
6060 * protection.
6061 */
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006062 pn = beg + pkt->pn_offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006063
6064 tel = quic_packet_type_enc_level(pkt->type);
6065 qel = &qc->els[tel];
6066
6067 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01006068 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
6069
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006070 /* Note that the following function enables us to unprotect the packet
6071 * number and its length subsequently used to decrypt the entire
6072 * packets.
6073 */
Frédéric Lécaille72027782023-02-22 16:20:09 +01006074 if (!qc_do_rm_hp(qc, pkt, tls_ctx,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006075 qel->pktns->rx.largest_pn, pn, beg)) {
6076 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
6077 goto out;
6078 }
6079
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006080 qc_handle_spin_bit(qc, pkt, qel);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006081 /* The AAD includes the packet number field. */
6082 pkt->aad_len = pkt->pn_offset + pkt->pnl;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006083 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
6084 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
6085 goto out;
6086 }
6087
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006088 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006089 }
6090 else {
6091 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
6092 /* If the packet number space has been discarded, this packet
6093 * will be not parsed.
6094 */
6095 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
6096 goto out;
6097 }
6098
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006099 TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006100 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
6101 quic_rx_packet_refinc(pkt);
6102 }
6103
6104 *el = qel;
6105 /* No reference counter incrementation here!!! */
6106 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
6107 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
6108 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
6109 b_add(&qc->rx.buf, pkt->len);
6110
6111 ret = 1;
6112 out:
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006113 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006114 return ret;
6115}
6116
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006117/* Return the QUIC version (quic_version struct) with <version> as version number
6118 * if supported or NULL if not.
6119 */
6120static inline const struct quic_version *qc_supported_version(uint32_t version)
6121{
6122 int i;
6123
6124 if (unlikely(!version))
6125 return &quic_version_VN_reserved;
6126
6127 for (i = 0; i < quic_versions_nb; i++)
6128 if (quic_versions[i].num == version)
6129 return &quic_versions[i];
6130
6131 return NULL;
6132}
6133
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006134/* Parse a QUIC packet header starting at <pos> postion without exceeding <end>.
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006135 * Version and type are stored in <pkt> packet instance. Type is set to unknown
6136 * on two occasions : for unsupported version, in this case version field is
6137 * set to NULL; for Version Negotiation packet with version number set to 0.
6138 *
6139 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006140 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006141int qc_parse_hd_form(struct quic_rx_packet *pkt,
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006142 unsigned char **pos, const unsigned char *end)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006143{
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006144 uint32_t version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006145 int ret = 0;
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006146 const unsigned char byte0 = **pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006147
6148 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006149 pkt->version = NULL;
6150 pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006151
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006152 (*pos)++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006153 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
6154 unsigned char type =
6155 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
6156
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006157 /* Version */
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006158 if (!quic_read_uint32(&version, (const unsigned char **)pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006159 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
6160 goto out;
6161 }
6162
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006163 pkt->version = qc_supported_version(version);
6164 if (version && pkt->version) {
6165 if (version != QUIC_PROTOCOL_VERSION_2) {
6166 pkt->type = type;
6167 }
6168 else {
6169 switch (type) {
6170 case 0:
6171 pkt->type = QUIC_PACKET_TYPE_RETRY;
6172 break;
6173 case 1:
6174 pkt->type = QUIC_PACKET_TYPE_INITIAL;
6175 break;
6176 case 2:
6177 pkt->type = QUIC_PACKET_TYPE_0RTT;
6178 break;
6179 case 3:
6180 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
6181 break;
6182 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006183 }
6184 }
6185 }
6186 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006187 if (byte0 & QUIC_PACKET_SPIN_BIT)
6188 pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006189 pkt->type = QUIC_PACKET_TYPE_SHORT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006190 }
6191
6192 ret = 1;
6193 out:
6194 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6195 return ret;
6196}
6197
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006198/*
6199 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
6200 * address <addr>.
6201 * Implementation of RFC9000 6. Version Negotiation
6202 *
6203 * TODO implement a rate-limiting sending of Version Negotiation packets
6204 *
6205 * Returns 0 on success else non-zero
6206 */
6207static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
6208 struct quic_rx_packet *pkt)
6209{
6210 char buf[256];
6211 int ret = 0, i = 0, j;
6212 uint32_t version;
6213 const socklen_t addrlen = get_addr_len(addr);
6214
6215 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6216 /*
6217 * header form
6218 * long header, fixed bit to 0 for Version Negotiation
6219 */
6220 /* TODO: RAND_bytes() should be replaced? */
6221 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
6222 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
6223 goto out;
6224 }
6225
6226 buf[i++] |= '\x80';
6227 /* null version for Version Negotiation */
6228 buf[i++] = '\x00';
6229 buf[i++] = '\x00';
6230 buf[i++] = '\x00';
6231 buf[i++] = '\x00';
6232
6233 /* source connection id */
6234 buf[i++] = pkt->scid.len;
6235 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6236 i += pkt->scid.len;
6237
6238 /* destination connection id */
6239 buf[i++] = pkt->dcid.len;
6240 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
6241 i += pkt->dcid.len;
6242
6243 /* supported version */
6244 for (j = 0; j < quic_versions_nb; j++) {
6245 version = htonl(quic_versions[j].num);
6246 memcpy(&buf[i], &version, sizeof(version));
6247 i += sizeof(version);
6248 }
6249
6250 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
6251 goto out;
6252
6253 ret = 1;
6254 out:
6255 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6256 return !ret;
6257}
6258
6259/* Send a stateless reset packet depending on <pkt> RX packet information
6260 * from <fd> UDP socket to <dst>
6261 * Return 1 if succeeded, 0 if not.
6262 */
6263static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
6264 struct quic_rx_packet *rxpkt)
6265{
6266 int ret = 0, pktlen, rndlen;
6267 unsigned char pkt[64];
6268 const socklen_t addrlen = get_addr_len(dstaddr);
6269 struct proxy *prx;
6270 struct quic_counters *prx_counters;
6271
6272 TRACE_ENTER(QUIC_EV_STATELESS_RST);
6273
6274 prx = l->bind_conf->frontend;
6275 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6276 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
6277 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
6278 * Reset is difficult to distinguish from other packets if the recipient requires
6279 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
6280 * that all packets it sends are at least 22 bytes longer than the minimum
6281 * connection ID length that it requests the peer to include in its packets,
6282 * adding PADDING frames as necessary. This ensures that any Stateless Reset
6283 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
6284 * An endpoint that sends a Stateless Reset in response to a packet that is
6285 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
6286 * than the packet it responds to.
6287 */
6288
6289 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
6290 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
6291 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
6292 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
6293
6294 /* Put a header of random bytes */
6295 /* TODO: RAND_bytes() should be replaced */
6296 if (RAND_bytes(pkt, rndlen) != 1) {
6297 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
6298 goto leave;
6299 }
6300
6301 /* Clear the most significant bit, and set the second one */
6302 *pkt = (*pkt & ~0x80) | 0x40;
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02006303 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006304 rxpkt->dcid.data, rxpkt->dcid.len))
6305 goto leave;
6306
6307 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
6308 goto leave;
6309
6310 ret = 1;
6311 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
6312 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
6313 leave:
6314 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
6315 return ret;
6316}
6317
6318/* QUIC server only function.
6319 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
6320 * This is the responsibility of the caller to check <aad> size is big enough
6321 * to contain these data.
6322 * Return the number of bytes copied to <aad>.
6323 */
6324static int quic_generate_retry_token_aad(unsigned char *aad,
6325 uint32_t version,
6326 const struct quic_cid *cid,
6327 const struct sockaddr_storage *addr)
6328{
6329 unsigned char *p;
6330
6331 p = aad;
6332 memcpy(p, &version, sizeof version);
6333 p += sizeof version;
6334 p += quic_saddr_cpy(p, addr);
6335 memcpy(p, cid->data, cid->len);
6336 p += cid->len;
6337
6338 return p - aad;
6339}
6340
6341/* QUIC server only function.
6342 * Generate the token to be used in Retry packets. The token is written to
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006343 * <token> with <len> as length. <odcid> is the original destination connection
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006344 * ID and <dcid> is our side destination connection ID (or client source
6345 * connection ID).
6346 * Returns the length of the encoded token or 0 on error.
6347 */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006348static int quic_generate_retry_token(unsigned char *token, size_t len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006349 const uint32_t version,
6350 const struct quic_cid *odcid,
6351 const struct quic_cid *dcid,
6352 struct sockaddr_storage *addr)
6353{
6354 int ret = 0;
6355 unsigned char *p;
6356 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006357 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006358 size_t aadlen;
6359 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
6360 unsigned char key[QUIC_TLS_KEY_LEN];
6361 unsigned char iv[QUIC_TLS_IV_LEN];
6362 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6363 size_t seclen = strlen(global.cluster_secret);
6364 EVP_CIPHER_CTX *ctx = NULL;
6365 const EVP_CIPHER *aead = EVP_aes_128_gcm();
6366 uint32_t timestamp = now_ms;
6367
6368 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6369
6370 /* We copy the odcid into the token, prefixed by its one byte
6371 * length, the format token byte. It is followed by an AEAD TAG, and finally
6372 * the random bytes used to derive the secret to encrypt the token.
6373 */
6374 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
6375 goto err;
6376
6377 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
6378 /* TODO: RAND_bytes() should be replaced */
6379 if (RAND_bytes(salt, sizeof salt) != 1) {
6380 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
6381 goto err;
6382 }
6383
6384 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6385 salt, sizeof salt, sec, seclen)) {
6386 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
6387 goto err;
6388 }
6389
6390 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
6391 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
6392 goto err;
6393 }
6394
6395 /* Token build */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006396 p = token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006397 *p++ = QUIC_TOKEN_FMT_RETRY,
6398 *p++ = odcid->len;
6399 memcpy(p, odcid->data, odcid->len);
6400 p += odcid->len;
6401 write_u32(p, htonl(timestamp));
6402 p += sizeof timestamp;
6403
6404 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006405 if (!quic_tls_encrypt(token + 1, p - token - 1, aad, aadlen, ctx, aead, key, iv)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006406 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
6407 goto err;
6408 }
6409
6410 p += QUIC_TLS_TAG_LEN;
6411 memcpy(p, salt, sizeof salt);
6412 p += sizeof salt;
6413 EVP_CIPHER_CTX_free(ctx);
6414
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006415 ret = p - token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006416 leave:
6417 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6418 return ret;
6419
6420 err:
6421 if (ctx)
6422 EVP_CIPHER_CTX_free(ctx);
6423 goto leave;
6424}
6425
6426/* QUIC server only function.
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006427 *
6428 * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
6429 * the UDP datagram containing <pkt> and <l> is the listener instance on which
6430 * it was received. If the token is valid, the ODCID of <qc> QUIC connection
6431 * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
6432 * to validate the token but it can be NULL : in this case the version will be
6433 * retrieved from the packet.
6434 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006435 * Return 1 if succeeded, 0 if not.
6436 */
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006437
6438static int quic_retry_token_check(struct quic_rx_packet *pkt,
6439 struct quic_dgram *dgram,
6440 struct listener *l,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006441 struct quic_conn *qc,
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006442 struct quic_cid *odcid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006443{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006444 struct proxy *prx;
6445 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006446 int ret = 0;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006447 unsigned char *token = pkt->token;
6448 const uint64_t tokenlen = pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006449 unsigned char buf[128];
6450 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006451 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006452 size_t aadlen;
6453 const unsigned char *salt;
6454 unsigned char key[QUIC_TLS_KEY_LEN];
6455 unsigned char iv[QUIC_TLS_IV_LEN];
6456 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6457 size_t seclen = strlen(global.cluster_secret);
6458 EVP_CIPHER_CTX *ctx = NULL;
6459 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006460 const struct quic_version *qv = qc ? qc->original_version :
6461 pkt->version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006462
6463 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6464
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006465 /* The caller must ensure this. */
6466 BUG_ON(!global.cluster_secret || !pkt->token_len);
6467
6468 prx = l->bind_conf->frontend;
6469 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6470
6471 if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
6472 /* TODO: New token check */
6473 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6474 goto leave;
6475 }
6476
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006477 if (sizeof buf < tokenlen) {
6478 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
6479 goto err;
6480 }
6481
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006482 aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006483 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
6484 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6485 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
6486 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
6487 goto err;
6488 }
6489
6490 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
6491 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
6492 goto err;
6493 }
6494
6495 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
6496 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
6497 ctx, aead, key, iv)) {
6498 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
6499 goto err;
6500 }
6501
6502 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
6503 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
6504 goto err;
6505 }
6506
6507 EVP_CIPHER_CTX_free(ctx);
6508
6509 ret = 1;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006510 HA_ATOMIC_INC(&prx_counters->retry_validated);
6511
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006512 leave:
6513 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6514 return ret;
6515
6516 err:
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006517 HA_ATOMIC_INC(&prx_counters->retry_error);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006518 if (ctx)
6519 EVP_CIPHER_CTX_free(ctx);
6520 goto leave;
6521}
6522
6523/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
6524 * the Initial <pkt> packet.
6525 *
6526 * Returns 0 on success else non-zero.
6527 */
6528static int send_retry(int fd, struct sockaddr_storage *addr,
6529 struct quic_rx_packet *pkt, const struct quic_version *qv)
6530{
6531 int ret = 0;
6532 unsigned char buf[128];
6533 int i = 0, token_len;
6534 const socklen_t addrlen = get_addr_len(addr);
6535 struct quic_cid scid;
6536
6537 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6538
6539 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
6540 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
6541 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
6542 /* version */
6543 buf[i++] = *((unsigned char *)&qv->num + 3);
6544 buf[i++] = *((unsigned char *)&qv->num + 2);
6545 buf[i++] = *((unsigned char *)&qv->num + 1);
6546 buf[i++] = *(unsigned char *)&qv->num;
6547
6548 /* Use the SCID from <pkt> for Retry DCID. */
6549 buf[i++] = pkt->scid.len;
6550 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6551 i += pkt->scid.len;
6552
6553 /* Generate a new CID to be used as SCID for the Retry packet. */
6554 scid.len = QUIC_HAP_CID_LEN;
6555 /* TODO: RAND_bytes() should be replaced */
6556 if (RAND_bytes(scid.data, scid.len) != 1) {
6557 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
6558 goto out;
6559 }
6560
6561 buf[i++] = scid.len;
6562 memcpy(&buf[i], scid.data, scid.len);
6563 i += scid.len;
6564
6565 /* token */
6566 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
6567 &pkt->dcid, &pkt->scid, addr))) {
6568 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
6569 goto out;
6570 }
6571
6572 i += token_len;
6573
6574 /* token integrity tag */
6575 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
6576 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
6577 pkt->dcid.len, buf, i, qv)) {
6578 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6579 goto out;
6580 }
6581
6582 i += QUIC_TLS_TAG_LEN;
6583
6584 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
6585 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6586 goto out;
6587 }
6588
6589 ret = 1;
6590 out:
6591 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6592 return !ret;
6593}
6594
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006595/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
6596 * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
6597 * is used.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006598 *
6599 * Returns the instance or NULL if not found.
6600 */
6601static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
6602 struct listener *l,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006603 struct sockaddr_storage *saddr,
6604 int *new_tid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006605{
6606 struct quic_conn *qc = NULL;
6607 struct ebmb_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006608 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006609 struct quic_cid_tree *tree;
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006610 uint conn_id_tid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006611
6612 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006613 *new_tid = -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006614
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006615 /* First look into DCID tree. */
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006616 tree = &quic_cid_trees[_quic_cid_tree_idx(pkt->dcid.data)];
6617 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6618 node = ebmb_lookup(&tree->root, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006619
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006620 /* If not found on an Initial/0-RTT packet, it could be because an
6621 * ODCID is reused by the client. Calculate the derived CID value to
6622 * retrieve it from the DCID tree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006623 */
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006624 if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
6625 pkt->type == QUIC_PACKET_TYPE_0RTT)) {
Amaury Denoyellec2a92642023-04-13 15:26:18 +02006626 const struct quic_cid derive_cid = quic_derive_cid(&pkt->dcid, saddr);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006627
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006628 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
6629
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006630 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
6631 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6632 node = ebmb_lookup(&tree->root, derive_cid.data, derive_cid.len);
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006633 }
6634
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006635 if (!node)
6636 goto end;
6637
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006638 conn_id = ebmb_entry(node, struct quic_connection_id, node);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006639 conn_id_tid = HA_ATOMIC_LOAD(&conn_id->tid);
6640 if (conn_id_tid != tid) {
6641 *new_tid = conn_id_tid;
6642 goto end;
6643 }
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006644 qc = conn_id->qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006645
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006646 end:
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006647 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006648 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6649 return qc;
6650}
6651
6652/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
6653 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
6654 * parameters of this session.
6655 * This is the responsibility of the caller to check the validity of all the
6656 * pointers passed as parameter to this function.
6657 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
6658 * CO_ER_SSL_NO_MEM.
6659 */
6660static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
6661 unsigned char *params, size_t params_len)
6662{
6663 int retry, ret = -1;
6664
6665 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6666
6667 retry = 1;
6668 retry:
6669 *ssl = SSL_new(ssl_ctx);
6670 if (!*ssl) {
6671 if (!retry--)
6672 goto err;
6673
6674 pool_gc(NULL);
6675 goto retry;
6676 }
6677
6678 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
6679 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
6680 SSL_free(*ssl);
6681 *ssl = NULL;
6682 if (!retry--)
6683 goto err;
6684
6685 pool_gc(NULL);
6686 goto retry;
6687 }
6688
6689 ret = 0;
6690 leave:
6691 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6692 return ret;
6693
6694 err:
6695 qc->conn->err_code = CO_ER_SSL_NO_MEM;
6696 goto leave;
6697}
6698
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006699/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
6700 * used to process <qc> received packets. The allocated context is stored in
6701 * <qc.xprt_ctx>.
6702 *
6703 * Returns 0 on success else non-zero.
6704 */
6705static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
6706{
6707 int ret = 0;
6708 struct bind_conf *bc = qc->li->bind_conf;
6709 struct ssl_sock_ctx *ctx = NULL;
6710
6711 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6712
6713 ctx = pool_zalloc(pool_head_quic_conn_ctx);
6714 if (!ctx) {
6715 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
6716 goto err;
6717 }
6718
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006719 ctx->subs = NULL;
6720 ctx->xprt_ctx = NULL;
6721 ctx->qc = qc;
6722
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006723 if (qc_is_listener(qc)) {
6724 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
6725 qc->enc_params, qc->enc_params_len) == -1) {
6726 goto err;
6727 }
6728#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6729 /* Enabling 0-RTT */
6730 if (bc->ssl_conf.early_data)
6731 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
6732#endif
6733
6734 SSL_set_accept_state(ctx->ssl);
6735 }
6736
6737 ctx->xprt = xprt_get(XPRT_QUIC);
6738
6739 /* Store the allocated context in <qc>. */
6740 qc->xprt_ctx = ctx;
6741
6742 ret = 1;
6743 leave:
6744 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6745 return !ret;
6746
6747 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006748 pool_free(pool_head_quic_conn_ctx, ctx);
6749 goto leave;
6750}
6751
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006752/* Check that all the bytes between <pos> included and <end> address
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006753 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006754 * check that there is at least one byte between <pos> end <end>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006755 * Return 1 if this all the bytes are null, 0 if not.
6756 */
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006757static inline int quic_padding_check(const unsigned char *pos,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006758 const unsigned char *end)
6759{
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006760 while (pos < end && !*pos)
6761 pos++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006762
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006763 return pos == end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006764}
6765
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006766/* Find the associated connection to the packet <pkt> or create a new one if
6767 * this is an Initial packet. <dgram> is the datagram containing the packet and
6768 * <l> is the listener instance on which it was received.
6769 *
Amaury Denoyelle25174d52023-04-05 17:52:05 +02006770 * By default, <new_tid> is set to -1. However, if thread affinity has been
6771 * chanbed, it will be set to its new thread ID.
6772 *
6773 * Returns the quic-conn instance or NULL if not found or thread affinity
6774 * changed.
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006775 */
6776static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
6777 struct quic_dgram *dgram,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006778 struct listener *l,
6779 int *new_tid)
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006780{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006781 struct quic_cid token_odcid = { .len = 0 };
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006782 struct quic_conn *qc = NULL;
6783 struct proxy *prx;
6784 struct quic_counters *prx_counters;
6785
6786 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6787
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006788 *new_tid = -1;
6789
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006790 prx = l->bind_conf->frontend;
6791 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6792
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006793 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr, new_tid);
6794
Amaury Denoyelle25174d52023-04-05 17:52:05 +02006795 /* If connection already created or rebinded on another thread. */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006796 if (!qc && *new_tid != -1 && tid != *new_tid)
6797 goto out;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006798
6799 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6800 BUG_ON(!pkt->version); /* This must not happen. */
6801
6802 if (global.cluster_secret && pkt->token_len) {
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006803 if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
6804 goto err;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006805 }
6806
6807 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006808 struct quic_cid_tree *tree;
6809 struct ebmb_node *node;
6810 struct quic_connection_id *conn_id;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006811 int ipv4;
6812
6813 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
6814 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
6815 TRACE_PROTO("Initial without token, sending retry",
6816 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6817 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
6818 TRACE_ERROR("Error during Retry generation",
6819 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6820 goto out;
6821 }
6822
6823 HA_ATOMIC_INC(&prx_counters->retry_sent);
6824 goto out;
6825 }
6826
6827 /* RFC 9000 7.2. Negotiating Connection IDs:
6828 * When an Initial packet is sent by a client that has not previously
6829 * received an Initial or Retry packet from the server, the client
6830 * populates the Destination Connection ID field with an unpredictable
6831 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6832 */
6833 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
6834 TRACE_PROTO("dropped packet",
6835 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6836 goto err;
6837 }
6838
6839 pkt->saddr = dgram->saddr;
6840 ipv4 = dgram->saddr.ss_family == AF_INET;
6841
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006842 /* Generate the first connection CID. This is derived from the client
6843 * ODCID and address. This allows to retrieve the connection from the
6844 * ODCID without storing it in the CID tree. This is an interesting
6845 * optimization as the client is expected to stop using its ODCID in
6846 * favor of our generated value.
6847 */
6848 conn_id = new_quic_cid(NULL, NULL, &pkt->dcid, &pkt->saddr);
6849 if (!conn_id)
6850 goto err;
6851
6852 tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)];
6853 HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock);
6854 node = ebmb_insert(&tree->root, &conn_id->node, conn_id->cid.len);
6855 if (node != &conn_id->node) {
6856 pool_free(pool_head_quic_connection_id, conn_id);
6857
6858 conn_id = ebmb_entry(node, struct quic_connection_id, node);
6859 *new_tid = HA_ATOMIC_LOAD(&conn_id->tid);
6860 }
6861 HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock);
6862
6863 if (*new_tid != -1)
6864 goto out;
6865
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006866 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006867 conn_id, &dgram->daddr, &pkt->saddr, 1,
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006868 !!pkt->token_len, l);
6869 if (qc == NULL)
6870 goto err;
6871
6872 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006873 }
6874 }
6875 else if (!qc) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006876 TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006877 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
6878 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
6879 goto err;
6880 }
6881
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006882 out:
6883 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6884 return qc;
6885
6886 err:
6887 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
6888 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
6889 return NULL;
6890}
6891
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006892/* Parse a QUIC packet starting at <pos>. Data won't be read after <end> even
Amaury Denoyelle98289692022-10-19 15:37:44 +02006893 * if the packet is incomplete. This function will populate fields of <pkt>
6894 * instance, most notably its length. <dgram> is the UDP datagram which
6895 * contains the parsed packet. <l> is the listener instance on which it was
6896 * received.
6897 *
6898 * 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 +02006899 * the real packet value or to cover all data between <pos> and <end> : this is
Amaury Denoyelle98289692022-10-19 15:37:44 +02006900 * useful to reject a whole datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006901 */
Amaury Denoyelle98289692022-10-19 15:37:44 +02006902static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006903 unsigned char *pos, const unsigned char *end,
Amaury Denoyelle98289692022-10-19 15:37:44 +02006904 struct quic_dgram *dgram, struct listener *l)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006905{
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006906 const unsigned char *beg = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006907 struct proxy *prx;
6908 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006909
6910 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6911
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006912 prx = l->bind_conf->frontend;
6913 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6914 /* This ist only to please to traces and distinguish the
6915 * packet with parsed packet number from others.
6916 */
6917 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006918 if (end <= pos) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006919 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6920 goto drop;
6921 }
6922
6923 /* Fixed bit */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006924 if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006925 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006926 quic_padding_check(pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006927 /* Some browsers may pad the remaining datagram space with null bytes.
6928 * That is what we called add padding out of QUIC packets. Such
6929 * datagrams must be considered as valid. But we can only consume
6930 * the remaining space.
6931 */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006932 pkt->len = end - pos;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006933 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006934 }
6935
6936 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6937 goto drop;
6938 }
6939
6940 /* Header form */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006941 if (!qc_parse_hd_form(pkt, &pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006942 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6943 goto drop;
6944 }
6945
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006946 if (pkt->type != QUIC_PACKET_TYPE_SHORT) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006947 uint64_t len;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006948 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006949
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006950 if (!quic_packet_read_long_header(&pos, end, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006951 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6952 goto drop;
6953 }
6954
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006955 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6956 * they must have the same DCID.
6957 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006958 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006959 (pkt->dcid.len != dgram->dcid_len ||
6960 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006961 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006962 goto drop;
6963 }
6964
6965 /* Retry of Version Negotiation packets are only sent by servers */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006966 if (pkt->type == QUIC_PACKET_TYPE_RETRY ||
6967 (pkt->version && !pkt->version->num)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006968 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6969 goto drop;
6970 }
6971
6972 /* RFC9000 6. Version Negotiation */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006973 if (!pkt->version) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006974 /* unsupported version, send Negotiation packet */
6975 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
6976 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006977 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006978 }
6979
6980 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006981 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006982 }
6983
6984 /* For Initial packets, and for servers (QUIC clients connections),
6985 * there is no Initial connection IDs storage.
6986 */
6987 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6988 uint64_t token_len;
6989
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006990 if (!quic_dec_int(&token_len, (const unsigned char **)&pos, end) ||
6991 end - pos < token_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006992 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02006993 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006994 goto drop;
6995 }
6996
6997 /* TODO Retry should be automatically activated if
6998 * suspect network usage is detected.
6999 */
7000 if (global.cluster_secret && !token_len) {
7001 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
7002 TRACE_PROTO("Initial without token, sending retry",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007003 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7004 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007005 TRACE_PROTO("Error during Retry generation",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007006 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007007 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007008 }
7009
7010 HA_ATOMIC_INC(&prx_counters->retry_sent);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007011 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007012 }
7013 }
7014 else if (!global.cluster_secret && token_len) {
7015 /* Impossible case: a token was received without configured
7016 * cluster secret.
7017 */
7018 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007019 NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007020 goto drop;
7021 }
7022
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007023 pkt->token = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007024 pkt->token_len = token_len;
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007025 pos += pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007026 }
7027 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
7028 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
7029 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007030 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007031 goto drop;
7032 }
7033 }
7034
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007035 if (!quic_dec_int(&len, (const unsigned char **)&pos, end) ||
7036 end - pos < len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007037 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007038 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007039 goto drop;
7040 }
7041
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007042 /* Packet Number is stored here. Packet Length totalizes the
7043 * rest of the content.
7044 */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007045 pkt->pn_offset = pos - beg;
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007046 pkt->len = pkt->pn_offset + len;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007047
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007048 /* RFC 9000. Initial Datagram Size
7049 *
7050 * A server MUST discard an Initial packet that is carried in a UDP datagram
7051 * with a payload that is smaller than the smallest allowed maximum datagram
7052 * size of 1200 bytes.
7053 */
7054 if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
7055 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007056 TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007057 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
7058 goto drop;
7059 }
7060
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007061 /* Interrupt parsing after packet length retrieval : this
7062 * ensures that only the packet is dropped but not the whole
7063 * datagram.
7064 */
7065 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007066 TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007067 goto drop;
7068 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007069 }
7070 else {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007071 TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007072 if (end - pos < QUIC_HAP_CID_LEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007073 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007074 goto drop;
7075 }
7076
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007077 memcpy(pkt->dcid.data, pos, QUIC_HAP_CID_LEN);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007078 pkt->dcid.len = QUIC_HAP_CID_LEN;
7079
7080 /* When multiple QUIC packets are coalesced on the same UDP datagram,
7081 * they must have the same DCID.
7082 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007083 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007084 (pkt->dcid.len != dgram->dcid_len ||
7085 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007086 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007087 goto drop;
7088 }
7089
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007090 pos += QUIC_HAP_CID_LEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007091
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007092 pkt->pn_offset = pos - beg;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007093 /* A short packet is the last one of a UDP datagram. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007094 pkt->len = end - beg;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007095 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007096
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007097 TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007098 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007099 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007100
Amaury Denoyelle98289692022-10-19 15:37:44 +02007101 drop:
7102 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007103 drop_silent:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007104 if (!pkt->len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007105 pkt->len = end - beg;
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007106 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 +01007107 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007108 return -1;
7109}
7110
7111/* Check if received packet <pkt> should be drop due to <qc> already in closing
7112 * state. This can be true if a CONNECTION_CLOSE has already been emitted for
7113 * this connection.
7114 *
7115 * Returns false if connection is not in closing state else true. The caller
7116 * should drop the whole datagram in the last case to not mess up <qc>
7117 * CONNECTION_CLOSE rate limit counter.
7118 */
7119static int qc_rx_check_closing(struct quic_conn *qc,
7120 struct quic_rx_packet *pkt)
7121{
7122 if (!(qc->flags & QUIC_FL_CONN_CLOSING))
7123 return 0;
7124
7125 TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
7126
7127 /* Check if CONNECTION_CLOSE rate reemission is reached. */
7128 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
7129 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
7130 qc->nb_pkt_for_cc++;
7131 qc->nb_pkt_since_cc = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007132 }
7133
Amaury Denoyelle98289692022-10-19 15:37:44 +02007134 return 1;
7135}
7136
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007137/* React to a connection migration initiated on <qc> by a client with the new
7138 * path addresses <peer_addr>/<local_addr>.
7139 *
7140 * Returns 0 on success else non-zero.
7141 */
7142static int qc_handle_conn_migration(struct quic_conn *qc,
7143 const struct sockaddr_storage *peer_addr,
7144 const struct sockaddr_storage *local_addr)
7145{
7146 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7147
Frédéric Lécaille6fc86972023-01-12 08:29:23 +01007148 /* RFC 9000. Connection Migration
7149 *
7150 * If the peer sent the disable_active_migration transport parameter,
7151 * an endpoint also MUST NOT send packets (including probing packets;
7152 * see Section 9.1) from a different local address to the address the peer
7153 * used during the handshake, unless the endpoint has acted on a
7154 * preferred_address transport parameter from the peer.
7155 */
7156 if (qc->li->bind_conf->quic_params.disable_active_migration) {
7157 TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
7158 goto err;
7159 }
7160
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007161 /* RFC 9000 9. Connection Migration
7162 *
Amaury Denoyelleeb6be982022-11-21 11:14:45 +01007163 * The design of QUIC relies on endpoints retaining a stable address for
7164 * the duration of the handshake. An endpoint MUST NOT initiate
7165 * connection migration before the handshake is confirmed, as defined in
7166 * Section 4.1.2 of [QUIC-TLS].
7167 */
7168 if (qc->state < QUIC_HS_ST_COMPLETE) {
7169 TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
7170 goto err;
7171 }
7172
7173 /* RFC 9000 9. Connection Migration
7174 *
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007175 * TODO
7176 * An endpoint MUST
7177 * perform path validation (Section 8.2) if it detects any change to a
7178 * peer's address, unless it has previously validated that address.
7179 */
7180
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007181 /* Update quic-conn owned socket if in used.
7182 * TODO try to reuse it instead of closing and opening a new one.
7183 */
7184 if (qc_test_fd(qc)) {
7185 /* TODO try to reuse socket instead of closing it and opening a new one. */
7186 TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
7187 qc_release_fd(qc, 1);
Amaury Denoyellefb375572023-02-01 09:28:32 +01007188 /* TODO need to adjust <jobs> on socket allocation failure. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007189 qc_alloc_fd(qc, local_addr, peer_addr);
7190 }
7191
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007192 qc->local_addr = *local_addr;
7193 qc->peer_addr = *peer_addr;
7194 HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done);
7195
7196 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7197 return 0;
7198
7199 err:
7200 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7201 return 1;
7202}
7203
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007204/* Release the memory for the RX packets which are no more referenced
7205 * and consume their payloads which have been copied to the RX buffer
7206 * for the connection.
7207 * Always succeeds.
7208 */
7209static inline void quic_rx_pkts_del(struct quic_conn *qc)
7210{
7211 struct quic_rx_packet *pkt, *pktback;
7212
7213 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
7214 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007215 "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007216 (long long)pkt->pn_node.key,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007217 pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007218 (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
7219 if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
7220 size_t cdata;
7221
7222 cdata = b_contig_data(&qc->rx.buf, 0);
7223 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007224 "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007225 if (cdata && !*b_head(&qc->rx.buf)) {
7226 /* Consume the remaining data */
7227 b_del(&qc->rx.buf, cdata);
7228 }
7229 break;
7230 }
7231
7232 if (pkt->refcnt)
7233 break;
7234
7235 b_del(&qc->rx.buf, pkt->raw_len);
7236 LIST_DELETE(&pkt->qc_rx_pkt_list);
7237 pool_free(pool_head_quic_rx_packet, pkt);
7238 }
7239
7240 /* In frequent cases the buffer will be emptied at this stage. */
7241 b_realign_if_empty(&qc->rx.buf);
7242}
7243
Amaury Denoyelle98289692022-10-19 15:37:44 +02007244/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
7245 * into <qc> receive buffer after header protection removal procedure.
7246 *
7247 * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
7248 * must point to packet buffer first byte.
7249 *
7250 * <tasklist_head> may be non-NULL when the caller treat several datagrams for
7251 * different quic-conn. In this case, each quic-conn tasklet will be appended
7252 * to it in order to be woken up after the current task.
7253 *
7254 * The caller can safely removed the packet data. If packet refcount was not
7255 * incremented by this function, it means that the connection did not handled
7256 * it and it should be freed by the caller.
7257 */
7258static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
7259 struct quic_dgram *dgram, unsigned char *beg,
7260 struct list **tasklist_head)
7261{
7262 const struct quic_version *qv = pkt->version;
7263 struct quic_enc_level *qel = NULL;
7264 size_t b_cspace;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007265
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007266 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7267 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Amaury Denoyelle3f474e62022-11-24 17:15:08 +01007268
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007269 if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007270 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
7271 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
7272 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007273 TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
7274 QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007275 /* Reset the anti-amplification bit. It will be set again
7276 * when sending the next packet if reached again.
7277 */
7278 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007279 qc_set_timer(qc);
7280 if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
7281 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007282 }
7283
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007284 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
7285 TRACE_PROTO("Connection error",
7286 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7287 goto out;
7288 }
7289
7290 pkt->raw_len = pkt->len;
7291 quic_rx_pkts_del(qc);
7292 b_cspace = b_contig_space(&qc->rx.buf);
7293 if (b_cspace < pkt->len) {
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007294 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007295 "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007296 /* Do not consume buf if space not at the end. */
7297 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
7298 TRACE_PROTO("Packet dropped",
7299 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007300 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007301 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007302 }
7303
7304 /* Let us consume the remaining contiguous space. */
7305 if (b_cspace) {
7306 b_putchr(&qc->rx.buf, 0x00);
7307 b_cspace--;
7308 }
7309 b_add(&qc->rx.buf, b_cspace);
7310 if (b_contig_space(&qc->rx.buf) < pkt->len) {
7311 TRACE_PROTO("Too big packet",
7312 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007313 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007314 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007315 }
7316 }
7317
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007318 if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007319 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7320 goto drop;
7321 }
7322
7323 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7324 if (pkt->aad_len)
7325 qc_pkt_insert(qc, pkt, qel);
7326 out:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02007327 *tasklist_head = tasklet_wakeup_after(*tasklist_head,
7328 qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007329
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007330 drop_silent:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007331 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7332 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007333 return;
7334
7335 drop:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007336 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007337 TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7338 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007339}
7340
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007341/* This function builds into a buffer at <pos> position a QUIC long packet header,
7342 * <end> being one byte past the end of this buffer.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007343 * Return 1 if enough room to build this header, 0 if not.
7344 */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007345static int quic_build_packet_long_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007346 int type, size_t pn_len,
7347 struct quic_conn *qc, const struct quic_version *ver)
7348{
7349 int ret = 0;
7350
7351 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7352
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007353 if (end - *pos < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007354 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7355 goto leave;
7356 }
7357
7358 type = quic_pkt_type(type, ver->num);
7359 /* #0 byte flags */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007360 *(*pos)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007361 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
7362 /* Version */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007363 quic_write_uint32(pos, end, ver->num);
7364 *(*pos)++ = qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007365 /* Destination connection ID */
7366 if (qc->dcid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007367 memcpy(*pos, qc->dcid.data, qc->dcid.len);
7368 *pos += qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007369 }
7370 /* Source connection ID */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007371 *(*pos)++ = qc->scid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007372 if (qc->scid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007373 memcpy(*pos, qc->scid.data, qc->scid.len);
7374 *pos += qc->scid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007375 }
7376
7377 ret = 1;
7378 leave:
7379 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7380 return ret;
7381}
7382
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007383/* This function builds into a buffer at <pos> position a QUIC short packet header,
7384 * <end> being one byte past the end of this buffer.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007385 * Return 1 if enough room to build this header, 0 if not.
7386 */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007387static int quic_build_packet_short_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007388 size_t pn_len, struct quic_conn *qc,
7389 unsigned char tls_flags)
7390{
7391 int ret = 0;
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007392 unsigned char spin_bit =
7393 (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007394
7395 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7396
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007397 if (end - *pos < 1 + qc->dcid.len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007398 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7399 goto leave;
7400 }
7401
7402 /* #0 byte flags */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007403 *(*pos)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007404 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
7405 /* Destination connection ID */
7406 if (qc->dcid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007407 memcpy(*pos, qc->dcid.data, qc->dcid.len);
7408 *pos += qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007409 }
7410
7411 ret = 1;
7412 leave:
7413 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7414 return ret;
7415}
7416
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007417/* Apply QUIC header protection to the packet with <pos> as first byte address,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007418 * <pn> as address of the Packet number field, <pnlen> being this field length
7419 * with <aead> as AEAD cipher and <key> as secret key.
7420 * Returns 1 if succeeded or 0 if failed.
7421 */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007422static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *pos,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007423 unsigned char *pn, size_t pnlen,
7424 struct quic_tls_ctx *tls_ctx)
7425
7426{
7427 int i, ret = 0;
7428 /* We need an IV of at least 5 bytes: one byte for bytes #0
7429 * and at most 4 bytes for the packet number
7430 */
7431 unsigned char mask[5] = {0};
7432 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
7433
7434 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7435
7436 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
7437 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
7438 goto out;
7439 }
7440
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007441 *pos ^= mask[0] & (*pos & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007442 for (i = 0; i < pnlen; i++)
7443 pn[i] ^= mask[i + 1];
7444
7445 ret = 1;
7446 out:
7447 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7448 return ret;
7449}
7450
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007451/* Prepare into <outlist> as most as possible ack-eliciting frame from their
7452 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
7453 * with <room> as available room, and <*len> the packet Length field initialized
7454 * with the number of bytes already present in this buffer which must be taken
7455 * into an account for the Length packet field value. <headlen> is the number of
7456 * bytes already present in this packet before building frames.
7457 *
7458 * Update consequently <*len> to reflect the size of these frames built
7459 * by this function. Also attach these frames to <l> frame list.
7460 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
7461 */
7462static inline int qc_build_frms(struct list *outlist, struct list *inlist,
7463 size_t room, size_t *len, size_t headlen,
7464 struct quic_enc_level *qel,
7465 struct quic_conn *qc)
7466{
7467 int ret;
7468 struct quic_frame *cf, *cfbak;
7469
7470 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
7471
7472 ret = 0;
7473 if (*len > room)
7474 goto leave;
7475
7476 /* If we are not probing we must take into an account the congestion
7477 * control window.
7478 */
7479 if (!qel->pktns->tx.pto_probe) {
7480 size_t remain = quic_path_prep_data(qc->path);
7481
7482 if (headlen > remain)
7483 goto leave;
7484
7485 room = QUIC_MIN(room, remain - headlen);
7486 }
7487
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007488 TRACE_PROTO("TX frms build (headlen)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007489 QUIC_EV_CONN_BCFRMS, qc, &headlen);
7490
7491 /* NOTE: switch/case block inside a loop, a successful status must be
7492 * returned by this function only if at least one frame could be built
7493 * in the switch/case block.
7494 */
7495 list_for_each_entry_safe(cf, cfbak, inlist, list) {
7496 /* header length, data length, frame length. */
7497 size_t hlen, dlen, dlen_sz, avail_room, flen;
7498
7499 if (!room)
7500 break;
7501
7502 switch (cf->type) {
7503 case QUIC_FT_CRYPTO:
7504 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
7505 QUIC_EV_CONN_BCFRMS, qc, &room, len);
7506 /* Compute the length of this CRYPTO frame header */
7507 hlen = 1 + quic_int_getsize(cf->crypto.offset);
7508 /* Compute the data length of this CRyPTO frame. */
7509 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
7510 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
7511 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
7512 if (!dlen)
7513 continue;
7514
7515 /* CRYPTO frame length. */
7516 flen = hlen + quic_int_getsize(dlen) + dlen;
7517 TRACE_DEVEL(" CRYPTO frame length (flen)",
7518 QUIC_EV_CONN_BCFRMS, qc, &flen);
7519 /* Add the CRYPTO data length and its encoded length to the packet
7520 * length and the length of this length.
7521 */
7522 *len += flen;
7523 room -= flen;
7524 if (dlen == cf->crypto.len) {
7525 /* <cf> CRYPTO data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007526 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007527 LIST_APPEND(outlist, &cf->list);
7528 }
7529 else {
7530 struct quic_frame *new_cf;
7531
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007532 new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007533 if (!new_cf) {
7534 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
7535 continue;
7536 }
7537
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007538 new_cf->crypto.len = dlen;
7539 new_cf->crypto.offset = cf->crypto.offset;
7540 new_cf->crypto.qel = qel;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007541 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007542 if (cf->origin) {
7543 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7544 /* This <cf> frame was duplicated */
7545 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7546 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007547 /* Detach the remaining CRYPTO frame from its original frame */
7548 LIST_DEL_INIT(&cf->ref);
7549 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007550 }
7551 LIST_APPEND(outlist, &new_cf->list);
7552 /* Consume <dlen> bytes of the current frame. */
7553 cf->crypto.len -= dlen;
7554 cf->crypto.offset += dlen;
7555 }
7556 break;
7557
7558 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007559 if (cf->stream.dup) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007560 struct eb64_node *node = NULL;
7561 struct qc_stream_desc *stream_desc = NULL;
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007562 struct qf_stream *strm_frm = &cf->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007563
7564 /* As this frame has been already lost, ensure the stream is always
7565 * available or the range of this frame is not consumed before
7566 * resending it.
7567 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007568 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007569 if (!node) {
7570 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007571 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007572 continue;
7573 }
7574
7575 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007576 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007577 TRACE_DEVEL("ignored frame frame in already acked range",
7578 QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007579 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007580 continue;
7581 }
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007582 else if (strm_frm->offset.key < stream_desc->ack_offset) {
7583 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
Frédéric Lécailleca079792023-03-17 08:56:50 +01007584
Frédéric Lécaillec425e032023-03-20 14:32:59 +01007585 qc_stream_frm_mv_fwd(cf, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007586 TRACE_DEVEL("updated partially acked frame",
7587 QUIC_EV_CONN_PRSAFRM, qc, cf);
7588 }
7589 }
7590 /* Note that these frames are accepted in short packets only without
7591 * "Length" packet field. Here, <*len> is used only to compute the
7592 * sum of the lengths of the already built frames for this packet.
7593 *
7594 * Compute the length of this STREAM frame "header" made a all the field
7595 * excepting the variable ones. Note that +1 is for the type of this frame.
7596 */
7597 hlen = 1 + quic_int_getsize(cf->stream.id) +
7598 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
7599 /* Compute the data length of this STREAM frame. */
7600 avail_room = room - hlen - *len;
7601 if ((ssize_t)avail_room <= 0)
7602 continue;
7603
7604 TRACE_DEVEL(" New STREAM frame build (room, len)",
7605 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007606
7607 /* hlen contains STREAM id and offset. Ensure there is
7608 * enough room for length field.
7609 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007610 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007611 dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
7612 cf->stream.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007613 dlen_sz = quic_int_getsize(dlen);
7614 flen = hlen + dlen_sz + dlen;
7615 }
7616 else {
7617 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
7618 flen = hlen + dlen;
7619 }
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007620
7621 if (cf->stream.len && !dlen) {
7622 /* Only a small gap is left on buffer, not
7623 * enough to encode the STREAM data length.
7624 */
7625 continue;
7626 }
7627
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007628 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
7629 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
7630 TRACE_DEVEL(" STREAM frame length (flen)",
7631 QUIC_EV_CONN_BCFRMS, qc, &flen);
7632 /* Add the STREAM data length and its encoded length to the packet
7633 * length and the length of this length.
7634 */
7635 *len += flen;
7636 room -= flen;
7637 if (dlen == cf->stream.len) {
7638 /* <cf> STREAM data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007639 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007640 LIST_APPEND(outlist, &cf->list);
7641
7642 /* Do not notify MUX on retransmission. */
7643 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7644 qcc_streams_sent_done(cf->stream.stream->ctx,
7645 cf->stream.len,
7646 cf->stream.offset.key);
7647 }
7648 }
7649 else {
7650 struct quic_frame *new_cf;
7651 struct buffer cf_buf;
7652
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007653 new_cf = qc_frm_alloc(cf->type);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007654 if (!new_cf) {
7655 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
7656 continue;
7657 }
7658
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007659 new_cf->stream.stream = cf->stream.stream;
7660 new_cf->stream.buf = cf->stream.buf;
7661 new_cf->stream.id = cf->stream.id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01007662 new_cf->stream.offset = cf->stream.offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007663 new_cf->stream.len = dlen;
7664 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
7665 /* FIN bit reset */
7666 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
7667 new_cf->stream.data = cf->stream.data;
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007668 new_cf->stream.dup = cf->stream.dup;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007669 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007670 if (cf->origin) {
7671 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7672 /* This <cf> frame was duplicated */
7673 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7674 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007675 /* Detach this STREAM frame from its origin */
7676 LIST_DEL_INIT(&cf->ref);
7677 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007678 }
7679 LIST_APPEND(outlist, &new_cf->list);
7680 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
7681 /* Consume <dlen> bytes of the current frame. */
7682 cf_buf = b_make(b_orig(cf->stream.buf),
7683 b_size(cf->stream.buf),
7684 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
7685 cf->stream.len -= dlen;
7686 cf->stream.offset.key += dlen;
7687 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
7688
7689 /* Do not notify MUX on retransmission. */
7690 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7691 qcc_streams_sent_done(new_cf->stream.stream->ctx,
7692 new_cf->stream.len,
7693 new_cf->stream.offset.key);
7694 }
7695 }
7696
7697 /* TODO the MUX is notified about the frame sending via
7698 * previous qcc_streams_sent_done call. However, the
7699 * sending can fail later, for example if the sendto
7700 * system call returns an error. As the MUX has been
7701 * notified, the transport layer is responsible to
7702 * bufferize and resent the announced data later.
7703 */
7704
7705 break;
7706
7707 default:
7708 flen = qc_frm_len(cf);
7709 BUG_ON(!flen);
7710 if (flen > room)
7711 continue;
7712
7713 *len += flen;
7714 room -= flen;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007715 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007716 LIST_APPEND(outlist, &cf->list);
7717 break;
7718 }
7719
7720 /* Successful status as soon as a frame could be built */
7721 ret = 1;
7722 }
7723
7724 leave:
7725 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
7726 return ret;
7727}
7728
7729/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
7730 * is used as return parameter and should be zero'ed by the caller.
7731 */
7732static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
7733 struct quic_frame *out)
7734{
7735 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
7736 *
7737 * A CONNECTION_CLOSE frame should be sent in several packets with
7738 * different encryption levels depending on the client context. This is
7739 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
7740 * more details on how to implement it.
7741 */
7742 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
7743
7744
7745 if (qc->err.app) {
7746 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
7747 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
7748 /* RFC 9000 10.2.3. Immediate Close during the Handshake
7749 *
7750 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
7751 * packet could expose application state or be used to alter application
7752 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
7753 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
7754 * Handshake packets. Otherwise, information about the application
7755 * state might be revealed. Endpoints MUST clear the value of the
7756 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
7757 * converting to a CONNECTION_CLOSE of type 0x1c.
7758 */
7759 out->type = QUIC_FT_CONNECTION_CLOSE;
7760 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
7761 out->connection_close.reason_phrase_len = 0;
7762 }
7763 else {
7764 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
7765 out->connection_close.error_code = qc->err.code;
7766 }
7767 }
7768 else {
7769 out->type = QUIC_FT_CONNECTION_CLOSE;
7770 out->connection_close.error_code = qc->err.code;
7771 }
7772 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
7773
7774}
7775
7776/* This function builds a clear packet from <pkt> information (its type)
7777 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
7778 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
7779 * filling the buffer with as much frames as possible from <frms> list of
7780 * prebuilt frames.
7781 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
7782 * reserved so that to ensure there is enough room to build this AEAD TAG after
7783 * having returned from this function.
7784 * This function also updates the value of <buf_pn> pointer to point to the packet
7785 * number field in this packet. <pn_len> will also have the packet number
7786 * length as value.
7787 *
7788 * Return 1 if succeeded (enough room to buile this packet), O if not.
7789 */
7790static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
7791 size_t dglen, struct quic_tx_packet *pkt,
7792 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007793 int must_ack, int padding, int cc, int probe,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007794 struct quic_enc_level *qel, struct quic_conn *qc,
7795 const struct quic_version *ver, struct list *frms)
7796{
7797 unsigned char *beg, *payload;
7798 size_t len, len_sz, len_frms, padding_len;
7799 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
7800 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
7801 struct quic_frame cc_frm = { };
7802 size_t ack_frm_len, head_len;
7803 int64_t rx_largest_acked_pn;
7804 int add_ping_frm;
7805 struct list frm_list = LIST_HEAD_INIT(frm_list);
7806 struct quic_frame *cf;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007807 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007808
7809 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7810
7811 /* Length field value with CRYPTO frames if present. */
7812 len_frms = 0;
7813 beg = pos;
7814 /* When not probing, and no immediate close is required, reduce the size of this
7815 * buffer to respect the congestion controller window.
7816 * This size will be limited if we have ack-eliciting frames to send from <frms>.
7817 */
7818 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
7819 size_t path_room;
7820
7821 path_room = quic_path_prep_data(qc->path);
7822 if (end - beg > path_room)
7823 end = beg + path_room;
7824 }
7825
7826 /* Ensure there is enough room for the TLS encryption tag and a zero token
7827 * length field if any.
7828 */
7829 if (end - pos < QUIC_TLS_TAG_LEN +
7830 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
7831 goto no_room;
7832
7833 end -= QUIC_TLS_TAG_LEN;
7834 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
7835 /* packet number length */
7836 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
7837 /* Build the header */
7838 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
7839 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
7840 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
7841 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
7842 goto no_room;
7843
7844 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02007845 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7846 if (end <= pos)
7847 goto no_room;
7848
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007849 *pos++ = 0;
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02007850 }
7851
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007852 head_len = pos - beg;
7853 /* Build an ACK frame if required. */
7854 ack_frm_len = 0;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007855 /* Do not ack and probe at the same time. */
7856 if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007857 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
7858 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
7859 ack_frm.tx_ack.arngs = arngs;
7860 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
7861 qel->pktns->tx.ack_delay =
7862 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
7863 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
7864 }
7865 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
7866 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
7867 * smallest frame (PING) and <*pn_len> more for the packet number. Note
7868 * that from here, we do not know if we will have to send a PING frame.
7869 * This will be decided after having computed the ack-eliciting frames
7870 * to be added to this packet.
7871 */
Frédéric Lécaille9d68c6a2023-04-12 20:49:29 +02007872 if (end - pos <= 1 + *pn_len)
7873 goto no_room;
7874
Frédéric Lécaille4b2627b2023-04-17 13:42:42 +02007875 ack_frm_len = qc_frm_len(&ack_frm);
7876 if (ack_frm_len > end - 1 - *pn_len - pos)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007877 goto no_room;
7878 }
7879
7880 /* Length field value without the ack-eliciting frames. */
7881 len = ack_frm_len + *pn_len;
7882 len_frms = 0;
7883 if (!cc && !LIST_ISEMPTY(frms)) {
7884 ssize_t room = end - pos;
7885
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02007886 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007887 /* Initialize the length of the frames built below to <len>.
7888 * If any frame could be successfully built by qc_build_frms(),
7889 * we will have len_frms > len.
7890 */
7891 len_frms = len;
7892 if (!qc_build_frms(&frm_list, frms,
7893 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02007894 TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007895 qc, NULL, NULL, &room);
7896 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
7897 goto no_room;
7898 }
7899 }
7900
7901 /* Length (of the remaining data). Must not fail because, the buffer size
7902 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
7903 * for the encryption tag. It must be taken into an account for the length
7904 * of this packet.
7905 */
7906 if (len_frms)
7907 len = len_frms + QUIC_TLS_TAG_LEN;
7908 else
7909 len += QUIC_TLS_TAG_LEN;
7910 /* CONNECTION_CLOSE frame */
7911 if (cc) {
7912 qc_build_cc_frm(qc, qel, &cc_frm);
7913 len += qc_frm_len(&cc_frm);
7914 }
7915 add_ping_frm = 0;
7916 padding_len = 0;
7917 len_sz = quic_int_getsize(len);
7918 /* Add this packet size to <dglen> */
7919 dglen += head_len + len_sz + len;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007920 /* Note that <padding> is true only when building an Handshake packet
7921 * coalesced to an Initial packet.
7922 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007923 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
7924 /* This is a maximum padding size */
7925 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7926 /* The length field value is of this packet is <len> + <padding_len>
7927 * the size of which may be greater than the initial computed size
7928 * <len_sz>. So, let's deduce the difference between these to packet
7929 * sizes from <padding_len>.
7930 */
7931 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7932 len += padding_len;
7933 }
Frédéric Lécaille5faf5772023-02-16 17:30:53 +01007934 else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
7935 len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
7936 }
7937 else if (LIST_ISEMPTY(&frm_list)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007938 if (qel->pktns->tx.pto_probe) {
7939 /* If we cannot send a frame, we send a PING frame. */
7940 add_ping_frm = 1;
7941 len += 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007942 dglen += 1;
7943 /* Note that only we are in the case where this Initial packet
7944 * is not coalesced to an Handshake packet. We must directly
7945 * pad the datragram.
7946 */
Frédéric Lécaille9c317b12023-03-28 15:39:11 +02007947 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7948 if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
7949 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7950 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7951 len += padding_len;
7952 }
7953 }
7954 else {
7955 /* Note that +1 is for the PING frame */
7956 if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
7957 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007958 }
7959 }
7960 else {
7961 /* If there is no frame at all to follow, add at least a PADDING frame. */
7962 if (!ack_frm_len && !cc)
7963 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007964 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007965 }
7966
7967 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
7968 goto no_room;
7969
7970 /* Packet number field address. */
7971 *buf_pn = pos;
7972
7973 /* Packet number encoding. */
7974 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
7975 goto no_room;
7976
7977 /* payload building (ack-eliciting or not frames) */
7978 payload = pos;
7979 if (ack_frm_len) {
7980 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
7981 goto no_room;
7982
7983 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
7984 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
7985 }
7986
7987 /* Ack-eliciting frames */
7988 if (!LIST_ISEMPTY(&frm_list)) {
7989 struct quic_frame *tmp_cf;
7990 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
7991 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
7992 ssize_t room = end - pos;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02007993 TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007994 qc, NULL, NULL, &room);
7995 /* Note that <cf> was added from <frms> to <frm_list> list by
7996 * qc_build_frms().
7997 */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007998 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007999 LIST_INSERT(frms, &cf->list);
8000 continue;
8001 }
8002
8003 quic_tx_packet_refinc(pkt);
8004 cf->pkt = pkt;
8005 }
8006 }
8007
8008 /* Build a PING frame if needed. */
8009 if (add_ping_frm) {
8010 frm.type = QUIC_FT_PING;
8011 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
8012 goto no_room;
8013 }
8014
8015 /* Build a CONNECTION_CLOSE frame if needed. */
8016 if (cc) {
8017 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
8018 goto no_room;
8019
8020 pkt->flags |= QUIC_FL_TX_PACKET_CC;
8021 }
8022
8023 /* Build a PADDING frame if needed. */
8024 if (padding_len) {
8025 frm.type = QUIC_FT_PADDING;
8026 frm.padding.len = padding_len;
8027 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
8028 goto no_room;
8029 }
8030
8031 if (pos == payload) {
8032 /* No payload was built because of congestion control */
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008033 TRACE_PROTO("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008034 goto no_room;
8035 }
8036
8037 /* If this packet is ack-eliciting and we are probing let's
8038 * decrement the PTO probe counter.
8039 */
8040 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
8041 qel->pktns->tx.pto_probe)
8042 qel->pktns->tx.pto_probe--;
8043
8044 pkt->len = pos - beg;
8045 LIST_SPLICE(&pkt->frms, &frm_list);
8046
8047 ret = 1;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008048 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008049 leave:
8050 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
8051 return ret;
8052
8053 no_room:
8054 /* Replace the pre-built frames which could not be add to this packet */
8055 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008056 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008057 goto leave;
8058}
8059
8060static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
8061{
8062 pkt->type = type;
8063 pkt->len = 0;
8064 pkt->in_flight_len = 0;
8065 pkt->pn_node.key = (uint64_t)-1;
8066 LIST_INIT(&pkt->frms);
8067 pkt->time_sent = TICK_ETERNITY;
8068 pkt->next = NULL;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01008069 pkt->prev = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008070 pkt->largest_acked_pn = -1;
8071 pkt->flags = 0;
8072 pkt->refcnt = 0;
8073}
8074
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008075/* Build a packet into a buffer at <pos> position, <end> pointing to one byte past
8076 * the end of this buffer, with <pkt_type> as packet type for <qc> QUIC connection
8077 * at <qel> encryption level with <frms> list of prebuilt frames.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008078 *
8079 * Return -2 if the packet could not be allocated or encrypted for any reason,
8080 * -1 if there was not enough room to build a packet.
8081 * XXX NOTE XXX
8082 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
8083 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
8084 * control window limitation.
8085 */
8086static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008087 const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008088 struct quic_enc_level *qel,
8089 struct quic_tls_ctx *tls_ctx, struct list *frms,
8090 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008091 size_t dglen, int pkt_type, int must_ack,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008092 int padding, int probe, int cc, int *err)
8093{
8094 struct quic_tx_packet *ret_pkt = NULL;
8095 /* The pointer to the packet number field. */
8096 unsigned char *buf_pn;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008097 unsigned char *first_byte, *last_byte, *payload;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008098 int64_t pn;
8099 size_t pn_len, payload_len, aad_len;
8100 struct quic_tx_packet *pkt;
8101
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008102 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
8103 TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008104 *err = 0;
8105 pkt = pool_alloc(pool_head_quic_tx_packet);
8106 if (!pkt) {
8107 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
8108 *err = -2;
8109 goto err;
8110 }
8111
8112 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008113 first_byte = *pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008114 pn_len = 0;
8115 buf_pn = NULL;
8116
8117 pn = qel->pktns->tx.next_pn + 1;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008118 if (!qc_do_build_pkt(*pos, end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008119 must_ack, padding, cc, probe, qel, qc, ver, frms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008120 // trace already emitted by function above
8121 *err = -1;
8122 goto err;
8123 }
8124
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008125 last_byte = first_byte + pkt->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008126 payload = buf_pn + pn_len;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008127 payload_len = last_byte - payload;
8128 aad_len = payload - first_byte;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008129
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008130 if (!quic_packet_encrypt(payload, payload_len, first_byte, aad_len, pn, tls_ctx, qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008131 // trace already emitted by function above
8132 *err = -2;
8133 goto err;
8134 }
8135
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008136 last_byte += QUIC_TLS_TAG_LEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008137 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008138 if (!quic_apply_header_protection(qc, first_byte, buf_pn, pn_len, tls_ctx)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008139 // trace already emitted by function above
8140 *err = -2;
8141 goto err;
8142 }
8143
8144 /* Consume a packet number */
8145 qel->pktns->tx.next_pn++;
8146 qc->tx.prep_bytes += pkt->len;
8147 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
8148 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
8149 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
8150 }
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008151
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008152 /* Now that a correct packet is built, let us consume <*pos> buffer. */
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008153 *pos = last_byte;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008154 /* Attach the built packet to its tree. */
8155 pkt->pn_node.key = pn;
8156 /* Set the packet in fligth length for in flight packet only. */
8157 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
8158 pkt->in_flight_len = pkt->len;
8159 qc->path->prep_in_flight += pkt->len;
8160 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01008161 /* Always reset this flag */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008162 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
8163 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
8164 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
8165 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
Frédéric Lécailled7215712023-03-24 18:13:37 +01008166 qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
8167 if (tick_isset(qc->ack_expire)) {
8168 qc->ack_expire = TICK_ETERNITY;
8169 qc->idle_timer_task->expire = qc->idle_expire;
8170 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02008171 TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01008172 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008173 }
8174
8175 pkt->pktns = qel->pktns;
8176
8177 ret_pkt = pkt;
8178 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008179 TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
8180 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008181 return ret_pkt;
8182
8183 err:
8184 /* TODO: what about the frames which have been built
8185 * for this packet.
8186 */
8187 free_quic_tx_packet(qc, pkt);
8188 goto leave;
8189}
8190
8191
8192static void __quic_conn_init(void)
8193{
8194 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
8195}
8196INITCALL0(STG_REGISTER, __quic_conn_init);
8197
8198static void __quic_conn_deinit(void)
8199{
8200 BIO_meth_free(ha_quic_meth);
8201}
8202REGISTER_POST_DEINIT(__quic_conn_deinit);
8203
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008204/* Handle a new <dgram> received. Parse each QUIC packets and copied their
8205 * content to a quic-conn instance. The datagram content can be released after
8206 * this function.
8207 *
8208 * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
8209 * to the connection instance. <li> is the attached listener. The caller is
8210 * responsible to ensure that the first packet is destined to this connection
8211 * by comparing CIDs.
8212 *
8213 * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
8214 * function will thus retrieve the connection from the CID tree or allocate a
8215 * new one if possible. <li> is the listener attached to the receiver.
8216 *
8217 * Returns 0 on success else non-zero. If an error happens, some packets from
8218 * the datagram may not have been parsed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008219 */
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008220int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
8221 struct listener *li)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008222{
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008223 struct quic_rx_packet *pkt;
8224 struct quic_conn *qc = NULL;
8225 unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008226 struct list *tasklist_head = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008227
8228 TRACE_ENTER(QUIC_EV_CONN_LPKT);
8229
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008230 pos = dgram->buf;
8231 end = pos + dgram->len;
8232 do {
8233 /* TODO replace zalloc -> alloc. */
8234 pkt = pool_zalloc(pool_head_quic_rx_packet);
8235 if (!pkt) {
8236 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
8237 goto err;
8238 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008239
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008240 pkt->version = NULL;
8241 pkt->pn_offset = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008242
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008243 /* Set flag if pkt is the first one in dgram. */
8244 if (pos == dgram->buf)
8245 pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008246
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008247 LIST_INIT(&pkt->qc_rx_pkt_list);
8248 pkt->time_received = now_ms;
8249 quic_rx_packet_refinc(pkt);
8250 if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
8251 goto next;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008252
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008253 /* Search quic-conn instance for first packet of the datagram.
8254 * quic_rx_packet_parse() is responsible to discard packets
8255 * with different DCID as the first one in the same datagram.
8256 */
8257 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008258 int new_tid = -1;
8259
8260 qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li, &new_tid);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008261 /* qc is NULL if receiving a non Initial packet for an
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008262 * unknown connection or on connection affinity rebind.
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008263 */
8264 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008265 if (new_tid >= 0) {
8266 MT_LIST_APPEND(&quic_dghdlrs[new_tid].dgrams,
8267 &dgram->handler_list);
8268 tasklet_wakeup(quic_dghdlrs[new_tid].task);
8269 goto out;
8270 }
8271
Amaury Denoyelle98289692022-10-19 15:37:44 +02008272 /* Skip the entire datagram. */
8273 pkt->len = end - pos;
8274 goto next;
8275 }
8276
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008277 dgram->qc = qc;
8278 }
Amaury Denoyelle98289692022-10-19 15:37:44 +02008279
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008280 if (qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED)
8281 qc_finalize_affinity_rebind(qc);
8282
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008283 if (qc_rx_check_closing(qc, pkt)) {
8284 /* Skip the entire datagram. */
8285 pkt->len = end - pos;
8286 goto next;
8287 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008288
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008289 /* Detect QUIC connection migration. */
Frédéric Lécaillef6769542023-01-12 10:36:26 +01008290 if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008291 if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
8292 /* Skip the entire datagram. */
8293 TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
8294 pkt->len = end - pos;
8295 goto next;
8296 }
8297 }
8298
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008299 qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008300
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008301 next:
8302 pos += pkt->len;
8303 quic_rx_packet_refdec(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008304
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008305 /* Free rejected packets */
8306 if (!pkt->refcnt) {
8307 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
8308 pool_free(pool_head_quic_rx_packet, pkt);
8309 }
8310 } while (pos < end);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008311
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008312 /* Increasing the received bytes counter by the UDP datagram length
8313 * if this datagram could be associated to a connection.
8314 */
8315 if (dgram->qc)
8316 dgram->qc->rx.bytes += dgram->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008317
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008318 /* This must never happen. */
8319 BUG_ON(pos > end);
8320 BUG_ON(pos < end || pos > dgram->buf + dgram->len);
8321 /* Mark this datagram as consumed */
8322 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008323
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008324 out:
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008325 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
8326 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008327
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008328 err:
Amaury Denoyellea65dd3a2023-04-19 14:26:16 +02008329 /* Mark this datagram as consumed as maybe at least some packets were parsed. */
8330 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008331 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008332 return -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008333}
8334
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008335/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
8336 * CIDs. This can be used to determine if a datagram is addressed to the right
8337 * connection instance.
8338 *
8339 * Returns a boolean value.
8340 */
8341int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len)
8342{
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008343 const uchar idx = _quic_cid_tree_idx(dcid);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008344 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008345 struct ebmb_node *node = NULL;
8346 struct quic_cid_tree *tree = &quic_cid_trees[idx];
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008347
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008348 /* Test against our default CID or client ODCID. */
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008349 if ((qc->scid.len == dcid_len &&
8350 memcmp(qc->scid.data, dcid, dcid_len) == 0) ||
8351 (qc->odcid.len == dcid_len &&
Frédéric Lécaille07846cb2023-02-13 16:14:24 +01008352 memcmp(qc->odcid.data, dcid, dcid_len) == 0)) {
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008353 return 1;
8354 }
8355
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008356 /* Test against our other CIDs. This can happen if the client has
8357 * decided to switch to a new one.
8358 *
8359 * TODO to avoid locking, loop through qc.cids as an alternative.
8360 *
8361 * TODO set it to our default CID to avoid this operation next time.
8362 */
8363 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
8364 node = ebmb_lookup(&tree->root, dcid, dcid_len);
8365 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
8366
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008367 if (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008368 conn_id = ebmb_entry(node, struct quic_connection_id, node);
8369 if (qc == conn_id->qc)
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008370 return 1;
8371 }
8372
8373 return 0;
8374}
8375
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008376/* Retrieve the DCID from a QUIC datagram or packet at <pos> postition,
8377 * <end> being at one byte past the end of this datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008378 * Returns 1 if succeeded, 0 if not.
8379 */
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008380int quic_get_dgram_dcid(unsigned char *pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008381 unsigned char **dcid, size_t *dcid_len)
8382{
8383 int ret = 0, long_header;
8384 size_t minlen, skip;
8385
8386 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
8387
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008388 if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008389 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
8390 goto err;
8391 }
8392
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008393 long_header = *pos & QUIC_PACKET_LONG_HEADER_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008394 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
8395 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
8396 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008397 if (end - pos < minlen)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008398 goto err;
8399
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008400 pos += skip;
8401 *dcid_len = long_header ? *pos++ : QUIC_HAP_CID_LEN;
8402 if (*dcid_len > QUIC_CID_MAXLEN || end - pos <= *dcid_len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008403 goto err;
8404
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008405 *dcid = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008406
8407 ret = 1;
8408 leave:
8409 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
8410 return ret;
8411
8412 err:
8413 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
8414 goto leave;
8415}
8416
8417/* Notify the MUX layer if alive about an imminent close of <qc>. */
8418void qc_notify_close(struct quic_conn *qc)
8419{
8420 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
8421
8422 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
8423 goto leave;
8424
8425 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
8426 /* wake up the MUX */
8427 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
8428 TRACE_STATE("connection closure notidfied to mux",
8429 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8430 qc->conn->mux->wake(qc->conn);
8431 }
8432 else
8433 TRACE_STATE("connection closure not notidfied to mux",
8434 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8435 leave:
8436 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
8437}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008438
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008439/* Wake-up upper layer for sending if all conditions are met :
8440 * - room in congestion window or probe packet to sent
8441 * - socket FD ready to sent or listener socket used
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008442 *
8443 * Returns 1 if upper layer has been woken up else 0.
8444 */
8445int qc_notify_send(struct quic_conn *qc)
8446{
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008447 const struct quic_pktns *pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8448
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008449 if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008450 /* RFC 9002 7.5. Probe Timeout
8451 *
8452 * Probe packets MUST NOT be blocked by the congestion controller.
8453 */
8454 if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
Amaury Denoyellecaa16542023-02-28 15:11:26 +01008455 (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008456 tasklet_wakeup(qc->subs->tasklet);
8457 qc->subs->events &= ~SUB_RETRY_SEND;
8458 if (!qc->subs->events)
8459 qc->subs = NULL;
8460
8461 return 1;
8462 }
8463 }
8464
8465 return 0;
8466}
8467
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008468/* Move a <qc> QUIC connection and its resources from the current thread to the
Willy Tarreau77d37b02023-04-20 19:03:49 +02008469 * new one <new_tid> optionally in association with <new_li> (since it may need
8470 * to change when migrating to a thread from a different group, otherwise leave
8471 * it NULL). After this call, the connection cannot be dereferenced anymore on
8472 * the current thread.
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008473 *
8474 * Returns 0 on success else non-zero.
8475 */
Willy Tarreau77d37b02023-04-20 19:03:49 +02008476int qc_set_tid_affinity(struct quic_conn *qc, uint new_tid, struct listener *new_li)
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008477{
8478 struct task *t1 = NULL, *t2 = NULL;
8479 struct tasklet *t3 = NULL;
8480
8481 struct quic_connection_id *conn_id;
8482 struct eb64_node *node;
8483
8484 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8485
8486 /* Pre-allocate all required resources. This ensures we do not left a
8487 * connection with only some of its field rebinded.
8488 */
8489 if (((t1 = task_new_on(new_tid)) == NULL) ||
8490 (qc->timer_task && (t2 = task_new_on(new_tid)) == NULL) ||
8491 (t3 = tasklet_new()) == NULL) {
8492 goto err;
8493 }
8494
8495 /* Reinit idle timer task. */
8496 task_kill(qc->idle_timer_task);
8497 t1->expire = qc->idle_timer_task->expire;
8498 qc->idle_timer_task = t1;
8499 qc->idle_timer_task->process = qc_idle_timer_task;
8500 qc->idle_timer_task->context = qc;
8501
8502 /* Reinit timer task if allocated. */
8503 if (qc->timer_task) {
8504 task_kill(qc->timer_task);
8505 qc->timer_task = t2;
8506 qc->timer_task->process = qc_process_timer;
8507 qc->timer_task->context = qc;
8508 }
8509
8510 /* Reinit IO tasklet. */
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008511 if (qc->wait_event.tasklet->state & TASK_IN_LIST)
8512 qc->flags |= QUIC_FL_CONN_IO_TO_REQUEUE;
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008513 tasklet_kill(qc->wait_event.tasklet);
8514 /* In most cases quic_conn_app_io_cb is used but for 0-RTT quic_conn_io_cb can be still activated. */
8515 t3->process = qc->wait_event.tasklet->process;
8516 qc->wait_event.tasklet = t3;
8517 qc->wait_event.tasklet->tid = new_tid;
8518 qc->wait_event.tasklet->context = qc;
8519 qc->wait_event.events = 0;
8520
8521 /* Rebind the connection FD. */
8522 if (qc_test_fd(qc)) {
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008523 /* Reading is reactivated by the new thread. */
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008524 fd_migrate_on(qc->fd, new_tid);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008525 }
8526
Amaury Denoyelle7b516d32023-04-26 16:12:12 +02008527 /* Remove conn from per-thread list instance. It will be hidden from
8528 * "show quic" until rebinding is completed.
8529 */
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008530 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008531
8532 node = eb64_first(&qc->cids);
8533 BUG_ON(!node || eb64_next(node)); /* One and only one CID must be present before affinity rebind. */
8534 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
Willy Tarreau77d37b02023-04-20 19:03:49 +02008535
8536 /* At this point no connection was accounted for yet on this
8537 * listener so it's OK to just swap the pointer.
8538 */
8539 if (new_li && new_li != qc->li)
8540 qc->li = new_li;
8541
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008542 /* Rebinding is considered done when CID points to the new thread. No
8543 * access should be done to quic-conn instance after it.
8544 */
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008545 qc->flags |= QUIC_FL_CONN_AFFINITY_CHANGED;
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008546 HA_ATOMIC_STORE(&conn_id->tid, new_tid);
8547 qc = NULL;
8548
8549 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, NULL);
8550 return 0;
8551
8552 err:
8553 task_destroy(t1);
8554 task_destroy(t2);
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02008555 tasklet_free(t3);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008556
8557 TRACE_DEVEL("leaving on error", QUIC_EV_CONN_SET_AFFINITY, qc);
8558 return 1;
8559}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008560
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008561/* Must be called after qc_set_tid_affinity() on the new thread. */
8562void qc_finalize_affinity_rebind(struct quic_conn *qc)
8563{
8564 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8565
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008566 /* This function must not be called twice after an affinity rebind. */
8567 BUG_ON(!(qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED));
8568 qc->flags &= ~QUIC_FL_CONN_AFFINITY_CHANGED;
8569
Amaury Denoyelle7b516d32023-04-26 16:12:12 +02008570 /* A connection must not pass to closing state until affinity rebind
8571 * is completed. Else quic_handle_stopping() may miss it during process
8572 * stopping cleanup.
8573 */
8574 BUG_ON(qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING));
8575
8576 /* Reinsert connection in ha_thread_ctx global list. */
8577 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
8578 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
8579
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008580 /* Reactivate FD polling if connection socket is active. */
8581 qc_want_recv(qc);
8582
8583 /* Reactivate timer task if needed. */
8584 qc_set_timer(qc);
8585
8586 /* Idle timer task is always active. */
8587 task_queue(qc->idle_timer_task);
8588
8589 /* Reactivate IO tasklet if needed. */
8590 if (qc->flags & QUIC_FL_CONN_IO_TO_REQUEUE) {
8591 tasklet_wakeup(qc->wait_event.tasklet);
8592 qc->flags &= ~QUIC_FL_CONN_IO_TO_REQUEUE;
8593 }
8594
8595 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, qc);
8596}
8597
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008598enum quic_dump_format {
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008599 QUIC_DUMP_FMT_ONELINE,
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008600 QUIC_DUMP_FMT_FULL,
8601};
8602
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008603/* appctx context used by "show quic" command */
8604struct show_quic_ctx {
8605 unsigned int epoch;
8606 struct bref bref; /* back-reference to the quic-conn being dumped */
8607 unsigned int thr;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008608 int flags;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008609 enum quic_dump_format format;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008610};
8611
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008612#define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */
8613
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008614static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private)
8615{
8616 struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008617 int argc = 2;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008618
8619 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
8620 return 1;
8621
8622 ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1);
8623 ctx->thr = 0;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008624 ctx->flags = 0;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008625 ctx->format = QUIC_DUMP_FMT_ONELINE;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008626
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008627 if (strcmp(args[argc], "oneline") == 0) {
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008628 /* format already used as default value */
8629 ++argc;
8630 }
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008631 else if (strcmp(args[argc], "full") == 0) {
8632 ctx->format = QUIC_DUMP_FMT_FULL;
8633 ++argc;
8634 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008635
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008636 while (*args[argc]) {
8637 if (strcmp(args[argc], "all") == 0)
8638 ctx->flags |= QC_CLI_FL_SHOW_ALL;
8639
8640 ++argc;
8641 }
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01008642
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008643 LIST_INIT(&ctx->bref.users);
8644
8645 return 0;
8646}
8647
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008648/* Dump for "show quic" with "oneline" format. */
8649static void dump_quic_oneline(struct show_quic_ctx *ctx, struct quic_conn *qc)
8650{
8651 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
8652 unsigned char cid_len;
8653
8654 chunk_appendf(&trash, "%p[%02u]/%-.12s ", qc, ctx->thr,
8655 qc->li->bind_conf->frontend->id);
8656
8657 /* State */
8658 if (qc->flags & QUIC_FL_CONN_CLOSING)
8659 chunk_appendf(&trash, "CLOSE ");
8660 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8661 chunk_appendf(&trash, "DRAIN ");
8662 else if (qc->state < QUIC_HS_ST_COMPLETE)
8663 chunk_appendf(&trash, "HDSHK ");
8664 else
8665 chunk_appendf(&trash, "ESTAB ");
8666
8667 /* Bytes in flight / Lost packets */
8668 chunk_appendf(&trash, "%9llu %6llu %6llu ",
8669 (ullong)qc->path->in_flight,
8670 (ullong)qc->path->ifae_pkts,
8671 (ullong)qc->path->loss.nb_lost_pkt);
8672
8673 /* Socket */
8674 if (qc->local_addr.ss_family == AF_INET ||
8675 qc->local_addr.ss_family == AF_INET6) {
8676 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8677 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
8678 chunk_appendf(&trash, "%15s:%s ", bufaddr, bufport);
8679
8680 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8681 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
8682 chunk_appendf(&trash, "%15s:%s ", bufaddr, bufport);
8683 }
8684
8685 /* CIDs */
8686 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
8687 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
8688
8689 chunk_appendf(&trash, " ");
8690 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
8691 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
8692
8693 chunk_appendf(&trash, "\n");
8694}
8695
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008696/* Dump for "show quic" with "full" format. */
8697static void dump_quic_full(struct show_quic_ctx *ctx, struct quic_conn *qc)
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008698{
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008699 struct quic_pktns *pktns;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008700 struct eb64_node *node;
8701 struct qc_stream_desc *stream;
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008702 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008703 int expire, i;
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008704 unsigned char cid_len;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008705
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008706 /* CIDs */
8707 chunk_appendf(&trash, "* %p[%02u]: scid=", qc, ctx->thr);
8708 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
8709 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
8710 while (cid_len++ < 20)
8711 chunk_appendf(&trash, "..");
8712
8713 chunk_appendf(&trash, " dcid=");
8714 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
8715 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
8716 while (cid_len++ < 20)
8717 chunk_appendf(&trash, "..");
8718
8719 chunk_appendf(&trash, "\n");
8720
8721 chunk_appendf(&trash, " loc. TPs:");
8722 quic_transport_params_dump(&trash, qc, &qc->rx.params);
8723 chunk_appendf(&trash, "\n");
8724 chunk_appendf(&trash, " rem. TPs:");
8725 quic_transport_params_dump(&trash, qc, &qc->tx.params);
8726 chunk_appendf(&trash, "\n");
8727
8728 /* Connection state */
8729 if (qc->flags & QUIC_FL_CONN_CLOSING)
8730 chunk_appendf(&trash, " st=closing ");
8731 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8732 chunk_appendf(&trash, " st=draining ");
8733 else if (qc->state < QUIC_HS_ST_CONFIRMED)
8734 chunk_appendf(&trash, " st=handshake ");
8735 else
8736 chunk_appendf(&trash, " st=opened ");
8737
8738 if (qc->mux_state == QC_MUX_NULL)
8739 chunk_appendf(&trash, "mux=null ");
8740 else if (qc->mux_state == QC_MUX_READY)
8741 chunk_appendf(&trash, "mux=ready ");
8742 else
8743 chunk_appendf(&trash, "mux=released ");
8744
8745 expire = qc->idle_expire;
8746 chunk_appendf(&trash, "expire=%02ds ",
8747 TICKS_TO_MS(tick_remain(now_ms, expire)) / 1000);
8748
8749 chunk_appendf(&trash, "\n");
8750
8751 /* Socket */
8752 chunk_appendf(&trash, " fd=%d", qc->fd);
8753 if (qc->local_addr.ss_family == AF_INET ||
8754 qc->local_addr.ss_family == AF_INET6) {
8755 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8756 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
8757 chunk_appendf(&trash, " from=%s:%s", bufaddr, bufport);
8758
8759 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8760 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
8761 chunk_appendf(&trash, " to=%s:%s", bufaddr, bufport);
8762 }
8763
8764 chunk_appendf(&trash, "\n");
8765
8766 /* Packet number spaces information */
8767 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
8768 chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu",
8769 pktns->rx.arngs.sz, pktns->tx.in_flight);
8770 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
8771 chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
8772 pktns->rx.arngs.sz, pktns->tx.in_flight);
8773 pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8774 chunk_appendf(&trash, " [01rtt] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
8775 pktns->rx.arngs.sz, pktns->tx.in_flight);
8776
8777 chunk_appendf(&trash, " srtt=%-4u rttvar=%-4u rttmin=%-4u ptoc=%-4u cwnd=%-6llu"
8778 " mcwnd=%-6llu lostpkts=%-6llu\n",
8779 qc->path->loss.srtt >> 3, qc->path->loss.rtt_var >> 2,
8780 qc->path->loss.rtt_min, qc->path->loss.pto_count, (ullong)qc->path->cwnd,
8781 (ullong)qc->path->mcwnd, (ullong)qc->path->loss.nb_lost_pkt);
8782
8783
8784 /* Streams */
8785 node = eb64_first(&qc->streams_by_id);
8786 i = 0;
8787 while (node) {
8788 stream = eb64_entry(node, struct qc_stream_desc, by_id);
8789 node = eb64_next(node);
8790
8791 chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key);
8792 chunk_appendf(&trash, " off=%-8llu ack=%-8llu",
8793 (unsigned long long)stream->buf_offset,
8794 (unsigned long long)stream->ack_offset);
8795
8796 if (!(++i % 3)) {
8797 chunk_appendf(&trash, "\n");
8798 i = 0;
8799 }
8800 }
8801
8802 chunk_appendf(&trash, "\n");
8803}
8804
8805static int cli_io_handler_dump_quic(struct appctx *appctx)
8806{
8807 struct show_quic_ctx *ctx = appctx->svcctx;
8808 struct stconn *sc = appctx_sc(appctx);
8809 struct quic_conn *qc;
8810
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008811 thread_isolate();
8812
8813 if (ctx->thr >= global.nbthread)
8814 goto done;
8815
Christopher Faulet87633c32023-04-03 18:32:50 +02008816 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02008817 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008818 /* If we're forced to shut down, we might have to remove our
8819 * reference to the last stream being dumped.
8820 */
8821 if (!LIST_ISEMPTY(&ctx->bref.users))
8822 LIST_DEL_INIT(&ctx->bref.users);
8823 goto done;
8824 }
8825
8826 chunk_reset(&trash);
8827
8828 if (!LIST_ISEMPTY(&ctx->bref.users)) {
8829 /* Remove show_quic_ctx from previous quic_conn instance. */
8830 LIST_DEL_INIT(&ctx->bref.users);
8831 }
8832 else if (!ctx->bref.ref) {
8833 /* First invocation. */
8834 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008835
8836 /* Print legend for oneline format. */
8837 if (ctx->format == QUIC_DUMP_FMT_ONELINE) {
8838 chunk_appendf(&trash, "# conn/frontend state "
8839 "in_flight infl_p lost_p "
8840 "from to "
8841 "local & remote CIDs\n");
8842 applet_putchk(appctx, &trash);
8843 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008844 }
8845
8846 while (1) {
8847 int done = 0;
8848
8849 if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008850 /* If closing connections requested through "all", move
8851 * to quic_conns_clo list after browsing quic_conns.
8852 * Else move directly to the next quic_conns thread.
8853 */
8854 if (ctx->flags & QC_CLI_FL_SHOW_ALL) {
8855 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns_clo.n;
8856 continue;
8857 }
8858
8859 done = 1;
8860 }
8861 else if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns_clo) {
8862 /* Closing list entirely browsed, go to next quic_conns
8863 * thread.
8864 */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008865 done = 1;
8866 }
8867 else {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008868 /* Retrieve next element of the current list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008869 qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx);
8870 if ((int)(qc->qc_epoch - ctx->epoch) > 0)
8871 done = 1;
8872 }
8873
8874 if (done) {
8875 ++ctx->thr;
8876 if (ctx->thr >= global.nbthread)
8877 break;
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008878 /* Switch to next thread quic_conns list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008879 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8880 continue;
8881 }
8882
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008883 switch (ctx->format) {
8884 case QUIC_DUMP_FMT_FULL:
8885 dump_quic_full(ctx, qc);
8886 break;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008887 case QUIC_DUMP_FMT_ONELINE:
8888 dump_quic_oneline(ctx, qc);
8889 break;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008890 }
8891
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008892 if (applet_putchk(appctx, &trash) == -1) {
8893 /* Register show_quic_ctx to quic_conn instance. */
8894 LIST_APPEND(&qc->back_refs, &ctx->bref.users);
8895 goto full;
8896 }
8897
8898 ctx->bref.ref = qc->el_th_ctx.n;
8899 }
8900
8901 done:
8902 thread_release();
8903 return 1;
8904
8905 full:
8906 thread_release();
8907 return 0;
8908}
8909
8910static void cli_release_show_quic(struct appctx *appctx)
8911{
8912 struct show_quic_ctx *ctx = appctx->svcctx;
8913
8914 if (ctx->thr < global.nbthread) {
8915 thread_isolate();
8916 if (!LIST_ISEMPTY(&ctx->bref.users))
8917 LIST_DEL_INIT(&ctx->bref.users);
8918 thread_release();
8919 }
8920}
8921
8922static struct cli_kw_list cli_kws = {{ }, {
8923 { { "show", "quic", NULL }, "show quic : display quic connections status", cli_parse_show_quic, cli_io_handler_dump_quic, cli_release_show_quic },
Frédéric Lécaille91376d62023-02-11 20:24:42 +01008924 {{},}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008925}};
8926
8927INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
8928
8929static void init_quic()
8930{
8931 int thr;
8932
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008933 for (thr = 0; thr < MAX_THREADS; ++thr) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008934 LIST_INIT(&ha_thread_ctx[thr].quic_conns);
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008935 LIST_INIT(&ha_thread_ctx[thr].quic_conns_clo);
8936 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008937}
8938INITCALL0(STG_INIT, init_quic);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008939
8940/*
8941 * Local variables:
8942 * c-indent-level: 8
8943 * c-basic-offset: 8
8944 * End:
8945 */