blob: 31dc1b9793c57589bbac8783f4a456bf5a781a31 [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;
510
511 if (qel) {
512 const struct quic_pktns *pktns = qel->pktns;
513 chunk_appendf(&trace_buf,
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200514 " 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 +0200515 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200516 qel->pktns->flags,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200517 quic_hdshk_state_str(qc->state),
518 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille45400532023-02-13 18:39:19 +0100519 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200520 (unsigned long long)qc->path->cwnd,
521 (unsigned long long)qc->path->prep_in_flight,
522 (unsigned long long)qc->path->in_flight,
523 (unsigned long long)pktns->tx.in_flight,
Amaury Denoyelle2f668f02022-11-18 15:24:08 +0100524 pktns->tx.pto_probe,
525 qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200526 }
527 }
528
529 if (mask & QUIC_EV_CONN_ENCPKT) {
530 const struct enc_debug_info *edi = a2;
531
532 if (edi)
533 chunk_appendf(&trace_buf,
534 " payload=@%p payload_len=%llu"
535 " aad=@%p aad_len=%llu pn=%llu",
536 edi->payload, (unsigned long long)edi->payload_len,
537 edi->aad, (unsigned long long)edi->aad_len,
538 (unsigned long long)edi->pn);
539 }
540
541 if (mask & QUIC_EV_CONN_RMHP) {
542 const struct quic_rx_packet *pkt = a2;
543
544 if (pkt) {
545 const int *ret = a3;
546
547 chunk_appendf(&trace_buf, " pkt@%p", pkt);
548 if (ret && *ret)
549 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
550 pkt->pnl, (unsigned long long)pkt->pn);
551 }
552 }
553
554 if (mask & QUIC_EV_CONN_PRSAFRM) {
555 const struct quic_frame *frm = a2;
556 const unsigned long *val1 = a3;
557 const unsigned long *val2 = a4;
558
559 if (frm) {
560 chunk_appendf(&trace_buf, " frm@%p", frm);
561 chunk_frm_appendf(&trace_buf, frm);
562 }
563 if (val1)
564 chunk_appendf(&trace_buf, " %lu", *val1);
565 if (val2)
566 chunk_appendf(&trace_buf, "..%lu", *val2);
567 }
568
569 if (mask & QUIC_EV_CONN_ACKSTRM) {
570 const struct quic_stream *s = a2;
571 const struct qc_stream_desc *stream = a3;
572
573 if (s)
574 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
575 if (stream)
576 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
577 }
578
579 if (mask & QUIC_EV_CONN_RTTUPDT) {
580 const unsigned int *rtt_sample = a2;
581 const unsigned int *ack_delay = a3;
582 const struct quic_loss *ql = a4;
583
584 if (rtt_sample)
585 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
586 if (ack_delay)
587 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
588 if (ql)
589 chunk_appendf(&trace_buf,
590 " srtt=%ums rttvar=%ums min_rtt=%ums",
591 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
592 }
593 if (mask & QUIC_EV_CONN_CC) {
594 const struct quic_cc_event *ev = a2;
595 const struct quic_cc *cc = a3;
596
597 if (a2)
598 quic_cc_event_trace(&trace_buf, ev);
599 if (a3)
600 quic_cc_state_trace(&trace_buf, cc);
601 }
602
603 if (mask & QUIC_EV_CONN_PKTLOSS) {
604 const struct quic_pktns *pktns = a2;
605 const struct list *lost_pkts = a3;
606
607 if (pktns) {
608 chunk_appendf(&trace_buf, " pktns=%s",
609 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
610 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
611 if (pktns->tx.loss_time)
612 chunk_appendf(&trace_buf, " loss_time=%dms",
613 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
614 }
615 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
616 struct quic_tx_packet *pkt;
617
618 chunk_appendf(&trace_buf, " lost_pkts:");
619 list_for_each_entry(pkt, lost_pkts, list)
620 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
621 }
622 }
623
624 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
625 const struct quic_pktns *pktns = a2;
626 const int *duration = a3;
627 const uint64_t *ifae_pkts = a4;
628
629 if (ifae_pkts)
630 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
631 (unsigned long long)*ifae_pkts);
632 if (pktns) {
633 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
634 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
635 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
636 pktns->tx.pto_probe);
637 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
638 if (pktns->tx.in_flight)
639 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
640 if (pktns->tx.loss_time)
641 chunk_appendf(&trace_buf, " loss_time=%dms",
642 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
643 }
644 if (mask & QUIC_EV_CONN_SPTO) {
645 if (pktns->tx.time_of_last_eliciting)
646 chunk_appendf(&trace_buf, " tole=%dms",
647 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
648 if (duration)
649 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
650 }
651 }
652
653 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
654 chunk_appendf(&trace_buf,
655 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
656 }
657 }
658
659 if (mask & QUIC_EV_CONN_SPPKTS) {
660 const struct quic_tx_packet *pkt = a2;
661
Frédéric Lécaille45400532023-02-13 18:39:19 +0100662 chunk_appendf(&trace_buf, " pto_count=%d cwnd=%llu ppif=%llu pif=%llu",
663 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200664 (unsigned long long)qc->path->cwnd,
665 (unsigned long long)qc->path->prep_in_flight,
666 (unsigned long long)qc->path->in_flight);
667 if (pkt) {
668 const struct quic_frame *frm;
Frédéric Lécaille6fd25762023-04-07 19:01:33 +0200669 if (pkt->flags & QUIC_FL_TX_PACKET_ACK)
670 chunk_appendf(&trace_buf, " ack");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200671 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
672 (unsigned long)pkt->pn_node.key,
673 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
674 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
675 (unsigned long long)pkt->in_flight_len);
676 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
677 (unsigned long long)qc->rx.bytes,
678 (unsigned long long)qc->tx.bytes);
679 list_for_each_entry(frm, &pkt->frms, list) {
680 chunk_appendf(&trace_buf, " frm@%p", frm);
681 chunk_frm_appendf(&trace_buf, frm);
682 }
Frédéric Lécaillebc09f742023-02-13 17:45:36 +0100683
684 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
685 chunk_appendf(&trace_buf, " with scid");
686 quic_cid_dump(&trace_buf, &qc->scid);
687 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200688 }
689 }
690
691 if (mask & QUIC_EV_CONN_SSLALERT) {
692 const uint8_t *alert = a2;
693 const enum ssl_encryption_level_t *level = a3;
694
695 if (alert)
696 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
697 if (level)
698 chunk_appendf(&trace_buf, " el=%c",
699 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
700 }
701
702 if (mask & QUIC_EV_CONN_BCFRMS) {
703 const size_t *sz1 = a2;
704 const size_t *sz2 = a3;
705 const size_t *sz3 = a4;
706
707 if (sz1)
708 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
709 if (sz2)
710 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
711 if (sz3)
712 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
713 }
714
715 if (mask & QUIC_EV_CONN_PSTRM) {
716 const struct quic_frame *frm = a2;
717
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100718 if (frm)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200719 chunk_frm_appendf(&trace_buf, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200720 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +0200721
722 if (mask & QUIC_EV_CONN_ELEVELSEL) {
723 const enum quic_handshake_state *state = a2;
724 const enum quic_tls_enc_level *level = a3;
725 const enum quic_tls_enc_level *next_level = a4;
726
727 if (state)
728 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state));
729 if (level)
730 chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level));
731 if (next_level)
732 chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level));
733
734 }
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200735
736 if (mask & QUIC_EV_CONN_RCV) {
737 const struct quic_dgram *dgram = a2;
738
739 if (dgram)
740 chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len);
741 }
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200742
743 if (mask & QUIC_EV_CONN_IDLE_TIMER) {
744 if (tick_isset(qc->ack_expire))
745 chunk_appendf(&trace_buf, " ack_expire=%ums",
746 TICKS_TO_MS(tick_remain(now_ms, qc->ack_expire)));
747 if (tick_isset(qc->idle_expire))
748 chunk_appendf(&trace_buf, " idle_expire=%ums",
749 TICKS_TO_MS(tick_remain(now_ms, qc->idle_expire)));
Frédéric Lécaillece5c1452023-04-05 09:44:21 +0200750 if (qc->idle_timer_task && tick_isset(qc->idle_timer_task->expire))
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200751 chunk_appendf(&trace_buf, " expire=%ums",
752 TICKS_TO_MS(tick_remain(now_ms, qc->idle_timer_task->expire)));
753 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200754 }
755 if (mask & QUIC_EV_CONN_LPKT) {
756 const struct quic_rx_packet *pkt = a2;
757 const uint64_t *len = a3;
758 const struct quic_version *ver = a4;
759
760 if (pkt) {
761 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
762 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
763 if (pkt->pn_node.key != (uint64_t)-1)
764 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
765 }
766
767 if (len)
768 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
769
770 if (ver)
771 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
772 }
773
774 if (mask & QUIC_EV_STATELESS_RST) {
775 const struct quic_cid *cid = a2;
776
777 if (cid)
778 quic_cid_dump(&trace_buf, cid);
779 }
780
781}
782
783/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
784static inline int quic_peer_validated_addr(struct quic_conn *qc)
785{
786 struct quic_pktns *hdshk_pktns, *app_pktns;
787
788 if (!qc_is_listener(qc))
789 return 1;
790
791 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
792 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
793 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
794 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
795 qc->state >= QUIC_HS_ST_COMPLETE)
796 return 1;
797
798 return 0;
799}
800
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100801/* To be called to kill a connection as soon as possible (without sending any packet). */
802void qc_kill_conn(struct quic_conn *qc)
803{
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100804 TRACE_ENTER(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200805 TRACE_PROTO("killing the connection", QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100806 qc->flags |= QUIC_FL_CONN_TO_KILL;
807 task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER);
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100808 TRACE_LEAVE(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100809}
810
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200811/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
812 * both loss detection and PTO and schedule the task assiated to this timer if needed.
813 */
814static inline void qc_set_timer(struct quic_conn *qc)
815{
816 struct quic_pktns *pktns;
817 unsigned int pto;
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100818 int handshake_confirmed;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200819
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100820 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc);
821 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200822
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100823 pktns = NULL;
824 if (!qc->timer_task) {
825 TRACE_PROTO("already released timer task", QUIC_EV_CONN_STIMER, qc);
826 goto leave;
827 }
828
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200829 pktns = quic_loss_pktns(qc);
830 if (tick_isset(pktns->tx.loss_time)) {
831 qc->timer = pktns->tx.loss_time;
832 goto out;
833 }
834
835 /* anti-amplification: the timer must be
836 * cancelled for a server which reached the anti-amplification limit.
837 */
838 if (!quic_peer_validated_addr(qc) &&
839 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
840 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
841 qc->timer = TICK_ETERNITY;
842 goto out;
843 }
844
845 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
846 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
847 /* Timer cancellation. */
848 qc->timer = TICK_ETERNITY;
849 goto out;
850 }
851
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100852 handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED;
853 pktns = quic_pto_pktns(qc, handshake_confirmed, &pto);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200854 if (tick_isset(pto))
855 qc->timer = pto;
856 out:
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100857 if (qc->timer == TICK_ETERNITY) {
858 qc->timer_task->expire = TICK_ETERNITY;
859 }
860 else if (tick_is_expired(qc->timer, now_ms)) {
861 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
862 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
863 }
864 else {
865 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
866 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200867 }
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100868 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100869 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, pktns);
870 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200871}
872
873/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
874 * connection.
875 * Return 1 if succeeded, 0 if not.
876 */
877static int quic_tls_key_update(struct quic_conn *qc)
878{
879 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 +0100880 struct quic_tls_secrets *rx = &tls_ctx->rx;
881 struct quic_tls_secrets *tx = &tls_ctx->tx;
882 /* Used only for the traces */
883 struct quic_kp_trace kp_trace = {
884 .rx_sec = rx->secret,
885 .rx_seclen = rx->secretlen,
886 .tx_sec = tx->secret,
887 .tx_seclen = tx->secretlen,
888 };
889 /* The next key phase secrets to be derived */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200890 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
891 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
892 const struct quic_version *ver =
893 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
894 int ret = 0;
895
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100896 TRACE_ENTER(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200897
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200898 nxt_rx = &qc->ku.nxt_rx;
899 nxt_tx = &qc->ku.nxt_tx;
900
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100901 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
902 "nxt_rx->secretlen=%llu rx->secretlen=%llu",
903 (ull)nxt_rx->secretlen, (ull)rx->secretlen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200904 /* Prepare new RX secrets */
905 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
906 rx->secret, rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100907 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200908 goto leave;
909 }
910
911 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
912 nxt_rx->key, nxt_rx->keylen,
913 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
914 nxt_rx->secret, nxt_rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100915 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200916 goto leave;
917 }
918
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100919 kp_trace.rx = nxt_rx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200920 /* Prepare new TX secrets */
921 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
922 tx->secret, tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100923 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200924 goto leave;
925 }
926
927 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
928 nxt_tx->key, nxt_tx->keylen,
929 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
930 nxt_tx->secret, nxt_tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100931 TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200932 goto leave;
933 }
934
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100935 kp_trace.tx = nxt_tx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200936 if (nxt_rx->ctx) {
937 EVP_CIPHER_CTX_free(nxt_rx->ctx);
938 nxt_rx->ctx = NULL;
939 }
940
941 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 +0100942 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200943 goto leave;
944 }
945
946 if (nxt_tx->ctx) {
947 EVP_CIPHER_CTX_free(nxt_tx->ctx);
948 nxt_tx->ctx = NULL;
949 }
950
951 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 +0100952 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200953 goto leave;
954 }
955
956 ret = 1;
957 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100958 TRACE_PROTO("key update", QUIC_EV_CONN_KP, qc, &kp_trace);
959 TRACE_LEAVE(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200960 return ret;
961}
962
963/* Rotate the Key Update information for <qc> QUIC connection.
964 * Must be used after having updated them.
965 * Always succeeds.
966 */
967static void quic_tls_rotate_keys(struct quic_conn *qc)
968{
969 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
970 unsigned char *curr_secret, *curr_iv, *curr_key;
971 EVP_CIPHER_CTX *curr_ctx;
972
973 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
974
975 /* Rotate the RX secrets */
976 curr_ctx = tls_ctx->rx.ctx;
977 curr_secret = tls_ctx->rx.secret;
978 curr_iv = tls_ctx->rx.iv;
979 curr_key = tls_ctx->rx.key;
980
981 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
982 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
983 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
984 tls_ctx->rx.key = qc->ku.nxt_rx.key;
985
986 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
987 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
988 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
989 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
990
991 qc->ku.prv_rx.ctx = curr_ctx;
992 qc->ku.prv_rx.secret = curr_secret;
993 qc->ku.prv_rx.iv = curr_iv;
994 qc->ku.prv_rx.key = curr_key;
995 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
996
997 /* Update the TX secrets */
998 curr_ctx = tls_ctx->tx.ctx;
999 curr_secret = tls_ctx->tx.secret;
1000 curr_iv = tls_ctx->tx.iv;
1001 curr_key = tls_ctx->tx.key;
1002
1003 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
1004 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
1005 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
1006 tls_ctx->tx.key = qc->ku.nxt_tx.key;
1007
1008 qc->ku.nxt_tx.ctx = curr_ctx;
1009 qc->ku.nxt_tx.secret = curr_secret;
1010 qc->ku.nxt_tx.iv = curr_iv;
1011 qc->ku.nxt_tx.key = curr_key;
1012
1013 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1014}
1015
1016/* returns 0 on error, 1 on success */
1017int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
1018 const uint8_t *read_secret,
1019 const uint8_t *write_secret, size_t secret_len)
1020{
1021 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1022 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
1023 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001024 struct quic_tls_secrets *rx = NULL, *tx = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001025 const struct quic_version *ver =
1026 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
1027 int ret = 0;
1028
1029 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
1030 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001031
1032 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1033 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1034 goto out;
1035 }
1036
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001037 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1038 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001039 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001040 }
1041
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001042 if (!read_secret)
1043 goto write;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001044
1045 rx = &tls_ctx->rx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001046 if (!quic_tls_secrets_keys_alloc(rx)) {
1047 TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1048 goto leave;
1049 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001050
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001051 rx->aead = tls_aead(cipher);
1052 rx->md = tls_md(cipher);
1053 rx->hp = tls_hp(cipher);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001054
1055 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen,
1056 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
1057 read_secret, secret_len)) {
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001058 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001059 goto leave;
1060 }
1061
1062 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
1063 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1064 goto leave;
1065 }
1066
1067 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
1068 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1069 goto leave;
1070 }
1071
1072 /* Enqueue this connection asap if we could derive O-RTT secrets as
1073 * listener. Note that a listener derives only RX secrets for this
1074 * level.
1075 */
1076 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
1077 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
1078 quic_accept_push_qc(qc);
1079 }
1080
1081write:
1082
1083 if (!write_secret)
1084 goto out;
1085
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001086 tx = &tls_ctx->tx;
1087 if (!quic_tls_secrets_keys_alloc(tx)) {
1088 TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1089 goto leave;
1090 }
1091
1092 tx->aead = tls_aead(cipher);
1093 tx->md = tls_md(cipher);
1094 tx->hp = tls_hp(cipher);
1095
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001096 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen,
1097 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
1098 write_secret, secret_len)) {
1099 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
1100 goto leave;
1101 }
1102
1103 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
1104 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1105 goto leave;
1106 }
1107
1108 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
1109 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1110 goto leave;
1111 }
1112
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01001113 if (level == ssl_encryption_handshake && qc_is_listener(qc)) {
1114 qc->enc_params_len =
1115 quic_transport_params_encode(qc->enc_params,
1116 qc->enc_params + sizeof qc->enc_params,
1117 &qc->rx.params, ver, 1);
1118 if (!qc->enc_params_len) {
1119 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC);
1120 goto leave;
1121 }
1122
1123 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
1124 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC);
1125 goto leave;
1126 }
1127 }
1128
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001129 if (level == ssl_encryption_application) {
1130 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
1131 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
1132 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
1133
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001134 if (rx) {
1135 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1136 TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1137 goto leave;
1138 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001139
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001140 memcpy(rx->secret, read_secret, secret_len);
1141 rx->secretlen = secret_len;
1142 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001143
1144 if (tx) {
1145 if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1146 TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1147 goto leave;
1148 }
1149
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001150 memcpy(tx->secret, write_secret, secret_len);
1151 tx->secretlen = secret_len;
1152 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001153
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001154 /* Initialize all the secret keys lengths */
1155 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001156 }
1157
1158 out:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001159 ret = 1;
1160 leave:
1161 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
1162 return ret;
1163}
1164
1165/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1166 * with <len> as size in CRYPTO buffers dedicated to store the information about
1167 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1168 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
1169 * buffers to store all the data.
1170 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1171 */
1172static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
1173 const unsigned char *data, size_t len)
1174{
1175 struct quic_crypto_buf **qcb;
1176 /* The remaining byte to store in CRYPTO buffers. */
1177 size_t cf_offset, cf_len, *nb_buf;
1178 unsigned char *pos;
1179 int ret = 0;
1180
1181 nb_buf = &qel->tx.crypto.nb_buf;
1182 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1183 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1184 cf_len = len;
1185
1186 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1187
1188 while (len) {
1189 size_t to_copy, room;
1190
1191 pos = (*qcb)->data + (*qcb)->sz;
1192 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1193 to_copy = len > room ? room : len;
1194 if (to_copy) {
1195 memcpy(pos, data, to_copy);
1196 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1197 qel->tx.crypto.sz += to_copy;
1198 (*qcb)->sz += to_copy;
1199 len -= to_copy;
1200 data += to_copy;
1201 }
1202 else {
1203 struct quic_crypto_buf **tmp;
1204
1205 // FIXME: realloc!
1206 tmp = realloc(qel->tx.crypto.bufs,
1207 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1208 if (tmp) {
1209 qel->tx.crypto.bufs = tmp;
1210 qcb = &qel->tx.crypto.bufs[*nb_buf];
1211 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1212 if (!*qcb) {
1213 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1214 goto leave;
1215 }
1216
1217 (*qcb)->sz = 0;
1218 ++*nb_buf;
1219 }
1220 else {
1221 break;
1222 }
1223 }
1224 }
1225
1226 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1227 * have been buffered.
1228 */
1229 if (!len) {
1230 struct quic_frame *frm;
1231 struct quic_frame *found = NULL;
1232
1233 /* There is at most one CRYPTO frame in this packet number
1234 * space. Let's look for it.
1235 */
1236 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
1237 if (frm->type != QUIC_FT_CRYPTO)
1238 continue;
1239
1240 /* Found */
1241 found = frm;
1242 break;
1243 }
1244
1245 if (found) {
1246 found->crypto.len += cf_len;
1247 }
1248 else {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001249 frm = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001250 if (!frm) {
1251 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1252 goto leave;
1253 }
1254
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001255 frm->crypto.offset = cf_offset;
1256 frm->crypto.len = cf_len;
1257 frm->crypto.qel = qel;
1258 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
1259 }
1260 }
1261 ret = len == 0;
1262 leave:
1263 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1264 return ret;
1265}
1266
1267/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1268 * activity for <qc> will be interrupted.
1269 */
1270void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
1271{
1272 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1273 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
1274 goto leave;
1275
1276 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
1277 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
1278 qc->err.code = err.code;
1279 qc->err.app = err.app;
1280 leave:
1281 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1282}
1283
1284/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1285void quic_set_tls_alert(struct quic_conn *qc, int alert)
1286{
1287 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1288
1289 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1290 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
1291 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
1292 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1293 }
1294 quic_set_connection_close(qc, quic_err_tls(alert));
1295 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
1296 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1297
1298 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1299}
1300
1301/* Set the application for <qc> QUIC connection.
1302 * Return 1 if succeeded, 0 if not.
1303 */
1304int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1305{
1306 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1307 qc->app_ops = &h3_ops;
1308 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1309 qc->app_ops = &hq_interop_ops;
1310 else
1311 return 0;
1312
1313 return 1;
1314}
1315
1316/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1317 * wants to provide the QUIC layer with CRYPTO data.
1318 * Returns 1 if succeeded, 0 if not.
1319 */
1320int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1321 const uint8_t *data, size_t len)
1322{
1323 struct quic_conn *qc;
1324 enum quic_tls_enc_level tel;
1325 struct quic_enc_level *qel;
1326 int ret = 0;
1327
1328 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1329 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1330
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001331 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1332 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1333 goto out;
1334 }
1335
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001336 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1337 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
1338 goto out;
1339 }
1340
1341 tel = ssl_to_quic_enc_level(level);
1342 if (tel == -1) {
1343 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1344 goto leave;
1345 }
1346
1347 qel = &qc->els[tel];
1348 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1349 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1350 goto leave;
1351 }
1352
1353 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
1354 qc, &level, &len);
1355 out:
1356 ret = 1;
1357 leave:
1358 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1359 return ret;
1360}
1361
1362int ha_quic_flush_flight(SSL *ssl)
1363{
1364 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1365
1366 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1367 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
1368
1369 return 1;
1370}
1371
1372int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1373{
1374 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1375
1376 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1377
1378 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1379
1380 quic_set_tls_alert(qc, alert);
1381 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1382 return 1;
1383}
1384
1385/* QUIC TLS methods */
1386static SSL_QUIC_METHOD ha_quic_method = {
1387 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1388 .add_handshake_data = ha_quic_add_handshake_data,
1389 .flush_flight = ha_quic_flush_flight,
1390 .send_alert = ha_quic_send_alert,
1391};
1392
1393/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1394 * Returns an error count.
1395 */
1396int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1397{
1398 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1399 int cfgerr = 0;
1400
1401 long options =
1402 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1403 SSL_OP_SINGLE_ECDH_USE |
1404 SSL_OP_CIPHER_SERVER_PREFERENCE;
1405 SSL_CTX *ctx;
1406
1407 ctx = SSL_CTX_new(TLS_server_method());
1408 bind_conf->initial_ctx = ctx;
1409
1410 SSL_CTX_set_options(ctx, options);
1411 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1412 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1413 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1414
1415#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1416# if defined(HAVE_SSL_CLIENT_HELLO_CB)
1417# if defined(SSL_OP_NO_ANTI_REPLAY)
1418 if (bind_conf->ssl_conf.early_data) {
1419 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
1420 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
1421 }
1422# endif /* !SSL_OP_NO_ANTI_REPLAY */
1423 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1424 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1425# else /* ! HAVE_SSL_CLIENT_HELLO_CB */
1426 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1427# endif
1428 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1429#endif
1430 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1431
1432 return cfgerr;
1433}
1434
1435/* Decode an expected packet number from <truncated_on> its truncated value,
1436 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1437 * the number of bits used to encode this packet number (its length in bytes * 8).
1438 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1439 */
1440static uint64_t decode_packet_number(uint64_t largest_pn,
1441 uint32_t truncated_pn, unsigned int pn_nbits)
1442{
1443 uint64_t expected_pn = largest_pn + 1;
1444 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1445 uint64_t pn_hwin = pn_win / 2;
1446 uint64_t pn_mask = pn_win - 1;
1447 uint64_t candidate_pn;
1448
1449
1450 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1451 /* Note that <pn_win> > <pn_hwin>. */
1452 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1453 candidate_pn + pn_hwin <= expected_pn)
1454 return candidate_pn + pn_win;
1455
1456 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1457 return candidate_pn - pn_win;
1458
1459 return candidate_pn;
1460}
1461
1462/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1463 * cryptographic context.
1464 * <largest_pn> is the largest received packet number and <pn> the address of
1465 * the packet number field for this packet with <byte0> address of its first byte.
1466 * <end> points to one byte past the end of this packet.
1467 * Returns 1 if succeeded, 0 if not.
1468 */
1469static int qc_do_rm_hp(struct quic_conn *qc,
1470 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1471 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
1472{
1473 int ret, i, pnlen;
1474 uint64_t packet_number;
1475 uint32_t truncated_pn = 0;
1476 unsigned char mask[5] = {0};
1477 unsigned char *sample;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001478
1479 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1480
1481 ret = 0;
1482
1483 /* Check there is enough data in this packet. */
1484 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1485 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1486 goto leave;
1487 }
1488
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001489 sample = pn + QUIC_PACKET_PN_MAXLEN;
1490
1491 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1492 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
1493 goto leave;
1494 }
1495
1496 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1497 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1498 for (i = 0; i < pnlen; i++) {
1499 pn[i] ^= mask[i + 1];
1500 truncated_pn = (truncated_pn << 8) | pn[i];
1501 }
1502
1503 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1504 /* Store remaining information for this unprotected header */
1505 pkt->pn = packet_number;
1506 pkt->pnl = pnlen;
1507
1508 ret = 1;
1509 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001510 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
1511 return ret;
1512}
1513
1514/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1515 * address, with <payload_len> as payload length, <aad> as address of
1516 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1517 * context.
1518 * Returns 1 if succeeded, 0 if not.
1519 */
1520static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1521 unsigned char *aad, size_t aad_len, uint64_t pn,
1522 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
1523{
1524 int ret = 0;
1525 unsigned char iv[QUIC_TLS_IV_LEN];
1526 unsigned char *tx_iv = tls_ctx->tx.iv;
1527 size_t tx_iv_sz = tls_ctx->tx.ivlen;
1528 struct enc_debug_info edi;
1529
1530 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1531
1532 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1533 TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc);
1534 goto err;
1535 }
1536
1537 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1538 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1539 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
1540 goto err;
1541 }
1542
1543 ret = 1;
1544 leave:
1545 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1546 return ret;
1547
1548 err:
1549 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1550 goto leave;
1551}
1552
Frédéric Lécaille72027782023-02-22 16:20:09 +01001553/* Select the correct TLS cipher context to used to decipher <pkt> packet
1554 * attached to <qc> connection from <qel> encryption level.
1555 */
1556static inline struct quic_tls_ctx *qc_select_tls_ctx(struct quic_conn *qc,
1557 struct quic_enc_level *qel,
1558 struct quic_rx_packet *pkt)
1559{
1560 return pkt->type != QUIC_PACKET_TYPE_INITIAL ? &qel->tls_ctx :
1561 pkt->version == qc->negotiated_version ? &qc->negotiated_ictx : &qel->tls_ctx;
1562}
1563
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001564/* Decrypt <pkt> packet using encryption level <qel> for <qc> connection.
1565 * Decryption is done in place in packet buffer.
1566 *
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +05001567 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001568 */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001569static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel,
1570 struct quic_rx_packet *pkt)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001571{
1572 int ret, kp_changed;
1573 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaille72027782023-02-22 16:20:09 +01001574 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001575 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
1576 unsigned char *rx_iv = tls_ctx->rx.iv;
1577 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1578 unsigned char *rx_key = tls_ctx->rx.key;
1579
1580 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1581
1582 ret = 0;
1583 kp_changed = 0;
1584
1585 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1586 /* The two tested bits are not at the same position,
1587 * this is why they are first both inversed.
1588 */
1589 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1590 if (pkt->pn < tls_ctx->rx.pn) {
1591 /* The lowest packet number of a previous key phase
1592 * cannot be null if it really stores previous key phase
1593 * secrets.
1594 */
1595 // TODO: check if BUG_ON() more suitable
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001596 if (!qc->ku.prv_rx.pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001597 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1598 goto leave;
1599 }
1600
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001601 rx_ctx = qc->ku.prv_rx.ctx;
1602 rx_iv = qc->ku.prv_rx.iv;
1603 rx_key = qc->ku.prv_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001604 }
1605 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1606 /* Next key phase */
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +01001607 TRACE_PROTO("Key phase changed", QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001608 kp_changed = 1;
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001609 rx_ctx = qc->ku.nxt_rx.ctx;
1610 rx_iv = qc->ku.nxt_rx.iv;
1611 rx_key = qc->ku.nxt_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001612 }
1613 }
1614 }
1615
1616 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1617 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1618 goto leave;
1619 }
1620
1621 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1622 pkt->data, pkt->aad_len,
1623 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
1624 if (!ret) {
1625 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1626 goto leave;
1627 }
1628
1629 /* Update the keys only if the packet decryption succeeded. */
1630 if (kp_changed) {
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001631 quic_tls_rotate_keys(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001632 /* Toggle the Key Phase bit */
1633 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1634 /* Store the lowest packet number received for the current key phase */
1635 tls_ctx->rx.pn = pkt->pn;
1636 /* Prepare the next key update */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001637 if (!quic_tls_key_update(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001638 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1639 goto leave;
1640 }
1641 }
1642
1643 /* Update the packet length (required to parse the frames). */
1644 pkt->len -= QUIC_TLS_TAG_LEN;
1645 ret = 1;
1646 leave:
1647 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1648 return ret;
1649}
1650
1651
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001652/* Release <frm> frame and mark its copies as acknowledged */
1653void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
1654{
1655 uint64_t pn;
1656 struct quic_frame *origin, *f, *tmp;
1657
1658 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1659
1660 /* Identify this frame: a frame copy or one of its copies */
1661 origin = frm->origin ? frm->origin : frm;
1662 /* Ensure the source of the copies is flagged as acked, <frm> being
1663 * possibly a copy of <origin>
1664 */
1665 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1666 /* Mark all the copy of <origin> as acknowledged. We must
1667 * not release the packets (releasing the frames) at this time as
1668 * they are possibly also to be acknowledged alongside the
1669 * the current one.
1670 */
1671 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1672 if (f->pkt) {
1673 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1674 f->origin = NULL;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001675 LIST_DEL_INIT(&f->ref);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001676 pn = f->pkt->pn_node.key;
1677 TRACE_DEVEL("mark frame as acked from packet",
1678 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1679 }
1680 else {
1681 TRACE_DEVEL("freeing unsent frame",
1682 QUIC_EV_CONN_PRSAFRM, qc, f);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001683 LIST_DEL_INIT(&f->ref);
1684 qc_frm_free(&f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001685 }
1686 }
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001687 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001688 pn = frm->pkt->pn_node.key;
1689 quic_tx_packet_refdec(frm->pkt);
1690 TRACE_DEVEL("freeing frame from packet",
1691 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001692 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001693
1694 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1695}
1696
1697/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1698 * and all STREAM data are acknowledged. The MUX is responsible to have set
1699 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1700 *
1701 * TODO this should also be called on lost packet detection
1702 */
1703void qc_check_close_on_released_mux(struct quic_conn *qc)
1704{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001705 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1706
1707 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1708 /* Reuse errcode which should have been previously set by the MUX on release. */
1709 quic_set_connection_close(qc, qc->err);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02001710 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001711 }
1712
1713 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1714}
1715
1716/* Remove from <stream> the acknowledged frames.
1717 *
1718 * Returns 1 if at least one frame was removed else 0.
1719 */
1720static int quic_stream_try_to_consume(struct quic_conn *qc,
1721 struct qc_stream_desc *stream)
1722{
1723 int ret;
1724 struct eb64_node *frm_node;
1725
1726 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1727
1728 ret = 0;
1729 frm_node = eb64_first(&stream->acked_frms);
1730 while (frm_node) {
1731 struct quic_stream *strm;
1732 struct quic_frame *frm;
1733 size_t offset, len;
1734
1735 strm = eb64_entry(frm_node, struct quic_stream, offset);
1736 offset = strm->offset.key;
1737 len = strm->len;
1738
1739 if (offset > stream->ack_offset)
1740 break;
1741
1742 if (qc_stream_desc_ack(&stream, offset, len)) {
1743 /* cf. next comment : frame may be freed at this stage. */
1744 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1745 qc, stream ? strm : NULL, stream);
1746 ret = 1;
1747 }
1748
1749 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1750 * has been freed. with the stream frames tree. Nothing to do
1751 * anymore in here.
1752 */
1753 if (!stream) {
1754 qc_check_close_on_released_mux(qc);
1755 ret = 1;
1756 goto leave;
1757 }
1758
1759 frm_node = eb64_next(frm_node);
1760 eb64_delete(&strm->offset);
1761
1762 frm = container_of(strm, struct quic_frame, stream);
1763 qc_release_frm(qc, frm);
1764 }
1765
1766 leave:
1767 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
1768 return ret;
1769}
1770
1771/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1772static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1773 struct quic_frame *frm)
1774{
Frédéric Lécaille8f991942023-03-24 15:14:45 +01001775 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1776 TRACE_PROTO("RX ack TX frm", QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001777
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001778 switch (frm->type) {
1779 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1780 {
1781 struct quic_stream *strm_frm = &frm->stream;
1782 struct eb64_node *node = NULL;
1783 struct qc_stream_desc *stream = NULL;
1784 const size_t offset = strm_frm->offset.key;
1785 const size_t len = strm_frm->len;
1786
1787 /* do not use strm_frm->stream as the qc_stream_desc instance
1788 * might be freed at this stage. Use the id to do a proper
1789 * lookup.
1790 *
1791 * TODO if lookup operation impact on the perf is noticeable,
1792 * implement a refcount on qc_stream_desc instances.
1793 */
1794 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1795 if (!node) {
1796 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
1797 qc_release_frm(qc, frm);
1798 /* early return */
1799 goto leave;
1800 }
1801 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1802
1803 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
1804 if (offset <= stream->ack_offset) {
1805 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001806 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1807 qc, strm_frm, stream);
1808 }
1809
1810 if (!stream) {
1811 /* no need to continue if stream freed. */
1812 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
1813 qc_release_frm(qc, frm);
1814 qc_check_close_on_released_mux(qc);
1815 break;
1816 }
1817
1818 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1819 qc, strm_frm, stream);
1820 qc_release_frm(qc, frm);
1821 }
1822 else {
1823 eb64_insert(&stream->acked_frms, &strm_frm->offset);
1824 }
1825
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01001826 quic_stream_try_to_consume(qc, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001827 }
1828 break;
1829 default:
1830 qc_release_frm(qc, frm);
1831 }
1832
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001833 leave:
1834 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1835}
1836
1837/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1838 * deallocating them, and their TX frames.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001839 * May be NULL if <largest> node could not be found.
1840 */
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001841static inline void qc_ackrng_pkts(struct quic_conn *qc,
1842 struct eb_root *pkts,
1843 unsigned int *pkt_flags,
1844 struct list *newly_acked_pkts,
1845 struct eb64_node *largest_node,
1846 uint64_t largest, uint64_t smallest)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001847{
1848 struct eb64_node *node;
1849 struct quic_tx_packet *pkt;
1850
1851 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1852
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001853 node = eb64_lookup_ge(pkts, smallest);
1854 if (!node)
1855 goto leave;
1856
1857 largest_node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
1858 if (!largest_node)
1859 goto leave;
1860
1861 while (node && node->key <= largest_node->key) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001862 struct quic_frame *frm, *frmbak;
1863
1864 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
1865 *pkt_flags |= pkt->flags;
1866 LIST_INSERT(newly_acked_pkts, &pkt->list);
1867 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
1868 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1869 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaille814645f2022-11-18 18:15:28 +01001870 /* If there are others packet in the same datagram <pkt> is attached to,
1871 * detach the previous one and the next one from <pkt>.
1872 */
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01001873 quic_tx_packet_dgram_detach(pkt);
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001874 node = eb64_next(node);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001875 eb64_delete(&pkt->pn_node);
1876 }
1877
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001878 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001879 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001880}
1881
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001882/* Remove all frames from <pkt_frm_list> and reinsert them in the same order
1883 * they have been sent into <pktns_frm_list>. The loss counter of each frame is
1884 * incremented and checked if it does not exceed retransmission limit.
1885 *
1886 * Returns 1 on success, 0 if a frame loss limit is exceeded. A
1887 * CONNECTION_CLOSE is scheduled in this case.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001888 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001889static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1890 struct quic_tx_packet *pkt,
1891 struct list *pktns_frm_list)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001892{
1893 struct quic_frame *frm, *frmbak;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001894 struct list *pkt_frm_list = &pkt->frms;
1895 uint64_t pn = pkt->pn_node.key;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001896 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001897
1898 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1899
1900 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1901 /* First remove this frame from the packet it was attached to */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001902 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001903 quic_tx_packet_refdec(pkt);
1904 /* At this time, this frame is not freed but removed from its packet */
1905 frm->pkt = NULL;
1906 /* Remove any reference to this frame */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001907 qc_frm_unref(frm, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001908 switch (frm->type) {
1909 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1910 {
1911 struct quic_stream *strm_frm = &frm->stream;
1912 struct eb64_node *node = NULL;
1913 struct qc_stream_desc *stream_desc;
1914
1915 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1916 if (!node) {
1917 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1918 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1919 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001920 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001921 continue;
1922 }
1923
1924 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1925 /* Do not resend this frame if in the "already acked range" */
1926 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1927 TRACE_DEVEL("ignored frame in already acked range",
1928 QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001929 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001930 continue;
1931 }
1932 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01001933 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
1934
Frédéric Lécaillec425e032023-03-20 14:32:59 +01001935 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001936 TRACE_DEVEL("updated partially acked frame",
1937 QUIC_EV_CONN_PRSAFRM, qc, frm);
1938 }
1939 break;
1940 }
1941
1942 default:
1943 break;
1944 }
1945
1946 /* Do not resend probing packet with old data */
1947 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1948 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1949 qc, frm, &pn);
1950 if (frm->origin)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001951 LIST_DEL_INIT(&frm->ref);
1952 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001953 continue;
1954 }
1955
1956 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1957 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1958 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1959 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001960 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001961 }
1962 else {
Amaury Denoyelle24d5b722023-01-31 11:44:50 +01001963 if (++frm->loss_count >= global.tune.quic_max_frame_loss) {
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001964 TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc);
1965 quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR));
1966 close = 1;
1967 }
1968
Frédéric Lécaillebe795ce2023-03-08 18:23:13 +01001969 LIST_APPEND(pktns_frm_list, &frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001970 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
1971 }
1972 }
1973
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001974 end:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001975 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001976 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001977}
1978
1979/* Free <pkt> TX packet and its attached frames.
1980 * This is the responsibility of the caller to remove this packet of
1981 * any data structure it was possibly attached to.
1982 */
1983static inline void free_quic_tx_packet(struct quic_conn *qc,
1984 struct quic_tx_packet *pkt)
1985{
1986 struct quic_frame *frm, *frmbak;
1987
1988 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1989
1990 if (!pkt)
1991 goto leave;
1992
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001993 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1994 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001995 pool_free(pool_head_quic_tx_packet, pkt);
1996
1997 leave:
1998 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
1999}
2000
2001/* Free the TX packets of <pkts> list */
2002static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
2003{
2004 struct quic_tx_packet *pkt, *tmp;
2005
2006 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
2007
2008 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2009 LIST_DELETE(&pkt->list);
2010 eb64_delete(&pkt->pn_node);
2011 free_quic_tx_packet(qc, pkt);
2012 }
2013
2014 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
2015}
2016
2017/* Remove already sent ranges of acknowledged packet numbers from
2018 * <pktns> packet number space tree below <largest_acked_pn> possibly
2019 * updating the range which contains <largest_acked_pn>.
2020 * Never fails.
2021 */
2022static void qc_treat_ack_of_ack(struct quic_conn *qc,
2023 struct quic_pktns *pktns,
2024 int64_t largest_acked_pn)
2025{
2026 struct eb64_node *ar, *next_ar;
2027 struct quic_arngs *arngs = &pktns->rx.arngs;
2028
2029 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2030
2031 ar = eb64_first(&arngs->root);
2032 while (ar) {
2033 struct quic_arng_node *ar_node;
2034
2035 next_ar = eb64_next(ar);
2036 ar_node = eb64_entry(ar, struct quic_arng_node, first);
2037
2038 if ((int64_t)ar_node->first.key > largest_acked_pn) {
2039 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
2040 break;
2041 }
2042
2043 if (largest_acked_pn < ar_node->last) {
2044 eb64_delete(ar);
2045 ar_node->first.key = largest_acked_pn + 1;
2046 eb64_insert(&arngs->root, ar);
2047 break;
2048 }
2049
2050 eb64_delete(ar);
2051 pool_free(pool_head_quic_arng, ar_node);
2052 arngs->sz--;
2053 ar = next_ar;
2054 }
2055
2056 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2057}
2058
2059/* Send a packet ack event nofication for each newly acked packet of
2060 * <newly_acked_pkts> list and free them.
2061 * Always succeeds.
2062 */
2063static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
2064 struct list *newly_acked_pkts)
2065{
2066 struct quic_tx_packet *pkt, *tmp;
2067 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
2068
2069 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2070
2071 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
2072 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2073 qc->path->prep_in_flight -= pkt->in_flight_len;
2074 qc->path->in_flight -= pkt->in_flight_len;
2075 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2076 qc->path->ifae_pkts--;
2077 /* If this packet contained an ACK frame, proceed to the
2078 * acknowledging of range of acks from the largest acknowledged
2079 * packet number which was sent in an ACK frame by this packet.
2080 */
2081 if (pkt->largest_acked_pn != -1)
2082 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
2083 ev.ack.acked = pkt->in_flight_len;
2084 ev.ack.time_sent = pkt->time_sent;
2085 quic_cc_event(&qc->path->cc, &ev);
2086 LIST_DELETE(&pkt->list);
2087 eb64_delete(&pkt->pn_node);
2088 quic_tx_packet_refdec(pkt);
2089 }
2090
2091 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2092
2093}
2094
2095/* Release all the frames attached to <pktns> packet number space */
2096static inline void qc_release_pktns_frms(struct quic_conn *qc,
2097 struct quic_pktns *pktns)
2098{
2099 struct quic_frame *frm, *frmbak;
2100
2101 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2102
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002103 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list)
2104 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002105
2106 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2107}
2108
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002109/* Handle <pkts> list of lost packets detected at <now_us> handling their TX
2110 * frames. Send a packet loss event to the congestion controller if in flight
2111 * packet have been lost. Also frees the packet in <pkts> list.
2112 *
2113 * Returns 1 on success else 0 if loss limit has been exceeded. A
2114 * CONNECTION_CLOSE was prepared to close the connection ASAP.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002115 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002116static inline int qc_release_lost_pkts(struct quic_conn *qc,
2117 struct quic_pktns *pktns,
2118 struct list *pkts,
2119 uint64_t now_us)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002120{
2121 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002122 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002123
2124 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2125
2126 if (LIST_ISEMPTY(pkts))
2127 goto leave;
2128
2129 oldest_lost = newest_lost = NULL;
2130 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2131 struct list tmp = LIST_HEAD_INIT(tmp);
2132
2133 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2134 qc->path->prep_in_flight -= pkt->in_flight_len;
2135 qc->path->in_flight -= pkt->in_flight_len;
2136 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2137 qc->path->ifae_pkts--;
2138 /* Treat the frames of this lost packet. */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002139 if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms))
2140 close = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002141 LIST_DELETE(&pkt->list);
2142 if (!oldest_lost) {
2143 oldest_lost = newest_lost = pkt;
2144 }
2145 else {
2146 if (newest_lost != oldest_lost)
2147 quic_tx_packet_refdec(newest_lost);
2148 newest_lost = pkt;
2149 }
2150 }
2151
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002152 if (!close) {
2153 if (newest_lost) {
2154 /* Sent a congestion event to the controller */
2155 struct quic_cc_event ev = { };
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002156
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002157 ev.type = QUIC_CC_EVT_LOSS;
2158 ev.loss.time_sent = newest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002159
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002160 quic_cc_event(&qc->path->cc, &ev);
2161 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002162
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002163 /* If an RTT have been already sampled, <rtt_min> has been set.
2164 * We must check if we are experiencing a persistent congestion.
2165 * If this is the case, the congestion controller must re-enter
2166 * slow start state.
2167 */
2168 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2169 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002170
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002171 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2172 now_ms, qc->max_ack_delay))
2173 qc->path->cc.algo->slow_start(&qc->path->cc);
2174 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002175 }
2176
Amaury Denoyelle3a72ba22022-11-14 11:41:34 +01002177 /* <oldest_lost> cannot be NULL at this stage because we have ensured
2178 * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a
2179 * possible overflow on a 0 byte region with O2 optimization.
2180 */
2181 ALREADY_CHECKED(oldest_lost);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002182 quic_tx_packet_refdec(oldest_lost);
2183 if (newest_lost != oldest_lost)
2184 quic_tx_packet_refdec(newest_lost);
2185
2186 leave:
2187 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002188 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002189}
2190
2191/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2192 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
2193 * if the largest acked packet was newly acked and if there was at least one newly
2194 * acked ack-eliciting packet.
2195 * Return 1, if succeeded, 0 if not.
2196 */
2197static inline int qc_parse_ack_frm(struct quic_conn *qc,
2198 struct quic_frame *frm,
2199 struct quic_enc_level *qel,
2200 unsigned int *rtt_sample,
2201 const unsigned char **pos, const unsigned char *end)
2202{
2203 struct quic_ack *ack = &frm->ack;
2204 uint64_t smallest, largest;
2205 struct eb_root *pkts;
2206 struct eb64_node *largest_node;
2207 unsigned int time_sent, pkt_flags;
2208 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2209 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2210 int ret = 0;
2211
2212 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2213
2214 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2215 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
2216 qc, NULL, &ack->largest_ack);
2217 goto err;
2218 }
2219
2220 if (ack->first_ack_range > ack->largest_ack) {
2221 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
2222 qc, NULL, &ack->first_ack_range);
2223 goto err;
2224 }
2225
2226 largest = ack->largest_ack;
2227 smallest = largest - ack->first_ack_range;
2228 pkts = &qel->pktns->tx.pkts;
2229 pkt_flags = 0;
2230 largest_node = NULL;
2231 time_sent = 0;
2232
2233 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
2234 largest_node = eb64_lookup(pkts, largest);
2235 if (!largest_node) {
2236 TRACE_DEVEL("Largest acked packet not found",
2237 QUIC_EV_CONN_PRSAFRM, qc);
2238 }
2239 else {
2240 time_sent = eb64_entry(largest_node,
2241 struct quic_tx_packet, pn_node)->time_sent;
2242 }
2243 }
2244
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002245 TRACE_PROTO("RX ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002246 qc, NULL, &largest, &smallest);
2247 do {
2248 uint64_t gap, ack_range;
2249
2250 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2251 largest_node, largest, smallest);
2252 if (!ack->ack_range_num--)
2253 break;
2254
2255 if (!quic_dec_int(&gap, pos, end)) {
2256 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
2257 goto err;
2258 }
2259
2260 if (smallest < gap + 2) {
2261 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
2262 qc, NULL, &gap, &smallest);
2263 goto err;
2264 }
2265
2266 largest = smallest - gap - 2;
2267 if (!quic_dec_int(&ack_range, pos, end)) {
2268 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
2269 goto err;
2270 }
2271
2272 if (largest < ack_range) {
2273 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
2274 qc, NULL, &largest, &ack_range);
2275 goto err;
2276 }
2277
2278 /* Do not use this node anymore. */
2279 largest_node = NULL;
2280 /* Next range */
2281 smallest = largest - ack_range;
2282
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002283 TRACE_PROTO("RX next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002284 qc, NULL, &largest, &smallest);
2285 } while (1);
2286
2287 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2288 *rtt_sample = tick_remain(time_sent, now_ms);
2289 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
2290 }
2291
2292 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2293 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
2294 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002295 if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms))
2296 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002297 }
2298 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2299 if (quic_peer_validated_addr(qc))
2300 qc->path->loss.pto_count = 0;
2301 qc_set_timer(qc);
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01002302 qc_notify_send(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002303 }
2304
2305 ret = 1;
2306 leave:
2307 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2308 return ret;
2309
2310 err:
2311 free_quic_tx_pkts(qc, &newly_acked_pkts);
2312 goto leave;
2313}
2314
2315/* This function gives the detail of the SSL error. It is used only
2316 * if the debug mode and the verbose mode are activated. It dump all
2317 * the SSL error until the stack was empty.
2318 */
2319static forceinline void qc_ssl_dump_errors(struct connection *conn)
2320{
2321 if (unlikely(global.mode & MODE_DEBUG)) {
2322 while (1) {
2323 const char *func = NULL;
2324 unsigned long ret;
2325
2326 ERR_peek_error_func(&func);
2327 ret = ERR_get_error();
2328 if (!ret)
2329 return;
2330
2331 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
2332 func, ERR_reason_error_string(ret));
2333 }
2334 }
2335}
2336
2337int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2338 const char **str, int *len);
2339
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002340/* Finalize <qc> QUIC connection:
2341 * - initialize the Initial QUIC TLS context for negotiated version,
2342 * - derive the secrets for this context,
2343 * - set them into the TLS stack,
2344 *
2345 * MUST be called after having received the remote transport parameters which
2346 * are parsed when the TLS callback for the ClientHello message is called upon
2347 * SSL_do_handshake() calls, not necessarily at the first time as this TLS
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002348 * message may be split between packets
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002349 * Return 1 if succeeded, 0 if not.
2350 */
2351static int qc_conn_finalize(struct quic_conn *qc, int server)
2352{
2353 int ret = 0;
2354
2355 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
2356
2357 if (qc->flags & QUIC_FL_CONN_FINALIZED)
2358 goto finalized;
2359
2360 if (qc->negotiated_version &&
2361 !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
2362 qc->odcid.data, qc->odcid.len, server))
2363 goto out;
2364
2365 /* This connection is functional (ready to send/receive) */
2366 qc->flags |= QUIC_FL_CONN_FINALIZED;
2367
2368 finalized:
2369 ret = 1;
2370 out:
2371 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
2372 return ret;
2373}
2374
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002375/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2376 * from <qel> encryption level with <ctx> as QUIC connection context.
2377 * Remaining parameter are there for debugging purposes.
2378 * Return 1 if succeeded, 0 if not.
2379 */
2380static inline int qc_provide_cdata(struct quic_enc_level *el,
2381 struct ssl_sock_ctx *ctx,
2382 const unsigned char *data, size_t len,
2383 struct quic_rx_packet *pkt,
2384 struct quic_rx_crypto_frm *cf)
2385{
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002386#ifdef DEBUG_STRICT
2387 enum ncb_ret ncb_ret;
2388#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002389 int ssl_err, state;
2390 struct quic_conn *qc;
2391 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002392 struct ncbuf *ncbuf = &el->cstream->rx.ncbuf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002393
2394 ssl_err = SSL_ERROR_NONE;
2395 qc = ctx->qc;
2396
2397 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2398
2399 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2400 TRACE_ERROR("SSL_provide_quic_data() error",
2401 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
2402 goto leave;
2403 }
2404
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002405 TRACE_PROTO("in order CRYPTO data",
2406 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
2407
2408 state = qc->state;
2409 if (state < QUIC_HS_ST_COMPLETE) {
2410 ssl_err = SSL_do_handshake(ctx->ssl);
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002411
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01002412 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
2413 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
2414 goto leave;
2415 }
2416
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002417 /* Finalize the connection as soon as possible if the peer transport parameters
2418 * have been received. This may be useful to send packets even if this
2419 * handshake fails.
2420 */
2421 if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) {
2422 TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state);
2423 goto leave;
2424 }
2425
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002426 if (ssl_err != 1) {
2427 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2428 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2429 TRACE_PROTO("SSL handshake in progress",
2430 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2431 goto out;
2432 }
2433
2434 /* TODO: Should close the connection asap */
2435 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2436 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2437 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2438 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2439 }
2440 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2441 qc_ssl_dump_errors(ctx->conn);
2442 ERR_clear_error();
2443 goto leave;
2444 }
2445
2446 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
2447
2448 /* Check the alpn could be negotiated */
2449 if (!qc->app_ops) {
2450 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2451 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2452 goto leave;
2453 }
2454
2455 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2456 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
2457 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2458 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2459 }
2460 /* I/O callback switch */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02002461 qc->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002462 if (qc_is_listener(ctx->qc)) {
Amaury Denoyelle1304d192023-04-11 16:46:03 +02002463 qc->flags |= QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002464 qc->state = QUIC_HS_ST_CONFIRMED;
2465 /* The connection is ready to be accepted. */
2466 quic_accept_push_qc(qc);
2467 }
2468 else {
2469 qc->state = QUIC_HS_ST_COMPLETE;
2470 }
2471
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02002472 /* Prepare the next key update */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002473 if (!quic_tls_key_update(qc)) {
2474 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc);
2475 goto leave;
2476 }
2477 } else {
2478 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2479 if (ssl_err != 1) {
2480 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2481 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2482 TRACE_PROTO("SSL post handshake in progress",
2483 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2484 goto out;
2485 }
2486
2487 TRACE_ERROR("SSL post handshake error",
2488 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2489 goto leave;
2490 }
2491
2492 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
2493 }
2494
2495 out:
2496 ret = 1;
2497 leave:
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002498 /* The CRYPTO data are consumed even in case of an error to release
2499 * the memory asap.
2500 */
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002501 if (!ncb_is_null(ncbuf)) {
2502#ifdef DEBUG_STRICT
2503 ncb_ret = ncb_advance(ncbuf, len);
2504 /* ncb_advance() must always succeed. This is guaranteed as
2505 * this is only done inside a data block. If false, this will
2506 * lead to handshake failure with quic_enc_level offset shifted
2507 * from buffer data.
2508 */
2509 BUG_ON(ncb_ret != NCB_RET_OK);
2510#else
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002511 ncb_advance(ncbuf, len);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002512#endif
2513 }
2514
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002515 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
2516 return ret;
2517}
2518
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002519/* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc>
2520 * connection. <fin> is true if FIN bit is set on frame type.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002521 *
2522 * Return 1 on success. On error, 0 is returned. In this case, the packet
2523 * containing the frame must not be acknowledged.
2524 */
2525static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2526 struct quic_stream *strm_frm,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002527 struct quic_conn *qc, char fin)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002528{
2529 int ret;
2530
2531 /* RFC9000 13.1. Packet Processing
2532 *
2533 * A packet MUST NOT be acknowledged until packet protection has been
2534 * successfully removed and all frames contained in the packet have
2535 * been processed. For STREAM frames, this means the data has been
2536 * enqueued in preparation to be received by the application protocol,
2537 * but it does not require that data be delivered and consumed.
2538 */
2539 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2540
2541 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002542 strm_frm->offset.key, fin, (char *)strm_frm->data);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002543
2544 /* frame rejected - packet must not be acknowledeged */
2545 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2546 return !ret;
2547}
2548
2549/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2550 * for <qc> QUIC connection.
2551 * This is a best effort function which never fails even if no memory could be
2552 * allocated to duplicate these frames.
2553 */
2554static void qc_dup_pkt_frms(struct quic_conn *qc,
2555 struct list *pkt_frm_list, struct list *out_frm_list)
2556{
2557 struct quic_frame *frm, *frmbak;
2558 struct list tmp = LIST_HEAD_INIT(tmp);
2559
2560 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2561
2562 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2563 struct quic_frame *dup_frm, *origin;
2564
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002565 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
2566 TRACE_DEVEL("already acknowledged frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2567 continue;
2568 }
2569
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002570 switch (frm->type) {
2571 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2572 {
2573 struct quic_stream *strm_frm = &frm->stream;
2574 struct eb64_node *node = NULL;
2575 struct qc_stream_desc *stream_desc;
2576
2577 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2578 if (!node) {
2579 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
2580 continue;
2581 }
2582
2583 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2584 /* Do not resend this frame if in the "already acked range" */
2585 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2586 TRACE_DEVEL("ignored frame in already acked range",
2587 QUIC_EV_CONN_PRSAFRM, qc, frm);
2588 continue;
2589 }
2590 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01002591 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
2592
Frédéric Lécaillec425e032023-03-20 14:32:59 +01002593 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002594 TRACE_DEVEL("updated partially acked frame",
2595 QUIC_EV_CONN_PRSAFRM, qc, frm);
2596 }
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01002597
2598 strm_frm->dup = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002599 break;
2600 }
2601
2602 default:
2603 break;
2604 }
2605
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002606 /* If <frm> is already a copy of another frame, we must take
2607 * its original frame as source for the copy.
2608 */
2609 origin = frm->origin ? frm->origin : frm;
2610 dup_frm = qc_frm_dup(origin);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002611 if (!dup_frm) {
2612 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2613 break;
2614 }
2615
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002616 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002617 if (origin->pkt) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002618 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
2619 qc, NULL, &origin->pkt->pn_node.key);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002620 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002621 else {
2622 /* <origin> is a frame which was sent from a packet detected as lost. */
2623 TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
2624 }
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002625
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002626 LIST_APPEND(&tmp, &dup_frm->list);
2627 }
2628
2629 LIST_SPLICE(out_frm_list, &tmp);
2630
2631 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2632}
2633
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002634/* Boolean function which return 1 if <pkt> TX packet is only made of
2635 * already acknowledged frame.
2636 */
2637static inline int qc_pkt_with_only_acked_frms(struct quic_tx_packet *pkt)
2638{
2639 struct quic_frame *frm;
2640
2641 list_for_each_entry(frm, &pkt->frms, list)
2642 if (!(frm->flags & QUIC_FL_TX_FRAME_ACKED))
2643 return 0;
2644
2645 return 1;
2646}
2647
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002648/* Prepare a fast retransmission from <qel> encryption level */
2649static void qc_prep_fast_retrans(struct quic_conn *qc,
2650 struct quic_enc_level *qel,
2651 struct list *frms1, struct list *frms2)
2652{
2653 struct eb_root *pkts = &qel->pktns->tx.pkts;
2654 struct list *frms = frms1;
2655 struct eb64_node *node;
2656 struct quic_tx_packet *pkt;
2657
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002658 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002659
2660 BUG_ON(frms1 == frms2);
2661
2662 pkt = NULL;
2663 node = eb64_first(pkts);
2664 start:
2665 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002666 struct quic_tx_packet *p;
2667
2668 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002669 node = eb64_next(node);
2670 /* Skip the empty and coalesced packets */
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002671 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002672 "--> pn=%llu (%d %d %d)", (ull)p->pn_node.key,
2673 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED),
2674 qc_pkt_with_only_acked_frms(p));
2675 if (!LIST_ISEMPTY(&p->frms) && !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002676 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002677 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002678 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002679 }
2680
2681 if (!pkt)
2682 goto leave;
2683
2684 /* When building a packet from another one, the field which may increase the
2685 * packet size is the packet number. And the maximum increase is 4 bytes.
2686 */
2687 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2688 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002689 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002690 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2691 goto leave;
2692 }
2693
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002694 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002695 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2696 if (frms == frms1 && frms2) {
2697 frms = frms2;
2698 goto start;
2699 }
2700 leave:
2701 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
2702}
2703
2704/* Prepare a fast retransmission during a handshake after a client
2705 * has resent Initial packets. According to the RFC a server may retransmit
2706 * Initial packets send them coalescing with others (Handshake here).
2707 * (Listener only function).
2708 */
2709static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2710 struct list *ifrms, struct list *hfrms)
2711{
2712 struct list itmp = LIST_HEAD_INIT(itmp);
2713 struct list htmp = LIST_HEAD_INIT(htmp);
2714
2715 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2716 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2717 struct quic_enc_level *qel = iqel;
2718 struct eb_root *pkts;
2719 struct eb64_node *node;
2720 struct quic_tx_packet *pkt;
2721 struct list *tmp = &itmp;
2722
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002723 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002724 start:
2725 pkt = NULL;
2726 pkts = &qel->pktns->tx.pkts;
2727 node = eb64_first(pkts);
2728 /* Skip the empty packet (they have already been retransmitted) */
2729 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002730 struct quic_tx_packet *p;
2731
2732 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002733 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002734 "--> pn=%llu (%d %d)", (ull)p->pn_node.key,
2735 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED));
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002736 if (!LIST_ISEMPTY(&p->frms) && !(p->flags & QUIC_FL_TX_PACKET_COALESCED) &&
2737 !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002738 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002739 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002740 }
2741
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002742 node = eb64_next(node);
2743 }
2744
2745 if (!pkt)
2746 goto end;
2747
2748 /* When building a packet from another one, the field which may increase the
2749 * packet size is the packet number. And the maximum increase is 4 bytes.
2750 */
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002751 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2752 size_t dglen = pkt->len + 4;
2753
2754 dglen += pkt->next ? pkt->next->len + 4 : 0;
2755 if (dglen > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002756 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002757 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2758 if (pkt->next)
2759 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt->next);
2760 goto end;
2761 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002762 }
2763
2764 qel->pktns->tx.pto_probe += 1;
2765
2766 /* No risk to loop here, #packet per datagram is bounded */
2767 requeue:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002768 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002769 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
2770 if (qel == iqel) {
2771 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2772 pkt = pkt->next;
2773 tmp = &htmp;
2774 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002775 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002776 goto requeue;
2777 }
2778 }
2779
2780 end:
2781 LIST_SPLICE(ifrms, &itmp);
2782 LIST_SPLICE(hfrms, &htmp);
2783
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002784 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002785}
2786
2787static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
2788{
2789 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2790
2791 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2792 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2793 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2794 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
2795 goto out;
2796
2797 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2798 }
2799
2800 out:
2801 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
2802}
2803
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002804/* Cancel a request on connection <qc> for stream id <id>. This is useful when
2805 * the client opens a new stream but the MUX has already been released. A
Amaury Denoyelle75463012023-02-20 10:31:27 +01002806 * STOP_SENDING + RESET_STREAM frames are prepared for emission.
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002807 *
2808 * TODO this function is closely related to H3. Its place should be in H3 layer
2809 * instead of quic-conn but this requires an architecture adjustment.
2810 *
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002811 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002812 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002813static int qc_h3_request_reject(struct quic_conn *qc, uint64_t id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002814{
2815 int ret = 0;
Amaury Denoyelle75463012023-02-20 10:31:27 +01002816 struct quic_frame *ss, *rs;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002817 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002818 const uint64_t app_error_code = H3_REQUEST_REJECTED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002819
2820 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2821
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002822 /* Do not emit rejection for unknown unidirectional stream as it is
2823 * forbidden to close some of them (H3 control stream and QPACK
2824 * encoder/decoder streams).
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002825 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002826 if (quic_stream_is_uni(id)) {
2827 ret = 1;
2828 goto out;
2829 }
2830
Amaury Denoyelle75463012023-02-20 10:31:27 +01002831 ss = qc_frm_alloc(QUIC_FT_STOP_SENDING);
2832 if (!ss) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002833 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2834 goto out;
2835 }
2836
Amaury Denoyelle75463012023-02-20 10:31:27 +01002837 ss->stop_sending.id = id;
2838 ss->stop_sending.app_error_code = app_error_code;
2839
2840 rs = qc_frm_alloc(QUIC_FT_RESET_STREAM);
2841 if (!rs) {
2842 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2843 qc_frm_free(&ss);
2844 goto out;
2845 }
2846
2847 rs->reset_stream.id = id;
2848 rs->reset_stream.app_error_code = app_error_code;
2849 rs->reset_stream.final_size = 0;
2850
2851 LIST_APPEND(&qel->pktns->tx.frms, &ss->list);
2852 LIST_APPEND(&qel->pktns->tx.frms, &rs->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002853 ret = 1;
2854 out:
2855 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2856 return ret;
2857}
2858
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002859/* Release the underlying memory use by <ncbuf> non-contiguous buffer */
2860static void quic_free_ncbuf(struct ncbuf *ncbuf)
2861{
2862 struct buffer buf;
2863
2864 if (ncb_is_null(ncbuf))
2865 return;
2866
2867 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
2868 b_free(&buf);
2869 offer_buffers(NULL, 1);
2870
2871 *ncbuf = NCBUF_NULL;
2872}
2873
2874/* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */
2875static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf)
2876{
2877 struct buffer buf = BUF_NULL;
2878
2879 if (!ncb_is_null(ncbuf))
2880 return ncbuf;
2881
2882 b_alloc(&buf);
2883 BUG_ON(b_is_null(&buf));
2884
2885 *ncbuf = ncb_make(buf.area, buf.size, 0);
2886 ncb_init(ncbuf, 0);
2887
2888 return ncbuf;
2889}
2890
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002891/* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn.
2892 * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the
2893 * speed up handshake completion may be run after having received duplicated
2894 * CRYPTO data.
2895 */
2896static int qc_handle_crypto_frm(struct quic_conn *qc,
2897 struct quic_crypto *frm, struct quic_rx_packet *pkt,
2898 struct quic_enc_level *qel, int *fast_retrans)
2899{
2900 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002901 enum ncb_ret ncb_ret;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002902 /* XXX TO DO: <cfdebug> is used only for the traces. */
2903 struct quic_rx_crypto_frm cfdebug = {
2904 .offset_node.key = frm->offset,
2905 .len = frm->len,
2906 };
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002907 struct quic_cstream *cstream = qel->cstream;
2908 struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002909
2910 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2911 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2912 TRACE_PROTO("CRYPTO data discarded",
2913 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2914 goto done;
2915 }
2916
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002917 if (unlikely(frm->offset < cstream->rx.offset)) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002918 size_t diff;
2919
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002920 if (frm->offset + frm->len <= cstream->rx.offset) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002921 /* Nothing to do */
2922 TRACE_PROTO("Already received CRYPTO data",
2923 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2924 if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
2925 !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP))
2926 *fast_retrans = 1;
2927 goto done;
2928 }
2929
2930 TRACE_PROTO("Partially already received CRYPTO data",
2931 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2932
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002933 diff = cstream->rx.offset - frm->offset;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002934 frm->len -= diff;
2935 frm->data += diff;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002936 frm->offset = cstream->rx.offset;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002937 }
2938
Amaury Denoyelleff95f2d2022-11-18 14:50:06 +01002939 if (frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002940 if (!qc_provide_cdata(qel, qc->xprt_ctx, frm->data, frm->len,
2941 pkt, &cfdebug)) {
2942 // trace already emitted by function above
2943 goto leave;
2944 }
2945
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002946 cstream->rx.offset += frm->len;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002947 TRACE_DEVEL("increment crypto level offset", QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002948 goto done;
2949 }
2950
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002951 if (!quic_get_ncbuf(ncbuf) ||
2952 ncb_is_null(ncbuf)) {
2953 TRACE_ERROR("CRYPTO ncbuf allocation failed", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002954 goto leave;
2955 }
2956
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002957 /* frm->offset > cstream-trx.offset */
2958 ncb_ret = ncb_add(ncbuf, frm->offset - cstream->rx.offset,
2959 (const char *)frm->data, frm->len, NCB_ADD_COMPARE);
2960 if (ncb_ret != NCB_RET_OK) {
2961 if (ncb_ret == NCB_RET_DATA_REJ) {
2962 TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc);
2963 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
2964 }
2965 else if (ncb_ret == NCB_RET_GAP_SIZE) {
2966 TRACE_ERROR("cannot bufferize frame due to gap size limit",
2967 QUIC_EV_CONN_PRSHPKT, qc);
2968 }
2969 goto leave;
2970 }
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002971
2972 done:
2973 ret = 1;
2974 leave:
2975 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2976 return ret;
2977}
2978
Amaury Denoyelle591e7982023-04-12 10:04:49 +02002979/* Build a NEW_CONNECTION_ID frame for <conn_id> CID of <qc> connection.
2980 *
2981 * Returns 1 on success else 0.
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01002982 */
2983static int qc_build_new_connection_id_frm(struct quic_conn *qc,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02002984 struct quic_connection_id *conn_id)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01002985{
2986 int ret = 0;
2987 struct quic_frame *frm;
2988 struct quic_enc_level *qel;
2989
2990 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2991
2992 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2993 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
2994 if (!frm) {
2995 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
2996 goto leave;
2997 }
2998
Amaury Denoyelle591e7982023-04-12 10:04:49 +02002999 quic_connection_id_to_frm_cpy(frm, conn_id);
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003000 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
3001 ret = 1;
3002 leave:
3003 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3004 return ret;
3005}
3006
3007
3008/* Handle RETIRE_CONNECTION_ID frame from <frm> frame.
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003009 * Return 1 if succeeded, 0 if not. If succeeded, also set <to_retire>
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003010 * to the CID to be retired if not already retired.
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003011 */
3012static int qc_handle_retire_connection_id_frm(struct quic_conn *qc,
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003013 struct quic_frame *frm,
3014 struct quic_cid *dcid,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003015 struct quic_connection_id **to_retire)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003016{
3017 int ret = 0;
3018 struct quic_retire_connection_id *rcid = &frm->retire_connection_id;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003019 struct eb64_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003020 struct quic_connection_id *conn_id;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003021
3022 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3023
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003024 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3025 * Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number greater
3026 * than any previously sent to the peer MUST be treated as a connection error
3027 * of type PROTOCOL_VIOLATION.
3028 */
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003029 if (rcid->seq_num >= qc->next_cid_seq_num) {
3030 TRACE_PROTO("CID seq. number too big", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003031 goto protocol_violation;
3032 }
3033
3034 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3035 * The sequence number specified in a RETIRE_CONNECTION_ID frame MUST NOT refer to
3036 * the Destination Connection ID field of the packet in which the frame is contained.
3037 * The peer MAY treat this as a connection error of type PROTOCOL_VIOLATION.
3038 */
3039 node = eb64_lookup(&qc->cids, rcid->seq_num);
3040 if (!node) {
3041 TRACE_PROTO("CID already retired", QUIC_EV_CONN_PSTRM, qc, frm);
3042 goto out;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003043 }
3044
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003045 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003046 /* Note that the length of <dcid> has already been checked. It must match the
3047 * length of the CIDs which have been provided to the peer.
3048 */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003049 if (!memcmp(dcid->data, conn_id->cid.data, QUIC_HAP_CID_LEN)) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003050 TRACE_PROTO("cannot retire the current CID", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003051 goto protocol_violation;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003052 }
3053
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003054 *to_retire = conn_id;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003055 out:
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003056 ret = 1;
3057 leave:
3058 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3059 return ret;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003060 protocol_violation:
3061 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
3062 goto leave;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003063}
3064
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003065/* Remove a <qc> quic-conn from its ha_thread_ctx list. If <closing> is true,
3066 * it will immediately be reinserted in the ha_thread_ctx quic_conns_clo list.
3067 */
3068static void qc_detach_th_ctx_list(struct quic_conn *qc, int closing)
3069{
3070 struct bref *bref, *back;
3071
3072 /* Detach CLI context watchers currently dumping this connection.
3073 * Reattach them to the next quic_conn instance.
3074 */
3075 list_for_each_entry_safe(bref, back, &qc->back_refs, users) {
3076 /* Remove watcher from this quic_conn instance. */
3077 LIST_DEL_INIT(&bref->users);
3078
3079 /* Attach it to next instance unless it was the last list element. */
3080 if (qc->el_th_ctx.n != &th_ctx->quic_conns &&
3081 qc->el_th_ctx.n != &th_ctx->quic_conns_clo) {
3082 struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx,
3083 struct quic_conn *,
3084 el_th_ctx);
3085 LIST_APPEND(&next->back_refs, &bref->users);
3086 }
3087 bref->ref = qc->el_th_ctx.n;
3088 __ha_barrier_store();
3089 }
3090
3091 /* Remove quic_conn from global ha_thread_ctx list. */
3092 LIST_DEL_INIT(&qc->el_th_ctx);
3093
3094 if (closing)
3095 LIST_APPEND(&th_ctx->quic_conns_clo, &qc->el_th_ctx);
3096}
3097
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003098/* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel>
3099 * as encryption level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003100 * Returns 1 if succeeded, 0 if failed.
3101 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003102static int qc_parse_pkt_frms(struct quic_conn *qc, struct quic_rx_packet *pkt,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003103 struct quic_enc_level *qel)
3104{
3105 struct quic_frame frm;
3106 const unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003107 int fast_retrans = 0, ret = 0;
3108
3109 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3110 /* Skip the AAD */
3111 pos = pkt->data + pkt->aad_len;
3112 end = pkt->data + pkt->len;
3113
3114 while (pos < end) {
3115 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
3116 // trace already emitted by function above
3117 goto leave;
3118 }
3119
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003120 switch (frm.type) {
3121 case QUIC_FT_PADDING:
3122 break;
3123 case QUIC_FT_PING:
3124 break;
3125 case QUIC_FT_ACK:
3126 {
3127 unsigned int rtt_sample;
3128
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003129 rtt_sample = UINT_MAX;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003130 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
3131 // trace already emitted by function above
3132 goto leave;
3133 }
3134
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003135 if (rtt_sample != UINT_MAX) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003136 unsigned int ack_delay;
3137
3138 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
3139 qc->state >= QUIC_HS_ST_CONFIRMED ?
3140 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
3141 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
3142 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
3143 }
3144 break;
3145 }
3146 case QUIC_FT_RESET_STREAM:
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01003147 if (qc->mux_state == QC_MUX_READY) {
3148 struct quic_reset_stream *rs = &frm.reset_stream;
3149 qcc_recv_reset_stream(qc->qcc, rs->id, rs->app_error_code, rs->final_size);
3150 }
3151 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003152 case QUIC_FT_STOP_SENDING:
3153 {
3154 struct quic_stop_sending *stop_sending = &frm.stop_sending;
3155 if (qc->mux_state == QC_MUX_READY) {
3156 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
3157 stop_sending->app_error_code)) {
3158 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
3159 goto leave;
3160 }
3161 }
3162 break;
3163 }
3164 case QUIC_FT_CRYPTO:
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003165 if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003166 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003167 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003168 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
3169 {
3170 struct quic_stream *stream = &frm.stream;
3171 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Amaury Denoyelle2216b082023-02-02 14:59:36 +01003172 const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003173
3174 /* The upper layer may not be allocated. */
3175 if (qc->mux_state != QC_MUX_READY) {
3176 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
3177 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003178 }
3179 else {
3180 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle38836b62023-02-07 14:24:54 +01003181 if (qc->app_ops == &h3_ops) {
Amaury Denoyelle156a89a2023-02-20 10:32:16 +01003182 if (!qc_h3_request_reject(qc, stream->id)) {
3183 TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc);
3184 /* This packet will not be acknowledged */
3185 goto leave;
3186 }
3187 }
3188 else {
3189 /* This packet will not be acknowledged */
3190 goto leave;
Frédéric Lécailled18025e2023-01-20 15:33:50 +01003191 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003192 }
Amaury Denoyelle315a4f62023-03-06 09:10:53 +01003193
3194 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003195 }
3196
Amaury Denoyelle2216b082023-02-02 14:59:36 +01003197 if (!qc_handle_strm_frm(pkt, stream, qc, fin)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003198 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
3199 goto leave;
3200 }
3201
3202 break;
3203 }
3204 case QUIC_FT_MAX_DATA:
3205 if (qc->mux_state == QC_MUX_READY) {
3206 struct quic_max_data *data = &frm.max_data;
3207 qcc_recv_max_data(qc->qcc, data->max_data);
3208 }
3209 break;
3210 case QUIC_FT_MAX_STREAM_DATA:
3211 if (qc->mux_state == QC_MUX_READY) {
3212 struct quic_max_stream_data *data = &frm.max_stream_data;
3213 if (qcc_recv_max_stream_data(qc->qcc, data->id,
3214 data->max_stream_data)) {
3215 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
3216 goto leave;
3217 }
3218 }
3219 break;
3220 case QUIC_FT_MAX_STREAMS_BIDI:
3221 case QUIC_FT_MAX_STREAMS_UNI:
3222 break;
3223 case QUIC_FT_DATA_BLOCKED:
3224 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
3225 break;
3226 case QUIC_FT_STREAM_DATA_BLOCKED:
3227 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
3228 break;
3229 case QUIC_FT_STREAMS_BLOCKED_BIDI:
3230 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
3231 break;
3232 case QUIC_FT_STREAMS_BLOCKED_UNI:
3233 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
3234 break;
3235 case QUIC_FT_NEW_CONNECTION_ID:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003236 /* XXX TO DO XXX */
3237 break;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003238 case QUIC_FT_RETIRE_CONNECTION_ID:
3239 {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003240 struct quic_connection_id *conn_id = NULL;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003241
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003242 if (!qc_handle_retire_connection_id_frm(qc, &frm, &pkt->dcid, &conn_id))
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003243 goto leave;
3244
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003245 if (!conn_id)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003246 break;
3247
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003248 ebmb_delete(&conn_id->node);
3249 eb64_delete(&conn_id->seq_num);
3250 pool_free(pool_head_quic_connection_id, conn_id);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003251 TRACE_PROTO("CID retired", QUIC_EV_CONN_PSTRM, qc);
3252
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003253 conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
3254 if (!conn_id) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003255 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
3256 }
3257 else {
Amaury Denoyellee83f9372023-04-18 11:10:54 +02003258 quic_cid_insert(conn_id);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003259 qc_build_new_connection_id_frm(qc, conn_id);
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003260 }
3261 break;
3262 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003263 case QUIC_FT_CONNECTION_CLOSE:
3264 case QUIC_FT_CONNECTION_CLOSE_APP:
3265 /* Increment the error counters */
3266 qc_cc_err_count_inc(qc, &frm);
3267 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
3268 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
3269 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
3270 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
3271 }
3272 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
3273 /* RFC 9000 10.2. Immediate Close:
3274 * The closing and draining connection states exist to ensure
3275 * that connections close cleanly and that delayed or reordered
3276 * packets are properly discarded. These states SHOULD persist
3277 * for at least three times the current PTO interval...
3278 *
3279 * Rearm the idle timeout only one time when entering draining
3280 * state.
3281 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003282 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003283 qc_detach_th_ctx_list(qc, 1);
Frédéric Lécailled7215712023-03-24 18:13:37 +01003284 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003285 qc_notify_close(qc);
3286 }
3287 break;
3288 case QUIC_FT_HANDSHAKE_DONE:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003289 if (qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003290 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
3291 QUIC_EV_CONN_PRSHPKT, qc);
3292 goto leave;
3293 }
3294
3295 qc->state = QUIC_HS_ST_CONFIRMED;
3296 break;
3297 default:
3298 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
3299 goto leave;
3300 }
3301 }
3302
3303 /* Flag this packet number space as having received a packet. */
3304 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
3305
3306 if (fast_retrans) {
3307 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3308 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3309
3310 TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc);
3311 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
3312 qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP;
3313 }
3314
3315 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
3316 * has successfully parse a Handshake packet. The Initial encryption must also
3317 * be discarded.
3318 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003319 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003320 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
3321 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
3322 QUIC_FL_TLS_SECRETS_DCD)) {
3323 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3324 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
3325 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003326 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003327 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3328 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3329 }
3330 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
3331 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
3332 }
3333 }
3334
3335 ret = 1;
3336 leave:
3337 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3338 return ret;
3339}
3340
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003341
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003342/* Allocate Tx buffer from <qc> quic-conn if needed.
3343 *
3344 * Returns allocated buffer or NULL on error.
3345 */
3346static struct buffer *qc_txb_alloc(struct quic_conn *qc)
3347{
3348 struct buffer *buf = &qc->tx.buf;
3349 if (!b_alloc(buf))
3350 return NULL;
3351
3352 return buf;
3353}
3354
3355/* Free Tx buffer from <qc> if it is empty. */
3356static void qc_txb_release(struct quic_conn *qc)
3357{
3358 struct buffer *buf = &qc->tx.buf;
3359
3360 /* For the moment sending function is responsible to purge the buffer
3361 * entirely. It may change in the future but this requires to be able
3362 * to reuse old data.
Frédéric Lécaillebbf86be2023-02-20 09:28:58 +01003363 * For the momemt we do not care to leave data in the buffer for
3364 * a connection which is supposed to be killed asap.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003365 */
3366 BUG_ON_HOT(buf && b_data(buf));
3367
3368 if (!b_data(buf)) {
3369 b_free(buf);
3370 offer_buffers(NULL, 1);
3371 }
3372}
3373
3374/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
3375 * must contains the address of the first packet stored in the payload.
3376 *
3377 * Caller is responsible that there is enough space in the buffer.
3378 */
3379static void qc_txb_store(struct buffer *buf, uint16_t length,
3380 struct quic_tx_packet *first_pkt)
3381{
3382 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
3383 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
3384
3385 write_u16(b_tail(buf), length);
3386 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
3387 b_add(buf, hdlen + length);
3388}
3389
3390/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
3391 * with <frms> as ack-eliciting frame list to send, 0 if not.
3392 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
3393 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003394 * Also set <*must_ack> to inform the caller if an acknowledgement should be sent.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003395 */
3396static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003397 struct quic_enc_level *qel, int cc, int probe,
3398 int *must_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003399{
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003400 int force_ack =
3401 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3402 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3403 int nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
3404
3405 /* An acknowledgement must be sent if this has been forced by the caller,
3406 * typically during the handshake when the packets must be acknowledged as
3407 * soon as possible. This is also the case when the ack delay timer has been
3408 * triggered, or at least every QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK packets.
3409 */
3410 *must_ack = (qc->flags & QUIC_FL_CONN_ACK_TIMER_FIRED) ||
3411 ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3412 (force_ack || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003413
3414 /* Do not build any more packet if the TX secrets are not available or
3415 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
3416 * and if there is no more packets to send upon PTO expiration
3417 * and if there is no more ack-eliciting frames to send or in flight
3418 * congestion control limit is reached for prepared data
3419 */
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02003420 if (!quic_tls_has_tx_sec(qel) ||
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003421 (!cc && !probe && !*must_ack &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003422 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
3423 return 0;
3424 }
3425
3426 return 1;
3427}
3428
3429/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3430 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
3431 *
3432 * Each datagram is prepended by a two fields header : the datagram length and
3433 * the address of the packet contained in the datagram.
3434 *
3435 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3436 * -1 if something wrong happened.
3437 */
3438static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
3439 struct list *frms)
3440{
3441 int ret = -1;
3442 struct quic_enc_level *qel;
3443 unsigned char *end, *pos;
3444 struct quic_tx_packet *pkt;
3445 size_t total;
3446 /* Each datagram is prepended with its length followed by the address
3447 * of the first packet in the datagram.
3448 */
3449 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
3450
3451 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3452
3453 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3454 total = 0;
3455 pos = (unsigned char *)b_tail(buf);
3456 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003457 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003458
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003459 TRACE_PROTO("TX prep app pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003460 probe = 0;
3461 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3462 /* We do not probe if an immediate close was asked */
3463 if (!cc)
3464 probe = qel->pktns->tx.pto_probe;
3465
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003466 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003467 break;
3468
3469 /* Leave room for the datagram header */
3470 pos += dg_headlen;
3471 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3472 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3473 }
3474 else {
3475 end = pos + qc->path->mtu;
3476 }
3477
3478 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003479 QUIC_PACKET_TYPE_SHORT, must_ack, 0, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003480 switch (err) {
3481 case -2:
3482 // trace already emitted by function above
3483 goto leave;
3484 case -1:
3485 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
3486 * MTU, we are here because of the congestion control window. There is
3487 * no need to try to reuse this buffer.
3488 */
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003489 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003490 goto out;
3491 default:
3492 break;
3493 }
3494
3495 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
3496 BUG_ON(!pkt);
3497
3498 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3499 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3500
3501 total += pkt->len;
3502
3503 /* Write datagram header. */
3504 qc_txb_store(buf, pkt->len, pkt);
3505 }
3506
3507 out:
3508 ret = total;
3509 leave:
3510 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3511 return ret;
3512}
3513
3514/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3515 * <frms>. Several packets can be regrouped in a single datagram. The result is
3516 * written into <buf>.
3517 *
3518 * Each datagram is prepended by a two fields header : the datagram length and
3519 * the address of first packet in the datagram.
3520 *
3521 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3522 * -1 if something wrong happened.
3523 */
3524static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
3525 enum quic_tls_enc_level tel, struct list *tel_frms,
3526 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3527{
3528 struct quic_enc_level *qel;
3529 unsigned char *end, *pos;
3530 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3531 /* length of datagrams */
3532 uint16_t dglen;
3533 size_t total;
3534 int ret = -1, padding;
3535 /* Each datagram is prepended with its length followed by the address
3536 * of the first packet in the datagram.
3537 */
3538 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
3539 struct list *frms;
3540
3541 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3542
3543 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05003544 * caller must ensure that buf is reset.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003545 */
3546 BUG_ON_HOT(buf->head || buf->data);
3547
3548 total = 0;
3549 qel = &qc->els[tel];
3550 frms = tel_frms;
3551 dglen = 0;
3552 padding = 0;
3553 pos = (unsigned char *)b_head(buf);
3554 first_pkt = prv_pkt = NULL;
3555 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003556 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003557 enum quic_pkt_type pkt_type;
3558 struct quic_tls_ctx *tls_ctx;
3559 const struct quic_version *ver;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003560
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003561 TRACE_PROTO("TX prep pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003562 probe = 0;
3563 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3564 /* We do not probe if an immediate close was asked */
3565 if (!cc)
3566 probe = qel->pktns->tx.pto_probe;
3567
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003568 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003569 if (prv_pkt)
3570 qc_txb_store(buf, dglen, first_pkt);
3571 /* Let's select the next encryption level */
3572 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3573 tel = next_tel;
3574 frms = next_tel_frms;
3575 qel = &qc->els[tel];
3576 /* Build a new datagram */
3577 prv_pkt = NULL;
3578 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
3579 continue;
3580 }
3581 break;
3582 }
3583
3584 pkt_type = quic_tls_level_pkt_type(tel);
3585 if (!prv_pkt) {
3586 /* Leave room for the datagram header */
3587 pos += dg_headlen;
3588 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3589 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3590 }
3591 else {
3592 end = pos + qc->path->mtu;
3593 }
3594 }
3595
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003596 /* RFC 9000 14.1 Initial datagram size
3597 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3598 * Initial packets to at least the smallest allowed maximum datagram size of
3599 * 1200 bytes.
3600 *
3601 * Ensure that no ack-eliciting packets are sent into too small datagrams
3602 */
3603 if (pkt_type == QUIC_PACKET_TYPE_INITIAL && !LIST_ISEMPTY(tel_frms)) {
3604 if (end - pos < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01003605 TRACE_PROTO("No more enough room to build an Initial packet",
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003606 QUIC_EV_CONN_PHPKTS, qc);
3607 goto out;
3608 }
3609
3610 /* Pad this Initial packet if there is no ack-eliciting frames to send from
3611 * the next packet number space.
3612 */
Frédéric Lécailleec937212023-03-03 17:34:41 +01003613 if (!next_tel_frms || LIST_ISEMPTY(next_tel_frms))
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003614 padding = 1;
3615 }
3616
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003617 if (qc->negotiated_version) {
3618 ver = qc->negotiated_version;
3619 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3620 tls_ctx = &qc->negotiated_ictx;
3621 else
3622 tls_ctx = &qel->tls_ctx;
3623 }
3624 else {
3625 ver = qc->original_version;
3626 tls_ctx = &qel->tls_ctx;
3627 }
3628
3629 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
3630 qc, ver, dglen, pkt_type,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003631 must_ack, padding, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003632 switch (err) {
3633 case -2:
3634 // trace already emitted by function above
3635 goto leave;
3636 case -1:
3637 /* If there was already a correct packet present, set the
3638 * current datagram as prepared into <cbuf>.
3639 */
3640 if (prv_pkt)
3641 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003642 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003643 goto out;
3644 default:
3645 break;
3646 }
3647
3648 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
3649 BUG_ON(!cur_pkt);
3650
3651 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3652 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3653
3654 total += cur_pkt->len;
3655 /* keep trace of the first packet in the datagram */
3656 if (!first_pkt)
3657 first_pkt = cur_pkt;
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01003658 /* Attach the current one to the previous one and vice versa */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003659 if (prv_pkt) {
3660 prv_pkt->next = cur_pkt;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01003661 cur_pkt->prev = prv_pkt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003662 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3663 }
3664 /* Let's say we have to build a new dgram */
3665 prv_pkt = NULL;
3666 dglen += cur_pkt->len;
3667 /* Client: discard the Initial encryption keys as soon as
3668 * a handshake packet could be built.
3669 */
3670 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
3671 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3672 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3673 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3674 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3675 qc_set_timer(qc);
3676 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3677 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3678 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
3679 }
3680 /* If the data for the current encryption level have all been sent,
3681 * select the next level.
3682 */
3683 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillea576c1b2023-04-13 15:59:02 +02003684 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms))) {
Frédéric Lécaille895700b2023-04-13 18:30:16 +02003685 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try
3686 * QUIC_TLS_ENC_LEVEL_APP except if the connection was probing.
3687 */
3688 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel) {
3689 if ((qc->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED)) {
3690 TRACE_PROTO("skip APP enc. level", QUIC_EV_CONN_PHPKTS, qc);
3691 qc_txb_store(buf, dglen, first_pkt);
3692 goto out;
3693 }
3694
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003695 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaille895700b2023-04-13 18:30:16 +02003696 }
3697
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003698 tel = next_tel;
3699 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3700 frms = &qc->els[tel].pktns->tx.frms;
3701 else
3702 frms = next_tel_frms;
3703 qel = &qc->els[tel];
3704 if (!LIST_ISEMPTY(frms)) {
3705 /* If there is data for the next level, do not
3706 * consume a datagram.
3707 */
3708 prv_pkt = cur_pkt;
3709 }
3710 }
3711
3712 /* If we have to build a new datagram, set the current datagram as
3713 * prepared into <cbuf>.
3714 */
3715 if (!prv_pkt) {
3716 qc_txb_store(buf, dglen, first_pkt);
3717 first_pkt = NULL;
3718 dglen = 0;
3719 padding = 0;
3720 }
3721 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
3722 (!qc_is_listener(qc) ||
3723 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3724 padding = 1;
3725 }
3726 }
3727
3728 out:
3729 ret = total;
3730 leave:
3731 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3732 return ret;
3733}
3734
3735/* Send datagrams stored in <buf>.
3736 *
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003737 * This function returns 1 for success. On error, there is several behavior
3738 * depending on underlying sendto() error :
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003739 * - for an unrecoverable error, 0 is returned and connection is killed.
3740 * - a transient error is handled differently if connection has its owned
3741 * socket. If this is the case, 0 is returned and socket is subscribed on the
3742 * poller. The other case is assimilated to a success case with 1 returned.
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003743 * Remaining data are purged from the buffer and will eventually be detected
3744 * as lost which gives the opportunity to retry sending.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003745 */
3746int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
3747{
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003748 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003749 struct quic_conn *qc;
3750 char skip_sendto = 0;
3751
3752 qc = ctx->qc;
3753 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
3754 while (b_contig_data(buf, 0)) {
3755 unsigned char *pos;
3756 struct buffer tmpbuf = { };
3757 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3758 uint16_t dglen;
3759 size_t headlen = sizeof dglen + sizeof first_pkt;
3760 unsigned int time_sent;
3761
3762 pos = (unsigned char *)b_head(buf);
3763 dglen = read_u16(pos);
3764 BUG_ON_HOT(!dglen); /* this should not happen */
3765
3766 pos += sizeof dglen;
3767 first_pkt = read_ptr(pos);
3768 pos += sizeof first_pkt;
3769 tmpbuf.area = (char *)pos;
3770 tmpbuf.size = tmpbuf.data = dglen;
3771
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003772 TRACE_PROTO("TX dgram", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003773 /* If sendto is on error just skip the call to it for the rest
3774 * of the loop but continue to purge the buffer. Data will be
3775 * transmitted when QUIC packets are detected as lost on our
3776 * side.
3777 *
3778 * TODO use fd-monitoring to detect when send operation can be
3779 * retry. This should improve the bandwidth without relying on
3780 * retransmission timer. However, it requires a major rework on
3781 * quic-conn fd management.
3782 */
3783 if (!skip_sendto) {
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003784 int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0);
3785 if (ret < 0) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003786 TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003787 qc_kill_conn(qc);
3788 b_del(buf, buf->data);
3789 goto leave;
3790 }
3791 else if (!ret) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003792 /* Connection owned socket : poller will wake us up when transient error is cleared. */
3793 if (qc_test_fd(qc)) {
3794 TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc);
3795 goto leave;
3796 }
3797
3798 /* No connection owned-socket : rely on retransmission to retry sending. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003799 skip_sendto = 1;
3800 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
3801 }
3802 }
3803
3804 b_del(buf, dglen + headlen);
3805 qc->tx.bytes += tmpbuf.data;
3806 time_sent = now_ms;
3807
3808 for (pkt = first_pkt; pkt; pkt = next_pkt) {
Frédéric Lécailleceb88b82023-02-20 14:43:55 +01003809 /* RFC 9000 14.1 Initial datagram size
3810 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3811 * Initial packets to at least the smallest allowed maximum datagram size of
3812 * 1200 bytes.
3813 */
3814 BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3815 (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
3816 dglen < QUIC_INITIAL_PACKET_MINLEN);
3817
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003818 pkt->time_sent = time_sent;
3819 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3820 pkt->pktns->tx.time_of_last_eliciting = time_sent;
3821 qc->path->ifae_pkts++;
3822 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
Frédéric Lécailled7215712023-03-24 18:13:37 +01003823 qc_idle_timer_rearm(qc, 0, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003824 }
3825 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3826 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3827 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003828 qc_detach_th_ctx_list(qc, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003829 qc_notify_close(qc);
3830
3831 /* RFC 9000 10.2. Immediate Close:
3832 * The closing and draining connection states exist to ensure
3833 * that connections close cleanly and that delayed or reordered
3834 * packets are properly discarded. These states SHOULD persist
3835 * for at least three times the current PTO interval...
3836 *
3837 * Rearm the idle timeout only one time when entering closing
3838 * state.
3839 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01003840 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003841 if (qc->timer_task) {
3842 task_destroy(qc->timer_task);
3843 qc->timer_task = NULL;
3844 }
3845 }
3846 qc->path->in_flight += pkt->in_flight_len;
3847 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3848 if (pkt->in_flight_len)
3849 qc_set_timer(qc);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003850 TRACE_PROTO("TX pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003851 next_pkt = pkt->next;
3852 quic_tx_packet_refinc(pkt);
3853 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
3854 }
3855 }
3856
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003857 ret = 1;
3858leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003859 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3860
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003861 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003862}
3863
3864/* Copy into <buf> buffer a stateless reset token depending on the
3865 * <salt> salt input. This is the cluster secret which will be derived
3866 * as HKDF input secret to generate this token.
3867 * Return 1 if succeeded, 0 if not.
3868 */
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02003869static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003870 const unsigned char *salt, size_t saltlen)
3871{
3872 /* Input secret */
3873 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3874 size_t keylen = strlen(global.cluster_secret);
3875 /* Info */
3876 const unsigned char label[] = "stateless token";
3877 size_t labellen = sizeof label - 1;
3878 int ret;
3879
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003880 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
3881 key, keylen, salt, saltlen, label, labellen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003882 return ret;
3883}
3884
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003885/* Initialize the stateless reset token attached to <conn_id> connection ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003886 * Returns 1 if succeeded, 0 if not.
3887 */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003888static int quic_stateless_reset_token_init(struct quic_connection_id *conn_id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003889{
3890 int ret;
3891
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003892 if (global.cluster_secret) {
3893 /* Output secret */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003894 unsigned char *token = conn_id->stateless_reset_token;
3895 size_t tokenlen = sizeof conn_id->stateless_reset_token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003896 /* Salt */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003897 const unsigned char *cid = conn_id->cid.data;
3898 size_t cidlen = conn_id->cid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003899
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02003900 ret = quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003901 }
3902 else {
3903 /* TODO: RAND_bytes() should be replaced */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003904 ret = RAND_bytes(conn_id->stateless_reset_token,
3905 sizeof conn_id->stateless_reset_token) == 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003906 }
3907
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003908 return ret;
3909}
3910
Amaury Denoyelle1e959ad2023-04-13 17:34:56 +02003911/* Generate a CID directly derived from <orig> CID and <addr> address.
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003912 *
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003913 * Returns the derived CID.
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003914 */
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003915struct quic_cid quic_derive_cid(const struct quic_cid *orig,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003916 const struct sockaddr_storage *addr)
3917{
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003918 struct quic_cid cid;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003919 const struct sockaddr_in *in;
3920 const struct sockaddr_in6 *in6;
3921 char *buf = trash.area;
3922 size_t idx = 0;
3923 uint64_t hash;
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003924 int i;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003925
3926 /* Prepare buffer for hash using original CID first. */
3927 memcpy(buf, orig->data, orig->len);
3928 idx += orig->len;
3929
3930 /* Concatenate client address. */
3931 switch (addr->ss_family) {
3932 case AF_INET:
3933 in = (struct sockaddr_in *)addr;
3934
3935 memcpy(&buf[idx], &in->sin_addr, sizeof(in->sin_addr));
3936 idx += sizeof(in->sin_addr);
3937 memcpy(&buf[idx], &in->sin_port, sizeof(in->sin_port));
3938 idx += sizeof(in->sin_port);
3939 break;
3940
3941 case AF_INET6:
3942 in6 = (struct sockaddr_in6 *)addr;
3943
3944 memcpy(&buf[idx], &in6->sin6_addr, sizeof(in6->sin6_addr));
3945 idx += sizeof(in6->sin6_addr);
3946 memcpy(&buf[idx], &in6->sin6_port, sizeof(in6->sin6_port));
3947 idx += sizeof(in6->sin6_port);
3948 break;
3949
3950 default:
3951 /* TODO to implement */
3952 ABORT_NOW();
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003953 }
3954
3955 /* Avoid similar values between multiple haproxy process. */
3956 memcpy(&buf[idx], boot_seed, sizeof(boot_seed));
3957 idx += sizeof(boot_seed);
3958
3959 /* Hash the final buffer content. */
3960 hash = XXH64(buf, idx, 0);
3961
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003962 for (i = 0; i < sizeof(hash); ++i)
3963 cid.data[i] = hash >> ((sizeof(hash) * 7) - (8 * i));
3964 cid.len = sizeof(hash);
3965
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003966 return cid;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003967}
3968
Amaury Denoyellee83f9372023-04-18 11:10:54 +02003969/* Retrieve the thread ID associated to QUIC connection ID <cid> of length
3970 * <cid_len>. CID may be not found on the CID tree because it is an ODCID. In
3971 * this case, it will derived using client address <cli_addr> as hash
3972 * parameter. However, this is done only if <buf> points to an INITIAL or 0RTT
3973 * packet of length <len>.
3974 *
3975 * Returns the thread ID or a negative error code.
3976 */
3977int quic_get_cid_tid(const unsigned char *cid, size_t cid_len,
3978 const struct sockaddr_storage *cli_addr,
3979 unsigned char *buf, size_t buf_len)
3980{
3981 struct quic_cid_tree *tree;
3982 struct quic_connection_id *conn_id;
3983 struct ebmb_node *node;
3984
3985 tree = &quic_cid_trees[_quic_cid_tree_idx(cid)];
3986 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
3987 node = ebmb_lookup(&tree->root, cid, cid_len);
3988 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
3989
3990 if (!node) {
3991 struct quic_cid orig, derive_cid;
3992 struct quic_rx_packet pkt;
3993
3994 if (!qc_parse_hd_form(&pkt, &buf, buf + buf_len))
3995 goto not_found;
3996
3997 if (pkt.type != QUIC_PACKET_TYPE_INITIAL &&
3998 pkt.type != QUIC_PACKET_TYPE_0RTT) {
3999 goto not_found;
4000 }
4001
4002 memcpy(orig.data, cid, cid_len);
4003 orig.len = cid_len;
4004 derive_cid = quic_derive_cid(&orig, cli_addr);
4005
4006 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
4007 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
4008 node = ebmb_lookup(&tree->root, cid, cid_len);
4009 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
4010 }
4011
4012 if (!node)
4013 goto not_found;
4014
4015 conn_id = ebmb_entry(node, struct quic_connection_id, node);
4016 return HA_ATOMIC_LOAD(&conn_id->tid);
4017
4018 not_found:
4019 return -1;
4020}
4021
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004022/* Allocate a new CID and attach it to <root> ebtree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004023 *
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004024 * If <orig> and <addr> params are non null, the new CID value is directly
4025 * derived from them. Else a random value is generated. The CID is then marked
4026 * with the current thread ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004027 *
4028 * Returns the new CID if succeeded, NULL if not.
4029 */
4030static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004031 struct quic_conn *qc,
4032 const struct quic_cid *orig,
4033 const struct sockaddr_storage *addr)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004034{
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004035 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004036
4037 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4038
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004039 /* Caller must set either none or both values. */
4040 BUG_ON(!!orig != !!addr);
4041
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004042 conn_id = pool_alloc(pool_head_quic_connection_id);
4043 if (!conn_id) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004044 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
4045 goto err;
4046 }
4047
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004048 conn_id->cid.len = QUIC_HAP_CID_LEN;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004049
4050 if (!orig) {
4051 /* TODO: RAND_bytes() should be replaced */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004052 if (RAND_bytes(conn_id->cid.data, conn_id->cid.len) != 1) {
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004053 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
4054 goto err;
4055 }
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004056 }
4057 else {
4058 /* Derive the new CID value from original CID. */
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004059 conn_id->cid = quic_derive_cid(orig, addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004060 }
4061
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004062 if (quic_stateless_reset_token_init(conn_id) != 1) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004063 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
4064 goto err;
4065 }
4066
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004067 conn_id->qc = qc;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004068 HA_ATOMIC_STORE(&conn_id->tid, tid);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004069
Amaury Denoyellef16ec342023-04-13 17:42:34 +02004070 conn_id->seq_num.key = qc ? qc->next_cid_seq_num++ : 0;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004071 conn_id->retire_prior_to = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004072 /* insert the allocated CID in the quic_conn tree */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02004073 if (root)
4074 eb64_insert(root, &conn_id->seq_num);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004075
4076 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004077 return conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004078
4079 err:
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004080 pool_free(pool_head_quic_connection_id, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004081 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4082 return NULL;
4083}
4084
4085/* Build all the frames which must be sent just after the handshake have succeeded.
4086 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
4087 * a HANDSHAKE_DONE frame.
4088 * Return 1 if succeeded, 0 if not.
4089 */
4090static int quic_build_post_handshake_frames(struct quic_conn *qc)
4091{
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004092 int ret = 0, max;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004093 struct quic_enc_level *qel;
4094 struct quic_frame *frm, *frmbak;
4095 struct list frm_list = LIST_HEAD_INIT(frm_list);
4096 struct eb64_node *node;
4097
4098 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4099
4100 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4101 /* Only servers must send a HANDSHAKE_DONE frame. */
4102 if (qc_is_listener(qc)) {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004103 frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004104 if (!frm) {
4105 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4106 goto leave;
4107 }
4108
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004109 LIST_APPEND(&frm_list, &frm->list);
4110 }
4111
4112 /* Initialize <max> connection IDs minus one: there is
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004113 * already one connection ID used for the current connection. Also limit
4114 * the number of connection IDs sent to the peer to 4 (3 from this function
4115 * plus 1 for the current connection.
4116 * Note that active_connection_id_limit >= 2: this has been already checked
4117 * when receiving this parameter.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004118 */
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004119 max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3);
4120 while (max--) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004121 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004122
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004123 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004124 if (!frm) {
4125 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4126 goto err;
4127 }
4128
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004129 conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
4130 if (!conn_id) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004131 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004132 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
4133 goto err;
4134 }
4135
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004136 /* TODO To prevent CID tree locking, all CIDs created here
4137 * could be allocated at the same time as the first one.
4138 */
4139 quic_cid_insert(conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004140
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004141 quic_connection_id_to_frm_cpy(frm, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004142 LIST_APPEND(&frm_list, &frm->list);
4143 }
4144
4145 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004146 qc->flags &= ~QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004147
4148 ret = 1;
4149 leave:
4150 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4151 return ret;
4152
4153 err:
4154 /* free the frames */
4155 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004156 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004157
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004158 /* The first CID sequence number value used to allocated CIDs by this function is 1,
4159 * 0 being the sequence number of the CID for this connection.
4160 */
4161 node = eb64_lookup_ge(&qc->cids, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004162 while (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004163 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004164
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004165 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
4166 if (conn_id->seq_num.key >= max)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004167 break;
4168
4169 node = eb64_next(node);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004170 quic_cid_delete(conn_id);
4171
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004172 eb64_delete(&conn_id->seq_num);
4173 pool_free(pool_head_quic_connection_id, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004174 }
4175 goto leave;
4176}
4177
4178/* Deallocate <l> list of ACK ranges. */
4179void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
4180{
4181 struct eb64_node *n;
4182 struct quic_arng_node *ar;
4183
4184 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4185
4186 n = eb64_first(&arngs->root);
4187 while (n) {
4188 struct eb64_node *next;
4189
4190 ar = eb64_entry(n, struct quic_arng_node, first);
4191 next = eb64_next(n);
4192 eb64_delete(n);
4193 pool_free(pool_head_quic_arng, ar);
4194 n = next;
4195 }
4196
4197 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4198}
4199
4200/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
4201 * descending order.
4202 */
4203static inline size_t sack_gap(struct quic_arng_node *p,
4204 struct quic_arng_node *q)
4205{
4206 return p->first.key - q->last - 2;
4207}
4208
4209
4210/* Remove the last elements of <ack_ranges> list of ack range updating its
4211 * encoded size until it goes below <limit>.
4212 * Returns 1 if succeeded, 0 if not (no more element to remove).
4213 */
4214static int quic_rm_last_ack_ranges(struct quic_conn *qc,
4215 struct quic_arngs *arngs, size_t limit)
4216{
4217 int ret = 0;
4218 struct eb64_node *last, *prev;
4219
4220 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4221
4222 last = eb64_last(&arngs->root);
4223 while (last && arngs->enc_sz > limit) {
4224 struct quic_arng_node *last_node, *prev_node;
4225
4226 prev = eb64_prev(last);
4227 if (!prev) {
4228 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
4229 goto out;
4230 }
4231
4232 last_node = eb64_entry(last, struct quic_arng_node, first);
4233 prev_node = eb64_entry(prev, struct quic_arng_node, first);
4234 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
4235 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
4236 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
4237 --arngs->sz;
4238 eb64_delete(last);
4239 pool_free(pool_head_quic_arng, last);
4240 last = prev;
4241 }
4242
4243 ret = 1;
4244 out:
4245 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4246 return ret;
4247}
4248
4249/* Set the encoded size of <arngs> QUIC ack ranges. */
4250static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
4251{
4252 struct eb64_node *node, *next;
4253 struct quic_arng_node *ar, *ar_next;
4254
4255 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4256
4257 node = eb64_last(&arngs->root);
4258 if (!node)
4259 goto leave;
4260
4261 ar = eb64_entry(node, struct quic_arng_node, first);
4262 arngs->enc_sz = quic_int_getsize(ar->last) +
4263 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
4264
4265 while ((next = eb64_prev(node))) {
4266 ar_next = eb64_entry(next, struct quic_arng_node, first);
4267 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
4268 quic_int_getsize(ar_next->last - ar_next->first.key);
4269 node = next;
4270 ar = eb64_entry(node, struct quic_arng_node, first);
4271 }
4272
4273 leave:
4274 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4275}
4276
4277/* Insert <ar> ack range into <argns> tree of ack ranges.
4278 * Returns the ack range node which has been inserted if succeeded, NULL if not.
4279 */
4280static inline
4281struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
4282 struct quic_arngs *arngs,
4283 struct quic_arng *ar)
4284{
4285 struct quic_arng_node *new_ar;
4286
4287 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4288
4289 new_ar = pool_alloc(pool_head_quic_arng);
4290 if (!new_ar) {
4291 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
4292 goto leave;
4293 }
4294
4295 new_ar->first.key = ar->first;
4296 new_ar->last = ar->last;
4297 eb64_insert(&arngs->root, &new_ar->first);
4298 arngs->sz++;
4299
4300 leave:
4301 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4302 return new_ar;
4303}
4304
4305/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
4306 * Note that this function computes the number of bytes required to encode
4307 * this tree of ACK ranges in descending order.
4308 *
4309 * Descending order
4310 * ------------->
4311 * range1 range2
4312 * ..........|--------|..............|--------|
4313 * ^ ^ ^ ^
4314 * | | | |
4315 * last1 first1 last2 first2
4316 * ..........+--------+--------------+--------+......
4317 * diff1 gap12 diff2
4318 *
4319 * To encode the previous list of ranges we must encode integers as follows in
4320 * descending order:
4321 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
4322 * with diff1 = last1 - first1
4323 * diff2 = last2 - first2
4324 * gap12 = first1 - last2 - 2 (>= 0)
4325 *
4326
4327returns 0 on error
4328
4329 */
4330int quic_update_ack_ranges_list(struct quic_conn *qc,
4331 struct quic_arngs *arngs,
4332 struct quic_arng *ar)
4333{
4334 int ret = 0;
4335 struct eb64_node *le;
4336 struct quic_arng_node *new_node;
4337 struct eb64_node *new;
4338
4339 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4340
4341 new = NULL;
4342 if (eb_is_empty(&arngs->root)) {
4343 new_node = quic_insert_new_range(qc, arngs, ar);
4344 if (new_node)
4345 ret = 1;
4346
4347 goto leave;
4348 }
4349
4350 le = eb64_lookup_le(&arngs->root, ar->first);
4351 if (!le) {
4352 new_node = quic_insert_new_range(qc, arngs, ar);
4353 if (!new_node)
4354 goto leave;
4355
4356 new = &new_node->first;
4357 }
4358 else {
4359 struct quic_arng_node *le_ar =
4360 eb64_entry(le, struct quic_arng_node, first);
4361
4362 /* Already existing range */
4363 if (le_ar->last >= ar->last) {
4364 ret = 1;
4365 }
4366 else if (le_ar->last + 1 >= ar->first) {
4367 le_ar->last = ar->last;
4368 new = le;
4369 new_node = le_ar;
4370 }
4371 else {
4372 new_node = quic_insert_new_range(qc, arngs, ar);
4373 if (!new_node)
4374 goto leave;
4375
4376 new = &new_node->first;
4377 }
4378 }
4379
4380 /* Verify that the new inserted node does not overlap the nodes
4381 * which follow it.
4382 */
4383 if (new) {
4384 struct eb64_node *next;
4385 struct quic_arng_node *next_node;
4386
4387 while ((next = eb64_next(new))) {
4388 next_node =
4389 eb64_entry(next, struct quic_arng_node, first);
4390 if (new_node->last + 1 < next_node->first.key)
4391 break;
4392
4393 if (next_node->last > new_node->last)
4394 new_node->last = next_node->last;
4395 eb64_delete(next);
4396 pool_free(pool_head_quic_arng, next_node);
4397 /* Decrement the size of these ranges. */
4398 arngs->sz--;
4399 }
4400 }
4401
4402 ret = 1;
4403 leave:
4404 quic_arngs_set_enc_sz(qc, arngs);
4405 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4406 return ret;
4407}
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004408
4409/* Detect the value of the spin bit to be used. */
4410static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt,
4411 struct quic_enc_level *qel)
4412{
4413 uint64_t largest_pn = qel->pktns->rx.largest_pn;
4414
4415 if (qel != &qc->els[QUIC_TLS_ENC_LEVEL_APP] || largest_pn == -1 ||
4416 pkt->pn <= largest_pn)
4417 return;
4418
4419 if (qc_is_listener(qc)) {
4420 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4421 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4422 else
4423 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4424 }
4425 else {
4426 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4427 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4428 else
4429 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4430 }
4431}
4432
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004433/* Remove the header protection of packets at <el> encryption level.
4434 * Always succeeds.
4435 */
4436static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
4437{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004438 struct quic_rx_packet *pqpkt, *pkttmp;
4439 struct quic_enc_level *app_qel;
4440
4441 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
4442 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4443 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
4444 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004445 TRACE_PROTO("RX hp not removed (handshake not completed)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004446 QUIC_EV_CONN_ELRMHP, qc);
4447 goto out;
4448 }
Frédéric Lécaille72027782023-02-22 16:20:09 +01004449
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004450 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01004451 struct quic_tls_ctx *tls_ctx;
4452
4453 tls_ctx = qc_select_tls_ctx(qc, el, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004454 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
4455 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004456 TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004457 }
4458 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004459 qc_handle_spin_bit(qc, pqpkt, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004460 /* The AAD includes the packet number field */
4461 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
4462 /* Store the packet into the tree of packets to decrypt. */
4463 pqpkt->pn_node.key = pqpkt->pn;
4464 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
4465 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004466 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004467 }
4468 LIST_DELETE(&pqpkt->list);
4469 quic_rx_packet_refdec(pqpkt);
4470 }
4471
4472 out:
4473 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
4474}
4475
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004476/* Process all the CRYPTO frame at <el> encryption level. This is the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004477 * responsibility of the called to ensure there exists a CRYPTO data
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004478 * stream for this level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004479 * Return 1 if succeeded, 0 if not.
4480 */
4481static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
4482 struct quic_enc_level *el,
4483 struct ssl_sock_ctx *ctx)
4484{
4485 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004486 struct ncbuf *ncbuf;
4487 struct quic_cstream *cstream = el->cstream;
4488 ncb_sz_t data;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004489
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004490 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004491
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004492 BUG_ON(!cstream);
4493 ncbuf = &cstream->rx.ncbuf;
4494 if (ncb_is_null(ncbuf))
4495 goto done;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004496
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004497 /* TODO not working if buffer is wrapping */
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004498 while ((data = ncb_data(ncbuf, 0))) {
4499 const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004500
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004501 if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004502 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004503
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004504 cstream->rx.offset += data;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004505 TRACE_DEVEL("buffered crypto data were provided to TLS stack",
4506 QUIC_EV_CONN_PHPKTS, qc, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004507 }
4508
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004509 done:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004510 ret = 1;
4511 leave:
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004512 if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
4513 TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004514 quic_free_ncbuf(ncbuf);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004515 }
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004516 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004517 return ret;
4518}
4519
4520/* Process all the packets at <el> and <next_el> encryption level.
4521 * This is the caller responsibility to check that <cur_el> is different of <next_el>
4522 * as pointer value.
4523 * Return 1 if succeeded, 0 if not.
4524 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004525int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el,
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004526 struct quic_enc_level *next_el)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004527{
4528 int ret = 0;
4529 struct eb64_node *node;
4530 int64_t largest_pn = -1;
4531 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004532 struct quic_enc_level *qel = cur_el;
4533
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004534 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004535 qel = cur_el;
4536 next_tel:
4537 if (!qel)
4538 goto out;
4539
4540 node = eb64_first(&qel->rx.pkts);
4541 while (node) {
4542 struct quic_rx_packet *pkt;
4543
4544 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
4545 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004546 qc, pkt, NULL, qc->xprt_ctx->ssl);
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01004547 if (!qc_pkt_decrypt(qc, qel, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004548 /* Drop the packet */
4549 TRACE_ERROR("packet decryption failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004550 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004551 }
4552 else {
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004553 if (!qc_parse_pkt_frms(qc, pkt, qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004554 /* Drop the packet */
4555 TRACE_ERROR("packet parsing failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004556 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004557 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
4558 }
4559 else {
4560 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
4561
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004562 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
Frédéric Lécailleb5efe792023-04-14 09:56:17 +02004563 int arm_ack_timer =
4564 qc->state >= QUIC_HS_ST_COMPLETE &&
4565 qel->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT];
4566
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004567 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
4568 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécailleb5efe792023-04-14 09:56:17 +02004569 qc_idle_timer_rearm(qc, 1, arm_ack_timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004570 }
4571 if (pkt->pn > largest_pn) {
4572 largest_pn = pkt->pn;
4573 largest_pn_time_received = pkt->time_received;
4574 }
4575 /* Update the list of ranges to acknowledge. */
4576 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
4577 TRACE_ERROR("Could not update ack range list",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004578 QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004579 }
4580 }
4581 node = eb64_next(node);
4582 eb64_delete(&pkt->pn_node);
4583 quic_rx_packet_refdec(pkt);
4584 }
4585
4586 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
4587 /* Update the largest packet number. */
4588 qel->pktns->rx.largest_pn = largest_pn;
4589 /* Update the largest acknowledged packet timestamps */
4590 qel->pktns->rx.largest_time_received = largest_pn_time_received;
4591 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
4592 }
4593
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004594 if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004595 // trace already emitted by function above
4596 goto leave;
4597 }
4598
4599 if (qel == cur_el) {
4600 BUG_ON(qel == next_el);
4601 qel = next_el;
4602 largest_pn = -1;
4603 goto next_tel;
4604 }
4605
4606 out:
4607 ret = 1;
4608 leave:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004609 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004610 return ret;
4611}
4612
4613/* Check if it's possible to remove header protection for packets related to
4614 * encryption level <qel>. If <qel> is NULL, assume it's false.
4615 *
4616 * Return true if the operation is possible else false.
4617 */
4618static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
4619{
4620 int ret = 0;
4621 enum quic_tls_enc_level tel;
4622
4623 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
4624
4625 if (!qel)
4626 goto cant_rm_hp;
4627
4628 tel = ssl_to_quic_enc_level(qel->level);
4629
4630 /* check if tls secrets are available */
4631 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004632 TRACE_PROTO("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004633 goto cant_rm_hp;
4634 }
4635
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004636 if (!quic_tls_has_rx_sec(qel)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004637 TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004638 goto cant_rm_hp;
4639 }
4640
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004641 if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004642 TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004643 goto cant_rm_hp;
4644 }
4645
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004646 /* check if the connection layer is ready before using app level */
4647 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
4648 qc->mux_state == QC_MUX_NULL) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004649 TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004650 goto cant_rm_hp;
4651 }
4652
4653 ret = 1;
4654 cant_rm_hp:
4655 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
4656 return ret;
4657}
4658
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004659/* Flush txbuf for <qc> connection. This must be called prior to a packet
4660 * preparation when txbuf contains older data. A send will be conducted for
4661 * these data.
4662 *
4663 * Returns 1 on success : buffer is empty and can be use for packet
4664 * preparation. On error 0 is returned.
4665 */
4666static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
4667{
4668 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4669
4670 /* This operation can only be conducted if txbuf is not empty. This
4671 * case only happens for connection with their owned socket due to an
4672 * older transient sendto() error.
4673 */
4674 BUG_ON(!qc_test_fd(qc));
4675
4676 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4677 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4678 qc_txb_release(qc);
4679 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4680 return 0;
4681 }
4682
4683 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4684 return 1;
4685}
4686
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004687/* Try to send application frames from list <frms> on connection <qc>.
4688 *
4689 * Use qc_send_app_probing wrapper when probing with old data.
4690 *
4691 * Returns 1 on success. Some data might not have been sent due to congestion,
4692 * in this case they are left in <frms> input list. The caller may subscribe on
4693 * quic-conn to retry later.
4694 *
4695 * Returns 0 on critical error.
4696 * TODO review and classify more distinctly transient from definitive errors to
4697 * allow callers to properly handle it.
4698 */
4699static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
4700{
4701 int status = 0;
4702 struct buffer *buf;
4703
4704 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4705
4706 buf = qc_txb_alloc(qc);
4707 if (!buf) {
4708 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004709 goto err;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004710 }
4711
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004712 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4713 goto err;
4714
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004715 /* Prepare and send packets until we could not further prepare packets. */
4716 while (1) {
4717 int ret;
4718 /* Currently buf cannot be non-empty at this stage. Even if a
4719 * previous sendto() has failed it is emptied to simulate
4720 * packet emission and rely on QUIC lost detection to try to
4721 * emit it.
4722 */
4723 BUG_ON_HOT(b_data(buf));
4724 b_reset(buf);
4725
4726 ret = qc_prep_app_pkts(qc, buf, frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004727 if (ret == -1) {
4728 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004729 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004730 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004731
Amaury Denoyelle37333862023-02-28 11:53:48 +01004732 if (!ret)
4733 break;
4734
4735 if (!qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004736 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4737 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004738 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004739 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004740 }
4741
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004742 status = 1;
4743 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004744 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4745 return status;
4746
4747 err:
Amaury Denoyelle37333862023-02-28 11:53:48 +01004748 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4749 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004750}
4751
4752/* Try to send application frames from list <frms> on connection <qc>. Use this
4753 * function when probing is required.
4754 *
4755 * Returns the result from qc_send_app_pkts function.
4756 */
4757static forceinline int qc_send_app_probing(struct quic_conn *qc,
4758 struct list *frms)
4759{
4760 int ret;
4761
4762 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4763
4764 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
4765 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4766 ret = qc_send_app_pkts(qc, frms);
4767 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
4768
4769 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4770 return ret;
4771}
4772
4773/* Try to send application frames from list <frms> on connection <qc>. This
4774 * function is provided for MUX upper layer usage only.
4775 *
4776 * Returns the result from qc_send_app_pkts function.
4777 */
4778int qc_send_mux(struct quic_conn *qc, struct list *frms)
4779{
4780 int ret;
4781
4782 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4783 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
4784
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004785 /* Try to send post handshake frames first unless on 0-RTT. */
4786 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) &&
4787 qc->state >= QUIC_HS_ST_COMPLETE) {
4788 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4789 quic_build_post_handshake_frames(qc);
4790 qc_send_app_pkts(qc, &qel->pktns->tx.frms);
4791 }
4792
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004793 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4794 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
4795 ret = qc_send_app_pkts(qc, frms);
4796 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
4797
4798 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4799 return ret;
4800}
4801
4802/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4803 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
4804 * QUIC connection. <old_data> is used as boolean to send data already sent but
4805 * not already acknowledged (in flight).
4806 * Returns 1 if succeeded, 0 if not.
4807 */
4808int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4809 enum quic_tls_enc_level tel, struct list *tel_frms,
4810 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4811{
4812 int ret, status = 0;
4813 struct buffer *buf = qc_txb_alloc(qc);
4814
4815 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4816
4817 if (!buf) {
4818 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4819 goto leave;
4820 }
4821
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004822 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4823 goto out;
4824
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004825 /* Currently buf cannot be non-empty at this stage. Even if a previous
4826 * sendto() has failed it is emptied to simulate packet emission and
4827 * rely on QUIC lost detection to try to emit it.
4828 */
4829 BUG_ON_HOT(b_data(buf));
4830 b_reset(buf);
4831
4832 if (old_data) {
4833 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
4834 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4835 }
4836
4837 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004838 if (ret == -1) {
4839 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004840 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004841 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004842
Amaury Denoyelle37333862023-02-28 11:53:48 +01004843 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004844 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4845 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004846 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004847 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004848
Amaury Denoyelle37333862023-02-28 11:53:48 +01004849 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004850 status = 1;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004851
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004852 out:
4853 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
4854 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004855 leave:
4856 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4857 return status;
4858}
4859
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004860/* Retransmit up to two datagrams depending on packet number space.
4861 * Return 0 when failed, 0 if not.
4862 */
4863static int qc_dgrams_retransmit(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004864{
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004865 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004866 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4867 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4868 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4869
4870 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4871
4872 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004873 int i;
4874
4875 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4876 struct list ifrms = LIST_HEAD_INIT(ifrms);
4877 struct list hfrms = LIST_HEAD_INIT(hfrms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004878
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004879 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
4880 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4881 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
4882 if (!LIST_ISEMPTY(&ifrms)) {
4883 iqel->pktns->tx.pto_probe = 1;
4884 if (!LIST_ISEMPTY(&hfrms))
4885 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004886 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4887 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms))
4888 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004889 /* Put back unsent frames in their packet number spaces */
4890 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4891 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
4892 }
Frédéric Lécailleec937212023-03-03 17:34:41 +01004893 else {
4894 if (!(qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
4895 iqel->pktns->tx.pto_probe = 1;
4896 if (!qc_send_hdshk_pkts(qc, 0, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4897 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4898 goto leave;
4899 }
4900 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004901 }
4902 TRACE_STATE("no more need to probe Initial packet number space",
4903 QUIC_EV_CONN_TXPKT, qc);
4904 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004905 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004906 }
4907 else {
4908 int i;
4909
4910 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004911 hqel->pktns->tx.pto_probe = 0;
4912 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
Frédéric Lécaille7b5d9b12022-11-28 17:21:45 +01004913 struct list frms1 = LIST_HEAD_INIT(frms1);
4914
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004915 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
4916 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4917 if (!LIST_ISEMPTY(&frms1)) {
4918 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004919 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4920 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4921 goto leave;
4922
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004923 /* Put back unsent frames into their packet number spaces */
4924 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
4925 }
4926 }
4927 TRACE_STATE("no more need to probe Handshake packet number space",
4928 QUIC_EV_CONN_TXPKT, qc);
4929 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4930 }
4931 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4932 struct list frms2 = LIST_HEAD_INIT(frms2);
4933 struct list frms1 = LIST_HEAD_INIT(frms1);
4934
4935 aqel->pktns->tx.pto_probe = 0;
4936 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4937 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4938 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4939 if (!LIST_ISEMPTY(&frms1)) {
4940 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004941 if (!qc_send_app_probing(qc, &frms1))
4942 goto leave;
4943
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004944 /* Put back unsent frames into their packet number spaces */
4945 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
4946 }
4947 if (!LIST_ISEMPTY(&frms2)) {
4948 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004949 if (!qc_send_app_probing(qc, &frms2))
4950 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004951 /* Put back unsent frames into their packet number spaces */
4952 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
4953 }
4954 TRACE_STATE("no more need to probe 01RTT packet number space",
4955 QUIC_EV_CONN_TXPKT, qc);
4956 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4957 }
4958 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004959
4960 ret = 1;
4961 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004962 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004963 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004964}
4965
4966/* QUIC connection packet handler task (post handshake) */
4967struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4968{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004969 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004970 struct quic_enc_level *qel;
4971
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004972 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02004973
4974 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004975 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
4976
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01004977 if (qc_test_fd(qc))
4978 qc_rcv_buf(qc);
4979
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004980 /* Prepare post-handshake frames
4981 * - after connection is instantiated (accept is done)
4982 * - handshake state is completed (may not be the case here in 0-RTT)
4983 */
4984 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) && qc->conn &&
4985 qc->state >= QUIC_HS_ST_COMPLETE) {
4986 quic_build_post_handshake_frames(qc);
4987 }
4988
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004989 /* Retranmissions */
4990 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
4991 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
4992 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004993 if (!qc_dgrams_retransmit(qc))
4994 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004995 }
4996
4997 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4998 qc_rm_hp_pkts(qc, qel);
4999
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005000 if (!qc_treat_rx_pkts(qc, qel, NULL)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005001 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
5002 goto out;
5003 }
5004
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005005 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5006 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
5007 goto out;
5008 }
5009
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005010 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5011 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
5012 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
5013 goto out;
5014 }
5015
5016 /* XXX TODO: how to limit the list frames to send */
5017 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
5018 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
5019 goto out;
5020 }
5021
5022 out:
5023 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
5024 return t;
5025}
5026
5027/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
5028static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
5029{
5030 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
5031 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
5032 qel->pktns->tx.pto_probe ||
5033 !LIST_ISEMPTY(&qel->pktns->tx.frms);
5034}
5035
5036/* QUIC connection packet handler task. */
5037struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
5038{
5039 int ret, ssl_err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005040 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005041 enum quic_tls_enc_level tel, next_tel;
5042 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005043 /* Early-data encryption level */
5044 struct quic_enc_level *eqel;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005045 struct buffer *buf = NULL;
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005046 int st, zero_rtt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005047
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005048 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02005049
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005050 eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005051 st = qc->state;
5052 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
5053
5054 /* Retranmissions */
5055 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
5056 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
5057 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005058 if (!qc_dgrams_retransmit(qc))
5059 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005060 }
5061
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005062 ssl_err = SSL_ERROR_NONE;
5063 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005064 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005065 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01005066
5067 if (qc_test_fd(qc))
5068 qc_rcv_buf(qc);
5069
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005070 if (st >= QUIC_HS_ST_COMPLETE &&
5071 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
5072 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
5073 /* There may be remaining Handshake packets to treat and acknowledge. */
5074 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
5075 next_tel = QUIC_TLS_ENC_LEVEL_APP;
5076 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005077 else if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, zero_rtt))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005078 goto out;
5079
5080 qel = &qc->els[tel];
5081 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
5082
5083 next_level:
5084 /* Treat packets waiting for header packet protection decryption */
5085 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
5086 qc_rm_hp_pkts(qc, qel);
5087
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005088 if (!qc_treat_rx_pkts(qc, qel, next_qel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005089 goto out;
5090
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005091 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5092 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
5093 goto out;
5094 }
5095
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005096 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5097 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
5098 goto out;
5099
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005100 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005101 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005102 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
5103 if (next_qel && next_qel == eqel && zero_rtt) {
5104 TRACE_DEVEL("select 0RTT as next encryption level",
5105 QUIC_EV_CONN_PHPKTS, qc);
5106 qel = next_qel;
5107 next_qel = NULL;
5108 goto next_level;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005109 }
5110
5111 st = qc->state;
5112 if (st >= QUIC_HS_ST_COMPLETE) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005113 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
5114 QUIC_FL_TLS_SECRETS_DCD)) {
5115 /* Discard the Handshake keys. */
5116 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5117 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
5118 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
5119 qc_set_timer(qc);
5120 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5121 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
5122 }
5123
5124 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
5125 /* There may be remaining handshake to build (acks) */
5126 st = QUIC_HS_ST_SERVER_HANDSHAKE;
5127 }
5128 }
5129
5130 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
5131 * be considered.
5132 */
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005133 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005134 goto out;
5135
5136 if (!qc_need_sending(qc, qel) &&
5137 (!next_qel || !qc_need_sending(qc, next_qel))) {
5138 goto skip_send;
5139 }
5140
5141 buf = qc_txb_alloc(qc);
5142 if (!buf)
5143 goto out;
5144
Amaury Denoyelle147862d2023-02-28 15:10:00 +01005145 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
5146 goto skip_send;
5147
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005148 /* Currently buf cannot be non-empty at this stage. Even if a previous
5149 * sendto() has failed it is emptied to simulate packet emission and
5150 * rely on QUIC lost detection to try to emit it.
5151 */
5152 BUG_ON_HOT(b_data(buf));
5153 b_reset(buf);
5154
5155 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
5156 next_tel, &qc->els[next_tel].pktns->tx.frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01005157 if (ret == -1) {
5158 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005159 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005160 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005161
Amaury Denoyelle37333862023-02-28 11:53:48 +01005162 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01005163 if (qc->flags & QUIC_FL_CONN_TO_KILL)
5164 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005165 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005166 }
5167
5168 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005169
5170 skip_send:
5171 /* Check if there is something to do for the next level.
5172 */
5173 if (next_qel && next_qel != qel &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005174 quic_tls_has_rx_sec(next_qel) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005175 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
5176 qel = next_qel;
5177 next_qel = NULL;
5178 goto next_level;
5179 }
5180
5181 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005182 TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
5183 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005184 return t;
5185}
5186
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005187/* Release the memory allocated for <cs> CRYPTO stream */
5188void quic_cstream_free(struct quic_cstream *cs)
5189{
5190 if (!cs) {
5191 /* This is the case for ORTT encryption level */
5192 return;
5193 }
5194
Amaury Denoyellebc174b22022-11-17 10:12:52 +01005195 quic_free_ncbuf(&cs->rx.ncbuf);
5196
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005197 qc_stream_desc_release(cs->desc);
5198 pool_free(pool_head_quic_cstream, cs);
5199}
5200
5201/* Allocate a new QUIC stream for <qc>.
5202 * Return it if succeeded, NULL if not.
5203 */
5204struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
5205{
5206 struct quic_cstream *cs, *ret_cs = NULL;
5207
5208 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5209 cs = pool_alloc(pool_head_quic_cstream);
5210 if (!cs) {
5211 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5212 goto leave;
5213 }
5214
5215 cs->rx.offset = 0;
5216 cs->rx.ncbuf = NCBUF_NULL;
5217 cs->rx.offset = 0;
5218
5219 cs->tx.offset = 0;
5220 cs->tx.sent_offset = 0;
5221 cs->tx.buf = BUF_NULL;
5222 cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
5223 if (!cs->desc) {
5224 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5225 goto err;
5226 }
5227
5228 ret_cs = cs;
5229 leave:
5230 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5231 return ret_cs;
5232
5233 err:
5234 pool_free(pool_head_quic_cstream, cs);
5235 goto leave;
5236}
5237
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005238/* Uninitialize <qel> QUIC encryption level. Never fails. */
5239static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
5240{
5241 int i;
5242
5243 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5244
5245 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
5246 if (qel->tx.crypto.bufs[i]) {
5247 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
5248 qel->tx.crypto.bufs[i] = NULL;
5249 }
5250 }
5251 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005252 quic_cstream_free(qel->cstream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005253
5254 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5255}
5256
5257/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
5258 * connection allocating everything needed.
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005259 *
5260 * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use
5261 * quic_conn_enc_level_uninit() to cleanup partially allocated content.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005262 */
5263static int quic_conn_enc_level_init(struct quic_conn *qc,
5264 enum quic_tls_enc_level level)
5265{
5266 int ret = 0;
5267 struct quic_enc_level *qel;
5268
5269 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5270
5271 qel = &qc->els[level];
5272 qel->level = quic_to_ssl_enc_level(level);
5273 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
5274 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
5275 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
5276 qel->tls_ctx.flags = 0;
5277
5278 qel->rx.pkts = EB_ROOT;
5279 LIST_INIT(&qel->rx.pqpkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005280
5281 /* Allocate only one buffer. */
5282 /* TODO: use a pool */
5283 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
5284 if (!qel->tx.crypto.bufs)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005285 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005286
5287 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
5288 if (!qel->tx.crypto.bufs[0])
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005289 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005290
5291 qel->tx.crypto.bufs[0]->sz = 0;
5292 qel->tx.crypto.nb_buf = 1;
5293
5294 qel->tx.crypto.sz = 0;
5295 qel->tx.crypto.offset = 0;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005296 /* No CRYPTO data for early data TLS encryption level */
5297 if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA)
5298 qel->cstream = NULL;
5299 else {
5300 qel->cstream = quic_cstream_new(qc);
5301 if (!qel->cstream)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005302 goto leave;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005303 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005304
5305 ret = 1;
5306 leave:
5307 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5308 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005309}
5310
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005311/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
5312 * This is not the case if the remote peer address is not validated and if
5313 * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
5314 */
5315static int qc_may_probe_ipktns(struct quic_conn *qc)
5316{
5317 return quic_peer_validated_addr(qc) ||
5318 (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
5319}
5320
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005321/* Callback called upon loss detection and PTO timer expirations. */
5322struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
5323{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005324 struct quic_conn *qc = ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005325 struct quic_pktns *pktns;
5326
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005327 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
5328 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005329 NULL, NULL, &qc->path->ifae_pkts);
5330 task->expire = TICK_ETERNITY;
5331 pktns = quic_loss_pktns(qc);
5332 if (tick_isset(pktns->tx.loss_time)) {
5333 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
5334
5335 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
5336 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005337 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01005338 if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
5339 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005340 goto out;
5341 }
5342
5343 if (qc->path->in_flight) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +01005344 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
Frédéric Lécaille68737312023-04-07 16:28:46 +02005345 if (!pktns->tx.in_flight) {
5346 TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
5347 goto out;
5348 }
5349
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005350 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
5351 if (qc_may_probe_ipktns(qc)) {
5352 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5353 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5354 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5355 }
5356 else {
5357 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5358 }
5359 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
5360 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5361 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5362 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5363 }
Frédéric Lécaillee25fce02023-03-20 17:23:19 +01005364 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005365 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
5366 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5367 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5368 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5369 if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005370 if (qc_may_probe_ipktns(qc)) {
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005371 qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005372 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5373 }
5374 else {
5375 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5376 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005377 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005378 }
5379 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01005380 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005381 /* Wake up upper layer if waiting to send new data. */
5382 if (!qc_notify_send(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005383 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005384 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5385 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005386 }
5387 }
5388 }
5389 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
5390 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
5391 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
5392
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005393 if (quic_tls_has_tx_sec(hel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005394 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005395 if (quic_tls_has_tx_sec(iel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005396 iel->pktns->tx.pto_probe = 1;
5397 }
5398
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005399 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005400 qc->path->loss.pto_count++;
5401
5402 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005403 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
5404 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005405
5406 return task;
5407}
5408
5409/* Parse the Retry token from buffer <token> with <end> a pointer to
5410 * one byte past the end of this buffer. This will extract the ODCID
5411 * which will be stored into <odcid>
5412 *
5413 * Returns 0 on success else non-zero.
5414 */
5415static int parse_retry_token(struct quic_conn *qc,
5416 const unsigned char *token, const unsigned char *end,
5417 struct quic_cid *odcid)
5418{
5419 int ret = 0;
5420 uint64_t odcid_len;
5421 uint32_t timestamp;
5422
5423 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5424
5425 if (!quic_dec_int(&odcid_len, &token, end)) {
5426 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
5427 goto leave;
5428 }
5429
5430 /* RFC 9000 7.2. Negotiating Connection IDs:
5431 * When an Initial packet is sent by a client that has not previously
5432 * received an Initial or Retry packet from the server, the client
5433 * populates the Destination Connection ID field with an unpredictable
5434 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5435 */
5436 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
5437 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
5438 goto leave;
5439 }
5440
5441 if (end - token < odcid_len + sizeof timestamp) {
5442 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
5443 goto leave;
5444 }
5445
5446 timestamp = ntohl(read_u32(token + odcid_len));
5447 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
5448 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
5449 goto leave;
5450 }
5451
5452 ret = 1;
5453 memcpy(odcid->data, token, odcid_len);
5454 odcid->len = odcid_len;
5455 leave:
5456 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5457 return !ret;
5458}
5459
5460/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
5461 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
5462 * for QUIC servers (or haproxy listeners).
5463 * <dcid> is the destination connection ID, <scid> is the source connection ID,
5464 * <token> the token found to be used for this connection with <token_len> as
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005465 * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005466 * Returns the connection if succeeded, NULL if not.
5467 */
5468static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
5469 struct quic_cid *dcid, struct quic_cid *scid,
5470 const struct quic_cid *token_odcid,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005471 struct quic_connection_id *conn_id,
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005472 struct sockaddr_storage *local_addr,
5473 struct sockaddr_storage *peer_addr,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005474 int server, int token, void *owner)
5475{
5476 int i;
5477 struct quic_conn *qc;
5478 /* Initial CID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005479 char *buf_area = NULL;
5480 struct listener *l = NULL;
5481 struct quic_cc_algo *cc_algo = NULL;
5482 struct quic_tls_ctx *ictx;
5483 TRACE_ENTER(QUIC_EV_CONN_INIT);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005484 /* TODO replace pool_zalloc by pool_alloc(). This requires special care
5485 * to properly initialized internal quic_conn members to safely use
5486 * quic_conn_release() on alloc failure.
5487 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005488 qc = pool_zalloc(pool_head_quic_conn);
5489 if (!qc) {
5490 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
5491 goto err;
5492 }
5493
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005494 /* Initialize in priority qc members required for a safe dealloc. */
5495
5496 /* required to use MTLIST_IN_LIST */
5497 MT_LIST_INIT(&qc->accept_list);
5498
5499 LIST_INIT(&qc->rx.pkt_list);
5500
Amaury Denoyelle42448332022-12-12 11:24:05 +01005501 qc_init_fd(qc);
5502
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005503 LIST_INIT(&qc->back_refs);
5504
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005505 /* Now proceeds to allocation of qc members. */
5506
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005507 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
5508 if (!buf_area) {
5509 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
5510 goto err;
5511 }
5512
5513 qc->cids = EB_ROOT;
5514 /* QUIC Server (or listener). */
5515 if (server) {
5516 struct proxy *prx;
5517
5518 l = owner;
5519 prx = l->bind_conf->frontend;
5520 cc_algo = l->bind_conf->quic_cc_algo;
5521
5522 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
5523 &quic_stats_module);
5524 qc->flags |= QUIC_FL_CONN_LISTENER;
5525 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005526 /* Copy the client original DCID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005527 qc->odcid.len = dcid->len;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005528 memcpy(qc->odcid.data, dcid->data, dcid->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005529
5530 /* copy the packet SCID to reuse it as DCID for sending */
5531 if (scid->len)
5532 memcpy(qc->dcid.data, scid->data, scid->len);
5533 qc->dcid.len = scid->len;
5534 qc->tx.buf = BUF_NULL;
5535 qc->li = l;
5536 }
5537 /* QUIC Client (outgoing connection to servers) */
5538 else {
5539 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
5540 if (dcid->len)
5541 memcpy(qc->dcid.data, dcid->data, dcid->len);
5542 qc->dcid.len = dcid->len;
5543 }
5544 qc->mux_state = QC_MUX_NULL;
5545 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
5546
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005547 conn_id->qc = qc;
5548 eb64_insert(&qc->cids, &conn_id->seq_num);
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01005549 /* Initialize the next CID sequence number to be used for this connection. */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005550 qc->next_cid_seq_num = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005551
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005552 if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
5553 is_addr(local_addr)) {
5554 TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
5555 qc_alloc_fd(qc, local_addr, peer_addr);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005556
5557 /* haproxy soft-stop is supported only for QUIC connections
5558 * with their owned socket.
5559 */
5560 if (qc_test_fd(qc))
5561 _HA_ATOMIC_INC(&jobs);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005562 }
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005563
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005564 /* Select our SCID which is the first CID with 0 as sequence number. */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005565 qc->scid = conn_id->cid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005566
5567 /* Packet number spaces initialization. */
5568 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
5569 quic_pktns_init(&qc->pktns[i]);
5570 /* QUIC encryption level context initialization. */
5571 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5572 if (!quic_conn_enc_level_init(qc, i)) {
5573 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
5574 goto err;
5575 }
5576 /* Initialize the packet number space. */
5577 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
5578 }
5579
5580 qc->original_version = qv;
5581 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
5582 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
5583 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
5584 /* TX part. */
5585 LIST_INIT(&qc->tx.frms_to_send);
5586 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
5587 qc->tx.wbuf = qc->tx.rbuf = 0;
5588 qc->tx.bytes = 0;
5589 qc->tx.buf = BUF_NULL;
5590 /* RX part. */
5591 qc->rx.bytes = 0;
5592 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
5593 for (i = 0; i < QCS_MAX_TYPES; i++)
5594 qc->rx.strms[i].nb_streams = 0;
5595
5596 qc->nb_pkt_for_cc = 1;
5597 qc->nb_pkt_since_cc = 0;
5598
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005599 if (!quic_tls_ku_init(qc)) {
5600 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
5601 goto err;
5602 }
5603
5604 /* XXX TO DO: Only one path at this time. */
5605 qc->path = &qc->paths[0];
5606 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
5607
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005608 qc->streams_by_id = EB_ROOT_UNIQUE;
5609 qc->stream_buf_count = 0;
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005610 memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
5611 memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005612
5613 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005614 conn_id->stateless_reset_token,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005615 dcid->data, dcid->len,
5616 qc->scid.data, qc->scid.len, token_odcid))
5617 goto err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005618
Frédéric Lécailledeb97812023-03-22 11:29:45 +01005619 /* Initialize the idle timeout of the connection at the "max_idle_timeout"
5620 * value from local transport parameters.
5621 */
5622 qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005623 qc->wait_event.tasklet = tasklet_new();
5624 if (!qc->wait_event.tasklet) {
5625 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
5626 goto err;
5627 }
5628 qc->wait_event.tasklet->process = quic_conn_io_cb;
5629 qc->wait_event.tasklet->context = qc;
5630 qc->wait_event.events = 0;
Amaury Denoyellebbb1c682022-09-28 15:15:51 +02005631 qc->subs = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005632
5633 if (qc_conn_alloc_ssl_ctx(qc) ||
5634 !quic_conn_init_timer(qc) ||
5635 !quic_conn_init_idle_timer_task(qc))
5636 goto err;
5637
5638 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
5639 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
5640 goto err;
5641
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005642 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
5643 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
5644
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005645 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
5646
5647 return qc;
5648
5649 err:
5650 pool_free(pool_head_quic_conn_rxbuf, buf_area);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005651 if (qc) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005652 qc->rx.buf.area = NULL;
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005653 quic_conn_release(qc);
5654 }
5655 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005656 return NULL;
5657}
5658
5659/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
5660 * The connection tasklet is killed.
5661 *
5662 * This function must only be called by the thread responsible of the quic_conn
5663 * tasklet.
5664 */
5665void quic_conn_release(struct quic_conn *qc)
5666{
5667 int i;
5668 struct ssl_sock_ctx *conn_ctx;
5669 struct eb64_node *node;
5670 struct quic_tls_ctx *app_tls_ctx;
5671 struct quic_rx_packet *pkt, *pktback;
5672
5673 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5674
5675 /* We must not free the quic-conn if the MUX is still allocated. */
5676 BUG_ON(qc->mux_state == QC_MUX_READY);
5677
Amaury Denoyellefb375572023-02-01 09:28:32 +01005678 if (qc_test_fd(qc))
5679 _HA_ATOMIC_DEC(&jobs);
5680
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005681 /* Close quic-conn socket fd. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01005682 qc_release_fd(qc, 0);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005683
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005684 /* in the unlikely (but possible) case the connection was just added to
5685 * the accept_list we must delete it from there.
5686 */
5687 MT_LIST_DELETE(&qc->accept_list);
5688
5689 /* free remaining stream descriptors */
5690 node = eb64_first(&qc->streams_by_id);
5691 while (node) {
5692 struct qc_stream_desc *stream;
5693
5694 stream = eb64_entry(node, struct qc_stream_desc, by_id);
5695 node = eb64_next(node);
5696
5697 /* all streams attached to the quic-conn are released, so
5698 * qc_stream_desc_free will liberate the stream instance.
5699 */
5700 BUG_ON(!stream->release);
5701 qc_stream_desc_free(stream, 1);
5702 }
5703
5704 /* Purge Rx packet list. */
5705 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
5706 LIST_DELETE(&pkt->qc_rx_pkt_list);
5707 pool_free(pool_head_quic_rx_packet, pkt);
5708 }
5709
5710 if (qc->idle_timer_task) {
5711 task_destroy(qc->idle_timer_task);
5712 qc->idle_timer_task = NULL;
5713 }
5714
5715 if (qc->timer_task) {
5716 task_destroy(qc->timer_task);
5717 qc->timer_task = NULL;
5718 }
5719
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005720 if (qc->wait_event.tasklet)
5721 tasklet_free(qc->wait_event.tasklet);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005722
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005723 /* remove the connection from receiver cids trees */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005724 free_quic_conn_cids(qc);
5725
5726 conn_ctx = qc->xprt_ctx;
5727 if (conn_ctx) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005728 SSL_free(conn_ctx->ssl);
5729 pool_free(pool_head_quic_conn_ctx, conn_ctx);
5730 }
5731
5732 quic_tls_ku_free(qc);
5733 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5734 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
5735 quic_conn_enc_level_uninit(qc, &qc->els[i]);
5736 }
5737 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
5738
5739 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
5740 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
5741 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
5742
5743 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
5744 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
5745 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
5746 }
5747
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01005748 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005749
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005750 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
5751 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +02005752 qc = NULL;
Amaury Denoyellefb375572023-02-01 09:28:32 +01005753
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005754 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
5755
5756 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5757}
5758
5759/* Initialize the timer task of <qc> QUIC connection.
5760 * Returns 1 if succeeded, 0 if not.
5761 */
5762static int quic_conn_init_timer(struct quic_conn *qc)
5763{
5764 int ret = 0;
5765 /* Attach this task to the same thread ID used for the connection */
5766 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5767
Amaury Denoyelle66947282023-04-13 11:48:38 +02005768 qc->timer_task = task_new_here();
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005769 if (!qc->timer_task) {
5770 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5771 goto leave;
5772 }
5773
5774 qc->timer = TICK_ETERNITY;
5775 qc->timer_task->process = qc_process_timer;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005776 qc->timer_task->context = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005777
5778 ret = 1;
5779 leave:
5780 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5781 return ret;
5782}
5783
Frédéric Lécailled7215712023-03-24 18:13:37 +01005784/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
5785 * connection. */
5786static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005787{
5788 unsigned int expire;
5789
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005790 if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005791 TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005792 qc->ack_expire = TICK_ETERNITY;
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005793 task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
5794 }
5795 else {
5796 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005797 qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
5798 if (arm_ack) {
5799 /* Arm the ack timer only if not already armed. */
5800 if (!tick_isset(qc->ack_expire)) {
5801 qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
5802 qc->idle_timer_task->expire = qc->ack_expire;
5803 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005804 TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005805 }
5806 }
5807 else {
5808 qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
5809 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005810 TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005811 }
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005812 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005813}
5814
Frédéric Lécailled7215712023-03-24 18:13:37 +01005815/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
5816 * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
5817 * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
5818 * which must be rearmed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005819 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01005820static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005821{
5822 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5823
5824 if (read) {
5825 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5826 }
5827 else {
5828 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5829 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01005830 qc_idle_timer_do_rearm(qc, arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005831
5832 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5833}
5834
5835/* The task handling the idle timeout */
5836struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
5837{
5838 struct quic_conn *qc = ctx;
5839 struct quic_counters *prx_counters = qc->prx_counters;
5840 unsigned int qc_flags = qc->flags;
5841
5842 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5843
Frédéric Lécaille12eca3a2023-04-04 10:46:54 +02005844 if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
5845 goto requeue;
5846
Frédéric Lécailled7215712023-03-24 18:13:37 +01005847 if (tick_is_expired(qc->ack_expire, now_ms)) {
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005848 TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005849 qc->ack_expire = TICK_ETERNITY;
5850 /* Note that ->idle_expire is always set. */
5851 t->expire = qc->idle_expire;
5852 qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
5853 tasklet_wakeup(qc->wait_event.tasklet);
5854 goto requeue;
5855 }
5856
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005857 TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005858 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
5859 * might free the quic-conn too early via quic_close().
5860 */
5861 qc_notify_close(qc);
5862
5863 /* If the MUX is still alive, keep the quic-conn. The MUX is
5864 * responsible to call quic_close to release it.
5865 */
5866 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005867 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005868 quic_conn_release(qc);
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005869 qc = NULL;
5870 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005871
5872 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
5873 * least clean some parts of it such as the tasklet.
5874 */
5875
5876 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
5877 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005878 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005879 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
5880 }
5881
Frédéric Lécailled7215712023-03-24 18:13:37 +01005882 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005883 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5884 return NULL;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005885
5886 requeue:
5887 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5888 return t;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005889}
5890
5891/* Initialize the idle timeout task for <qc>.
5892 * Returns 1 if succeeded, 0 if not.
5893 */
5894static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
5895{
5896 int ret = 0;
5897
5898 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5899
5900 qc->idle_timer_task = task_new_here();
5901 if (!qc->idle_timer_task) {
5902 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5903 goto leave;
5904 }
5905
5906 qc->idle_timer_task->process = qc_idle_timer_task;
5907 qc->idle_timer_task->context = qc;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005908 qc->ack_expire = TICK_ETERNITY;
5909 qc_idle_timer_rearm(qc, 1, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005910 task_queue(qc->idle_timer_task);
5911
5912 ret = 1;
5913 leave:
5914 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5915 return ret;
5916}
5917
5918/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
5919 * past one byte of this buffer.
5920 */
5921static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
5922 struct quic_rx_packet *pkt)
5923{
5924 int ret = 0;
5925 unsigned char dcid_len, scid_len;
5926
5927 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5928
5929 if (end == *buf) {
5930 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
5931 goto leave;
5932 }
5933
5934 /* Destination Connection ID Length */
5935 dcid_len = *(*buf)++;
5936 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
5937 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
5938 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
5939 goto leave;
5940 }
5941
5942 if (dcid_len) {
5943 /* Check that the length of this received DCID matches the CID lengths
5944 * of our implementation for non Initials packets only.
5945 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02005946 if (pkt->version && pkt->version->num &&
5947 pkt->type != QUIC_PACKET_TYPE_INITIAL &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005948 pkt->type != QUIC_PACKET_TYPE_0RTT &&
5949 dcid_len != QUIC_HAP_CID_LEN) {
5950 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
5951 goto leave;
5952 }
5953
5954 memcpy(pkt->dcid.data, *buf, dcid_len);
5955 }
5956
5957 pkt->dcid.len = dcid_len;
5958 *buf += dcid_len;
5959
5960 /* Source Connection ID Length */
5961 scid_len = *(*buf)++;
5962 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
5963 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
5964 goto leave;
5965 }
5966
5967 if (scid_len)
5968 memcpy(pkt->scid.data, *buf, scid_len);
5969 pkt->scid.len = scid_len;
5970 *buf += scid_len;
5971
5972 ret = 1;
5973 leave:
5974 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
5975 return ret;
5976}
5977
5978/* Insert <pkt> RX packet in its <qel> RX packets tree */
5979static void qc_pkt_insert(struct quic_conn *qc,
5980 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
5981{
5982 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
5983
5984 pkt->pn_node.key = pkt->pn;
5985 quic_rx_packet_refinc(pkt);
5986 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
5987
5988 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
5989}
5990
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005991/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
5992 * address of the packet first byte, using the keys from encryption level <el>.
5993 *
5994 * If header protection has been successfully removed, packet data are copied
5995 * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
5996 * proceeded, and the packet is inserted into <qc> protected packets tree. In
5997 * both cases, packet can now be considered handled by the <qc> connection.
5998 *
5999 * If header protection cannot be removed due to <el> secrets already
6000 * discarded, no operation is conducted.
6001 *
6002 * Returns 1 on success : packet data is now handled by the connection. On
6003 * error 0 is returned : packet should be dropped by the caller.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006004 */
6005static inline int qc_try_rm_hp(struct quic_conn *qc,
6006 struct quic_rx_packet *pkt,
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006007 unsigned char *beg,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006008 struct quic_enc_level **el)
6009{
6010 int ret = 0;
6011 unsigned char *pn = NULL; /* Packet number field */
6012 enum quic_tls_enc_level tel;
6013 struct quic_enc_level *qel;
6014 /* Only for traces. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006015
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006016 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006017 BUG_ON(!pkt->pn_offset);
6018
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006019 /* The packet number is here. This is also the start minus
6020 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
6021 * protection.
6022 */
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006023 pn = beg + pkt->pn_offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006024
6025 tel = quic_packet_type_enc_level(pkt->type);
6026 qel = &qc->els[tel];
6027
6028 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01006029 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
6030
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006031 /* Note that the following function enables us to unprotect the packet
6032 * number and its length subsequently used to decrypt the entire
6033 * packets.
6034 */
Frédéric Lécaille72027782023-02-22 16:20:09 +01006035 if (!qc_do_rm_hp(qc, pkt, tls_ctx,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006036 qel->pktns->rx.largest_pn, pn, beg)) {
6037 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
6038 goto out;
6039 }
6040
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006041 qc_handle_spin_bit(qc, pkt, qel);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006042 /* The AAD includes the packet number field. */
6043 pkt->aad_len = pkt->pn_offset + pkt->pnl;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006044 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
6045 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
6046 goto out;
6047 }
6048
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006049 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006050 }
6051 else {
6052 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
6053 /* If the packet number space has been discarded, this packet
6054 * will be not parsed.
6055 */
6056 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
6057 goto out;
6058 }
6059
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006060 TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006061 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
6062 quic_rx_packet_refinc(pkt);
6063 }
6064
6065 *el = qel;
6066 /* No reference counter incrementation here!!! */
6067 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
6068 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
6069 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
6070 b_add(&qc->rx.buf, pkt->len);
6071
6072 ret = 1;
6073 out:
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006074 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006075 return ret;
6076}
6077
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006078/* Return the QUIC version (quic_version struct) with <version> as version number
6079 * if supported or NULL if not.
6080 */
6081static inline const struct quic_version *qc_supported_version(uint32_t version)
6082{
6083 int i;
6084
6085 if (unlikely(!version))
6086 return &quic_version_VN_reserved;
6087
6088 for (i = 0; i < quic_versions_nb; i++)
6089 if (quic_versions[i].num == version)
6090 return &quic_versions[i];
6091
6092 return NULL;
6093}
6094
6095/* Parse a QUIC packet header starting at <buf> without exceeding <end>.
6096 * Version and type are stored in <pkt> packet instance. Type is set to unknown
6097 * on two occasions : for unsupported version, in this case version field is
6098 * set to NULL; for Version Negotiation packet with version number set to 0.
6099 *
6100 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006101 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006102int qc_parse_hd_form(struct quic_rx_packet *pkt,
6103 unsigned char **buf, const unsigned char *end)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006104{
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006105 uint32_t version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006106 int ret = 0;
6107 const unsigned char byte0 = **buf;
6108
6109 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006110 pkt->version = NULL;
6111 pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006112
6113 (*buf)++;
6114 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
6115 unsigned char type =
6116 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
6117
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006118 /* Version */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006119 if (!quic_read_uint32(&version, (const unsigned char **)buf, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006120 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
6121 goto out;
6122 }
6123
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006124 pkt->version = qc_supported_version(version);
6125 if (version && pkt->version) {
6126 if (version != QUIC_PROTOCOL_VERSION_2) {
6127 pkt->type = type;
6128 }
6129 else {
6130 switch (type) {
6131 case 0:
6132 pkt->type = QUIC_PACKET_TYPE_RETRY;
6133 break;
6134 case 1:
6135 pkt->type = QUIC_PACKET_TYPE_INITIAL;
6136 break;
6137 case 2:
6138 pkt->type = QUIC_PACKET_TYPE_0RTT;
6139 break;
6140 case 3:
6141 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
6142 break;
6143 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006144 }
6145 }
6146 }
6147 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006148 if (byte0 & QUIC_PACKET_SPIN_BIT)
6149 pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006150 pkt->type = QUIC_PACKET_TYPE_SHORT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006151 }
6152
6153 ret = 1;
6154 out:
6155 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6156 return ret;
6157}
6158
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006159/*
6160 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
6161 * address <addr>.
6162 * Implementation of RFC9000 6. Version Negotiation
6163 *
6164 * TODO implement a rate-limiting sending of Version Negotiation packets
6165 *
6166 * Returns 0 on success else non-zero
6167 */
6168static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
6169 struct quic_rx_packet *pkt)
6170{
6171 char buf[256];
6172 int ret = 0, i = 0, j;
6173 uint32_t version;
6174 const socklen_t addrlen = get_addr_len(addr);
6175
6176 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6177 /*
6178 * header form
6179 * long header, fixed bit to 0 for Version Negotiation
6180 */
6181 /* TODO: RAND_bytes() should be replaced? */
6182 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
6183 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
6184 goto out;
6185 }
6186
6187 buf[i++] |= '\x80';
6188 /* null version for Version Negotiation */
6189 buf[i++] = '\x00';
6190 buf[i++] = '\x00';
6191 buf[i++] = '\x00';
6192 buf[i++] = '\x00';
6193
6194 /* source connection id */
6195 buf[i++] = pkt->scid.len;
6196 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6197 i += pkt->scid.len;
6198
6199 /* destination connection id */
6200 buf[i++] = pkt->dcid.len;
6201 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
6202 i += pkt->dcid.len;
6203
6204 /* supported version */
6205 for (j = 0; j < quic_versions_nb; j++) {
6206 version = htonl(quic_versions[j].num);
6207 memcpy(&buf[i], &version, sizeof(version));
6208 i += sizeof(version);
6209 }
6210
6211 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
6212 goto out;
6213
6214 ret = 1;
6215 out:
6216 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6217 return !ret;
6218}
6219
6220/* Send a stateless reset packet depending on <pkt> RX packet information
6221 * from <fd> UDP socket to <dst>
6222 * Return 1 if succeeded, 0 if not.
6223 */
6224static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
6225 struct quic_rx_packet *rxpkt)
6226{
6227 int ret = 0, pktlen, rndlen;
6228 unsigned char pkt[64];
6229 const socklen_t addrlen = get_addr_len(dstaddr);
6230 struct proxy *prx;
6231 struct quic_counters *prx_counters;
6232
6233 TRACE_ENTER(QUIC_EV_STATELESS_RST);
6234
6235 prx = l->bind_conf->frontend;
6236 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6237 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
6238 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
6239 * Reset is difficult to distinguish from other packets if the recipient requires
6240 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
6241 * that all packets it sends are at least 22 bytes longer than the minimum
6242 * connection ID length that it requests the peer to include in its packets,
6243 * adding PADDING frames as necessary. This ensures that any Stateless Reset
6244 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
6245 * An endpoint that sends a Stateless Reset in response to a packet that is
6246 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
6247 * than the packet it responds to.
6248 */
6249
6250 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
6251 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
6252 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
6253 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
6254
6255 /* Put a header of random bytes */
6256 /* TODO: RAND_bytes() should be replaced */
6257 if (RAND_bytes(pkt, rndlen) != 1) {
6258 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
6259 goto leave;
6260 }
6261
6262 /* Clear the most significant bit, and set the second one */
6263 *pkt = (*pkt & ~0x80) | 0x40;
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02006264 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006265 rxpkt->dcid.data, rxpkt->dcid.len))
6266 goto leave;
6267
6268 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
6269 goto leave;
6270
6271 ret = 1;
6272 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
6273 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
6274 leave:
6275 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
6276 return ret;
6277}
6278
6279/* QUIC server only function.
6280 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
6281 * This is the responsibility of the caller to check <aad> size is big enough
6282 * to contain these data.
6283 * Return the number of bytes copied to <aad>.
6284 */
6285static int quic_generate_retry_token_aad(unsigned char *aad,
6286 uint32_t version,
6287 const struct quic_cid *cid,
6288 const struct sockaddr_storage *addr)
6289{
6290 unsigned char *p;
6291
6292 p = aad;
6293 memcpy(p, &version, sizeof version);
6294 p += sizeof version;
6295 p += quic_saddr_cpy(p, addr);
6296 memcpy(p, cid->data, cid->len);
6297 p += cid->len;
6298
6299 return p - aad;
6300}
6301
6302/* QUIC server only function.
6303 * Generate the token to be used in Retry packets. The token is written to
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05006304 * <buf> with <len> as length. <odcid> is the original destination connection
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006305 * ID and <dcid> is our side destination connection ID (or client source
6306 * connection ID).
6307 * Returns the length of the encoded token or 0 on error.
6308 */
6309static int quic_generate_retry_token(unsigned char *buf, size_t len,
6310 const uint32_t version,
6311 const struct quic_cid *odcid,
6312 const struct quic_cid *dcid,
6313 struct sockaddr_storage *addr)
6314{
6315 int ret = 0;
6316 unsigned char *p;
6317 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006318 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006319 size_t aadlen;
6320 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
6321 unsigned char key[QUIC_TLS_KEY_LEN];
6322 unsigned char iv[QUIC_TLS_IV_LEN];
6323 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6324 size_t seclen = strlen(global.cluster_secret);
6325 EVP_CIPHER_CTX *ctx = NULL;
6326 const EVP_CIPHER *aead = EVP_aes_128_gcm();
6327 uint32_t timestamp = now_ms;
6328
6329 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6330
6331 /* We copy the odcid into the token, prefixed by its one byte
6332 * length, the format token byte. It is followed by an AEAD TAG, and finally
6333 * the random bytes used to derive the secret to encrypt the token.
6334 */
6335 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
6336 goto err;
6337
6338 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
6339 /* TODO: RAND_bytes() should be replaced */
6340 if (RAND_bytes(salt, sizeof salt) != 1) {
6341 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
6342 goto err;
6343 }
6344
6345 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6346 salt, sizeof salt, sec, seclen)) {
6347 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
6348 goto err;
6349 }
6350
6351 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
6352 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
6353 goto err;
6354 }
6355
6356 /* Token build */
6357 p = buf;
6358 *p++ = QUIC_TOKEN_FMT_RETRY,
6359 *p++ = odcid->len;
6360 memcpy(p, odcid->data, odcid->len);
6361 p += odcid->len;
6362 write_u32(p, htonl(timestamp));
6363 p += sizeof timestamp;
6364
6365 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
6366 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
6367 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
6368 goto err;
6369 }
6370
6371 p += QUIC_TLS_TAG_LEN;
6372 memcpy(p, salt, sizeof salt);
6373 p += sizeof salt;
6374 EVP_CIPHER_CTX_free(ctx);
6375
6376 ret = p - buf;
6377 leave:
6378 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6379 return ret;
6380
6381 err:
6382 if (ctx)
6383 EVP_CIPHER_CTX_free(ctx);
6384 goto leave;
6385}
6386
6387/* QUIC server only function.
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006388 *
6389 * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
6390 * the UDP datagram containing <pkt> and <l> is the listener instance on which
6391 * it was received. If the token is valid, the ODCID of <qc> QUIC connection
6392 * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
6393 * to validate the token but it can be NULL : in this case the version will be
6394 * retrieved from the packet.
6395 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006396 * Return 1 if succeeded, 0 if not.
6397 */
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006398
6399static int quic_retry_token_check(struct quic_rx_packet *pkt,
6400 struct quic_dgram *dgram,
6401 struct listener *l,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006402 struct quic_conn *qc,
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006403 struct quic_cid *odcid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006404{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006405 struct proxy *prx;
6406 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006407 int ret = 0;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006408 unsigned char *token = pkt->token;
6409 const uint64_t tokenlen = pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006410 unsigned char buf[128];
6411 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006412 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006413 size_t aadlen;
6414 const unsigned char *salt;
6415 unsigned char key[QUIC_TLS_KEY_LEN];
6416 unsigned char iv[QUIC_TLS_IV_LEN];
6417 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6418 size_t seclen = strlen(global.cluster_secret);
6419 EVP_CIPHER_CTX *ctx = NULL;
6420 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006421 const struct quic_version *qv = qc ? qc->original_version :
6422 pkt->version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006423
6424 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6425
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006426 /* The caller must ensure this. */
6427 BUG_ON(!global.cluster_secret || !pkt->token_len);
6428
6429 prx = l->bind_conf->frontend;
6430 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6431
6432 if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
6433 /* TODO: New token check */
6434 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6435 goto leave;
6436 }
6437
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006438 if (sizeof buf < tokenlen) {
6439 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
6440 goto err;
6441 }
6442
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006443 aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006444 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
6445 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6446 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
6447 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
6448 goto err;
6449 }
6450
6451 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
6452 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
6453 goto err;
6454 }
6455
6456 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
6457 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
6458 ctx, aead, key, iv)) {
6459 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
6460 goto err;
6461 }
6462
6463 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
6464 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
6465 goto err;
6466 }
6467
6468 EVP_CIPHER_CTX_free(ctx);
6469
6470 ret = 1;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006471 HA_ATOMIC_INC(&prx_counters->retry_validated);
6472
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006473 leave:
6474 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6475 return ret;
6476
6477 err:
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006478 HA_ATOMIC_INC(&prx_counters->retry_error);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006479 if (ctx)
6480 EVP_CIPHER_CTX_free(ctx);
6481 goto leave;
6482}
6483
6484/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
6485 * the Initial <pkt> packet.
6486 *
6487 * Returns 0 on success else non-zero.
6488 */
6489static int send_retry(int fd, struct sockaddr_storage *addr,
6490 struct quic_rx_packet *pkt, const struct quic_version *qv)
6491{
6492 int ret = 0;
6493 unsigned char buf[128];
6494 int i = 0, token_len;
6495 const socklen_t addrlen = get_addr_len(addr);
6496 struct quic_cid scid;
6497
6498 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6499
6500 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
6501 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
6502 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
6503 /* version */
6504 buf[i++] = *((unsigned char *)&qv->num + 3);
6505 buf[i++] = *((unsigned char *)&qv->num + 2);
6506 buf[i++] = *((unsigned char *)&qv->num + 1);
6507 buf[i++] = *(unsigned char *)&qv->num;
6508
6509 /* Use the SCID from <pkt> for Retry DCID. */
6510 buf[i++] = pkt->scid.len;
6511 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6512 i += pkt->scid.len;
6513
6514 /* Generate a new CID to be used as SCID for the Retry packet. */
6515 scid.len = QUIC_HAP_CID_LEN;
6516 /* TODO: RAND_bytes() should be replaced */
6517 if (RAND_bytes(scid.data, scid.len) != 1) {
6518 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
6519 goto out;
6520 }
6521
6522 buf[i++] = scid.len;
6523 memcpy(&buf[i], scid.data, scid.len);
6524 i += scid.len;
6525
6526 /* token */
6527 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
6528 &pkt->dcid, &pkt->scid, addr))) {
6529 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
6530 goto out;
6531 }
6532
6533 i += token_len;
6534
6535 /* token integrity tag */
6536 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
6537 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
6538 pkt->dcid.len, buf, i, qv)) {
6539 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6540 goto out;
6541 }
6542
6543 i += QUIC_TLS_TAG_LEN;
6544
6545 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
6546 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6547 goto out;
6548 }
6549
6550 ret = 1;
6551 out:
6552 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6553 return !ret;
6554}
6555
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006556/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
6557 * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
6558 * is used.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006559 *
6560 * Returns the instance or NULL if not found.
6561 */
6562static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
6563 struct listener *l,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006564 struct sockaddr_storage *saddr,
6565 int *new_tid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006566{
6567 struct quic_conn *qc = NULL;
6568 struct ebmb_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006569 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006570 struct quic_cid_tree *tree;
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006571 uint conn_id_tid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006572
6573 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006574 *new_tid = -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006575
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006576 /* First look into DCID tree. */
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006577 tree = &quic_cid_trees[_quic_cid_tree_idx(pkt->dcid.data)];
6578 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6579 node = ebmb_lookup(&tree->root, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006580
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006581 /* If not found on an Initial/0-RTT packet, it could be because an
6582 * ODCID is reused by the client. Calculate the derived CID value to
6583 * retrieve it from the DCID tree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006584 */
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006585 if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
6586 pkt->type == QUIC_PACKET_TYPE_0RTT)) {
Amaury Denoyellec2a92642023-04-13 15:26:18 +02006587 const struct quic_cid derive_cid = quic_derive_cid(&pkt->dcid, saddr);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006588
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006589 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
6590
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006591 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
6592 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6593 node = ebmb_lookup(&tree->root, derive_cid.data, derive_cid.len);
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006594 }
6595
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006596 if (!node)
6597 goto end;
6598
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006599 conn_id = ebmb_entry(node, struct quic_connection_id, node);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006600 conn_id_tid = HA_ATOMIC_LOAD(&conn_id->tid);
6601 if (conn_id_tid != tid) {
6602 *new_tid = conn_id_tid;
6603 goto end;
6604 }
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006605 qc = conn_id->qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006606
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006607 end:
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006608 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006609 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6610 return qc;
6611}
6612
6613/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
6614 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
6615 * parameters of this session.
6616 * This is the responsibility of the caller to check the validity of all the
6617 * pointers passed as parameter to this function.
6618 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
6619 * CO_ER_SSL_NO_MEM.
6620 */
6621static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
6622 unsigned char *params, size_t params_len)
6623{
6624 int retry, ret = -1;
6625
6626 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6627
6628 retry = 1;
6629 retry:
6630 *ssl = SSL_new(ssl_ctx);
6631 if (!*ssl) {
6632 if (!retry--)
6633 goto err;
6634
6635 pool_gc(NULL);
6636 goto retry;
6637 }
6638
6639 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
6640 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
6641 SSL_free(*ssl);
6642 *ssl = NULL;
6643 if (!retry--)
6644 goto err;
6645
6646 pool_gc(NULL);
6647 goto retry;
6648 }
6649
6650 ret = 0;
6651 leave:
6652 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6653 return ret;
6654
6655 err:
6656 qc->conn->err_code = CO_ER_SSL_NO_MEM;
6657 goto leave;
6658}
6659
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006660/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
6661 * used to process <qc> received packets. The allocated context is stored in
6662 * <qc.xprt_ctx>.
6663 *
6664 * Returns 0 on success else non-zero.
6665 */
6666static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
6667{
6668 int ret = 0;
6669 struct bind_conf *bc = qc->li->bind_conf;
6670 struct ssl_sock_ctx *ctx = NULL;
6671
6672 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6673
6674 ctx = pool_zalloc(pool_head_quic_conn_ctx);
6675 if (!ctx) {
6676 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
6677 goto err;
6678 }
6679
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006680 ctx->subs = NULL;
6681 ctx->xprt_ctx = NULL;
6682 ctx->qc = qc;
6683
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006684 if (qc_is_listener(qc)) {
6685 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
6686 qc->enc_params, qc->enc_params_len) == -1) {
6687 goto err;
6688 }
6689#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6690 /* Enabling 0-RTT */
6691 if (bc->ssl_conf.early_data)
6692 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
6693#endif
6694
6695 SSL_set_accept_state(ctx->ssl);
6696 }
6697
6698 ctx->xprt = xprt_get(XPRT_QUIC);
6699
6700 /* Store the allocated context in <qc>. */
6701 qc->xprt_ctx = ctx;
6702
6703 ret = 1;
6704 leave:
6705 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6706 return !ret;
6707
6708 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006709 pool_free(pool_head_quic_conn_ctx, ctx);
6710 goto leave;
6711}
6712
6713/* Check that all the bytes between <buf> included and <end> address
6714 * excluded are null. This is the responsibility of the caller to
6715 * check that there is at least one byte between <buf> end <end>.
6716 * Return 1 if this all the bytes are null, 0 if not.
6717 */
6718static inline int quic_padding_check(const unsigned char *buf,
6719 const unsigned char *end)
6720{
6721 while (buf < end && !*buf)
6722 buf++;
6723
6724 return buf == end;
6725}
6726
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006727/* Find the associated connection to the packet <pkt> or create a new one if
6728 * this is an Initial packet. <dgram> is the datagram containing the packet and
6729 * <l> is the listener instance on which it was received.
6730 *
Amaury Denoyelle25174d52023-04-05 17:52:05 +02006731 * By default, <new_tid> is set to -1. However, if thread affinity has been
6732 * chanbed, it will be set to its new thread ID.
6733 *
6734 * Returns the quic-conn instance or NULL if not found or thread affinity
6735 * changed.
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006736 */
6737static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
6738 struct quic_dgram *dgram,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006739 struct listener *l,
6740 int *new_tid)
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006741{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006742 struct quic_cid token_odcid = { .len = 0 };
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006743 struct quic_conn *qc = NULL;
6744 struct proxy *prx;
6745 struct quic_counters *prx_counters;
6746
6747 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6748
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006749 *new_tid = -1;
6750
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006751 prx = l->bind_conf->frontend;
6752 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6753
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006754 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr, new_tid);
6755
Amaury Denoyelle25174d52023-04-05 17:52:05 +02006756 /* If connection already created or rebinded on another thread. */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006757 if (!qc && *new_tid != -1 && tid != *new_tid)
6758 goto out;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006759
6760 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6761 BUG_ON(!pkt->version); /* This must not happen. */
6762
6763 if (global.cluster_secret && pkt->token_len) {
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006764 if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
6765 goto err;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006766 }
6767
6768 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006769 struct quic_cid_tree *tree;
6770 struct ebmb_node *node;
6771 struct quic_connection_id *conn_id;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006772 int ipv4;
6773
6774 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
6775 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
6776 TRACE_PROTO("Initial without token, sending retry",
6777 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6778 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
6779 TRACE_ERROR("Error during Retry generation",
6780 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6781 goto out;
6782 }
6783
6784 HA_ATOMIC_INC(&prx_counters->retry_sent);
6785 goto out;
6786 }
6787
6788 /* RFC 9000 7.2. Negotiating Connection IDs:
6789 * When an Initial packet is sent by a client that has not previously
6790 * received an Initial or Retry packet from the server, the client
6791 * populates the Destination Connection ID field with an unpredictable
6792 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6793 */
6794 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
6795 TRACE_PROTO("dropped packet",
6796 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6797 goto err;
6798 }
6799
6800 pkt->saddr = dgram->saddr;
6801 ipv4 = dgram->saddr.ss_family == AF_INET;
6802
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006803 /* Generate the first connection CID. This is derived from the client
6804 * ODCID and address. This allows to retrieve the connection from the
6805 * ODCID without storing it in the CID tree. This is an interesting
6806 * optimization as the client is expected to stop using its ODCID in
6807 * favor of our generated value.
6808 */
6809 conn_id = new_quic_cid(NULL, NULL, &pkt->dcid, &pkt->saddr);
6810 if (!conn_id)
6811 goto err;
6812
6813 tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)];
6814 HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock);
6815 node = ebmb_insert(&tree->root, &conn_id->node, conn_id->cid.len);
6816 if (node != &conn_id->node) {
6817 pool_free(pool_head_quic_connection_id, conn_id);
6818
6819 conn_id = ebmb_entry(node, struct quic_connection_id, node);
6820 *new_tid = HA_ATOMIC_LOAD(&conn_id->tid);
6821 }
6822 HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock);
6823
6824 if (*new_tid != -1)
6825 goto out;
6826
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006827 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006828 conn_id, &dgram->daddr, &pkt->saddr, 1,
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006829 !!pkt->token_len, l);
6830 if (qc == NULL)
6831 goto err;
6832
6833 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006834 }
6835 }
6836 else if (!qc) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006837 TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006838 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
6839 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
6840 goto err;
6841 }
6842
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006843 out:
6844 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6845 return qc;
6846
6847 err:
6848 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
6849 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
6850 return NULL;
6851}
6852
Amaury Denoyelle98289692022-10-19 15:37:44 +02006853/* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even
6854 * if the packet is incomplete. This function will populate fields of <pkt>
6855 * instance, most notably its length. <dgram> is the UDP datagram which
6856 * contains the parsed packet. <l> is the listener instance on which it was
6857 * received.
6858 *
6859 * Returns 0 on success else non-zero. Packet length is guaranteed to be set to
6860 * the real packet value or to cover all data between <buf> and <end> : this is
6861 * useful to reject a whole datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006862 */
Amaury Denoyelle98289692022-10-19 15:37:44 +02006863static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
6864 unsigned char *buf, const unsigned char *end,
6865 struct quic_dgram *dgram, struct listener *l)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006866{
Amaury Denoyelle98289692022-10-19 15:37:44 +02006867 const unsigned char *beg = buf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006868 struct proxy *prx;
6869 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006870 const struct quic_version *qv = NULL;
6871
6872 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6873
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006874 prx = l->bind_conf->frontend;
6875 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6876 /* This ist only to please to traces and distinguish the
6877 * packet with parsed packet number from others.
6878 */
6879 pkt->pn_node.key = (uint64_t)-1;
6880 if (end <= buf) {
6881 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6882 goto drop;
6883 }
6884
6885 /* Fixed bit */
6886 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006887 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
6888 quic_padding_check(buf, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006889 /* Some browsers may pad the remaining datagram space with null bytes.
6890 * That is what we called add padding out of QUIC packets. Such
6891 * datagrams must be considered as valid. But we can only consume
6892 * the remaining space.
6893 */
6894 pkt->len = end - buf;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006895 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006896 }
6897
6898 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6899 goto drop;
6900 }
6901
6902 /* Header form */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006903 if (!qc_parse_hd_form(pkt, &buf, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006904 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6905 goto drop;
6906 }
6907
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006908 if (pkt->type != QUIC_PACKET_TYPE_SHORT) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006909 uint64_t len;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006910 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006911
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006912 if (!quic_packet_read_long_header(&buf, end, pkt)) {
6913 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6914 goto drop;
6915 }
6916
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006917 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6918 * they must have the same DCID.
6919 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006920 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006921 (pkt->dcid.len != dgram->dcid_len ||
6922 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006923 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006924 goto drop;
6925 }
6926
6927 /* Retry of Version Negotiation packets are only sent by servers */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006928 if (pkt->type == QUIC_PACKET_TYPE_RETRY ||
6929 (pkt->version && !pkt->version->num)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006930 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6931 goto drop;
6932 }
6933
6934 /* RFC9000 6. Version Negotiation */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006935 if (!pkt->version) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006936 /* unsupported version, send Negotiation packet */
6937 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
6938 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006939 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006940 }
6941
6942 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006943 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006944 }
6945
6946 /* For Initial packets, and for servers (QUIC clients connections),
6947 * there is no Initial connection IDs storage.
6948 */
6949 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6950 uint64_t token_len;
6951
6952 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
6953 end - buf < token_len) {
6954 TRACE_PROTO("Packet dropped",
6955 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6956 goto drop;
6957 }
6958
6959 /* TODO Retry should be automatically activated if
6960 * suspect network usage is detected.
6961 */
6962 if (global.cluster_secret && !token_len) {
6963 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
6964 TRACE_PROTO("Initial without token, sending retry",
Amaury Denoyelle90121b32022-09-27 10:35:29 +02006965 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006966 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
6967 TRACE_PROTO("Error during Retry generation",
Amaury Denoyelle90121b32022-09-27 10:35:29 +02006968 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006969 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006970 }
6971
6972 HA_ATOMIC_INC(&prx_counters->retry_sent);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006973 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006974 }
6975 }
6976 else if (!global.cluster_secret && token_len) {
6977 /* Impossible case: a token was received without configured
6978 * cluster secret.
6979 */
6980 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
6981 NULL, NULL, NULL, qv);
6982 goto drop;
6983 }
6984
6985 pkt->token = buf;
6986 pkt->token_len = token_len;
6987 buf += pkt->token_len;
6988 }
6989 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
6990 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
6991 TRACE_PROTO("Packet dropped",
6992 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6993 goto drop;
6994 }
6995 }
6996
6997 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
6998 end - buf < len) {
6999 TRACE_PROTO("Packet dropped",
7000 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
7001 goto drop;
7002 }
7003
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007004 /* Packet Number is stored here. Packet Length totalizes the
7005 * rest of the content.
7006 */
7007 pkt->pn_offset = buf - beg;
7008 pkt->len = pkt->pn_offset + len;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007009
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007010 /* RFC 9000. Initial Datagram Size
7011 *
7012 * A server MUST discard an Initial packet that is carried in a UDP datagram
7013 * with a payload that is smaller than the smallest allowed maximum datagram
7014 * size of 1200 bytes.
7015 */
7016 if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
7017 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007018 TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007019 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
7020 goto drop;
7021 }
7022
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007023 /* Interrupt parsing after packet length retrieval : this
7024 * ensures that only the packet is dropped but not the whole
7025 * datagram.
7026 */
7027 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007028 TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007029 goto drop;
7030 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007031 }
7032 else {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007033 TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007034 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007035 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007036 goto drop;
7037 }
7038
7039 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
7040 pkt->dcid.len = QUIC_HAP_CID_LEN;
7041
7042 /* When multiple QUIC packets are coalesced on the same UDP datagram,
7043 * they must have the same DCID.
7044 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007045 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007046 (pkt->dcid.len != dgram->dcid_len ||
7047 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007048 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007049 goto drop;
7050 }
7051
7052 buf += QUIC_HAP_CID_LEN;
7053
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007054 pkt->pn_offset = buf - beg;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007055 /* A short packet is the last one of a UDP datagram. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007056 pkt->len = end - beg;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007057 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007058
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007059 TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv);
7060 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007061 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007062
Amaury Denoyelle98289692022-10-19 15:37:44 +02007063 drop:
7064 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007065 drop_silent:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007066 if (!pkt->len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007067 pkt->len = end - beg;
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007068 TRACE_PROTO("RX pkt parsing failed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv);
7069 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007070 return -1;
7071}
7072
7073/* Check if received packet <pkt> should be drop due to <qc> already in closing
7074 * state. This can be true if a CONNECTION_CLOSE has already been emitted for
7075 * this connection.
7076 *
7077 * Returns false if connection is not in closing state else true. The caller
7078 * should drop the whole datagram in the last case to not mess up <qc>
7079 * CONNECTION_CLOSE rate limit counter.
7080 */
7081static int qc_rx_check_closing(struct quic_conn *qc,
7082 struct quic_rx_packet *pkt)
7083{
7084 if (!(qc->flags & QUIC_FL_CONN_CLOSING))
7085 return 0;
7086
7087 TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
7088
7089 /* Check if CONNECTION_CLOSE rate reemission is reached. */
7090 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
7091 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
7092 qc->nb_pkt_for_cc++;
7093 qc->nb_pkt_since_cc = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007094 }
7095
Amaury Denoyelle98289692022-10-19 15:37:44 +02007096 return 1;
7097}
7098
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007099/* React to a connection migration initiated on <qc> by a client with the new
7100 * path addresses <peer_addr>/<local_addr>.
7101 *
7102 * Returns 0 on success else non-zero.
7103 */
7104static int qc_handle_conn_migration(struct quic_conn *qc,
7105 const struct sockaddr_storage *peer_addr,
7106 const struct sockaddr_storage *local_addr)
7107{
7108 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7109
Frédéric Lécaille6fc86972023-01-12 08:29:23 +01007110 /* RFC 9000. Connection Migration
7111 *
7112 * If the peer sent the disable_active_migration transport parameter,
7113 * an endpoint also MUST NOT send packets (including probing packets;
7114 * see Section 9.1) from a different local address to the address the peer
7115 * used during the handshake, unless the endpoint has acted on a
7116 * preferred_address transport parameter from the peer.
7117 */
7118 if (qc->li->bind_conf->quic_params.disable_active_migration) {
7119 TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
7120 goto err;
7121 }
7122
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007123 /* RFC 9000 9. Connection Migration
7124 *
Amaury Denoyelleeb6be982022-11-21 11:14:45 +01007125 * The design of QUIC relies on endpoints retaining a stable address for
7126 * the duration of the handshake. An endpoint MUST NOT initiate
7127 * connection migration before the handshake is confirmed, as defined in
7128 * Section 4.1.2 of [QUIC-TLS].
7129 */
7130 if (qc->state < QUIC_HS_ST_COMPLETE) {
7131 TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
7132 goto err;
7133 }
7134
7135 /* RFC 9000 9. Connection Migration
7136 *
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007137 * TODO
7138 * An endpoint MUST
7139 * perform path validation (Section 8.2) if it detects any change to a
7140 * peer's address, unless it has previously validated that address.
7141 */
7142
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007143 /* Update quic-conn owned socket if in used.
7144 * TODO try to reuse it instead of closing and opening a new one.
7145 */
7146 if (qc_test_fd(qc)) {
7147 /* TODO try to reuse socket instead of closing it and opening a new one. */
7148 TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
7149 qc_release_fd(qc, 1);
Amaury Denoyellefb375572023-02-01 09:28:32 +01007150 /* TODO need to adjust <jobs> on socket allocation failure. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007151 qc_alloc_fd(qc, local_addr, peer_addr);
7152 }
7153
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007154 qc->local_addr = *local_addr;
7155 qc->peer_addr = *peer_addr;
7156 HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done);
7157
7158 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7159 return 0;
7160
7161 err:
7162 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7163 return 1;
7164}
7165
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007166/* Release the memory for the RX packets which are no more referenced
7167 * and consume their payloads which have been copied to the RX buffer
7168 * for the connection.
7169 * Always succeeds.
7170 */
7171static inline void quic_rx_pkts_del(struct quic_conn *qc)
7172{
7173 struct quic_rx_packet *pkt, *pktback;
7174
7175 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
7176 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007177 "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007178 (long long)pkt->pn_node.key,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007179 pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007180 (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
7181 if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
7182 size_t cdata;
7183
7184 cdata = b_contig_data(&qc->rx.buf, 0);
7185 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007186 "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007187 if (cdata && !*b_head(&qc->rx.buf)) {
7188 /* Consume the remaining data */
7189 b_del(&qc->rx.buf, cdata);
7190 }
7191 break;
7192 }
7193
7194 if (pkt->refcnt)
7195 break;
7196
7197 b_del(&qc->rx.buf, pkt->raw_len);
7198 LIST_DELETE(&pkt->qc_rx_pkt_list);
7199 pool_free(pool_head_quic_rx_packet, pkt);
7200 }
7201
7202 /* In frequent cases the buffer will be emptied at this stage. */
7203 b_realign_if_empty(&qc->rx.buf);
7204}
7205
Amaury Denoyelle98289692022-10-19 15:37:44 +02007206/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
7207 * into <qc> receive buffer after header protection removal procedure.
7208 *
7209 * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
7210 * must point to packet buffer first byte.
7211 *
7212 * <tasklist_head> may be non-NULL when the caller treat several datagrams for
7213 * different quic-conn. In this case, each quic-conn tasklet will be appended
7214 * to it in order to be woken up after the current task.
7215 *
7216 * The caller can safely removed the packet data. If packet refcount was not
7217 * incremented by this function, it means that the connection did not handled
7218 * it and it should be freed by the caller.
7219 */
7220static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
7221 struct quic_dgram *dgram, unsigned char *beg,
7222 struct list **tasklist_head)
7223{
7224 const struct quic_version *qv = pkt->version;
7225 struct quic_enc_level *qel = NULL;
7226 size_t b_cspace;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007227
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007228 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7229 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Amaury Denoyelle3f474e62022-11-24 17:15:08 +01007230
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007231 if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007232 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
7233 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
7234 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007235 TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
7236 QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007237 /* Reset the anti-amplification bit. It will be set again
7238 * when sending the next packet if reached again.
7239 */
7240 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007241 qc_set_timer(qc);
7242 if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
7243 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007244 }
7245
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007246 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
7247 TRACE_PROTO("Connection error",
7248 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7249 goto out;
7250 }
7251
7252 pkt->raw_len = pkt->len;
7253 quic_rx_pkts_del(qc);
7254 b_cspace = b_contig_space(&qc->rx.buf);
7255 if (b_cspace < pkt->len) {
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007256 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007257 "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007258 /* Do not consume buf if space not at the end. */
7259 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
7260 TRACE_PROTO("Packet dropped",
7261 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007262 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007263 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007264 }
7265
7266 /* Let us consume the remaining contiguous space. */
7267 if (b_cspace) {
7268 b_putchr(&qc->rx.buf, 0x00);
7269 b_cspace--;
7270 }
7271 b_add(&qc->rx.buf, b_cspace);
7272 if (b_contig_space(&qc->rx.buf) < pkt->len) {
7273 TRACE_PROTO("Too big packet",
7274 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007275 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007276 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007277 }
7278 }
7279
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007280 if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007281 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7282 goto drop;
7283 }
7284
7285 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7286 if (pkt->aad_len)
7287 qc_pkt_insert(qc, pkt, qel);
7288 out:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02007289 *tasklist_head = tasklet_wakeup_after(*tasklist_head,
7290 qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007291
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007292 drop_silent:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007293 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7294 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007295 return;
7296
7297 drop:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007298 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007299 TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7300 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007301}
7302
7303/* This function builds into <buf> buffer a QUIC long packet header.
7304 * Return 1 if enough room to build this header, 0 if not.
7305 */
7306static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
7307 int type, size_t pn_len,
7308 struct quic_conn *qc, const struct quic_version *ver)
7309{
7310 int ret = 0;
7311
7312 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7313
7314 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
7315 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7316 goto leave;
7317 }
7318
7319 type = quic_pkt_type(type, ver->num);
7320 /* #0 byte flags */
7321 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
7322 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
7323 /* Version */
7324 quic_write_uint32(buf, end, ver->num);
7325 *(*buf)++ = qc->dcid.len;
7326 /* Destination connection ID */
7327 if (qc->dcid.len) {
7328 memcpy(*buf, qc->dcid.data, qc->dcid.len);
7329 *buf += qc->dcid.len;
7330 }
7331 /* Source connection ID */
7332 *(*buf)++ = qc->scid.len;
7333 if (qc->scid.len) {
7334 memcpy(*buf, qc->scid.data, qc->scid.len);
7335 *buf += qc->scid.len;
7336 }
7337
7338 ret = 1;
7339 leave:
7340 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7341 return ret;
7342}
7343
7344/* This function builds into <buf> buffer a QUIC short packet header.
7345 * Return 1 if enough room to build this header, 0 if not.
7346 */
7347static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
7348 size_t pn_len, struct quic_conn *qc,
7349 unsigned char tls_flags)
7350{
7351 int ret = 0;
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007352 unsigned char spin_bit =
7353 (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007354
7355 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7356
7357 if (end - *buf < 1 + qc->dcid.len) {
7358 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7359 goto leave;
7360 }
7361
7362 /* #0 byte flags */
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007363 *(*buf)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007364 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
7365 /* Destination connection ID */
7366 if (qc->dcid.len) {
7367 memcpy(*buf, qc->dcid.data, qc->dcid.len);
7368 *buf += qc->dcid.len;
7369 }
7370
7371 ret = 1;
7372 leave:
7373 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7374 return ret;
7375}
7376
7377/* Apply QUIC header protection to the packet with <buf> as first byte address,
7378 * <pn> as address of the Packet number field, <pnlen> being this field length
7379 * with <aead> as AEAD cipher and <key> as secret key.
7380 * Returns 1 if succeeded or 0 if failed.
7381 */
7382static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
7383 unsigned char *pn, size_t pnlen,
7384 struct quic_tls_ctx *tls_ctx)
7385
7386{
7387 int i, ret = 0;
7388 /* We need an IV of at least 5 bytes: one byte for bytes #0
7389 * and at most 4 bytes for the packet number
7390 */
7391 unsigned char mask[5] = {0};
7392 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
7393
7394 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7395
7396 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
7397 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
7398 goto out;
7399 }
7400
7401 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
7402 for (i = 0; i < pnlen; i++)
7403 pn[i] ^= mask[i + 1];
7404
7405 ret = 1;
7406 out:
7407 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7408 return ret;
7409}
7410
7411/* Reduce the encoded size of <ack_frm> ACK frame removing the last
7412 * ACK ranges if needed to a value below <limit> in bytes.
7413 * Return 1 if succeeded, 0 if not.
7414 */
7415static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
7416 struct quic_frame *ack_frm, size_t limit)
7417{
7418 size_t room, ack_delay_sz;
7419 int ret = 0;
7420
7421 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7422
7423 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
Frédéric Lécaille9d68c6a2023-04-12 20:49:29 +02007424 if (limit <= ack_delay_sz - 1)
7425 goto leave;
7426
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007427 /* A frame is made of 1 byte for the frame type. */
7428 room = limit - ack_delay_sz - 1;
7429 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
7430 goto leave;
7431
7432 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
7433 leave:
7434 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7435 return ret;
7436}
7437
7438/* Prepare into <outlist> as most as possible ack-eliciting frame from their
7439 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
7440 * with <room> as available room, and <*len> the packet Length field initialized
7441 * with the number of bytes already present in this buffer which must be taken
7442 * into an account for the Length packet field value. <headlen> is the number of
7443 * bytes already present in this packet before building frames.
7444 *
7445 * Update consequently <*len> to reflect the size of these frames built
7446 * by this function. Also attach these frames to <l> frame list.
7447 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
7448 */
7449static inline int qc_build_frms(struct list *outlist, struct list *inlist,
7450 size_t room, size_t *len, size_t headlen,
7451 struct quic_enc_level *qel,
7452 struct quic_conn *qc)
7453{
7454 int ret;
7455 struct quic_frame *cf, *cfbak;
7456
7457 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
7458
7459 ret = 0;
7460 if (*len > room)
7461 goto leave;
7462
7463 /* If we are not probing we must take into an account the congestion
7464 * control window.
7465 */
7466 if (!qel->pktns->tx.pto_probe) {
7467 size_t remain = quic_path_prep_data(qc->path);
7468
7469 if (headlen > remain)
7470 goto leave;
7471
7472 room = QUIC_MIN(room, remain - headlen);
7473 }
7474
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007475 TRACE_PROTO("TX frms build (headlen)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007476 QUIC_EV_CONN_BCFRMS, qc, &headlen);
7477
7478 /* NOTE: switch/case block inside a loop, a successful status must be
7479 * returned by this function only if at least one frame could be built
7480 * in the switch/case block.
7481 */
7482 list_for_each_entry_safe(cf, cfbak, inlist, list) {
7483 /* header length, data length, frame length. */
7484 size_t hlen, dlen, dlen_sz, avail_room, flen;
7485
7486 if (!room)
7487 break;
7488
7489 switch (cf->type) {
7490 case QUIC_FT_CRYPTO:
7491 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
7492 QUIC_EV_CONN_BCFRMS, qc, &room, len);
7493 /* Compute the length of this CRYPTO frame header */
7494 hlen = 1 + quic_int_getsize(cf->crypto.offset);
7495 /* Compute the data length of this CRyPTO frame. */
7496 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
7497 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
7498 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
7499 if (!dlen)
7500 continue;
7501
7502 /* CRYPTO frame length. */
7503 flen = hlen + quic_int_getsize(dlen) + dlen;
7504 TRACE_DEVEL(" CRYPTO frame length (flen)",
7505 QUIC_EV_CONN_BCFRMS, qc, &flen);
7506 /* Add the CRYPTO data length and its encoded length to the packet
7507 * length and the length of this length.
7508 */
7509 *len += flen;
7510 room -= flen;
7511 if (dlen == cf->crypto.len) {
7512 /* <cf> CRYPTO data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007513 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007514 LIST_APPEND(outlist, &cf->list);
7515 }
7516 else {
7517 struct quic_frame *new_cf;
7518
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007519 new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007520 if (!new_cf) {
7521 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
7522 continue;
7523 }
7524
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007525 new_cf->crypto.len = dlen;
7526 new_cf->crypto.offset = cf->crypto.offset;
7527 new_cf->crypto.qel = qel;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007528 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007529 if (cf->origin) {
7530 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7531 /* This <cf> frame was duplicated */
7532 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7533 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007534 /* Detach the remaining CRYPTO frame from its original frame */
7535 LIST_DEL_INIT(&cf->ref);
7536 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007537 }
7538 LIST_APPEND(outlist, &new_cf->list);
7539 /* Consume <dlen> bytes of the current frame. */
7540 cf->crypto.len -= dlen;
7541 cf->crypto.offset += dlen;
7542 }
7543 break;
7544
7545 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007546 if (cf->stream.dup) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007547 struct eb64_node *node = NULL;
7548 struct qc_stream_desc *stream_desc = NULL;
7549 struct quic_stream *strm = &cf->stream;
7550
7551 /* As this frame has been already lost, ensure the stream is always
7552 * available or the range of this frame is not consumed before
7553 * resending it.
7554 */
7555 node = eb64_lookup(&qc->streams_by_id, strm->id);
7556 if (!node) {
7557 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007558 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007559 continue;
7560 }
7561
7562 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
7563 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
7564 TRACE_DEVEL("ignored frame frame in already acked range",
7565 QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007566 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007567 continue;
7568 }
7569 else if (strm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01007570 uint64_t diff = stream_desc->ack_offset - strm->offset.key;
7571
Frédéric Lécaillec425e032023-03-20 14:32:59 +01007572 qc_stream_frm_mv_fwd(cf, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007573 TRACE_DEVEL("updated partially acked frame",
7574 QUIC_EV_CONN_PRSAFRM, qc, cf);
7575 }
7576 }
7577 /* Note that these frames are accepted in short packets only without
7578 * "Length" packet field. Here, <*len> is used only to compute the
7579 * sum of the lengths of the already built frames for this packet.
7580 *
7581 * Compute the length of this STREAM frame "header" made a all the field
7582 * excepting the variable ones. Note that +1 is for the type of this frame.
7583 */
7584 hlen = 1 + quic_int_getsize(cf->stream.id) +
7585 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
7586 /* Compute the data length of this STREAM frame. */
7587 avail_room = room - hlen - *len;
7588 if ((ssize_t)avail_room <= 0)
7589 continue;
7590
7591 TRACE_DEVEL(" New STREAM frame build (room, len)",
7592 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007593
7594 /* hlen contains STREAM id and offset. Ensure there is
7595 * enough room for length field.
7596 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007597 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007598 dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
7599 cf->stream.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007600 dlen_sz = quic_int_getsize(dlen);
7601 flen = hlen + dlen_sz + dlen;
7602 }
7603 else {
7604 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
7605 flen = hlen + dlen;
7606 }
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007607
7608 if (cf->stream.len && !dlen) {
7609 /* Only a small gap is left on buffer, not
7610 * enough to encode the STREAM data length.
7611 */
7612 continue;
7613 }
7614
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007615 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
7616 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
7617 TRACE_DEVEL(" STREAM frame length (flen)",
7618 QUIC_EV_CONN_BCFRMS, qc, &flen);
7619 /* Add the STREAM data length and its encoded length to the packet
7620 * length and the length of this length.
7621 */
7622 *len += flen;
7623 room -= flen;
7624 if (dlen == cf->stream.len) {
7625 /* <cf> STREAM data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007626 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007627 LIST_APPEND(outlist, &cf->list);
7628
7629 /* Do not notify MUX on retransmission. */
7630 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7631 qcc_streams_sent_done(cf->stream.stream->ctx,
7632 cf->stream.len,
7633 cf->stream.offset.key);
7634 }
7635 }
7636 else {
7637 struct quic_frame *new_cf;
7638 struct buffer cf_buf;
7639
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007640 new_cf = qc_frm_alloc(cf->type);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007641 if (!new_cf) {
7642 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
7643 continue;
7644 }
7645
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007646 new_cf->stream.stream = cf->stream.stream;
7647 new_cf->stream.buf = cf->stream.buf;
7648 new_cf->stream.id = cf->stream.id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01007649 new_cf->stream.offset = cf->stream.offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007650 new_cf->stream.len = dlen;
7651 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
7652 /* FIN bit reset */
7653 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
7654 new_cf->stream.data = cf->stream.data;
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007655 new_cf->stream.dup = cf->stream.dup;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007656 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007657 if (cf->origin) {
7658 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7659 /* This <cf> frame was duplicated */
7660 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7661 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007662 /* Detach this STREAM frame from its origin */
7663 LIST_DEL_INIT(&cf->ref);
7664 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007665 }
7666 LIST_APPEND(outlist, &new_cf->list);
7667 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
7668 /* Consume <dlen> bytes of the current frame. */
7669 cf_buf = b_make(b_orig(cf->stream.buf),
7670 b_size(cf->stream.buf),
7671 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
7672 cf->stream.len -= dlen;
7673 cf->stream.offset.key += dlen;
7674 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
7675
7676 /* Do not notify MUX on retransmission. */
7677 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7678 qcc_streams_sent_done(new_cf->stream.stream->ctx,
7679 new_cf->stream.len,
7680 new_cf->stream.offset.key);
7681 }
7682 }
7683
7684 /* TODO the MUX is notified about the frame sending via
7685 * previous qcc_streams_sent_done call. However, the
7686 * sending can fail later, for example if the sendto
7687 * system call returns an error. As the MUX has been
7688 * notified, the transport layer is responsible to
7689 * bufferize and resent the announced data later.
7690 */
7691
7692 break;
7693
7694 default:
7695 flen = qc_frm_len(cf);
7696 BUG_ON(!flen);
7697 if (flen > room)
7698 continue;
7699
7700 *len += flen;
7701 room -= flen;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007702 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007703 LIST_APPEND(outlist, &cf->list);
7704 break;
7705 }
7706
7707 /* Successful status as soon as a frame could be built */
7708 ret = 1;
7709 }
7710
7711 leave:
7712 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
7713 return ret;
7714}
7715
7716/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
7717 * is used as return parameter and should be zero'ed by the caller.
7718 */
7719static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
7720 struct quic_frame *out)
7721{
7722 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
7723 *
7724 * A CONNECTION_CLOSE frame should be sent in several packets with
7725 * different encryption levels depending on the client context. This is
7726 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
7727 * more details on how to implement it.
7728 */
7729 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
7730
7731
7732 if (qc->err.app) {
7733 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
7734 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
7735 /* RFC 9000 10.2.3. Immediate Close during the Handshake
7736 *
7737 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
7738 * packet could expose application state or be used to alter application
7739 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
7740 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
7741 * Handshake packets. Otherwise, information about the application
7742 * state might be revealed. Endpoints MUST clear the value of the
7743 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
7744 * converting to a CONNECTION_CLOSE of type 0x1c.
7745 */
7746 out->type = QUIC_FT_CONNECTION_CLOSE;
7747 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
7748 out->connection_close.reason_phrase_len = 0;
7749 }
7750 else {
7751 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
7752 out->connection_close.error_code = qc->err.code;
7753 }
7754 }
7755 else {
7756 out->type = QUIC_FT_CONNECTION_CLOSE;
7757 out->connection_close.error_code = qc->err.code;
7758 }
7759 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
7760
7761}
7762
7763/* This function builds a clear packet from <pkt> information (its type)
7764 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
7765 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
7766 * filling the buffer with as much frames as possible from <frms> list of
7767 * prebuilt frames.
7768 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
7769 * reserved so that to ensure there is enough room to build this AEAD TAG after
7770 * having returned from this function.
7771 * This function also updates the value of <buf_pn> pointer to point to the packet
7772 * number field in this packet. <pn_len> will also have the packet number
7773 * length as value.
7774 *
7775 * Return 1 if succeeded (enough room to buile this packet), O if not.
7776 */
7777static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
7778 size_t dglen, struct quic_tx_packet *pkt,
7779 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007780 int must_ack, int padding, int cc, int probe,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007781 struct quic_enc_level *qel, struct quic_conn *qc,
7782 const struct quic_version *ver, struct list *frms)
7783{
7784 unsigned char *beg, *payload;
7785 size_t len, len_sz, len_frms, padding_len;
7786 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
7787 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
7788 struct quic_frame cc_frm = { };
7789 size_t ack_frm_len, head_len;
7790 int64_t rx_largest_acked_pn;
7791 int add_ping_frm;
7792 struct list frm_list = LIST_HEAD_INIT(frm_list);
7793 struct quic_frame *cf;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007794 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007795
7796 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7797
7798 /* Length field value with CRYPTO frames if present. */
7799 len_frms = 0;
7800 beg = pos;
7801 /* When not probing, and no immediate close is required, reduce the size of this
7802 * buffer to respect the congestion controller window.
7803 * This size will be limited if we have ack-eliciting frames to send from <frms>.
7804 */
7805 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
7806 size_t path_room;
7807
7808 path_room = quic_path_prep_data(qc->path);
7809 if (end - beg > path_room)
7810 end = beg + path_room;
7811 }
7812
7813 /* Ensure there is enough room for the TLS encryption tag and a zero token
7814 * length field if any.
7815 */
7816 if (end - pos < QUIC_TLS_TAG_LEN +
7817 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
7818 goto no_room;
7819
7820 end -= QUIC_TLS_TAG_LEN;
7821 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
7822 /* packet number length */
7823 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
7824 /* Build the header */
7825 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
7826 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
7827 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
7828 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
7829 goto no_room;
7830
7831 /* Encode the token length (0) for an Initial packet. */
7832 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
7833 *pos++ = 0;
7834 head_len = pos - beg;
7835 /* Build an ACK frame if required. */
7836 ack_frm_len = 0;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007837 /* Do not ack and probe at the same time. */
7838 if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007839 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
7840 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
7841 ack_frm.tx_ack.arngs = arngs;
7842 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
7843 qel->pktns->tx.ack_delay =
7844 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
7845 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
7846 }
7847 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
7848 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
7849 * smallest frame (PING) and <*pn_len> more for the packet number. Note
7850 * that from here, we do not know if we will have to send a PING frame.
7851 * This will be decided after having computed the ack-eliciting frames
7852 * to be added to this packet.
7853 */
Frédéric Lécaille9d68c6a2023-04-12 20:49:29 +02007854 if (end - pos <= 1 + *pn_len)
7855 goto no_room;
7856
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007857 ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos);
7858 if (!ack_frm_len)
7859 goto no_room;
7860 }
7861
7862 /* Length field value without the ack-eliciting frames. */
7863 len = ack_frm_len + *pn_len;
7864 len_frms = 0;
7865 if (!cc && !LIST_ISEMPTY(frms)) {
7866 ssize_t room = end - pos;
7867
7868 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
7869 /* Initialize the length of the frames built below to <len>.
7870 * If any frame could be successfully built by qc_build_frms(),
7871 * we will have len_frms > len.
7872 */
7873 len_frms = len;
7874 if (!qc_build_frms(&frm_list, frms,
7875 end - pos, &len_frms, pos - beg, qel, qc)) {
7876 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7877 qc, NULL, NULL, &room);
7878 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
7879 goto no_room;
7880 }
7881 }
7882
7883 /* Length (of the remaining data). Must not fail because, the buffer size
7884 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
7885 * for the encryption tag. It must be taken into an account for the length
7886 * of this packet.
7887 */
7888 if (len_frms)
7889 len = len_frms + QUIC_TLS_TAG_LEN;
7890 else
7891 len += QUIC_TLS_TAG_LEN;
7892 /* CONNECTION_CLOSE frame */
7893 if (cc) {
7894 qc_build_cc_frm(qc, qel, &cc_frm);
7895 len += qc_frm_len(&cc_frm);
7896 }
7897 add_ping_frm = 0;
7898 padding_len = 0;
7899 len_sz = quic_int_getsize(len);
7900 /* Add this packet size to <dglen> */
7901 dglen += head_len + len_sz + len;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007902 /* Note that <padding> is true only when building an Handshake packet
7903 * coalesced to an Initial packet.
7904 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007905 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
7906 /* This is a maximum padding size */
7907 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7908 /* The length field value is of this packet is <len> + <padding_len>
7909 * the size of which may be greater than the initial computed size
7910 * <len_sz>. So, let's deduce the difference between these to packet
7911 * sizes from <padding_len>.
7912 */
7913 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7914 len += padding_len;
7915 }
Frédéric Lécaille5faf5772023-02-16 17:30:53 +01007916 else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
7917 len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
7918 }
7919 else if (LIST_ISEMPTY(&frm_list)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007920 if (qel->pktns->tx.pto_probe) {
7921 /* If we cannot send a frame, we send a PING frame. */
7922 add_ping_frm = 1;
7923 len += 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007924 dglen += 1;
7925 /* Note that only we are in the case where this Initial packet
7926 * is not coalesced to an Handshake packet. We must directly
7927 * pad the datragram.
7928 */
Frédéric Lécaille9c317b12023-03-28 15:39:11 +02007929 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7930 if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
7931 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7932 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7933 len += padding_len;
7934 }
7935 }
7936 else {
7937 /* Note that +1 is for the PING frame */
7938 if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
7939 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007940 }
7941 }
7942 else {
7943 /* If there is no frame at all to follow, add at least a PADDING frame. */
7944 if (!ack_frm_len && !cc)
7945 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007946 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007947 }
7948
7949 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
7950 goto no_room;
7951
7952 /* Packet number field address. */
7953 *buf_pn = pos;
7954
7955 /* Packet number encoding. */
7956 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
7957 goto no_room;
7958
7959 /* payload building (ack-eliciting or not frames) */
7960 payload = pos;
7961 if (ack_frm_len) {
7962 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
7963 goto no_room;
7964
7965 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
7966 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
7967 }
7968
7969 /* Ack-eliciting frames */
7970 if (!LIST_ISEMPTY(&frm_list)) {
7971 struct quic_frame *tmp_cf;
7972 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
7973 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
7974 ssize_t room = end - pos;
7975 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7976 qc, NULL, NULL, &room);
7977 /* Note that <cf> was added from <frms> to <frm_list> list by
7978 * qc_build_frms().
7979 */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007980 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007981 LIST_INSERT(frms, &cf->list);
7982 continue;
7983 }
7984
7985 quic_tx_packet_refinc(pkt);
7986 cf->pkt = pkt;
7987 }
7988 }
7989
7990 /* Build a PING frame if needed. */
7991 if (add_ping_frm) {
7992 frm.type = QUIC_FT_PING;
7993 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
7994 goto no_room;
7995 }
7996
7997 /* Build a CONNECTION_CLOSE frame if needed. */
7998 if (cc) {
7999 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
8000 goto no_room;
8001
8002 pkt->flags |= QUIC_FL_TX_PACKET_CC;
8003 }
8004
8005 /* Build a PADDING frame if needed. */
8006 if (padding_len) {
8007 frm.type = QUIC_FT_PADDING;
8008 frm.padding.len = padding_len;
8009 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
8010 goto no_room;
8011 }
8012
8013 if (pos == payload) {
8014 /* No payload was built because of congestion control */
8015 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
8016 goto no_room;
8017 }
8018
8019 /* If this packet is ack-eliciting and we are probing let's
8020 * decrement the PTO probe counter.
8021 */
8022 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
8023 qel->pktns->tx.pto_probe)
8024 qel->pktns->tx.pto_probe--;
8025
8026 pkt->len = pos - beg;
8027 LIST_SPLICE(&pkt->frms, &frm_list);
8028
8029 ret = 1;
8030 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
8031 leave:
8032 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
8033 return ret;
8034
8035 no_room:
8036 /* Replace the pre-built frames which could not be add to this packet */
8037 LIST_SPLICE(frms, &frm_list);
8038 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
8039 goto leave;
8040}
8041
8042static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
8043{
8044 pkt->type = type;
8045 pkt->len = 0;
8046 pkt->in_flight_len = 0;
8047 pkt->pn_node.key = (uint64_t)-1;
8048 LIST_INIT(&pkt->frms);
8049 pkt->time_sent = TICK_ETERNITY;
8050 pkt->next = NULL;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01008051 pkt->prev = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008052 pkt->largest_acked_pn = -1;
8053 pkt->flags = 0;
8054 pkt->refcnt = 0;
8055}
8056
8057/* Build a packet into <buf> packet buffer with <pkt_type> as packet
8058 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
8059 * of prebuilt frames.
8060 *
8061 * Return -2 if the packet could not be allocated or encrypted for any reason,
8062 * -1 if there was not enough room to build a packet.
8063 * XXX NOTE XXX
8064 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
8065 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
8066 * control window limitation.
8067 */
8068static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
8069 const unsigned char *buf_end,
8070 struct quic_enc_level *qel,
8071 struct quic_tls_ctx *tls_ctx, struct list *frms,
8072 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008073 size_t dglen, int pkt_type, int must_ack,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008074 int padding, int probe, int cc, int *err)
8075{
8076 struct quic_tx_packet *ret_pkt = NULL;
8077 /* The pointer to the packet number field. */
8078 unsigned char *buf_pn;
8079 unsigned char *beg, *end, *payload;
8080 int64_t pn;
8081 size_t pn_len, payload_len, aad_len;
8082 struct quic_tx_packet *pkt;
8083
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008084 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
8085 TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008086 *err = 0;
8087 pkt = pool_alloc(pool_head_quic_tx_packet);
8088 if (!pkt) {
8089 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
8090 *err = -2;
8091 goto err;
8092 }
8093
8094 quic_tx_packet_init(pkt, pkt_type);
8095 beg = *pos;
8096 pn_len = 0;
8097 buf_pn = NULL;
8098
8099 pn = qel->pktns->tx.next_pn + 1;
8100 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008101 must_ack, padding, cc, probe, qel, qc, ver, frms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008102 // trace already emitted by function above
8103 *err = -1;
8104 goto err;
8105 }
8106
8107 end = beg + pkt->len;
8108 payload = buf_pn + pn_len;
8109 payload_len = end - payload;
8110 aad_len = payload - beg;
8111
8112 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
8113 // trace already emitted by function above
8114 *err = -2;
8115 goto err;
8116 }
8117
8118 end += QUIC_TLS_TAG_LEN;
8119 pkt->len += QUIC_TLS_TAG_LEN;
8120 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
8121 // trace already emitted by function above
8122 *err = -2;
8123 goto err;
8124 }
8125
8126 /* Consume a packet number */
8127 qel->pktns->tx.next_pn++;
8128 qc->tx.prep_bytes += pkt->len;
8129 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
8130 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
8131 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
8132 }
8133 /* Now that a correct packet is built, let us consume <*pos> buffer. */
8134 *pos = end;
8135 /* Attach the built packet to its tree. */
8136 pkt->pn_node.key = pn;
8137 /* Set the packet in fligth length for in flight packet only. */
8138 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
8139 pkt->in_flight_len = pkt->len;
8140 qc->path->prep_in_flight += pkt->len;
8141 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01008142 /* Always reset this flag */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008143 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
8144 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
8145 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
8146 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
Frédéric Lécailled7215712023-03-24 18:13:37 +01008147 qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
8148 if (tick_isset(qc->ack_expire)) {
8149 qc->ack_expire = TICK_ETERNITY;
8150 qc->idle_timer_task->expire = qc->idle_expire;
8151 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02008152 TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01008153 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008154 }
8155
8156 pkt->pktns = qel->pktns;
8157
8158 ret_pkt = pkt;
8159 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008160 TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
8161 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008162 return ret_pkt;
8163
8164 err:
8165 /* TODO: what about the frames which have been built
8166 * for this packet.
8167 */
8168 free_quic_tx_packet(qc, pkt);
8169 goto leave;
8170}
8171
8172
8173static void __quic_conn_init(void)
8174{
8175 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
8176}
8177INITCALL0(STG_REGISTER, __quic_conn_init);
8178
8179static void __quic_conn_deinit(void)
8180{
8181 BIO_meth_free(ha_quic_meth);
8182}
8183REGISTER_POST_DEINIT(__quic_conn_deinit);
8184
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008185/* Handle a new <dgram> received. Parse each QUIC packets and copied their
8186 * content to a quic-conn instance. The datagram content can be released after
8187 * this function.
8188 *
8189 * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
8190 * to the connection instance. <li> is the attached listener. The caller is
8191 * responsible to ensure that the first packet is destined to this connection
8192 * by comparing CIDs.
8193 *
8194 * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
8195 * function will thus retrieve the connection from the CID tree or allocate a
8196 * new one if possible. <li> is the listener attached to the receiver.
8197 *
8198 * Returns 0 on success else non-zero. If an error happens, some packets from
8199 * the datagram may not have been parsed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008200 */
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008201int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
8202 struct listener *li)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008203{
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008204 struct quic_rx_packet *pkt;
8205 struct quic_conn *qc = NULL;
8206 unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008207 struct list *tasklist_head = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008208
8209 TRACE_ENTER(QUIC_EV_CONN_LPKT);
8210
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008211 pos = dgram->buf;
8212 end = pos + dgram->len;
8213 do {
8214 /* TODO replace zalloc -> alloc. */
8215 pkt = pool_zalloc(pool_head_quic_rx_packet);
8216 if (!pkt) {
8217 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
8218 goto err;
8219 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008220
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008221 pkt->version = NULL;
8222 pkt->pn_offset = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008223
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008224 /* Set flag if pkt is the first one in dgram. */
8225 if (pos == dgram->buf)
8226 pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008227
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008228 LIST_INIT(&pkt->qc_rx_pkt_list);
8229 pkt->time_received = now_ms;
8230 quic_rx_packet_refinc(pkt);
8231 if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
8232 goto next;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008233
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008234 /* Search quic-conn instance for first packet of the datagram.
8235 * quic_rx_packet_parse() is responsible to discard packets
8236 * with different DCID as the first one in the same datagram.
8237 */
8238 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008239 int new_tid = -1;
8240
8241 qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li, &new_tid);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008242 /* qc is NULL if receiving a non Initial packet for an
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008243 * unknown connection or on connection affinity rebind.
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008244 */
8245 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008246 if (new_tid >= 0) {
8247 MT_LIST_APPEND(&quic_dghdlrs[new_tid].dgrams,
8248 &dgram->handler_list);
8249 tasklet_wakeup(quic_dghdlrs[new_tid].task);
8250 goto out;
8251 }
8252
Amaury Denoyelle98289692022-10-19 15:37:44 +02008253 /* Skip the entire datagram. */
8254 pkt->len = end - pos;
8255 goto next;
8256 }
8257
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008258 dgram->qc = qc;
8259 }
Amaury Denoyelle98289692022-10-19 15:37:44 +02008260
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008261 if (qc_rx_check_closing(qc, pkt)) {
8262 /* Skip the entire datagram. */
8263 pkt->len = end - pos;
8264 goto next;
8265 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008266
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008267 /* Detect QUIC connection migration. */
Frédéric Lécaillef6769542023-01-12 10:36:26 +01008268 if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008269 if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
8270 /* Skip the entire datagram. */
8271 TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
8272 pkt->len = end - pos;
8273 goto next;
8274 }
8275 }
8276
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008277 qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008278
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008279 next:
8280 pos += pkt->len;
8281 quic_rx_packet_refdec(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008282
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008283 /* Free rejected packets */
8284 if (!pkt->refcnt) {
8285 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
8286 pool_free(pool_head_quic_rx_packet, pkt);
8287 }
8288 } while (pos < end);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008289
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008290 /* Increasing the received bytes counter by the UDP datagram length
8291 * if this datagram could be associated to a connection.
8292 */
8293 if (dgram->qc)
8294 dgram->qc->rx.bytes += dgram->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008295
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008296 /* This must never happen. */
8297 BUG_ON(pos > end);
8298 BUG_ON(pos < end || pos > dgram->buf + dgram->len);
8299 /* Mark this datagram as consumed */
8300 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008301
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008302 out:
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008303 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
8304 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008305
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008306 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008307 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008308 return -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008309}
8310
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008311/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
8312 * CIDs. This can be used to determine if a datagram is addressed to the right
8313 * connection instance.
8314 *
8315 * Returns a boolean value.
8316 */
8317int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len)
8318{
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008319 const uchar idx = _quic_cid_tree_idx(dcid);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008320 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008321 struct ebmb_node *node = NULL;
8322 struct quic_cid_tree *tree = &quic_cid_trees[idx];
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008323
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008324 /* Test against our default CID or client ODCID. */
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008325 if ((qc->scid.len == dcid_len &&
8326 memcmp(qc->scid.data, dcid, dcid_len) == 0) ||
8327 (qc->odcid.len == dcid_len &&
Frédéric Lécaille07846cb2023-02-13 16:14:24 +01008328 memcmp(qc->odcid.data, dcid, dcid_len) == 0)) {
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008329 return 1;
8330 }
8331
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008332 /* Test against our other CIDs. This can happen if the client has
8333 * decided to switch to a new one.
8334 *
8335 * TODO to avoid locking, loop through qc.cids as an alternative.
8336 *
8337 * TODO set it to our default CID to avoid this operation next time.
8338 */
8339 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
8340 node = ebmb_lookup(&tree->root, dcid, dcid_len);
8341 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
8342
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008343 if (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008344 conn_id = ebmb_entry(node, struct quic_connection_id, node);
8345 if (qc == conn_id->qc)
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008346 return 1;
8347 }
8348
8349 return 0;
8350}
8351
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008352/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
8353 * Returns 1 if succeeded, 0 if not.
8354 */
8355int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
8356 unsigned char **dcid, size_t *dcid_len)
8357{
8358 int ret = 0, long_header;
8359 size_t minlen, skip;
8360
8361 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
8362
8363 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
8364 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
8365 goto err;
8366 }
8367
8368 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
8369 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
8370 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
8371 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
8372 if (end - buf < minlen)
8373 goto err;
8374
8375 buf += skip;
8376 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
8377 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
8378 goto err;
8379
8380 *dcid = buf;
8381
8382 ret = 1;
8383 leave:
8384 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
8385 return ret;
8386
8387 err:
8388 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
8389 goto leave;
8390}
8391
8392/* Notify the MUX layer if alive about an imminent close of <qc>. */
8393void qc_notify_close(struct quic_conn *qc)
8394{
8395 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
8396
8397 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
8398 goto leave;
8399
8400 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
8401 /* wake up the MUX */
8402 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
8403 TRACE_STATE("connection closure notidfied to mux",
8404 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8405 qc->conn->mux->wake(qc->conn);
8406 }
8407 else
8408 TRACE_STATE("connection closure not notidfied to mux",
8409 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8410 leave:
8411 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
8412}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008413
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008414/* Wake-up upper layer for sending if all conditions are met :
8415 * - room in congestion window or probe packet to sent
8416 * - socket FD ready to sent or listener socket used
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008417 *
8418 * Returns 1 if upper layer has been woken up else 0.
8419 */
8420int qc_notify_send(struct quic_conn *qc)
8421{
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008422 const struct quic_pktns *pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8423
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008424 if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008425 /* RFC 9002 7.5. Probe Timeout
8426 *
8427 * Probe packets MUST NOT be blocked by the congestion controller.
8428 */
8429 if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
Amaury Denoyellecaa16542023-02-28 15:11:26 +01008430 (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008431 tasklet_wakeup(qc->subs->tasklet);
8432 qc->subs->events &= ~SUB_RETRY_SEND;
8433 if (!qc->subs->events)
8434 qc->subs = NULL;
8435
8436 return 1;
8437 }
8438 }
8439
8440 return 0;
8441}
8442
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008443/* Move a <qc> QUIC connection and its resources from the current thread to the
8444 * new one <new_tid>. After this call, the connection cannot be dereferenced
8445 * anymore on the current thread.
8446 *
8447 * Returns 0 on success else non-zero.
8448 */
8449int qc_set_tid_affinity(struct quic_conn *qc, uint new_tid)
8450{
8451 struct task *t1 = NULL, *t2 = NULL;
8452 struct tasklet *t3 = NULL;
8453
8454 struct quic_connection_id *conn_id;
8455 struct eb64_node *node;
8456
8457 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8458
8459 /* Pre-allocate all required resources. This ensures we do not left a
8460 * connection with only some of its field rebinded.
8461 */
8462 if (((t1 = task_new_on(new_tid)) == NULL) ||
8463 (qc->timer_task && (t2 = task_new_on(new_tid)) == NULL) ||
8464 (t3 = tasklet_new()) == NULL) {
8465 goto err;
8466 }
8467
8468 /* Reinit idle timer task. */
8469 task_kill(qc->idle_timer_task);
8470 t1->expire = qc->idle_timer_task->expire;
8471 qc->idle_timer_task = t1;
8472 qc->idle_timer_task->process = qc_idle_timer_task;
8473 qc->idle_timer_task->context = qc;
8474
8475 /* Reinit timer task if allocated. */
8476 if (qc->timer_task) {
8477 task_kill(qc->timer_task);
8478 qc->timer_task = t2;
8479 qc->timer_task->process = qc_process_timer;
8480 qc->timer_task->context = qc;
8481 }
8482
8483 /* Reinit IO tasklet. */
8484 tasklet_kill(qc->wait_event.tasklet);
8485 /* In most cases quic_conn_app_io_cb is used but for 0-RTT quic_conn_io_cb can be still activated. */
8486 t3->process = qc->wait_event.tasklet->process;
8487 qc->wait_event.tasklet = t3;
8488 qc->wait_event.tasklet->tid = new_tid;
8489 qc->wait_event.tasklet->context = qc;
8490 qc->wait_event.events = 0;
8491
8492 /* Rebind the connection FD. */
8493 if (qc_test_fd(qc)) {
8494 fd_migrate_on(qc->fd, new_tid);
8495 /* TODO need to reactivate reading on the new thread. */
8496 }
8497
8498 /* Remove conn from per-thread list instance. */
8499 qc_detach_th_ctx_list(qc, 0);
8500 /* Connection must not be closing or else it must be inserted in quic_conns_clo list instance instead. */
8501 BUG_ON(qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING));
8502 LIST_APPEND(&ha_thread_ctx[new_tid].quic_conns, &qc->el_th_ctx);
8503 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
8504
8505 node = eb64_first(&qc->cids);
8506 BUG_ON(!node || eb64_next(node)); /* One and only one CID must be present before affinity rebind. */
8507 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
8508 /* Rebinding is considered done when CID points to the new thread. No
8509 * access should be done to quic-conn instance after it.
8510 */
8511 HA_ATOMIC_STORE(&conn_id->tid, new_tid);
8512 qc = NULL;
8513
8514 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, NULL);
8515 return 0;
8516
8517 err:
8518 task_destroy(t1);
8519 task_destroy(t2);
8520 if (t3)
8521 tasklet_free(t3);
8522
8523 TRACE_DEVEL("leaving on error", QUIC_EV_CONN_SET_AFFINITY, qc);
8524 return 1;
8525}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008526
8527/* appctx context used by "show quic" command */
8528struct show_quic_ctx {
8529 unsigned int epoch;
8530 struct bref bref; /* back-reference to the quic-conn being dumped */
8531 unsigned int thr;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008532 int flags;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008533};
8534
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008535#define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */
8536
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008537static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private)
8538{
8539 struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
8540
8541 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
8542 return 1;
8543
8544 ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1);
8545 ctx->thr = 0;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008546 ctx->flags = 0;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008547
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01008548 if (*args[2] && strcmp(args[2], "all") == 0)
8549 ctx->flags |= QC_CLI_FL_SHOW_ALL;
8550
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008551 LIST_INIT(&ctx->bref.users);
8552
8553 return 0;
8554}
8555
8556static int cli_io_handler_dump_quic(struct appctx *appctx)
8557{
8558 struct show_quic_ctx *ctx = appctx->svcctx;
8559 struct stconn *sc = appctx_sc(appctx);
8560 struct quic_conn *qc;
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008561 struct quic_pktns *pktns;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008562 struct eb64_node *node;
8563 struct qc_stream_desc *stream;
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008564 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008565 int expire;
8566 unsigned char cid_len;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008567
8568 thread_isolate();
8569
8570 if (ctx->thr >= global.nbthread)
8571 goto done;
8572
Christopher Faulet87633c32023-04-03 18:32:50 +02008573 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02008574 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008575 /* If we're forced to shut down, we might have to remove our
8576 * reference to the last stream being dumped.
8577 */
8578 if (!LIST_ISEMPTY(&ctx->bref.users))
8579 LIST_DEL_INIT(&ctx->bref.users);
8580 goto done;
8581 }
8582
8583 chunk_reset(&trash);
8584
8585 if (!LIST_ISEMPTY(&ctx->bref.users)) {
8586 /* Remove show_quic_ctx from previous quic_conn instance. */
8587 LIST_DEL_INIT(&ctx->bref.users);
8588 }
8589 else if (!ctx->bref.ref) {
8590 /* First invocation. */
8591 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8592 }
8593
8594 while (1) {
8595 int done = 0;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008596 int i;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008597
8598 if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008599 /* If closing connections requested through "all", move
8600 * to quic_conns_clo list after browsing quic_conns.
8601 * Else move directly to the next quic_conns thread.
8602 */
8603 if (ctx->flags & QC_CLI_FL_SHOW_ALL) {
8604 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns_clo.n;
8605 continue;
8606 }
8607
8608 done = 1;
8609 }
8610 else if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns_clo) {
8611 /* Closing list entirely browsed, go to next quic_conns
8612 * thread.
8613 */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008614 done = 1;
8615 }
8616 else {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008617 /* Retrieve next element of the current list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008618 qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx);
8619 if ((int)(qc->qc_epoch - ctx->epoch) > 0)
8620 done = 1;
8621 }
8622
8623 if (done) {
8624 ++ctx->thr;
8625 if (ctx->thr >= global.nbthread)
8626 break;
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008627 /* Switch to next thread quic_conns list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008628 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8629 continue;
8630 }
8631
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008632 /* CIDs */
Amaury Denoyelle66947282023-04-13 11:48:38 +02008633 chunk_appendf(&trash, "* %p[%02u]: scid=", qc, ctx->thr);
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008634 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
8635 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
8636 while (cid_len++ < 20)
8637 chunk_appendf(&trash, "..");
8638
8639 chunk_appendf(&trash, " dcid=");
8640 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
8641 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
8642 while (cid_len++ < 20)
8643 chunk_appendf(&trash, "..");
8644
8645 chunk_appendf(&trash, "\n");
8646
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008647 chunk_appendf(&trash, " loc. TPs:");
8648 quic_transport_params_dump(&trash, qc, &qc->rx.params);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008649 chunk_appendf(&trash, "\n");
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008650 chunk_appendf(&trash, " rem. TPs:");
8651 quic_transport_params_dump(&trash, qc, &qc->tx.params);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008652 chunk_appendf(&trash, "\n");
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008653
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008654 /* Connection state */
8655 if (qc->flags & QUIC_FL_CONN_CLOSING)
8656 chunk_appendf(&trash, " st=closing ");
8657 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8658 chunk_appendf(&trash, " st=draining ");
8659 else if (qc->state < QUIC_HS_ST_CONFIRMED)
8660 chunk_appendf(&trash, " st=handshake ");
8661 else
8662 chunk_appendf(&trash, " st=opened ");
8663
8664 if (qc->mux_state == QC_MUX_NULL)
8665 chunk_appendf(&trash, "mux=null ");
8666 else if (qc->mux_state == QC_MUX_READY)
8667 chunk_appendf(&trash, "mux=ready ");
8668 else
8669 chunk_appendf(&trash, "mux=released ");
8670
Frédéric Lécaille92f4a7c2023-04-04 14:31:49 +02008671 expire = qc->idle_expire;
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008672 chunk_appendf(&trash, "expire=%02ds ",
Frédéric Lécaille92f4a7c2023-04-04 14:31:49 +02008673 TICKS_TO_MS(tick_remain(now_ms, expire)) / 1000);
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008674
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008675 chunk_appendf(&trash, "\n");
8676
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008677 /* Socket */
8678 chunk_appendf(&trash, " fd=%d", qc->fd);
8679 if (qc->local_addr.ss_family == AF_INET ||
8680 qc->local_addr.ss_family == AF_INET6) {
8681 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8682 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
8683 chunk_appendf(&trash, " from=%s:%s", bufaddr, bufport);
8684
8685 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8686 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
8687 chunk_appendf(&trash, " to=%s:%s", bufaddr, bufport);
8688 }
8689
8690 chunk_appendf(&trash, "\n");
8691
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008692 /* Packet number spaces information */
8693 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008694 chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008695 pktns->rx.arngs.sz, pktns->tx.in_flight);
8696 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008697 chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008698 pktns->rx.arngs.sz, pktns->tx.in_flight);
8699 pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8700 chunk_appendf(&trash, " [01rtt] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
8701 pktns->rx.arngs.sz, pktns->tx.in_flight);
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008702
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +02008703 chunk_appendf(&trash, " srtt=%-4u rttvar=%-4u rttmin=%-4u ptoc=%-4u cwnd=%-6llu"
8704 " mcwnd=%-6llu lostpkts=%-6llu\n",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008705 qc->path->loss.srtt >> 3, qc->path->loss.rtt_var >> 2,
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +02008706 qc->path->loss.rtt_min, qc->path->loss.pto_count, (ullong)qc->path->cwnd,
8707 (ullong)qc->path->mcwnd, (ullong)qc->path->loss.nb_lost_pkt);
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008708
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008709
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008710 /* Streams */
8711 node = eb64_first(&qc->streams_by_id);
8712 i = 0;
8713 while (node) {
8714 stream = eb64_entry(node, struct qc_stream_desc, by_id);
8715 node = eb64_next(node);
8716
Amaury Denoyellea9de25a2023-02-10 09:25:22 +01008717 chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key);
8718 chunk_appendf(&trash, " off=%-8llu ack=%-8llu",
8719 (unsigned long long)stream->buf_offset,
8720 (unsigned long long)stream->ack_offset);
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008721
8722 if (!(++i % 3)) {
8723 chunk_appendf(&trash, "\n");
8724 i = 0;
8725 }
8726 }
8727
8728 chunk_appendf(&trash, "\n");
8729
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008730 if (applet_putchk(appctx, &trash) == -1) {
8731 /* Register show_quic_ctx to quic_conn instance. */
8732 LIST_APPEND(&qc->back_refs, &ctx->bref.users);
8733 goto full;
8734 }
8735
8736 ctx->bref.ref = qc->el_th_ctx.n;
8737 }
8738
8739 done:
8740 thread_release();
8741 return 1;
8742
8743 full:
8744 thread_release();
8745 return 0;
8746}
8747
8748static void cli_release_show_quic(struct appctx *appctx)
8749{
8750 struct show_quic_ctx *ctx = appctx->svcctx;
8751
8752 if (ctx->thr < global.nbthread) {
8753 thread_isolate();
8754 if (!LIST_ISEMPTY(&ctx->bref.users))
8755 LIST_DEL_INIT(&ctx->bref.users);
8756 thread_release();
8757 }
8758}
8759
8760static struct cli_kw_list cli_kws = {{ }, {
8761 { { "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 +01008762 {{},}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008763}};
8764
8765INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
8766
8767static void init_quic()
8768{
8769 int thr;
8770
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008771 for (thr = 0; thr < MAX_THREADS; ++thr) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008772 LIST_INIT(&ha_thread_ctx[thr].quic_conns);
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008773 LIST_INIT(&ha_thread_ctx[thr].quic_conns_clo);
8774 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008775}
8776INITCALL0(STG_INIT, init_quic);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008777
8778/*
8779 * Local variables:
8780 * c-indent-level: 8
8781 * c-basic-offset: 8
8782 * End:
8783 */