blob: 314c978c4eb505aa6a29bdb20b237f678da88684 [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
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004209/* Set the encoded size of <arngs> QUIC ack ranges. */
4210static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
4211{
4212 struct eb64_node *node, *next;
4213 struct quic_arng_node *ar, *ar_next;
4214
4215 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4216
4217 node = eb64_last(&arngs->root);
4218 if (!node)
4219 goto leave;
4220
4221 ar = eb64_entry(node, struct quic_arng_node, first);
4222 arngs->enc_sz = quic_int_getsize(ar->last) +
4223 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
4224
4225 while ((next = eb64_prev(node))) {
4226 ar_next = eb64_entry(next, struct quic_arng_node, first);
4227 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
4228 quic_int_getsize(ar_next->last - ar_next->first.key);
4229 node = next;
4230 ar = eb64_entry(node, struct quic_arng_node, first);
4231 }
4232
4233 leave:
4234 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4235}
4236
4237/* Insert <ar> ack range into <argns> tree of ack ranges.
4238 * Returns the ack range node which has been inserted if succeeded, NULL if not.
4239 */
4240static inline
4241struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
4242 struct quic_arngs *arngs,
4243 struct quic_arng *ar)
4244{
4245 struct quic_arng_node *new_ar;
4246
4247 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4248
Frédéric Lécaille0ed94032023-04-17 14:10:14 +02004249 if (arngs->sz >= QUIC_MAX_ACK_RANGES) {
4250 struct eb64_node *last;
4251
4252 last = eb64_last(&arngs->root);
4253 BUG_ON(last == NULL);
4254 eb64_delete(last);
4255 pool_free(pool_head_quic_arng, last);
4256 arngs->sz--;
4257 }
4258
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004259 new_ar = pool_alloc(pool_head_quic_arng);
4260 if (!new_ar) {
4261 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
4262 goto leave;
4263 }
4264
4265 new_ar->first.key = ar->first;
4266 new_ar->last = ar->last;
4267 eb64_insert(&arngs->root, &new_ar->first);
4268 arngs->sz++;
4269
4270 leave:
4271 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4272 return new_ar;
4273}
4274
4275/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
4276 * Note that this function computes the number of bytes required to encode
4277 * this tree of ACK ranges in descending order.
4278 *
4279 * Descending order
4280 * ------------->
4281 * range1 range2
4282 * ..........|--------|..............|--------|
4283 * ^ ^ ^ ^
4284 * | | | |
4285 * last1 first1 last2 first2
4286 * ..........+--------+--------------+--------+......
4287 * diff1 gap12 diff2
4288 *
4289 * To encode the previous list of ranges we must encode integers as follows in
4290 * descending order:
4291 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
4292 * with diff1 = last1 - first1
4293 * diff2 = last2 - first2
4294 * gap12 = first1 - last2 - 2 (>= 0)
4295 *
4296
4297returns 0 on error
4298
4299 */
4300int quic_update_ack_ranges_list(struct quic_conn *qc,
4301 struct quic_arngs *arngs,
4302 struct quic_arng *ar)
4303{
4304 int ret = 0;
4305 struct eb64_node *le;
4306 struct quic_arng_node *new_node;
4307 struct eb64_node *new;
4308
4309 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4310
4311 new = NULL;
4312 if (eb_is_empty(&arngs->root)) {
4313 new_node = quic_insert_new_range(qc, arngs, ar);
4314 if (new_node)
4315 ret = 1;
4316
4317 goto leave;
4318 }
4319
4320 le = eb64_lookup_le(&arngs->root, ar->first);
4321 if (!le) {
4322 new_node = quic_insert_new_range(qc, arngs, ar);
4323 if (!new_node)
4324 goto leave;
4325
4326 new = &new_node->first;
4327 }
4328 else {
4329 struct quic_arng_node *le_ar =
4330 eb64_entry(le, struct quic_arng_node, first);
4331
4332 /* Already existing range */
4333 if (le_ar->last >= ar->last) {
4334 ret = 1;
4335 }
4336 else if (le_ar->last + 1 >= ar->first) {
4337 le_ar->last = ar->last;
4338 new = le;
4339 new_node = le_ar;
4340 }
4341 else {
4342 new_node = quic_insert_new_range(qc, arngs, ar);
4343 if (!new_node)
4344 goto leave;
4345
4346 new = &new_node->first;
4347 }
4348 }
4349
4350 /* Verify that the new inserted node does not overlap the nodes
4351 * which follow it.
4352 */
4353 if (new) {
4354 struct eb64_node *next;
4355 struct quic_arng_node *next_node;
4356
4357 while ((next = eb64_next(new))) {
4358 next_node =
4359 eb64_entry(next, struct quic_arng_node, first);
4360 if (new_node->last + 1 < next_node->first.key)
4361 break;
4362
4363 if (next_node->last > new_node->last)
4364 new_node->last = next_node->last;
4365 eb64_delete(next);
4366 pool_free(pool_head_quic_arng, next_node);
4367 /* Decrement the size of these ranges. */
4368 arngs->sz--;
4369 }
4370 }
4371
4372 ret = 1;
4373 leave:
4374 quic_arngs_set_enc_sz(qc, arngs);
4375 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4376 return ret;
4377}
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004378
4379/* Detect the value of the spin bit to be used. */
4380static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt,
4381 struct quic_enc_level *qel)
4382{
4383 uint64_t largest_pn = qel->pktns->rx.largest_pn;
4384
4385 if (qel != &qc->els[QUIC_TLS_ENC_LEVEL_APP] || largest_pn == -1 ||
4386 pkt->pn <= largest_pn)
4387 return;
4388
4389 if (qc_is_listener(qc)) {
4390 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4391 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4392 else
4393 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4394 }
4395 else {
4396 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4397 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4398 else
4399 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4400 }
4401}
4402
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004403/* Remove the header protection of packets at <el> encryption level.
4404 * Always succeeds.
4405 */
4406static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
4407{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004408 struct quic_rx_packet *pqpkt, *pkttmp;
4409 struct quic_enc_level *app_qel;
4410
4411 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
4412 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4413 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
4414 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004415 TRACE_PROTO("RX hp not removed (handshake not completed)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004416 QUIC_EV_CONN_ELRMHP, qc);
4417 goto out;
4418 }
Frédéric Lécaille72027782023-02-22 16:20:09 +01004419
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004420 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01004421 struct quic_tls_ctx *tls_ctx;
4422
4423 tls_ctx = qc_select_tls_ctx(qc, el, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004424 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
4425 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004426 TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004427 }
4428 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004429 qc_handle_spin_bit(qc, pqpkt, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004430 /* The AAD includes the packet number field */
4431 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
4432 /* Store the packet into the tree of packets to decrypt. */
4433 pqpkt->pn_node.key = pqpkt->pn;
4434 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
4435 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004436 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004437 }
4438 LIST_DELETE(&pqpkt->list);
4439 quic_rx_packet_refdec(pqpkt);
4440 }
4441
4442 out:
4443 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
4444}
4445
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004446/* Process all the CRYPTO frame at <el> encryption level. This is the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004447 * responsibility of the called to ensure there exists a CRYPTO data
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004448 * stream for this level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004449 * Return 1 if succeeded, 0 if not.
4450 */
4451static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
4452 struct quic_enc_level *el,
4453 struct ssl_sock_ctx *ctx)
4454{
4455 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004456 struct ncbuf *ncbuf;
4457 struct quic_cstream *cstream = el->cstream;
4458 ncb_sz_t data;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004459
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004460 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004461
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004462 BUG_ON(!cstream);
4463 ncbuf = &cstream->rx.ncbuf;
4464 if (ncb_is_null(ncbuf))
4465 goto done;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004466
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004467 /* TODO not working if buffer is wrapping */
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004468 while ((data = ncb_data(ncbuf, 0))) {
4469 const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004470
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004471 if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004472 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004473
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004474 cstream->rx.offset += data;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004475 TRACE_DEVEL("buffered crypto data were provided to TLS stack",
4476 QUIC_EV_CONN_PHPKTS, qc, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004477 }
4478
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004479 done:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004480 ret = 1;
4481 leave:
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004482 if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
4483 TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004484 quic_free_ncbuf(ncbuf);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004485 }
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004486 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004487 return ret;
4488}
4489
4490/* Process all the packets at <el> and <next_el> encryption level.
4491 * This is the caller responsibility to check that <cur_el> is different of <next_el>
4492 * as pointer value.
4493 * Return 1 if succeeded, 0 if not.
4494 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004495int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el,
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004496 struct quic_enc_level *next_el)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004497{
4498 int ret = 0;
4499 struct eb64_node *node;
4500 int64_t largest_pn = -1;
4501 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004502 struct quic_enc_level *qel = cur_el;
4503
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004504 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004505 qel = cur_el;
4506 next_tel:
4507 if (!qel)
4508 goto out;
4509
4510 node = eb64_first(&qel->rx.pkts);
4511 while (node) {
4512 struct quic_rx_packet *pkt;
4513
4514 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
4515 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004516 qc, pkt, NULL, qc->xprt_ctx->ssl);
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01004517 if (!qc_pkt_decrypt(qc, qel, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004518 /* Drop the packet */
4519 TRACE_ERROR("packet decryption failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004520 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004521 }
4522 else {
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004523 if (!qc_parse_pkt_frms(qc, pkt, qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004524 /* Drop the packet */
4525 TRACE_ERROR("packet parsing failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004526 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004527 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
4528 }
4529 else {
4530 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
4531
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004532 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
Frédéric Lécailleb5efe792023-04-14 09:56:17 +02004533 int arm_ack_timer =
4534 qc->state >= QUIC_HS_ST_COMPLETE &&
4535 qel->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT];
4536
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004537 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
4538 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécailleb5efe792023-04-14 09:56:17 +02004539 qc_idle_timer_rearm(qc, 1, arm_ack_timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004540 }
4541 if (pkt->pn > largest_pn) {
4542 largest_pn = pkt->pn;
4543 largest_pn_time_received = pkt->time_received;
4544 }
4545 /* Update the list of ranges to acknowledge. */
4546 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
4547 TRACE_ERROR("Could not update ack range list",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004548 QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004549 }
4550 }
4551 node = eb64_next(node);
4552 eb64_delete(&pkt->pn_node);
4553 quic_rx_packet_refdec(pkt);
4554 }
4555
4556 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
4557 /* Update the largest packet number. */
4558 qel->pktns->rx.largest_pn = largest_pn;
4559 /* Update the largest acknowledged packet timestamps */
4560 qel->pktns->rx.largest_time_received = largest_pn_time_received;
4561 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
4562 }
4563
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004564 if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004565 // trace already emitted by function above
4566 goto leave;
4567 }
4568
4569 if (qel == cur_el) {
4570 BUG_ON(qel == next_el);
4571 qel = next_el;
4572 largest_pn = -1;
4573 goto next_tel;
4574 }
4575
4576 out:
4577 ret = 1;
4578 leave:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004579 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004580 return ret;
4581}
4582
4583/* Check if it's possible to remove header protection for packets related to
4584 * encryption level <qel>. If <qel> is NULL, assume it's false.
4585 *
4586 * Return true if the operation is possible else false.
4587 */
4588static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
4589{
4590 int ret = 0;
4591 enum quic_tls_enc_level tel;
4592
4593 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
4594
4595 if (!qel)
4596 goto cant_rm_hp;
4597
4598 tel = ssl_to_quic_enc_level(qel->level);
4599
4600 /* check if tls secrets are available */
4601 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004602 TRACE_PROTO("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004603 goto cant_rm_hp;
4604 }
4605
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004606 if (!quic_tls_has_rx_sec(qel)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004607 TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004608 goto cant_rm_hp;
4609 }
4610
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004611 if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004612 TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004613 goto cant_rm_hp;
4614 }
4615
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004616 /* check if the connection layer is ready before using app level */
4617 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
4618 qc->mux_state == QC_MUX_NULL) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004619 TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004620 goto cant_rm_hp;
4621 }
4622
4623 ret = 1;
4624 cant_rm_hp:
4625 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
4626 return ret;
4627}
4628
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004629/* Flush txbuf for <qc> connection. This must be called prior to a packet
4630 * preparation when txbuf contains older data. A send will be conducted for
4631 * these data.
4632 *
4633 * Returns 1 on success : buffer is empty and can be use for packet
4634 * preparation. On error 0 is returned.
4635 */
4636static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
4637{
4638 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4639
4640 /* This operation can only be conducted if txbuf is not empty. This
4641 * case only happens for connection with their owned socket due to an
4642 * older transient sendto() error.
4643 */
4644 BUG_ON(!qc_test_fd(qc));
4645
4646 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4647 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4648 qc_txb_release(qc);
4649 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4650 return 0;
4651 }
4652
4653 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4654 return 1;
4655}
4656
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004657/* Try to send application frames from list <frms> on connection <qc>.
4658 *
4659 * Use qc_send_app_probing wrapper when probing with old data.
4660 *
4661 * Returns 1 on success. Some data might not have been sent due to congestion,
4662 * in this case they are left in <frms> input list. The caller may subscribe on
4663 * quic-conn to retry later.
4664 *
4665 * Returns 0 on critical error.
4666 * TODO review and classify more distinctly transient from definitive errors to
4667 * allow callers to properly handle it.
4668 */
4669static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
4670{
4671 int status = 0;
4672 struct buffer *buf;
4673
4674 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4675
4676 buf = qc_txb_alloc(qc);
4677 if (!buf) {
4678 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004679 goto err;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004680 }
4681
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004682 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4683 goto err;
4684
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004685 /* Prepare and send packets until we could not further prepare packets. */
4686 while (1) {
4687 int ret;
4688 /* Currently buf cannot be non-empty at this stage. Even if a
4689 * previous sendto() has failed it is emptied to simulate
4690 * packet emission and rely on QUIC lost detection to try to
4691 * emit it.
4692 */
4693 BUG_ON_HOT(b_data(buf));
4694 b_reset(buf);
4695
4696 ret = qc_prep_app_pkts(qc, buf, frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004697 if (ret == -1) {
4698 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004699 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004700 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004701
Amaury Denoyelle37333862023-02-28 11:53:48 +01004702 if (!ret)
4703 break;
4704
4705 if (!qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004706 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4707 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004708 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004709 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004710 }
4711
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004712 status = 1;
4713 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004714 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4715 return status;
4716
4717 err:
Amaury Denoyelle37333862023-02-28 11:53:48 +01004718 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4719 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004720}
4721
4722/* Try to send application frames from list <frms> on connection <qc>. Use this
4723 * function when probing is required.
4724 *
4725 * Returns the result from qc_send_app_pkts function.
4726 */
4727static forceinline int qc_send_app_probing(struct quic_conn *qc,
4728 struct list *frms)
4729{
4730 int ret;
4731
4732 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4733
4734 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
4735 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4736 ret = qc_send_app_pkts(qc, frms);
4737 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
4738
4739 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4740 return ret;
4741}
4742
4743/* Try to send application frames from list <frms> on connection <qc>. This
4744 * function is provided for MUX upper layer usage only.
4745 *
4746 * Returns the result from qc_send_app_pkts function.
4747 */
4748int qc_send_mux(struct quic_conn *qc, struct list *frms)
4749{
4750 int ret;
4751
4752 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4753 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
4754
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004755 /* Try to send post handshake frames first unless on 0-RTT. */
4756 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) &&
4757 qc->state >= QUIC_HS_ST_COMPLETE) {
4758 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4759 quic_build_post_handshake_frames(qc);
4760 qc_send_app_pkts(qc, &qel->pktns->tx.frms);
4761 }
4762
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004763 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4764 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
4765 ret = qc_send_app_pkts(qc, frms);
4766 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
4767
4768 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4769 return ret;
4770}
4771
4772/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4773 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
4774 * QUIC connection. <old_data> is used as boolean to send data already sent but
4775 * not already acknowledged (in flight).
4776 * Returns 1 if succeeded, 0 if not.
4777 */
4778int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4779 enum quic_tls_enc_level tel, struct list *tel_frms,
4780 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4781{
4782 int ret, status = 0;
4783 struct buffer *buf = qc_txb_alloc(qc);
4784
4785 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4786
4787 if (!buf) {
4788 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4789 goto leave;
4790 }
4791
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004792 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4793 goto out;
4794
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004795 /* Currently buf cannot be non-empty at this stage. Even if a previous
4796 * sendto() has failed it is emptied to simulate packet emission and
4797 * rely on QUIC lost detection to try to emit it.
4798 */
4799 BUG_ON_HOT(b_data(buf));
4800 b_reset(buf);
4801
4802 if (old_data) {
4803 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
4804 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4805 }
4806
4807 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004808 if (ret == -1) {
4809 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004810 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004811 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004812
Amaury Denoyelle37333862023-02-28 11:53:48 +01004813 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004814 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4815 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004816 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004817 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004818
Amaury Denoyelle37333862023-02-28 11:53:48 +01004819 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004820 status = 1;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004821
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004822 out:
4823 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
4824 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004825 leave:
4826 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4827 return status;
4828}
4829
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004830/* Retransmit up to two datagrams depending on packet number space.
4831 * Return 0 when failed, 0 if not.
4832 */
4833static int qc_dgrams_retransmit(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004834{
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004835 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004836 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4837 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4838 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4839
4840 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4841
4842 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004843 int i;
4844
4845 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4846 struct list ifrms = LIST_HEAD_INIT(ifrms);
4847 struct list hfrms = LIST_HEAD_INIT(hfrms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004848
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004849 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
4850 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4851 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
4852 if (!LIST_ISEMPTY(&ifrms)) {
4853 iqel->pktns->tx.pto_probe = 1;
4854 if (!LIST_ISEMPTY(&hfrms))
4855 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004856 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4857 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms))
4858 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004859 /* Put back unsent frames in their packet number spaces */
4860 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4861 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
4862 }
Frédéric Lécailleec937212023-03-03 17:34:41 +01004863 else {
4864 if (!(qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
4865 iqel->pktns->tx.pto_probe = 1;
4866 if (!qc_send_hdshk_pkts(qc, 0, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4867 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4868 goto leave;
4869 }
4870 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004871 }
4872 TRACE_STATE("no more need to probe Initial packet number space",
4873 QUIC_EV_CONN_TXPKT, qc);
4874 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004875 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004876 }
4877 else {
4878 int i;
4879
4880 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004881 hqel->pktns->tx.pto_probe = 0;
4882 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
Frédéric Lécaille7b5d9b12022-11-28 17:21:45 +01004883 struct list frms1 = LIST_HEAD_INIT(frms1);
4884
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004885 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
4886 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4887 if (!LIST_ISEMPTY(&frms1)) {
4888 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004889 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4890 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4891 goto leave;
4892
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004893 /* Put back unsent frames into their packet number spaces */
4894 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
4895 }
4896 }
4897 TRACE_STATE("no more need to probe Handshake packet number space",
4898 QUIC_EV_CONN_TXPKT, qc);
4899 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4900 }
4901 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4902 struct list frms2 = LIST_HEAD_INIT(frms2);
4903 struct list frms1 = LIST_HEAD_INIT(frms1);
4904
4905 aqel->pktns->tx.pto_probe = 0;
4906 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4907 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4908 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4909 if (!LIST_ISEMPTY(&frms1)) {
4910 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004911 if (!qc_send_app_probing(qc, &frms1))
4912 goto leave;
4913
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004914 /* Put back unsent frames into their packet number spaces */
4915 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
4916 }
4917 if (!LIST_ISEMPTY(&frms2)) {
4918 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004919 if (!qc_send_app_probing(qc, &frms2))
4920 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004921 /* Put back unsent frames into their packet number spaces */
4922 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
4923 }
4924 TRACE_STATE("no more need to probe 01RTT packet number space",
4925 QUIC_EV_CONN_TXPKT, qc);
4926 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4927 }
4928 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004929
4930 ret = 1;
4931 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004932 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004933 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004934}
4935
4936/* QUIC connection packet handler task (post handshake) */
4937struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4938{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004939 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004940 struct quic_enc_level *qel;
4941
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004942 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02004943
4944 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004945 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
4946
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01004947 if (qc_test_fd(qc))
4948 qc_rcv_buf(qc);
4949
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004950 /* Prepare post-handshake frames
4951 * - after connection is instantiated (accept is done)
4952 * - handshake state is completed (may not be the case here in 0-RTT)
4953 */
4954 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) && qc->conn &&
4955 qc->state >= QUIC_HS_ST_COMPLETE) {
4956 quic_build_post_handshake_frames(qc);
4957 }
4958
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004959 /* Retranmissions */
4960 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
4961 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
4962 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004963 if (!qc_dgrams_retransmit(qc))
4964 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004965 }
4966
4967 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4968 qc_rm_hp_pkts(qc, qel);
4969
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004970 if (!qc_treat_rx_pkts(qc, qel, NULL)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004971 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4972 goto out;
4973 }
4974
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01004975 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
4976 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
4977 goto out;
4978 }
4979
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004980 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4981 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4982 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
4983 goto out;
4984 }
4985
4986 /* XXX TODO: how to limit the list frames to send */
4987 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4988 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4989 goto out;
4990 }
4991
4992 out:
4993 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4994 return t;
4995}
4996
4997/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4998static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4999{
5000 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
5001 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
5002 qel->pktns->tx.pto_probe ||
5003 !LIST_ISEMPTY(&qel->pktns->tx.frms);
5004}
5005
5006/* QUIC connection packet handler task. */
5007struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
5008{
5009 int ret, ssl_err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005010 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005011 enum quic_tls_enc_level tel, next_tel;
5012 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005013 /* Early-data encryption level */
5014 struct quic_enc_level *eqel;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005015 struct buffer *buf = NULL;
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005016 int st, zero_rtt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005017
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005018 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02005019
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005020 eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005021 st = qc->state;
5022 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
5023
5024 /* Retranmissions */
5025 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
5026 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
5027 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005028 if (!qc_dgrams_retransmit(qc))
5029 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005030 }
5031
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005032 ssl_err = SSL_ERROR_NONE;
5033 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005034 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005035 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01005036
5037 if (qc_test_fd(qc))
5038 qc_rcv_buf(qc);
5039
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005040 if (st >= QUIC_HS_ST_COMPLETE &&
5041 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
5042 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
5043 /* There may be remaining Handshake packets to treat and acknowledge. */
5044 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
5045 next_tel = QUIC_TLS_ENC_LEVEL_APP;
5046 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005047 else if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, zero_rtt))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005048 goto out;
5049
5050 qel = &qc->els[tel];
5051 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
5052
5053 next_level:
5054 /* Treat packets waiting for header packet protection decryption */
5055 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
5056 qc_rm_hp_pkts(qc, qel);
5057
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005058 if (!qc_treat_rx_pkts(qc, qel, next_qel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005059 goto out;
5060
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005061 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5062 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
5063 goto out;
5064 }
5065
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005066 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5067 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
5068 goto out;
5069
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005070 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005071 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005072 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
5073 if (next_qel && next_qel == eqel && zero_rtt) {
5074 TRACE_DEVEL("select 0RTT as next encryption level",
5075 QUIC_EV_CONN_PHPKTS, qc);
5076 qel = next_qel;
5077 next_qel = NULL;
5078 goto next_level;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005079 }
5080
5081 st = qc->state;
5082 if (st >= QUIC_HS_ST_COMPLETE) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005083 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
5084 QUIC_FL_TLS_SECRETS_DCD)) {
5085 /* Discard the Handshake keys. */
5086 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5087 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
5088 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
5089 qc_set_timer(qc);
5090 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5091 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
5092 }
5093
5094 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
5095 /* There may be remaining handshake to build (acks) */
5096 st = QUIC_HS_ST_SERVER_HANDSHAKE;
5097 }
5098 }
5099
5100 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
5101 * be considered.
5102 */
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005103 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005104 goto out;
5105
5106 if (!qc_need_sending(qc, qel) &&
5107 (!next_qel || !qc_need_sending(qc, next_qel))) {
5108 goto skip_send;
5109 }
5110
5111 buf = qc_txb_alloc(qc);
5112 if (!buf)
5113 goto out;
5114
Amaury Denoyelle147862d2023-02-28 15:10:00 +01005115 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
5116 goto skip_send;
5117
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005118 /* Currently buf cannot be non-empty at this stage. Even if a previous
5119 * sendto() has failed it is emptied to simulate packet emission and
5120 * rely on QUIC lost detection to try to emit it.
5121 */
5122 BUG_ON_HOT(b_data(buf));
5123 b_reset(buf);
5124
5125 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
5126 next_tel, &qc->els[next_tel].pktns->tx.frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01005127 if (ret == -1) {
5128 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005129 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005130 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005131
Amaury Denoyelle37333862023-02-28 11:53:48 +01005132 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01005133 if (qc->flags & QUIC_FL_CONN_TO_KILL)
5134 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005135 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005136 }
5137
5138 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005139
5140 skip_send:
5141 /* Check if there is something to do for the next level.
5142 */
5143 if (next_qel && next_qel != qel &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005144 quic_tls_has_rx_sec(next_qel) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005145 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
5146 qel = next_qel;
5147 next_qel = NULL;
5148 goto next_level;
5149 }
5150
5151 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005152 TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
5153 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005154 return t;
5155}
5156
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005157/* Release the memory allocated for <cs> CRYPTO stream */
5158void quic_cstream_free(struct quic_cstream *cs)
5159{
5160 if (!cs) {
5161 /* This is the case for ORTT encryption level */
5162 return;
5163 }
5164
Amaury Denoyellebc174b22022-11-17 10:12:52 +01005165 quic_free_ncbuf(&cs->rx.ncbuf);
5166
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005167 qc_stream_desc_release(cs->desc);
5168 pool_free(pool_head_quic_cstream, cs);
5169}
5170
5171/* Allocate a new QUIC stream for <qc>.
5172 * Return it if succeeded, NULL if not.
5173 */
5174struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
5175{
5176 struct quic_cstream *cs, *ret_cs = NULL;
5177
5178 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5179 cs = pool_alloc(pool_head_quic_cstream);
5180 if (!cs) {
5181 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5182 goto leave;
5183 }
5184
5185 cs->rx.offset = 0;
5186 cs->rx.ncbuf = NCBUF_NULL;
5187 cs->rx.offset = 0;
5188
5189 cs->tx.offset = 0;
5190 cs->tx.sent_offset = 0;
5191 cs->tx.buf = BUF_NULL;
5192 cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
5193 if (!cs->desc) {
5194 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5195 goto err;
5196 }
5197
5198 ret_cs = cs;
5199 leave:
5200 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5201 return ret_cs;
5202
5203 err:
5204 pool_free(pool_head_quic_cstream, cs);
5205 goto leave;
5206}
5207
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005208/* Uninitialize <qel> QUIC encryption level. Never fails. */
5209static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
5210{
5211 int i;
5212
5213 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5214
5215 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
5216 if (qel->tx.crypto.bufs[i]) {
5217 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
5218 qel->tx.crypto.bufs[i] = NULL;
5219 }
5220 }
5221 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005222 quic_cstream_free(qel->cstream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005223
5224 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5225}
5226
5227/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
5228 * connection allocating everything needed.
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005229 *
5230 * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use
5231 * quic_conn_enc_level_uninit() to cleanup partially allocated content.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005232 */
5233static int quic_conn_enc_level_init(struct quic_conn *qc,
5234 enum quic_tls_enc_level level)
5235{
5236 int ret = 0;
5237 struct quic_enc_level *qel;
5238
5239 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5240
5241 qel = &qc->els[level];
5242 qel->level = quic_to_ssl_enc_level(level);
5243 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
5244 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
5245 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
5246 qel->tls_ctx.flags = 0;
5247
5248 qel->rx.pkts = EB_ROOT;
5249 LIST_INIT(&qel->rx.pqpkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005250
5251 /* Allocate only one buffer. */
5252 /* TODO: use a pool */
5253 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
5254 if (!qel->tx.crypto.bufs)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005255 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005256
5257 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
5258 if (!qel->tx.crypto.bufs[0])
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005259 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005260
5261 qel->tx.crypto.bufs[0]->sz = 0;
5262 qel->tx.crypto.nb_buf = 1;
5263
5264 qel->tx.crypto.sz = 0;
5265 qel->tx.crypto.offset = 0;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005266 /* No CRYPTO data for early data TLS encryption level */
5267 if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA)
5268 qel->cstream = NULL;
5269 else {
5270 qel->cstream = quic_cstream_new(qc);
5271 if (!qel->cstream)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005272 goto leave;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005273 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005274
5275 ret = 1;
5276 leave:
5277 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5278 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005279}
5280
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005281/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
5282 * This is not the case if the remote peer address is not validated and if
5283 * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
5284 */
5285static int qc_may_probe_ipktns(struct quic_conn *qc)
5286{
5287 return quic_peer_validated_addr(qc) ||
5288 (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
5289}
5290
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005291/* Callback called upon loss detection and PTO timer expirations. */
5292struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
5293{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005294 struct quic_conn *qc = ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005295 struct quic_pktns *pktns;
5296
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005297 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
5298 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005299 NULL, NULL, &qc->path->ifae_pkts);
5300 task->expire = TICK_ETERNITY;
5301 pktns = quic_loss_pktns(qc);
5302 if (tick_isset(pktns->tx.loss_time)) {
5303 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
5304
5305 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
5306 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005307 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01005308 if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
5309 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005310 goto out;
5311 }
5312
5313 if (qc->path->in_flight) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +01005314 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
Frédéric Lécaille68737312023-04-07 16:28:46 +02005315 if (!pktns->tx.in_flight) {
5316 TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
5317 goto out;
5318 }
5319
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005320 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
5321 if (qc_may_probe_ipktns(qc)) {
5322 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5323 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5324 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5325 }
5326 else {
5327 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5328 }
5329 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
5330 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5331 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5332 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5333 }
Frédéric Lécaillee25fce02023-03-20 17:23:19 +01005334 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005335 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
5336 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5337 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5338 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5339 if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005340 if (qc_may_probe_ipktns(qc)) {
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005341 qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005342 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5343 }
5344 else {
5345 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5346 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005347 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005348 }
5349 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01005350 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005351 /* Wake up upper layer if waiting to send new data. */
5352 if (!qc_notify_send(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005353 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005354 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5355 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005356 }
5357 }
5358 }
5359 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
5360 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
5361 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
5362
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005363 if (quic_tls_has_tx_sec(hel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005364 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005365 if (quic_tls_has_tx_sec(iel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005366 iel->pktns->tx.pto_probe = 1;
5367 }
5368
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005369 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005370 qc->path->loss.pto_count++;
5371
5372 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005373 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
5374 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005375
5376 return task;
5377}
5378
5379/* Parse the Retry token from buffer <token> with <end> a pointer to
5380 * one byte past the end of this buffer. This will extract the ODCID
5381 * which will be stored into <odcid>
5382 *
5383 * Returns 0 on success else non-zero.
5384 */
5385static int parse_retry_token(struct quic_conn *qc,
5386 const unsigned char *token, const unsigned char *end,
5387 struct quic_cid *odcid)
5388{
5389 int ret = 0;
5390 uint64_t odcid_len;
5391 uint32_t timestamp;
5392
5393 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5394
5395 if (!quic_dec_int(&odcid_len, &token, end)) {
5396 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
5397 goto leave;
5398 }
5399
5400 /* RFC 9000 7.2. Negotiating Connection IDs:
5401 * When an Initial packet is sent by a client that has not previously
5402 * received an Initial or Retry packet from the server, the client
5403 * populates the Destination Connection ID field with an unpredictable
5404 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5405 */
5406 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
5407 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
5408 goto leave;
5409 }
5410
5411 if (end - token < odcid_len + sizeof timestamp) {
5412 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
5413 goto leave;
5414 }
5415
5416 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécailled0742132023-04-19 17:31:28 +02005417 if (tick_is_expired(tick_add(timestamp, MS_TO_TICKS(QUIC_RETRY_DURATION_MS)), now_ms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005418 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
5419 goto leave;
5420 }
5421
5422 ret = 1;
5423 memcpy(odcid->data, token, odcid_len);
5424 odcid->len = odcid_len;
5425 leave:
5426 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5427 return !ret;
5428}
5429
5430/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
5431 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
5432 * for QUIC servers (or haproxy listeners).
5433 * <dcid> is the destination connection ID, <scid> is the source connection ID,
5434 * <token> the token found to be used for this connection with <token_len> as
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005435 * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005436 * Returns the connection if succeeded, NULL if not.
5437 */
5438static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
5439 struct quic_cid *dcid, struct quic_cid *scid,
5440 const struct quic_cid *token_odcid,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005441 struct quic_connection_id *conn_id,
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005442 struct sockaddr_storage *local_addr,
5443 struct sockaddr_storage *peer_addr,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005444 int server, int token, void *owner)
5445{
5446 int i;
5447 struct quic_conn *qc;
5448 /* Initial CID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005449 char *buf_area = NULL;
5450 struct listener *l = NULL;
5451 struct quic_cc_algo *cc_algo = NULL;
5452 struct quic_tls_ctx *ictx;
5453 TRACE_ENTER(QUIC_EV_CONN_INIT);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005454 /* TODO replace pool_zalloc by pool_alloc(). This requires special care
5455 * to properly initialized internal quic_conn members to safely use
5456 * quic_conn_release() on alloc failure.
5457 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005458 qc = pool_zalloc(pool_head_quic_conn);
5459 if (!qc) {
5460 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
5461 goto err;
5462 }
5463
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005464 /* Initialize in priority qc members required for a safe dealloc. */
5465
5466 /* required to use MTLIST_IN_LIST */
5467 MT_LIST_INIT(&qc->accept_list);
5468
5469 LIST_INIT(&qc->rx.pkt_list);
5470
Amaury Denoyelle42448332022-12-12 11:24:05 +01005471 qc_init_fd(qc);
5472
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005473 LIST_INIT(&qc->back_refs);
Amaury Denoyelled537ca72023-04-19 10:45:40 +02005474 LIST_INIT(&qc->el_th_ctx);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005475
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005476 /* Now proceeds to allocation of qc members. */
5477
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005478 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
5479 if (!buf_area) {
5480 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
5481 goto err;
5482 }
5483
5484 qc->cids = EB_ROOT;
5485 /* QUIC Server (or listener). */
5486 if (server) {
5487 struct proxy *prx;
5488
5489 l = owner;
5490 prx = l->bind_conf->frontend;
5491 cc_algo = l->bind_conf->quic_cc_algo;
5492
5493 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
5494 &quic_stats_module);
5495 qc->flags |= QUIC_FL_CONN_LISTENER;
5496 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005497 /* Copy the client original DCID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005498 qc->odcid.len = dcid->len;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005499 memcpy(qc->odcid.data, dcid->data, dcid->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005500
5501 /* copy the packet SCID to reuse it as DCID for sending */
5502 if (scid->len)
5503 memcpy(qc->dcid.data, scid->data, scid->len);
5504 qc->dcid.len = scid->len;
5505 qc->tx.buf = BUF_NULL;
5506 qc->li = l;
5507 }
5508 /* QUIC Client (outgoing connection to servers) */
5509 else {
5510 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
5511 if (dcid->len)
5512 memcpy(qc->dcid.data, dcid->data, dcid->len);
5513 qc->dcid.len = dcid->len;
5514 }
5515 qc->mux_state = QC_MUX_NULL;
5516 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
5517
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005518 conn_id->qc = qc;
5519 eb64_insert(&qc->cids, &conn_id->seq_num);
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01005520 /* Initialize the next CID sequence number to be used for this connection. */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005521 qc->next_cid_seq_num = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005522
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005523 if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
5524 is_addr(local_addr)) {
5525 TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
5526 qc_alloc_fd(qc, local_addr, peer_addr);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005527
5528 /* haproxy soft-stop is supported only for QUIC connections
5529 * with their owned socket.
5530 */
5531 if (qc_test_fd(qc))
5532 _HA_ATOMIC_INC(&jobs);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005533 }
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005534
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005535 /* Select our SCID which is the first CID with 0 as sequence number. */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005536 qc->scid = conn_id->cid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005537
5538 /* Packet number spaces initialization. */
5539 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
5540 quic_pktns_init(&qc->pktns[i]);
5541 /* QUIC encryption level context initialization. */
5542 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5543 if (!quic_conn_enc_level_init(qc, i)) {
5544 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
5545 goto err;
5546 }
5547 /* Initialize the packet number space. */
5548 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
5549 }
5550
5551 qc->original_version = qv;
5552 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
5553 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
5554 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
5555 /* TX part. */
5556 LIST_INIT(&qc->tx.frms_to_send);
5557 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
5558 qc->tx.wbuf = qc->tx.rbuf = 0;
5559 qc->tx.bytes = 0;
5560 qc->tx.buf = BUF_NULL;
5561 /* RX part. */
5562 qc->rx.bytes = 0;
5563 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
5564 for (i = 0; i < QCS_MAX_TYPES; i++)
5565 qc->rx.strms[i].nb_streams = 0;
5566
5567 qc->nb_pkt_for_cc = 1;
5568 qc->nb_pkt_since_cc = 0;
5569
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005570 if (!quic_tls_ku_init(qc)) {
5571 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
5572 goto err;
5573 }
5574
5575 /* XXX TO DO: Only one path at this time. */
5576 qc->path = &qc->paths[0];
5577 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
5578
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005579 qc->streams_by_id = EB_ROOT_UNIQUE;
5580 qc->stream_buf_count = 0;
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005581 memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
5582 memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005583
5584 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005585 conn_id->stateless_reset_token,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005586 dcid->data, dcid->len,
5587 qc->scid.data, qc->scid.len, token_odcid))
5588 goto err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005589
Frédéric Lécailledeb97812023-03-22 11:29:45 +01005590 /* Initialize the idle timeout of the connection at the "max_idle_timeout"
5591 * value from local transport parameters.
5592 */
5593 qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005594 qc->wait_event.tasklet = tasklet_new();
5595 if (!qc->wait_event.tasklet) {
5596 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
5597 goto err;
5598 }
5599 qc->wait_event.tasklet->process = quic_conn_io_cb;
5600 qc->wait_event.tasklet->context = qc;
5601 qc->wait_event.events = 0;
Amaury Denoyellebbb1c682022-09-28 15:15:51 +02005602 qc->subs = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005603
5604 if (qc_conn_alloc_ssl_ctx(qc) ||
5605 !quic_conn_init_timer(qc) ||
5606 !quic_conn_init_idle_timer_task(qc))
5607 goto err;
5608
5609 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
5610 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
5611 goto err;
5612
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005613 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
5614 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
5615
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005616 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
5617
5618 return qc;
5619
5620 err:
5621 pool_free(pool_head_quic_conn_rxbuf, buf_area);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005622 if (qc) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005623 qc->rx.buf.area = NULL;
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005624 quic_conn_release(qc);
5625 }
5626 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005627 return NULL;
5628}
5629
5630/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
5631 * The connection tasklet is killed.
5632 *
5633 * This function must only be called by the thread responsible of the quic_conn
5634 * tasklet.
5635 */
5636void quic_conn_release(struct quic_conn *qc)
5637{
5638 int i;
5639 struct ssl_sock_ctx *conn_ctx;
5640 struct eb64_node *node;
5641 struct quic_tls_ctx *app_tls_ctx;
5642 struct quic_rx_packet *pkt, *pktback;
5643
5644 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5645
5646 /* We must not free the quic-conn if the MUX is still allocated. */
5647 BUG_ON(qc->mux_state == QC_MUX_READY);
5648
Amaury Denoyellefb375572023-02-01 09:28:32 +01005649 if (qc_test_fd(qc))
5650 _HA_ATOMIC_DEC(&jobs);
5651
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005652 /* Close quic-conn socket fd. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01005653 qc_release_fd(qc, 0);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005654
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005655 /* in the unlikely (but possible) case the connection was just added to
5656 * the accept_list we must delete it from there.
5657 */
5658 MT_LIST_DELETE(&qc->accept_list);
5659
5660 /* free remaining stream descriptors */
5661 node = eb64_first(&qc->streams_by_id);
5662 while (node) {
5663 struct qc_stream_desc *stream;
5664
5665 stream = eb64_entry(node, struct qc_stream_desc, by_id);
5666 node = eb64_next(node);
5667
5668 /* all streams attached to the quic-conn are released, so
5669 * qc_stream_desc_free will liberate the stream instance.
5670 */
5671 BUG_ON(!stream->release);
5672 qc_stream_desc_free(stream, 1);
5673 }
5674
5675 /* Purge Rx packet list. */
5676 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
5677 LIST_DELETE(&pkt->qc_rx_pkt_list);
5678 pool_free(pool_head_quic_rx_packet, pkt);
5679 }
5680
5681 if (qc->idle_timer_task) {
5682 task_destroy(qc->idle_timer_task);
5683 qc->idle_timer_task = NULL;
5684 }
5685
5686 if (qc->timer_task) {
5687 task_destroy(qc->timer_task);
5688 qc->timer_task = NULL;
5689 }
5690
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005691 if (qc->wait_event.tasklet)
5692 tasklet_free(qc->wait_event.tasklet);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005693
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005694 /* remove the connection from receiver cids trees */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005695 free_quic_conn_cids(qc);
5696
5697 conn_ctx = qc->xprt_ctx;
5698 if (conn_ctx) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005699 SSL_free(conn_ctx->ssl);
5700 pool_free(pool_head_quic_conn_ctx, conn_ctx);
5701 }
5702
5703 quic_tls_ku_free(qc);
5704 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5705 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
5706 quic_conn_enc_level_uninit(qc, &qc->els[i]);
5707 }
5708 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
5709
5710 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
5711 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
5712 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
5713
5714 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
5715 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
5716 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
5717 }
5718
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01005719 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005720
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005721 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
5722 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +02005723 qc = NULL;
Amaury Denoyellefb375572023-02-01 09:28:32 +01005724
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005725 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
5726
5727 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5728}
5729
5730/* Initialize the timer task of <qc> QUIC connection.
5731 * Returns 1 if succeeded, 0 if not.
5732 */
5733static int quic_conn_init_timer(struct quic_conn *qc)
5734{
5735 int ret = 0;
5736 /* Attach this task to the same thread ID used for the connection */
5737 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5738
Amaury Denoyelle66947282023-04-13 11:48:38 +02005739 qc->timer_task = task_new_here();
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005740 if (!qc->timer_task) {
5741 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5742 goto leave;
5743 }
5744
5745 qc->timer = TICK_ETERNITY;
5746 qc->timer_task->process = qc_process_timer;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005747 qc->timer_task->context = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005748
5749 ret = 1;
5750 leave:
5751 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5752 return ret;
5753}
5754
Frédéric Lécailled7215712023-03-24 18:13:37 +01005755/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
5756 * connection. */
5757static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005758{
5759 unsigned int expire;
5760
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005761 if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005762 TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005763 qc->ack_expire = TICK_ETERNITY;
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005764 task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
5765 }
5766 else {
5767 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005768 qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
5769 if (arm_ack) {
5770 /* Arm the ack timer only if not already armed. */
5771 if (!tick_isset(qc->ack_expire)) {
5772 qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
5773 qc->idle_timer_task->expire = qc->ack_expire;
5774 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005775 TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005776 }
5777 }
5778 else {
5779 qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
5780 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005781 TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005782 }
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005783 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005784}
5785
Frédéric Lécailled7215712023-03-24 18:13:37 +01005786/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
5787 * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
5788 * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
5789 * which must be rearmed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005790 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01005791static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005792{
5793 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5794
5795 if (read) {
5796 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5797 }
5798 else {
5799 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5800 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01005801 qc_idle_timer_do_rearm(qc, arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005802
5803 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5804}
5805
5806/* The task handling the idle timeout */
5807struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
5808{
5809 struct quic_conn *qc = ctx;
5810 struct quic_counters *prx_counters = qc->prx_counters;
5811 unsigned int qc_flags = qc->flags;
5812
5813 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5814
Frédéric Lécaille12eca3a2023-04-04 10:46:54 +02005815 if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
5816 goto requeue;
5817
Frédéric Lécailled7215712023-03-24 18:13:37 +01005818 if (tick_is_expired(qc->ack_expire, now_ms)) {
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005819 TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005820 qc->ack_expire = TICK_ETERNITY;
5821 /* Note that ->idle_expire is always set. */
5822 t->expire = qc->idle_expire;
5823 qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
5824 tasklet_wakeup(qc->wait_event.tasklet);
5825 goto requeue;
5826 }
5827
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005828 TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005829 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
5830 * might free the quic-conn too early via quic_close().
5831 */
5832 qc_notify_close(qc);
5833
5834 /* If the MUX is still alive, keep the quic-conn. The MUX is
5835 * responsible to call quic_close to release it.
5836 */
5837 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005838 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005839 quic_conn_release(qc);
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005840 qc = NULL;
5841 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005842
5843 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
5844 * least clean some parts of it such as the tasklet.
5845 */
5846
5847 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
5848 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005849 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005850 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
5851 }
5852
Frédéric Lécailled7215712023-03-24 18:13:37 +01005853 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005854 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5855 return NULL;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005856
5857 requeue:
5858 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5859 return t;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005860}
5861
5862/* Initialize the idle timeout task for <qc>.
5863 * Returns 1 if succeeded, 0 if not.
5864 */
5865static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
5866{
5867 int ret = 0;
5868
5869 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5870
5871 qc->idle_timer_task = task_new_here();
5872 if (!qc->idle_timer_task) {
5873 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5874 goto leave;
5875 }
5876
5877 qc->idle_timer_task->process = qc_idle_timer_task;
5878 qc->idle_timer_task->context = qc;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005879 qc->ack_expire = TICK_ETERNITY;
5880 qc_idle_timer_rearm(qc, 1, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005881 task_queue(qc->idle_timer_task);
5882
5883 ret = 1;
5884 leave:
5885 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5886 return ret;
5887}
5888
5889/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
5890 * past one byte of this buffer.
5891 */
5892static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
5893 struct quic_rx_packet *pkt)
5894{
5895 int ret = 0;
5896 unsigned char dcid_len, scid_len;
5897
5898 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5899
5900 if (end == *buf) {
5901 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
5902 goto leave;
5903 }
5904
5905 /* Destination Connection ID Length */
5906 dcid_len = *(*buf)++;
5907 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
5908 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
5909 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
5910 goto leave;
5911 }
5912
5913 if (dcid_len) {
5914 /* Check that the length of this received DCID matches the CID lengths
5915 * of our implementation for non Initials packets only.
5916 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02005917 if (pkt->version && pkt->version->num &&
5918 pkt->type != QUIC_PACKET_TYPE_INITIAL &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005919 pkt->type != QUIC_PACKET_TYPE_0RTT &&
5920 dcid_len != QUIC_HAP_CID_LEN) {
5921 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
5922 goto leave;
5923 }
5924
5925 memcpy(pkt->dcid.data, *buf, dcid_len);
5926 }
5927
5928 pkt->dcid.len = dcid_len;
5929 *buf += dcid_len;
5930
5931 /* Source Connection ID Length */
5932 scid_len = *(*buf)++;
5933 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
5934 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
5935 goto leave;
5936 }
5937
5938 if (scid_len)
5939 memcpy(pkt->scid.data, *buf, scid_len);
5940 pkt->scid.len = scid_len;
5941 *buf += scid_len;
5942
5943 ret = 1;
5944 leave:
5945 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
5946 return ret;
5947}
5948
5949/* Insert <pkt> RX packet in its <qel> RX packets tree */
5950static void qc_pkt_insert(struct quic_conn *qc,
5951 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
5952{
5953 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
5954
5955 pkt->pn_node.key = pkt->pn;
5956 quic_rx_packet_refinc(pkt);
5957 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
5958
5959 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
5960}
5961
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005962/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
5963 * address of the packet first byte, using the keys from encryption level <el>.
5964 *
5965 * If header protection has been successfully removed, packet data are copied
5966 * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
5967 * proceeded, and the packet is inserted into <qc> protected packets tree. In
5968 * both cases, packet can now be considered handled by the <qc> connection.
5969 *
5970 * If header protection cannot be removed due to <el> secrets already
5971 * discarded, no operation is conducted.
5972 *
5973 * Returns 1 on success : packet data is now handled by the connection. On
5974 * error 0 is returned : packet should be dropped by the caller.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005975 */
5976static inline int qc_try_rm_hp(struct quic_conn *qc,
5977 struct quic_rx_packet *pkt,
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005978 unsigned char *beg,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005979 struct quic_enc_level **el)
5980{
5981 int ret = 0;
5982 unsigned char *pn = NULL; /* Packet number field */
5983 enum quic_tls_enc_level tel;
5984 struct quic_enc_level *qel;
5985 /* Only for traces. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005986
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005987 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005988 BUG_ON(!pkt->pn_offset);
5989
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005990 /* The packet number is here. This is also the start minus
5991 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5992 * protection.
5993 */
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005994 pn = beg + pkt->pn_offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005995
5996 tel = quic_packet_type_enc_level(pkt->type);
5997 qel = &qc->els[tel];
5998
5999 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01006000 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
6001
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006002 /* Note that the following function enables us to unprotect the packet
6003 * number and its length subsequently used to decrypt the entire
6004 * packets.
6005 */
Frédéric Lécaille72027782023-02-22 16:20:09 +01006006 if (!qc_do_rm_hp(qc, pkt, tls_ctx,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006007 qel->pktns->rx.largest_pn, pn, beg)) {
6008 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
6009 goto out;
6010 }
6011
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006012 qc_handle_spin_bit(qc, pkt, qel);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006013 /* The AAD includes the packet number field. */
6014 pkt->aad_len = pkt->pn_offset + pkt->pnl;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006015 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
6016 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
6017 goto out;
6018 }
6019
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006020 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006021 }
6022 else {
6023 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
6024 /* If the packet number space has been discarded, this packet
6025 * will be not parsed.
6026 */
6027 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
6028 goto out;
6029 }
6030
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006031 TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006032 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
6033 quic_rx_packet_refinc(pkt);
6034 }
6035
6036 *el = qel;
6037 /* No reference counter incrementation here!!! */
6038 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
6039 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
6040 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
6041 b_add(&qc->rx.buf, pkt->len);
6042
6043 ret = 1;
6044 out:
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006045 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006046 return ret;
6047}
6048
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006049/* Return the QUIC version (quic_version struct) with <version> as version number
6050 * if supported or NULL if not.
6051 */
6052static inline const struct quic_version *qc_supported_version(uint32_t version)
6053{
6054 int i;
6055
6056 if (unlikely(!version))
6057 return &quic_version_VN_reserved;
6058
6059 for (i = 0; i < quic_versions_nb; i++)
6060 if (quic_versions[i].num == version)
6061 return &quic_versions[i];
6062
6063 return NULL;
6064}
6065
6066/* Parse a QUIC packet header starting at <buf> without exceeding <end>.
6067 * Version and type are stored in <pkt> packet instance. Type is set to unknown
6068 * on two occasions : for unsupported version, in this case version field is
6069 * set to NULL; for Version Negotiation packet with version number set to 0.
6070 *
6071 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006072 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006073int qc_parse_hd_form(struct quic_rx_packet *pkt,
6074 unsigned char **buf, const unsigned char *end)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006075{
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006076 uint32_t version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006077 int ret = 0;
6078 const unsigned char byte0 = **buf;
6079
6080 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006081 pkt->version = NULL;
6082 pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006083
6084 (*buf)++;
6085 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
6086 unsigned char type =
6087 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
6088
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006089 /* Version */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006090 if (!quic_read_uint32(&version, (const unsigned char **)buf, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006091 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
6092 goto out;
6093 }
6094
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006095 pkt->version = qc_supported_version(version);
6096 if (version && pkt->version) {
6097 if (version != QUIC_PROTOCOL_VERSION_2) {
6098 pkt->type = type;
6099 }
6100 else {
6101 switch (type) {
6102 case 0:
6103 pkt->type = QUIC_PACKET_TYPE_RETRY;
6104 break;
6105 case 1:
6106 pkt->type = QUIC_PACKET_TYPE_INITIAL;
6107 break;
6108 case 2:
6109 pkt->type = QUIC_PACKET_TYPE_0RTT;
6110 break;
6111 case 3:
6112 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
6113 break;
6114 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006115 }
6116 }
6117 }
6118 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006119 if (byte0 & QUIC_PACKET_SPIN_BIT)
6120 pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006121 pkt->type = QUIC_PACKET_TYPE_SHORT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006122 }
6123
6124 ret = 1;
6125 out:
6126 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6127 return ret;
6128}
6129
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006130/*
6131 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
6132 * address <addr>.
6133 * Implementation of RFC9000 6. Version Negotiation
6134 *
6135 * TODO implement a rate-limiting sending of Version Negotiation packets
6136 *
6137 * Returns 0 on success else non-zero
6138 */
6139static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
6140 struct quic_rx_packet *pkt)
6141{
6142 char buf[256];
6143 int ret = 0, i = 0, j;
6144 uint32_t version;
6145 const socklen_t addrlen = get_addr_len(addr);
6146
6147 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6148 /*
6149 * header form
6150 * long header, fixed bit to 0 for Version Negotiation
6151 */
6152 /* TODO: RAND_bytes() should be replaced? */
6153 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
6154 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
6155 goto out;
6156 }
6157
6158 buf[i++] |= '\x80';
6159 /* null version for Version Negotiation */
6160 buf[i++] = '\x00';
6161 buf[i++] = '\x00';
6162 buf[i++] = '\x00';
6163 buf[i++] = '\x00';
6164
6165 /* source connection id */
6166 buf[i++] = pkt->scid.len;
6167 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6168 i += pkt->scid.len;
6169
6170 /* destination connection id */
6171 buf[i++] = pkt->dcid.len;
6172 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
6173 i += pkt->dcid.len;
6174
6175 /* supported version */
6176 for (j = 0; j < quic_versions_nb; j++) {
6177 version = htonl(quic_versions[j].num);
6178 memcpy(&buf[i], &version, sizeof(version));
6179 i += sizeof(version);
6180 }
6181
6182 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
6183 goto out;
6184
6185 ret = 1;
6186 out:
6187 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6188 return !ret;
6189}
6190
6191/* Send a stateless reset packet depending on <pkt> RX packet information
6192 * from <fd> UDP socket to <dst>
6193 * Return 1 if succeeded, 0 if not.
6194 */
6195static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
6196 struct quic_rx_packet *rxpkt)
6197{
6198 int ret = 0, pktlen, rndlen;
6199 unsigned char pkt[64];
6200 const socklen_t addrlen = get_addr_len(dstaddr);
6201 struct proxy *prx;
6202 struct quic_counters *prx_counters;
6203
6204 TRACE_ENTER(QUIC_EV_STATELESS_RST);
6205
6206 prx = l->bind_conf->frontend;
6207 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6208 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
6209 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
6210 * Reset is difficult to distinguish from other packets if the recipient requires
6211 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
6212 * that all packets it sends are at least 22 bytes longer than the minimum
6213 * connection ID length that it requests the peer to include in its packets,
6214 * adding PADDING frames as necessary. This ensures that any Stateless Reset
6215 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
6216 * An endpoint that sends a Stateless Reset in response to a packet that is
6217 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
6218 * than the packet it responds to.
6219 */
6220
6221 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
6222 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
6223 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
6224 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
6225
6226 /* Put a header of random bytes */
6227 /* TODO: RAND_bytes() should be replaced */
6228 if (RAND_bytes(pkt, rndlen) != 1) {
6229 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
6230 goto leave;
6231 }
6232
6233 /* Clear the most significant bit, and set the second one */
6234 *pkt = (*pkt & ~0x80) | 0x40;
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02006235 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006236 rxpkt->dcid.data, rxpkt->dcid.len))
6237 goto leave;
6238
6239 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
6240 goto leave;
6241
6242 ret = 1;
6243 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
6244 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
6245 leave:
6246 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
6247 return ret;
6248}
6249
6250/* QUIC server only function.
6251 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
6252 * This is the responsibility of the caller to check <aad> size is big enough
6253 * to contain these data.
6254 * Return the number of bytes copied to <aad>.
6255 */
6256static int quic_generate_retry_token_aad(unsigned char *aad,
6257 uint32_t version,
6258 const struct quic_cid *cid,
6259 const struct sockaddr_storage *addr)
6260{
6261 unsigned char *p;
6262
6263 p = aad;
6264 memcpy(p, &version, sizeof version);
6265 p += sizeof version;
6266 p += quic_saddr_cpy(p, addr);
6267 memcpy(p, cid->data, cid->len);
6268 p += cid->len;
6269
6270 return p - aad;
6271}
6272
6273/* QUIC server only function.
6274 * Generate the token to be used in Retry packets. The token is written to
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05006275 * <buf> with <len> as length. <odcid> is the original destination connection
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006276 * ID and <dcid> is our side destination connection ID (or client source
6277 * connection ID).
6278 * Returns the length of the encoded token or 0 on error.
6279 */
6280static int quic_generate_retry_token(unsigned char *buf, size_t len,
6281 const uint32_t version,
6282 const struct quic_cid *odcid,
6283 const struct quic_cid *dcid,
6284 struct sockaddr_storage *addr)
6285{
6286 int ret = 0;
6287 unsigned char *p;
6288 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006289 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006290 size_t aadlen;
6291 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
6292 unsigned char key[QUIC_TLS_KEY_LEN];
6293 unsigned char iv[QUIC_TLS_IV_LEN];
6294 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6295 size_t seclen = strlen(global.cluster_secret);
6296 EVP_CIPHER_CTX *ctx = NULL;
6297 const EVP_CIPHER *aead = EVP_aes_128_gcm();
6298 uint32_t timestamp = now_ms;
6299
6300 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6301
6302 /* We copy the odcid into the token, prefixed by its one byte
6303 * length, the format token byte. It is followed by an AEAD TAG, and finally
6304 * the random bytes used to derive the secret to encrypt the token.
6305 */
6306 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
6307 goto err;
6308
6309 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
6310 /* TODO: RAND_bytes() should be replaced */
6311 if (RAND_bytes(salt, sizeof salt) != 1) {
6312 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
6313 goto err;
6314 }
6315
6316 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6317 salt, sizeof salt, sec, seclen)) {
6318 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
6319 goto err;
6320 }
6321
6322 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
6323 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
6324 goto err;
6325 }
6326
6327 /* Token build */
6328 p = buf;
6329 *p++ = QUIC_TOKEN_FMT_RETRY,
6330 *p++ = odcid->len;
6331 memcpy(p, odcid->data, odcid->len);
6332 p += odcid->len;
6333 write_u32(p, htonl(timestamp));
6334 p += sizeof timestamp;
6335
6336 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
6337 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
6338 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
6339 goto err;
6340 }
6341
6342 p += QUIC_TLS_TAG_LEN;
6343 memcpy(p, salt, sizeof salt);
6344 p += sizeof salt;
6345 EVP_CIPHER_CTX_free(ctx);
6346
6347 ret = p - buf;
6348 leave:
6349 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6350 return ret;
6351
6352 err:
6353 if (ctx)
6354 EVP_CIPHER_CTX_free(ctx);
6355 goto leave;
6356}
6357
6358/* QUIC server only function.
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006359 *
6360 * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
6361 * the UDP datagram containing <pkt> and <l> is the listener instance on which
6362 * it was received. If the token is valid, the ODCID of <qc> QUIC connection
6363 * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
6364 * to validate the token but it can be NULL : in this case the version will be
6365 * retrieved from the packet.
6366 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006367 * Return 1 if succeeded, 0 if not.
6368 */
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006369
6370static int quic_retry_token_check(struct quic_rx_packet *pkt,
6371 struct quic_dgram *dgram,
6372 struct listener *l,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006373 struct quic_conn *qc,
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006374 struct quic_cid *odcid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006375{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006376 struct proxy *prx;
6377 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006378 int ret = 0;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006379 unsigned char *token = pkt->token;
6380 const uint64_t tokenlen = pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006381 unsigned char buf[128];
6382 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006383 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006384 size_t aadlen;
6385 const unsigned char *salt;
6386 unsigned char key[QUIC_TLS_KEY_LEN];
6387 unsigned char iv[QUIC_TLS_IV_LEN];
6388 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6389 size_t seclen = strlen(global.cluster_secret);
6390 EVP_CIPHER_CTX *ctx = NULL;
6391 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006392 const struct quic_version *qv = qc ? qc->original_version :
6393 pkt->version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006394
6395 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6396
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006397 /* The caller must ensure this. */
6398 BUG_ON(!global.cluster_secret || !pkt->token_len);
6399
6400 prx = l->bind_conf->frontend;
6401 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6402
6403 if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
6404 /* TODO: New token check */
6405 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6406 goto leave;
6407 }
6408
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006409 if (sizeof buf < tokenlen) {
6410 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
6411 goto err;
6412 }
6413
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006414 aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006415 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
6416 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6417 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
6418 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
6419 goto err;
6420 }
6421
6422 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
6423 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
6424 goto err;
6425 }
6426
6427 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
6428 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
6429 ctx, aead, key, iv)) {
6430 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
6431 goto err;
6432 }
6433
6434 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
6435 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
6436 goto err;
6437 }
6438
6439 EVP_CIPHER_CTX_free(ctx);
6440
6441 ret = 1;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006442 HA_ATOMIC_INC(&prx_counters->retry_validated);
6443
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006444 leave:
6445 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6446 return ret;
6447
6448 err:
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006449 HA_ATOMIC_INC(&prx_counters->retry_error);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006450 if (ctx)
6451 EVP_CIPHER_CTX_free(ctx);
6452 goto leave;
6453}
6454
6455/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
6456 * the Initial <pkt> packet.
6457 *
6458 * Returns 0 on success else non-zero.
6459 */
6460static int send_retry(int fd, struct sockaddr_storage *addr,
6461 struct quic_rx_packet *pkt, const struct quic_version *qv)
6462{
6463 int ret = 0;
6464 unsigned char buf[128];
6465 int i = 0, token_len;
6466 const socklen_t addrlen = get_addr_len(addr);
6467 struct quic_cid scid;
6468
6469 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6470
6471 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
6472 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
6473 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
6474 /* version */
6475 buf[i++] = *((unsigned char *)&qv->num + 3);
6476 buf[i++] = *((unsigned char *)&qv->num + 2);
6477 buf[i++] = *((unsigned char *)&qv->num + 1);
6478 buf[i++] = *(unsigned char *)&qv->num;
6479
6480 /* Use the SCID from <pkt> for Retry DCID. */
6481 buf[i++] = pkt->scid.len;
6482 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6483 i += pkt->scid.len;
6484
6485 /* Generate a new CID to be used as SCID for the Retry packet. */
6486 scid.len = QUIC_HAP_CID_LEN;
6487 /* TODO: RAND_bytes() should be replaced */
6488 if (RAND_bytes(scid.data, scid.len) != 1) {
6489 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
6490 goto out;
6491 }
6492
6493 buf[i++] = scid.len;
6494 memcpy(&buf[i], scid.data, scid.len);
6495 i += scid.len;
6496
6497 /* token */
6498 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
6499 &pkt->dcid, &pkt->scid, addr))) {
6500 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
6501 goto out;
6502 }
6503
6504 i += token_len;
6505
6506 /* token integrity tag */
6507 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
6508 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
6509 pkt->dcid.len, buf, i, qv)) {
6510 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6511 goto out;
6512 }
6513
6514 i += QUIC_TLS_TAG_LEN;
6515
6516 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
6517 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6518 goto out;
6519 }
6520
6521 ret = 1;
6522 out:
6523 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6524 return !ret;
6525}
6526
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006527/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
6528 * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
6529 * is used.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006530 *
6531 * Returns the instance or NULL if not found.
6532 */
6533static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
6534 struct listener *l,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006535 struct sockaddr_storage *saddr,
6536 int *new_tid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006537{
6538 struct quic_conn *qc = NULL;
6539 struct ebmb_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006540 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006541 struct quic_cid_tree *tree;
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006542 uint conn_id_tid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006543
6544 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006545 *new_tid = -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006546
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006547 /* First look into DCID tree. */
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006548 tree = &quic_cid_trees[_quic_cid_tree_idx(pkt->dcid.data)];
6549 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6550 node = ebmb_lookup(&tree->root, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006551
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006552 /* If not found on an Initial/0-RTT packet, it could be because an
6553 * ODCID is reused by the client. Calculate the derived CID value to
6554 * retrieve it from the DCID tree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006555 */
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006556 if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
6557 pkt->type == QUIC_PACKET_TYPE_0RTT)) {
Amaury Denoyellec2a92642023-04-13 15:26:18 +02006558 const struct quic_cid derive_cid = quic_derive_cid(&pkt->dcid, saddr);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006559
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006560 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
6561
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006562 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
6563 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6564 node = ebmb_lookup(&tree->root, derive_cid.data, derive_cid.len);
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006565 }
6566
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006567 if (!node)
6568 goto end;
6569
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006570 conn_id = ebmb_entry(node, struct quic_connection_id, node);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006571 conn_id_tid = HA_ATOMIC_LOAD(&conn_id->tid);
6572 if (conn_id_tid != tid) {
6573 *new_tid = conn_id_tid;
6574 goto end;
6575 }
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006576 qc = conn_id->qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006577
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006578 end:
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006579 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006580 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6581 return qc;
6582}
6583
6584/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
6585 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
6586 * parameters of this session.
6587 * This is the responsibility of the caller to check the validity of all the
6588 * pointers passed as parameter to this function.
6589 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
6590 * CO_ER_SSL_NO_MEM.
6591 */
6592static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
6593 unsigned char *params, size_t params_len)
6594{
6595 int retry, ret = -1;
6596
6597 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6598
6599 retry = 1;
6600 retry:
6601 *ssl = SSL_new(ssl_ctx);
6602 if (!*ssl) {
6603 if (!retry--)
6604 goto err;
6605
6606 pool_gc(NULL);
6607 goto retry;
6608 }
6609
6610 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
6611 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
6612 SSL_free(*ssl);
6613 *ssl = NULL;
6614 if (!retry--)
6615 goto err;
6616
6617 pool_gc(NULL);
6618 goto retry;
6619 }
6620
6621 ret = 0;
6622 leave:
6623 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6624 return ret;
6625
6626 err:
6627 qc->conn->err_code = CO_ER_SSL_NO_MEM;
6628 goto leave;
6629}
6630
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006631/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
6632 * used to process <qc> received packets. The allocated context is stored in
6633 * <qc.xprt_ctx>.
6634 *
6635 * Returns 0 on success else non-zero.
6636 */
6637static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
6638{
6639 int ret = 0;
6640 struct bind_conf *bc = qc->li->bind_conf;
6641 struct ssl_sock_ctx *ctx = NULL;
6642
6643 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6644
6645 ctx = pool_zalloc(pool_head_quic_conn_ctx);
6646 if (!ctx) {
6647 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
6648 goto err;
6649 }
6650
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006651 ctx->subs = NULL;
6652 ctx->xprt_ctx = NULL;
6653 ctx->qc = qc;
6654
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006655 if (qc_is_listener(qc)) {
6656 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
6657 qc->enc_params, qc->enc_params_len) == -1) {
6658 goto err;
6659 }
6660#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6661 /* Enabling 0-RTT */
6662 if (bc->ssl_conf.early_data)
6663 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
6664#endif
6665
6666 SSL_set_accept_state(ctx->ssl);
6667 }
6668
6669 ctx->xprt = xprt_get(XPRT_QUIC);
6670
6671 /* Store the allocated context in <qc>. */
6672 qc->xprt_ctx = ctx;
6673
6674 ret = 1;
6675 leave:
6676 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6677 return !ret;
6678
6679 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006680 pool_free(pool_head_quic_conn_ctx, ctx);
6681 goto leave;
6682}
6683
6684/* Check that all the bytes between <buf> included and <end> address
6685 * excluded are null. This is the responsibility of the caller to
6686 * check that there is at least one byte between <buf> end <end>.
6687 * Return 1 if this all the bytes are null, 0 if not.
6688 */
6689static inline int quic_padding_check(const unsigned char *buf,
6690 const unsigned char *end)
6691{
6692 while (buf < end && !*buf)
6693 buf++;
6694
6695 return buf == end;
6696}
6697
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006698/* Find the associated connection to the packet <pkt> or create a new one if
6699 * this is an Initial packet. <dgram> is the datagram containing the packet and
6700 * <l> is the listener instance on which it was received.
6701 *
Amaury Denoyelle25174d52023-04-05 17:52:05 +02006702 * By default, <new_tid> is set to -1. However, if thread affinity has been
6703 * chanbed, it will be set to its new thread ID.
6704 *
6705 * Returns the quic-conn instance or NULL if not found or thread affinity
6706 * changed.
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006707 */
6708static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
6709 struct quic_dgram *dgram,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006710 struct listener *l,
6711 int *new_tid)
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006712{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006713 struct quic_cid token_odcid = { .len = 0 };
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006714 struct quic_conn *qc = NULL;
6715 struct proxy *prx;
6716 struct quic_counters *prx_counters;
6717
6718 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6719
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006720 *new_tid = -1;
6721
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006722 prx = l->bind_conf->frontend;
6723 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6724
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006725 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr, new_tid);
6726
Amaury Denoyelle25174d52023-04-05 17:52:05 +02006727 /* If connection already created or rebinded on another thread. */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006728 if (!qc && *new_tid != -1 && tid != *new_tid)
6729 goto out;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006730
6731 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6732 BUG_ON(!pkt->version); /* This must not happen. */
6733
6734 if (global.cluster_secret && pkt->token_len) {
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006735 if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
6736 goto err;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006737 }
6738
6739 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006740 struct quic_cid_tree *tree;
6741 struct ebmb_node *node;
6742 struct quic_connection_id *conn_id;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006743 int ipv4;
6744
6745 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
6746 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
6747 TRACE_PROTO("Initial without token, sending retry",
6748 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6749 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
6750 TRACE_ERROR("Error during Retry generation",
6751 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6752 goto out;
6753 }
6754
6755 HA_ATOMIC_INC(&prx_counters->retry_sent);
6756 goto out;
6757 }
6758
6759 /* RFC 9000 7.2. Negotiating Connection IDs:
6760 * When an Initial packet is sent by a client that has not previously
6761 * received an Initial or Retry packet from the server, the client
6762 * populates the Destination Connection ID field with an unpredictable
6763 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6764 */
6765 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
6766 TRACE_PROTO("dropped packet",
6767 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6768 goto err;
6769 }
6770
6771 pkt->saddr = dgram->saddr;
6772 ipv4 = dgram->saddr.ss_family == AF_INET;
6773
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006774 /* Generate the first connection CID. This is derived from the client
6775 * ODCID and address. This allows to retrieve the connection from the
6776 * ODCID without storing it in the CID tree. This is an interesting
6777 * optimization as the client is expected to stop using its ODCID in
6778 * favor of our generated value.
6779 */
6780 conn_id = new_quic_cid(NULL, NULL, &pkt->dcid, &pkt->saddr);
6781 if (!conn_id)
6782 goto err;
6783
6784 tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)];
6785 HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock);
6786 node = ebmb_insert(&tree->root, &conn_id->node, conn_id->cid.len);
6787 if (node != &conn_id->node) {
6788 pool_free(pool_head_quic_connection_id, conn_id);
6789
6790 conn_id = ebmb_entry(node, struct quic_connection_id, node);
6791 *new_tid = HA_ATOMIC_LOAD(&conn_id->tid);
6792 }
6793 HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock);
6794
6795 if (*new_tid != -1)
6796 goto out;
6797
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006798 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006799 conn_id, &dgram->daddr, &pkt->saddr, 1,
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006800 !!pkt->token_len, l);
6801 if (qc == NULL)
6802 goto err;
6803
6804 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006805 }
6806 }
6807 else if (!qc) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006808 TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006809 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
6810 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
6811 goto err;
6812 }
6813
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006814 out:
6815 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6816 return qc;
6817
6818 err:
6819 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
6820 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
6821 return NULL;
6822}
6823
Amaury Denoyelle98289692022-10-19 15:37:44 +02006824/* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even
6825 * if the packet is incomplete. This function will populate fields of <pkt>
6826 * instance, most notably its length. <dgram> is the UDP datagram which
6827 * contains the parsed packet. <l> is the listener instance on which it was
6828 * received.
6829 *
6830 * Returns 0 on success else non-zero. Packet length is guaranteed to be set to
6831 * the real packet value or to cover all data between <buf> and <end> : this is
6832 * useful to reject a whole datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006833 */
Amaury Denoyelle98289692022-10-19 15:37:44 +02006834static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
6835 unsigned char *buf, const unsigned char *end,
6836 struct quic_dgram *dgram, struct listener *l)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006837{
Amaury Denoyelle98289692022-10-19 15:37:44 +02006838 const unsigned char *beg = buf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006839 struct proxy *prx;
6840 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006841
6842 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6843
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006844 prx = l->bind_conf->frontend;
6845 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6846 /* This ist only to please to traces and distinguish the
6847 * packet with parsed packet number from others.
6848 */
6849 pkt->pn_node.key = (uint64_t)-1;
6850 if (end <= buf) {
6851 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6852 goto drop;
6853 }
6854
6855 /* Fixed bit */
6856 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006857 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
6858 quic_padding_check(buf, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006859 /* Some browsers may pad the remaining datagram space with null bytes.
6860 * That is what we called add padding out of QUIC packets. Such
6861 * datagrams must be considered as valid. But we can only consume
6862 * the remaining space.
6863 */
6864 pkt->len = end - buf;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006865 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006866 }
6867
6868 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6869 goto drop;
6870 }
6871
6872 /* Header form */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006873 if (!qc_parse_hd_form(pkt, &buf, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006874 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6875 goto drop;
6876 }
6877
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006878 if (pkt->type != QUIC_PACKET_TYPE_SHORT) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006879 uint64_t len;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006880 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006881
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006882 if (!quic_packet_read_long_header(&buf, end, pkt)) {
6883 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6884 goto drop;
6885 }
6886
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006887 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6888 * they must have the same DCID.
6889 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006890 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006891 (pkt->dcid.len != dgram->dcid_len ||
6892 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006893 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006894 goto drop;
6895 }
6896
6897 /* Retry of Version Negotiation packets are only sent by servers */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006898 if (pkt->type == QUIC_PACKET_TYPE_RETRY ||
6899 (pkt->version && !pkt->version->num)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006900 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6901 goto drop;
6902 }
6903
6904 /* RFC9000 6. Version Negotiation */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006905 if (!pkt->version) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006906 /* unsupported version, send Negotiation packet */
6907 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
6908 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006909 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006910 }
6911
6912 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006913 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006914 }
6915
6916 /* For Initial packets, and for servers (QUIC clients connections),
6917 * there is no Initial connection IDs storage.
6918 */
6919 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6920 uint64_t token_len;
6921
6922 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
6923 end - buf < token_len) {
6924 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02006925 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006926 goto drop;
6927 }
6928
6929 /* TODO Retry should be automatically activated if
6930 * suspect network usage is detected.
6931 */
6932 if (global.cluster_secret && !token_len) {
6933 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
6934 TRACE_PROTO("Initial without token, sending retry",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02006935 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6936 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006937 TRACE_PROTO("Error during Retry generation",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02006938 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006939 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006940 }
6941
6942 HA_ATOMIC_INC(&prx_counters->retry_sent);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006943 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006944 }
6945 }
6946 else if (!global.cluster_secret && token_len) {
6947 /* Impossible case: a token was received without configured
6948 * cluster secret.
6949 */
6950 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02006951 NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006952 goto drop;
6953 }
6954
6955 pkt->token = buf;
6956 pkt->token_len = token_len;
6957 buf += pkt->token_len;
6958 }
6959 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
6960 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
6961 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02006962 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006963 goto drop;
6964 }
6965 }
6966
6967 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
6968 end - buf < len) {
6969 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02006970 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006971 goto drop;
6972 }
6973
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006974 /* Packet Number is stored here. Packet Length totalizes the
6975 * rest of the content.
6976 */
6977 pkt->pn_offset = buf - beg;
6978 pkt->len = pkt->pn_offset + len;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006979
Frédéric Lécaille35218c62023-02-16 11:40:11 +01006980 /* RFC 9000. Initial Datagram Size
6981 *
6982 * A server MUST discard an Initial packet that is carried in a UDP datagram
6983 * with a payload that is smaller than the smallest allowed maximum datagram
6984 * size of 1200 bytes.
6985 */
6986 if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
6987 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006988 TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille35218c62023-02-16 11:40:11 +01006989 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
6990 goto drop;
6991 }
6992
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006993 /* Interrupt parsing after packet length retrieval : this
6994 * ensures that only the packet is dropped but not the whole
6995 * datagram.
6996 */
6997 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006998 TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006999 goto drop;
7000 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007001 }
7002 else {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007003 TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007004 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007005 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007006 goto drop;
7007 }
7008
7009 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
7010 pkt->dcid.len = QUIC_HAP_CID_LEN;
7011
7012 /* When multiple QUIC packets are coalesced on the same UDP datagram,
7013 * they must have the same DCID.
7014 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007015 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007016 (pkt->dcid.len != dgram->dcid_len ||
7017 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007018 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007019 goto drop;
7020 }
7021
7022 buf += QUIC_HAP_CID_LEN;
7023
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007024 pkt->pn_offset = buf - beg;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007025 /* A short packet is the last one of a UDP datagram. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007026 pkt->len = end - beg;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007027 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007028
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007029 TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007030 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007031 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007032
Amaury Denoyelle98289692022-10-19 15:37:44 +02007033 drop:
7034 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007035 drop_silent:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007036 if (!pkt->len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007037 pkt->len = end - beg;
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007038 TRACE_PROTO("RX pkt parsing failed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007039 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007040 return -1;
7041}
7042
7043/* Check if received packet <pkt> should be drop due to <qc> already in closing
7044 * state. This can be true if a CONNECTION_CLOSE has already been emitted for
7045 * this connection.
7046 *
7047 * Returns false if connection is not in closing state else true. The caller
7048 * should drop the whole datagram in the last case to not mess up <qc>
7049 * CONNECTION_CLOSE rate limit counter.
7050 */
7051static int qc_rx_check_closing(struct quic_conn *qc,
7052 struct quic_rx_packet *pkt)
7053{
7054 if (!(qc->flags & QUIC_FL_CONN_CLOSING))
7055 return 0;
7056
7057 TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
7058
7059 /* Check if CONNECTION_CLOSE rate reemission is reached. */
7060 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
7061 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
7062 qc->nb_pkt_for_cc++;
7063 qc->nb_pkt_since_cc = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007064 }
7065
Amaury Denoyelle98289692022-10-19 15:37:44 +02007066 return 1;
7067}
7068
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007069/* React to a connection migration initiated on <qc> by a client with the new
7070 * path addresses <peer_addr>/<local_addr>.
7071 *
7072 * Returns 0 on success else non-zero.
7073 */
7074static int qc_handle_conn_migration(struct quic_conn *qc,
7075 const struct sockaddr_storage *peer_addr,
7076 const struct sockaddr_storage *local_addr)
7077{
7078 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7079
Frédéric Lécaille6fc86972023-01-12 08:29:23 +01007080 /* RFC 9000. Connection Migration
7081 *
7082 * If the peer sent the disable_active_migration transport parameter,
7083 * an endpoint also MUST NOT send packets (including probing packets;
7084 * see Section 9.1) from a different local address to the address the peer
7085 * used during the handshake, unless the endpoint has acted on a
7086 * preferred_address transport parameter from the peer.
7087 */
7088 if (qc->li->bind_conf->quic_params.disable_active_migration) {
7089 TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
7090 goto err;
7091 }
7092
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007093 /* RFC 9000 9. Connection Migration
7094 *
Amaury Denoyelleeb6be982022-11-21 11:14:45 +01007095 * The design of QUIC relies on endpoints retaining a stable address for
7096 * the duration of the handshake. An endpoint MUST NOT initiate
7097 * connection migration before the handshake is confirmed, as defined in
7098 * Section 4.1.2 of [QUIC-TLS].
7099 */
7100 if (qc->state < QUIC_HS_ST_COMPLETE) {
7101 TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
7102 goto err;
7103 }
7104
7105 /* RFC 9000 9. Connection Migration
7106 *
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007107 * TODO
7108 * An endpoint MUST
7109 * perform path validation (Section 8.2) if it detects any change to a
7110 * peer's address, unless it has previously validated that address.
7111 */
7112
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007113 /* Update quic-conn owned socket if in used.
7114 * TODO try to reuse it instead of closing and opening a new one.
7115 */
7116 if (qc_test_fd(qc)) {
7117 /* TODO try to reuse socket instead of closing it and opening a new one. */
7118 TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
7119 qc_release_fd(qc, 1);
Amaury Denoyellefb375572023-02-01 09:28:32 +01007120 /* TODO need to adjust <jobs> on socket allocation failure. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007121 qc_alloc_fd(qc, local_addr, peer_addr);
7122 }
7123
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007124 qc->local_addr = *local_addr;
7125 qc->peer_addr = *peer_addr;
7126 HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done);
7127
7128 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7129 return 0;
7130
7131 err:
7132 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7133 return 1;
7134}
7135
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007136/* Release the memory for the RX packets which are no more referenced
7137 * and consume their payloads which have been copied to the RX buffer
7138 * for the connection.
7139 * Always succeeds.
7140 */
7141static inline void quic_rx_pkts_del(struct quic_conn *qc)
7142{
7143 struct quic_rx_packet *pkt, *pktback;
7144
7145 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
7146 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007147 "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007148 (long long)pkt->pn_node.key,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007149 pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007150 (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
7151 if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
7152 size_t cdata;
7153
7154 cdata = b_contig_data(&qc->rx.buf, 0);
7155 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007156 "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007157 if (cdata && !*b_head(&qc->rx.buf)) {
7158 /* Consume the remaining data */
7159 b_del(&qc->rx.buf, cdata);
7160 }
7161 break;
7162 }
7163
7164 if (pkt->refcnt)
7165 break;
7166
7167 b_del(&qc->rx.buf, pkt->raw_len);
7168 LIST_DELETE(&pkt->qc_rx_pkt_list);
7169 pool_free(pool_head_quic_rx_packet, pkt);
7170 }
7171
7172 /* In frequent cases the buffer will be emptied at this stage. */
7173 b_realign_if_empty(&qc->rx.buf);
7174}
7175
Amaury Denoyelle98289692022-10-19 15:37:44 +02007176/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
7177 * into <qc> receive buffer after header protection removal procedure.
7178 *
7179 * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
7180 * must point to packet buffer first byte.
7181 *
7182 * <tasklist_head> may be non-NULL when the caller treat several datagrams for
7183 * different quic-conn. In this case, each quic-conn tasklet will be appended
7184 * to it in order to be woken up after the current task.
7185 *
7186 * The caller can safely removed the packet data. If packet refcount was not
7187 * incremented by this function, it means that the connection did not handled
7188 * it and it should be freed by the caller.
7189 */
7190static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
7191 struct quic_dgram *dgram, unsigned char *beg,
7192 struct list **tasklist_head)
7193{
7194 const struct quic_version *qv = pkt->version;
7195 struct quic_enc_level *qel = NULL;
7196 size_t b_cspace;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007197
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007198 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7199 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Amaury Denoyelle3f474e62022-11-24 17:15:08 +01007200
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007201 if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007202 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
7203 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
7204 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007205 TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
7206 QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007207 /* Reset the anti-amplification bit. It will be set again
7208 * when sending the next packet if reached again.
7209 */
7210 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007211 qc_set_timer(qc);
7212 if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
7213 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007214 }
7215
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007216 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
7217 TRACE_PROTO("Connection error",
7218 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7219 goto out;
7220 }
7221
7222 pkt->raw_len = pkt->len;
7223 quic_rx_pkts_del(qc);
7224 b_cspace = b_contig_space(&qc->rx.buf);
7225 if (b_cspace < pkt->len) {
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007226 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007227 "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007228 /* Do not consume buf if space not at the end. */
7229 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
7230 TRACE_PROTO("Packet dropped",
7231 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007232 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007233 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007234 }
7235
7236 /* Let us consume the remaining contiguous space. */
7237 if (b_cspace) {
7238 b_putchr(&qc->rx.buf, 0x00);
7239 b_cspace--;
7240 }
7241 b_add(&qc->rx.buf, b_cspace);
7242 if (b_contig_space(&qc->rx.buf) < pkt->len) {
7243 TRACE_PROTO("Too big packet",
7244 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007245 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007246 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007247 }
7248 }
7249
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007250 if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007251 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7252 goto drop;
7253 }
7254
7255 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7256 if (pkt->aad_len)
7257 qc_pkt_insert(qc, pkt, qel);
7258 out:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02007259 *tasklist_head = tasklet_wakeup_after(*tasklist_head,
7260 qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007261
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007262 drop_silent:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007263 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7264 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007265 return;
7266
7267 drop:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007268 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007269 TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7270 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007271}
7272
7273/* This function builds into <buf> buffer a QUIC long packet header.
7274 * Return 1 if enough room to build this header, 0 if not.
7275 */
7276static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
7277 int type, size_t pn_len,
7278 struct quic_conn *qc, const struct quic_version *ver)
7279{
7280 int ret = 0;
7281
7282 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7283
7284 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
7285 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7286 goto leave;
7287 }
7288
7289 type = quic_pkt_type(type, ver->num);
7290 /* #0 byte flags */
7291 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
7292 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
7293 /* Version */
7294 quic_write_uint32(buf, end, ver->num);
7295 *(*buf)++ = qc->dcid.len;
7296 /* Destination connection ID */
7297 if (qc->dcid.len) {
7298 memcpy(*buf, qc->dcid.data, qc->dcid.len);
7299 *buf += qc->dcid.len;
7300 }
7301 /* Source connection ID */
7302 *(*buf)++ = qc->scid.len;
7303 if (qc->scid.len) {
7304 memcpy(*buf, qc->scid.data, qc->scid.len);
7305 *buf += qc->scid.len;
7306 }
7307
7308 ret = 1;
7309 leave:
7310 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7311 return ret;
7312}
7313
7314/* This function builds into <buf> buffer a QUIC short packet header.
7315 * Return 1 if enough room to build this header, 0 if not.
7316 */
7317static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
7318 size_t pn_len, struct quic_conn *qc,
7319 unsigned char tls_flags)
7320{
7321 int ret = 0;
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007322 unsigned char spin_bit =
7323 (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007324
7325 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7326
7327 if (end - *buf < 1 + qc->dcid.len) {
7328 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7329 goto leave;
7330 }
7331
7332 /* #0 byte flags */
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007333 *(*buf)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007334 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
7335 /* Destination connection ID */
7336 if (qc->dcid.len) {
7337 memcpy(*buf, qc->dcid.data, qc->dcid.len);
7338 *buf += qc->dcid.len;
7339 }
7340
7341 ret = 1;
7342 leave:
7343 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7344 return ret;
7345}
7346
7347/* Apply QUIC header protection to the packet with <buf> as first byte address,
7348 * <pn> as address of the Packet number field, <pnlen> being this field length
7349 * with <aead> as AEAD cipher and <key> as secret key.
7350 * Returns 1 if succeeded or 0 if failed.
7351 */
7352static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
7353 unsigned char *pn, size_t pnlen,
7354 struct quic_tls_ctx *tls_ctx)
7355
7356{
7357 int i, ret = 0;
7358 /* We need an IV of at least 5 bytes: one byte for bytes #0
7359 * and at most 4 bytes for the packet number
7360 */
7361 unsigned char mask[5] = {0};
7362 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
7363
7364 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7365
7366 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
7367 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
7368 goto out;
7369 }
7370
7371 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
7372 for (i = 0; i < pnlen; i++)
7373 pn[i] ^= mask[i + 1];
7374
7375 ret = 1;
7376 out:
7377 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7378 return ret;
7379}
7380
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007381/* Prepare into <outlist> as most as possible ack-eliciting frame from their
7382 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
7383 * with <room> as available room, and <*len> the packet Length field initialized
7384 * with the number of bytes already present in this buffer which must be taken
7385 * into an account for the Length packet field value. <headlen> is the number of
7386 * bytes already present in this packet before building frames.
7387 *
7388 * Update consequently <*len> to reflect the size of these frames built
7389 * by this function. Also attach these frames to <l> frame list.
7390 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
7391 */
7392static inline int qc_build_frms(struct list *outlist, struct list *inlist,
7393 size_t room, size_t *len, size_t headlen,
7394 struct quic_enc_level *qel,
7395 struct quic_conn *qc)
7396{
7397 int ret;
7398 struct quic_frame *cf, *cfbak;
7399
7400 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
7401
7402 ret = 0;
7403 if (*len > room)
7404 goto leave;
7405
7406 /* If we are not probing we must take into an account the congestion
7407 * control window.
7408 */
7409 if (!qel->pktns->tx.pto_probe) {
7410 size_t remain = quic_path_prep_data(qc->path);
7411
7412 if (headlen > remain)
7413 goto leave;
7414
7415 room = QUIC_MIN(room, remain - headlen);
7416 }
7417
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007418 TRACE_PROTO("TX frms build (headlen)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007419 QUIC_EV_CONN_BCFRMS, qc, &headlen);
7420
7421 /* NOTE: switch/case block inside a loop, a successful status must be
7422 * returned by this function only if at least one frame could be built
7423 * in the switch/case block.
7424 */
7425 list_for_each_entry_safe(cf, cfbak, inlist, list) {
7426 /* header length, data length, frame length. */
7427 size_t hlen, dlen, dlen_sz, avail_room, flen;
7428
7429 if (!room)
7430 break;
7431
7432 switch (cf->type) {
7433 case QUIC_FT_CRYPTO:
7434 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
7435 QUIC_EV_CONN_BCFRMS, qc, &room, len);
7436 /* Compute the length of this CRYPTO frame header */
7437 hlen = 1 + quic_int_getsize(cf->crypto.offset);
7438 /* Compute the data length of this CRyPTO frame. */
7439 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
7440 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
7441 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
7442 if (!dlen)
7443 continue;
7444
7445 /* CRYPTO frame length. */
7446 flen = hlen + quic_int_getsize(dlen) + dlen;
7447 TRACE_DEVEL(" CRYPTO frame length (flen)",
7448 QUIC_EV_CONN_BCFRMS, qc, &flen);
7449 /* Add the CRYPTO data length and its encoded length to the packet
7450 * length and the length of this length.
7451 */
7452 *len += flen;
7453 room -= flen;
7454 if (dlen == cf->crypto.len) {
7455 /* <cf> CRYPTO data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007456 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007457 LIST_APPEND(outlist, &cf->list);
7458 }
7459 else {
7460 struct quic_frame *new_cf;
7461
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007462 new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007463 if (!new_cf) {
7464 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
7465 continue;
7466 }
7467
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007468 new_cf->crypto.len = dlen;
7469 new_cf->crypto.offset = cf->crypto.offset;
7470 new_cf->crypto.qel = qel;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007471 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007472 if (cf->origin) {
7473 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7474 /* This <cf> frame was duplicated */
7475 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7476 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007477 /* Detach the remaining CRYPTO frame from its original frame */
7478 LIST_DEL_INIT(&cf->ref);
7479 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007480 }
7481 LIST_APPEND(outlist, &new_cf->list);
7482 /* Consume <dlen> bytes of the current frame. */
7483 cf->crypto.len -= dlen;
7484 cf->crypto.offset += dlen;
7485 }
7486 break;
7487
7488 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007489 if (cf->stream.dup) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007490 struct eb64_node *node = NULL;
7491 struct qc_stream_desc *stream_desc = NULL;
7492 struct quic_stream *strm = &cf->stream;
7493
7494 /* As this frame has been already lost, ensure the stream is always
7495 * available or the range of this frame is not consumed before
7496 * resending it.
7497 */
7498 node = eb64_lookup(&qc->streams_by_id, strm->id);
7499 if (!node) {
7500 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007501 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007502 continue;
7503 }
7504
7505 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
7506 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
7507 TRACE_DEVEL("ignored frame frame in already acked range",
7508 QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007509 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007510 continue;
7511 }
7512 else if (strm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01007513 uint64_t diff = stream_desc->ack_offset - strm->offset.key;
7514
Frédéric Lécaillec425e032023-03-20 14:32:59 +01007515 qc_stream_frm_mv_fwd(cf, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007516 TRACE_DEVEL("updated partially acked frame",
7517 QUIC_EV_CONN_PRSAFRM, qc, cf);
7518 }
7519 }
7520 /* Note that these frames are accepted in short packets only without
7521 * "Length" packet field. Here, <*len> is used only to compute the
7522 * sum of the lengths of the already built frames for this packet.
7523 *
7524 * Compute the length of this STREAM frame "header" made a all the field
7525 * excepting the variable ones. Note that +1 is for the type of this frame.
7526 */
7527 hlen = 1 + quic_int_getsize(cf->stream.id) +
7528 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
7529 /* Compute the data length of this STREAM frame. */
7530 avail_room = room - hlen - *len;
7531 if ((ssize_t)avail_room <= 0)
7532 continue;
7533
7534 TRACE_DEVEL(" New STREAM frame build (room, len)",
7535 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007536
7537 /* hlen contains STREAM id and offset. Ensure there is
7538 * enough room for length field.
7539 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007540 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007541 dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
7542 cf->stream.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007543 dlen_sz = quic_int_getsize(dlen);
7544 flen = hlen + dlen_sz + dlen;
7545 }
7546 else {
7547 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
7548 flen = hlen + dlen;
7549 }
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007550
7551 if (cf->stream.len && !dlen) {
7552 /* Only a small gap is left on buffer, not
7553 * enough to encode the STREAM data length.
7554 */
7555 continue;
7556 }
7557
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007558 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
7559 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
7560 TRACE_DEVEL(" STREAM frame length (flen)",
7561 QUIC_EV_CONN_BCFRMS, qc, &flen);
7562 /* Add the STREAM data length and its encoded length to the packet
7563 * length and the length of this length.
7564 */
7565 *len += flen;
7566 room -= flen;
7567 if (dlen == cf->stream.len) {
7568 /* <cf> STREAM data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007569 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007570 LIST_APPEND(outlist, &cf->list);
7571
7572 /* Do not notify MUX on retransmission. */
7573 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7574 qcc_streams_sent_done(cf->stream.stream->ctx,
7575 cf->stream.len,
7576 cf->stream.offset.key);
7577 }
7578 }
7579 else {
7580 struct quic_frame *new_cf;
7581 struct buffer cf_buf;
7582
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007583 new_cf = qc_frm_alloc(cf->type);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007584 if (!new_cf) {
7585 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
7586 continue;
7587 }
7588
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007589 new_cf->stream.stream = cf->stream.stream;
7590 new_cf->stream.buf = cf->stream.buf;
7591 new_cf->stream.id = cf->stream.id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01007592 new_cf->stream.offset = cf->stream.offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007593 new_cf->stream.len = dlen;
7594 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
7595 /* FIN bit reset */
7596 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
7597 new_cf->stream.data = cf->stream.data;
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007598 new_cf->stream.dup = cf->stream.dup;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007599 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007600 if (cf->origin) {
7601 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7602 /* This <cf> frame was duplicated */
7603 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7604 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007605 /* Detach this STREAM frame from its origin */
7606 LIST_DEL_INIT(&cf->ref);
7607 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007608 }
7609 LIST_APPEND(outlist, &new_cf->list);
7610 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
7611 /* Consume <dlen> bytes of the current frame. */
7612 cf_buf = b_make(b_orig(cf->stream.buf),
7613 b_size(cf->stream.buf),
7614 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
7615 cf->stream.len -= dlen;
7616 cf->stream.offset.key += dlen;
7617 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
7618
7619 /* Do not notify MUX on retransmission. */
7620 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7621 qcc_streams_sent_done(new_cf->stream.stream->ctx,
7622 new_cf->stream.len,
7623 new_cf->stream.offset.key);
7624 }
7625 }
7626
7627 /* TODO the MUX is notified about the frame sending via
7628 * previous qcc_streams_sent_done call. However, the
7629 * sending can fail later, for example if the sendto
7630 * system call returns an error. As the MUX has been
7631 * notified, the transport layer is responsible to
7632 * bufferize and resent the announced data later.
7633 */
7634
7635 break;
7636
7637 default:
7638 flen = qc_frm_len(cf);
7639 BUG_ON(!flen);
7640 if (flen > room)
7641 continue;
7642
7643 *len += flen;
7644 room -= flen;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007645 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007646 LIST_APPEND(outlist, &cf->list);
7647 break;
7648 }
7649
7650 /* Successful status as soon as a frame could be built */
7651 ret = 1;
7652 }
7653
7654 leave:
7655 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
7656 return ret;
7657}
7658
7659/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
7660 * is used as return parameter and should be zero'ed by the caller.
7661 */
7662static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
7663 struct quic_frame *out)
7664{
7665 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
7666 *
7667 * A CONNECTION_CLOSE frame should be sent in several packets with
7668 * different encryption levels depending on the client context. This is
7669 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
7670 * more details on how to implement it.
7671 */
7672 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
7673
7674
7675 if (qc->err.app) {
7676 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
7677 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
7678 /* RFC 9000 10.2.3. Immediate Close during the Handshake
7679 *
7680 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
7681 * packet could expose application state or be used to alter application
7682 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
7683 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
7684 * Handshake packets. Otherwise, information about the application
7685 * state might be revealed. Endpoints MUST clear the value of the
7686 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
7687 * converting to a CONNECTION_CLOSE of type 0x1c.
7688 */
7689 out->type = QUIC_FT_CONNECTION_CLOSE;
7690 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
7691 out->connection_close.reason_phrase_len = 0;
7692 }
7693 else {
7694 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
7695 out->connection_close.error_code = qc->err.code;
7696 }
7697 }
7698 else {
7699 out->type = QUIC_FT_CONNECTION_CLOSE;
7700 out->connection_close.error_code = qc->err.code;
7701 }
7702 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
7703
7704}
7705
7706/* This function builds a clear packet from <pkt> information (its type)
7707 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
7708 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
7709 * filling the buffer with as much frames as possible from <frms> list of
7710 * prebuilt frames.
7711 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
7712 * reserved so that to ensure there is enough room to build this AEAD TAG after
7713 * having returned from this function.
7714 * This function also updates the value of <buf_pn> pointer to point to the packet
7715 * number field in this packet. <pn_len> will also have the packet number
7716 * length as value.
7717 *
7718 * Return 1 if succeeded (enough room to buile this packet), O if not.
7719 */
7720static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
7721 size_t dglen, struct quic_tx_packet *pkt,
7722 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007723 int must_ack, int padding, int cc, int probe,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007724 struct quic_enc_level *qel, struct quic_conn *qc,
7725 const struct quic_version *ver, struct list *frms)
7726{
7727 unsigned char *beg, *payload;
7728 size_t len, len_sz, len_frms, padding_len;
7729 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
7730 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
7731 struct quic_frame cc_frm = { };
7732 size_t ack_frm_len, head_len;
7733 int64_t rx_largest_acked_pn;
7734 int add_ping_frm;
7735 struct list frm_list = LIST_HEAD_INIT(frm_list);
7736 struct quic_frame *cf;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007737 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007738
7739 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7740
7741 /* Length field value with CRYPTO frames if present. */
7742 len_frms = 0;
7743 beg = pos;
7744 /* When not probing, and no immediate close is required, reduce the size of this
7745 * buffer to respect the congestion controller window.
7746 * This size will be limited if we have ack-eliciting frames to send from <frms>.
7747 */
7748 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
7749 size_t path_room;
7750
7751 path_room = quic_path_prep_data(qc->path);
7752 if (end - beg > path_room)
7753 end = beg + path_room;
7754 }
7755
7756 /* Ensure there is enough room for the TLS encryption tag and a zero token
7757 * length field if any.
7758 */
7759 if (end - pos < QUIC_TLS_TAG_LEN +
7760 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
7761 goto no_room;
7762
7763 end -= QUIC_TLS_TAG_LEN;
7764 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
7765 /* packet number length */
7766 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
7767 /* Build the header */
7768 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
7769 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
7770 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
7771 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
7772 goto no_room;
7773
7774 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02007775 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7776 if (end <= pos)
7777 goto no_room;
7778
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007779 *pos++ = 0;
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02007780 }
7781
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007782 head_len = pos - beg;
7783 /* Build an ACK frame if required. */
7784 ack_frm_len = 0;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007785 /* Do not ack and probe at the same time. */
7786 if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007787 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
7788 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
7789 ack_frm.tx_ack.arngs = arngs;
7790 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
7791 qel->pktns->tx.ack_delay =
7792 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
7793 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
7794 }
7795 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
7796 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
7797 * smallest frame (PING) and <*pn_len> more for the packet number. Note
7798 * that from here, we do not know if we will have to send a PING frame.
7799 * This will be decided after having computed the ack-eliciting frames
7800 * to be added to this packet.
7801 */
Frédéric Lécaille9d68c6a2023-04-12 20:49:29 +02007802 if (end - pos <= 1 + *pn_len)
7803 goto no_room;
7804
Frédéric Lécaille4b2627b2023-04-17 13:42:42 +02007805 ack_frm_len = qc_frm_len(&ack_frm);
7806 if (ack_frm_len > end - 1 - *pn_len - pos)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007807 goto no_room;
7808 }
7809
7810 /* Length field value without the ack-eliciting frames. */
7811 len = ack_frm_len + *pn_len;
7812 len_frms = 0;
7813 if (!cc && !LIST_ISEMPTY(frms)) {
7814 ssize_t room = end - pos;
7815
7816 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
7817 /* Initialize the length of the frames built below to <len>.
7818 * If any frame could be successfully built by qc_build_frms(),
7819 * we will have len_frms > len.
7820 */
7821 len_frms = len;
7822 if (!qc_build_frms(&frm_list, frms,
7823 end - pos, &len_frms, pos - beg, qel, qc)) {
7824 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7825 qc, NULL, NULL, &room);
7826 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
7827 goto no_room;
7828 }
7829 }
7830
7831 /* Length (of the remaining data). Must not fail because, the buffer size
7832 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
7833 * for the encryption tag. It must be taken into an account for the length
7834 * of this packet.
7835 */
7836 if (len_frms)
7837 len = len_frms + QUIC_TLS_TAG_LEN;
7838 else
7839 len += QUIC_TLS_TAG_LEN;
7840 /* CONNECTION_CLOSE frame */
7841 if (cc) {
7842 qc_build_cc_frm(qc, qel, &cc_frm);
7843 len += qc_frm_len(&cc_frm);
7844 }
7845 add_ping_frm = 0;
7846 padding_len = 0;
7847 len_sz = quic_int_getsize(len);
7848 /* Add this packet size to <dglen> */
7849 dglen += head_len + len_sz + len;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007850 /* Note that <padding> is true only when building an Handshake packet
7851 * coalesced to an Initial packet.
7852 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007853 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
7854 /* This is a maximum padding size */
7855 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7856 /* The length field value is of this packet is <len> + <padding_len>
7857 * the size of which may be greater than the initial computed size
7858 * <len_sz>. So, let's deduce the difference between these to packet
7859 * sizes from <padding_len>.
7860 */
7861 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7862 len += padding_len;
7863 }
Frédéric Lécaille5faf5772023-02-16 17:30:53 +01007864 else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
7865 len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
7866 }
7867 else if (LIST_ISEMPTY(&frm_list)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007868 if (qel->pktns->tx.pto_probe) {
7869 /* If we cannot send a frame, we send a PING frame. */
7870 add_ping_frm = 1;
7871 len += 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007872 dglen += 1;
7873 /* Note that only we are in the case where this Initial packet
7874 * is not coalesced to an Handshake packet. We must directly
7875 * pad the datragram.
7876 */
Frédéric Lécaille9c317b12023-03-28 15:39:11 +02007877 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7878 if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
7879 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7880 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7881 len += padding_len;
7882 }
7883 }
7884 else {
7885 /* Note that +1 is for the PING frame */
7886 if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
7887 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007888 }
7889 }
7890 else {
7891 /* If there is no frame at all to follow, add at least a PADDING frame. */
7892 if (!ack_frm_len && !cc)
7893 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007894 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007895 }
7896
7897 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
7898 goto no_room;
7899
7900 /* Packet number field address. */
7901 *buf_pn = pos;
7902
7903 /* Packet number encoding. */
7904 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
7905 goto no_room;
7906
7907 /* payload building (ack-eliciting or not frames) */
7908 payload = pos;
7909 if (ack_frm_len) {
7910 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
7911 goto no_room;
7912
7913 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
7914 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
7915 }
7916
7917 /* Ack-eliciting frames */
7918 if (!LIST_ISEMPTY(&frm_list)) {
7919 struct quic_frame *tmp_cf;
7920 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
7921 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
7922 ssize_t room = end - pos;
7923 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7924 qc, NULL, NULL, &room);
7925 /* Note that <cf> was added from <frms> to <frm_list> list by
7926 * qc_build_frms().
7927 */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007928 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007929 LIST_INSERT(frms, &cf->list);
7930 continue;
7931 }
7932
7933 quic_tx_packet_refinc(pkt);
7934 cf->pkt = pkt;
7935 }
7936 }
7937
7938 /* Build a PING frame if needed. */
7939 if (add_ping_frm) {
7940 frm.type = QUIC_FT_PING;
7941 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
7942 goto no_room;
7943 }
7944
7945 /* Build a CONNECTION_CLOSE frame if needed. */
7946 if (cc) {
7947 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
7948 goto no_room;
7949
7950 pkt->flags |= QUIC_FL_TX_PACKET_CC;
7951 }
7952
7953 /* Build a PADDING frame if needed. */
7954 if (padding_len) {
7955 frm.type = QUIC_FT_PADDING;
7956 frm.padding.len = padding_len;
7957 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
7958 goto no_room;
7959 }
7960
7961 if (pos == payload) {
7962 /* No payload was built because of congestion control */
7963 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
7964 goto no_room;
7965 }
7966
7967 /* If this packet is ack-eliciting and we are probing let's
7968 * decrement the PTO probe counter.
7969 */
7970 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
7971 qel->pktns->tx.pto_probe)
7972 qel->pktns->tx.pto_probe--;
7973
7974 pkt->len = pos - beg;
7975 LIST_SPLICE(&pkt->frms, &frm_list);
7976
7977 ret = 1;
7978 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
7979 leave:
7980 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7981 return ret;
7982
7983 no_room:
7984 /* Replace the pre-built frames which could not be add to this packet */
7985 LIST_SPLICE(frms, &frm_list);
7986 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
7987 goto leave;
7988}
7989
7990static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
7991{
7992 pkt->type = type;
7993 pkt->len = 0;
7994 pkt->in_flight_len = 0;
7995 pkt->pn_node.key = (uint64_t)-1;
7996 LIST_INIT(&pkt->frms);
7997 pkt->time_sent = TICK_ETERNITY;
7998 pkt->next = NULL;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01007999 pkt->prev = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008000 pkt->largest_acked_pn = -1;
8001 pkt->flags = 0;
8002 pkt->refcnt = 0;
8003}
8004
8005/* Build a packet into <buf> packet buffer with <pkt_type> as packet
8006 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
8007 * of prebuilt frames.
8008 *
8009 * Return -2 if the packet could not be allocated or encrypted for any reason,
8010 * -1 if there was not enough room to build a packet.
8011 * XXX NOTE XXX
8012 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
8013 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
8014 * control window limitation.
8015 */
8016static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
8017 const unsigned char *buf_end,
8018 struct quic_enc_level *qel,
8019 struct quic_tls_ctx *tls_ctx, struct list *frms,
8020 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008021 size_t dglen, int pkt_type, int must_ack,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008022 int padding, int probe, int cc, int *err)
8023{
8024 struct quic_tx_packet *ret_pkt = NULL;
8025 /* The pointer to the packet number field. */
8026 unsigned char *buf_pn;
8027 unsigned char *beg, *end, *payload;
8028 int64_t pn;
8029 size_t pn_len, payload_len, aad_len;
8030 struct quic_tx_packet *pkt;
8031
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008032 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
8033 TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008034 *err = 0;
8035 pkt = pool_alloc(pool_head_quic_tx_packet);
8036 if (!pkt) {
8037 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
8038 *err = -2;
8039 goto err;
8040 }
8041
8042 quic_tx_packet_init(pkt, pkt_type);
8043 beg = *pos;
8044 pn_len = 0;
8045 buf_pn = NULL;
8046
8047 pn = qel->pktns->tx.next_pn + 1;
8048 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 +02008049 must_ack, padding, cc, probe, qel, qc, ver, frms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008050 // trace already emitted by function above
8051 *err = -1;
8052 goto err;
8053 }
8054
8055 end = beg + pkt->len;
8056 payload = buf_pn + pn_len;
8057 payload_len = end - payload;
8058 aad_len = payload - beg;
8059
8060 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
8061 // trace already emitted by function above
8062 *err = -2;
8063 goto err;
8064 }
8065
8066 end += QUIC_TLS_TAG_LEN;
8067 pkt->len += QUIC_TLS_TAG_LEN;
8068 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
8069 // trace already emitted by function above
8070 *err = -2;
8071 goto err;
8072 }
8073
8074 /* Consume a packet number */
8075 qel->pktns->tx.next_pn++;
8076 qc->tx.prep_bytes += pkt->len;
8077 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
8078 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
8079 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
8080 }
8081 /* Now that a correct packet is built, let us consume <*pos> buffer. */
8082 *pos = end;
8083 /* Attach the built packet to its tree. */
8084 pkt->pn_node.key = pn;
8085 /* Set the packet in fligth length for in flight packet only. */
8086 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
8087 pkt->in_flight_len = pkt->len;
8088 qc->path->prep_in_flight += pkt->len;
8089 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01008090 /* Always reset this flag */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008091 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
8092 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
8093 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
8094 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
Frédéric Lécailled7215712023-03-24 18:13:37 +01008095 qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
8096 if (tick_isset(qc->ack_expire)) {
8097 qc->ack_expire = TICK_ETERNITY;
8098 qc->idle_timer_task->expire = qc->idle_expire;
8099 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02008100 TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01008101 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008102 }
8103
8104 pkt->pktns = qel->pktns;
8105
8106 ret_pkt = pkt;
8107 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008108 TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
8109 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008110 return ret_pkt;
8111
8112 err:
8113 /* TODO: what about the frames which have been built
8114 * for this packet.
8115 */
8116 free_quic_tx_packet(qc, pkt);
8117 goto leave;
8118}
8119
8120
8121static void __quic_conn_init(void)
8122{
8123 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
8124}
8125INITCALL0(STG_REGISTER, __quic_conn_init);
8126
8127static void __quic_conn_deinit(void)
8128{
8129 BIO_meth_free(ha_quic_meth);
8130}
8131REGISTER_POST_DEINIT(__quic_conn_deinit);
8132
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008133/* Handle a new <dgram> received. Parse each QUIC packets and copied their
8134 * content to a quic-conn instance. The datagram content can be released after
8135 * this function.
8136 *
8137 * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
8138 * to the connection instance. <li> is the attached listener. The caller is
8139 * responsible to ensure that the first packet is destined to this connection
8140 * by comparing CIDs.
8141 *
8142 * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
8143 * function will thus retrieve the connection from the CID tree or allocate a
8144 * new one if possible. <li> is the listener attached to the receiver.
8145 *
8146 * Returns 0 on success else non-zero. If an error happens, some packets from
8147 * the datagram may not have been parsed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008148 */
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008149int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
8150 struct listener *li)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008151{
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008152 struct quic_rx_packet *pkt;
8153 struct quic_conn *qc = NULL;
8154 unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008155 struct list *tasklist_head = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008156
8157 TRACE_ENTER(QUIC_EV_CONN_LPKT);
8158
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008159 pos = dgram->buf;
8160 end = pos + dgram->len;
8161 do {
8162 /* TODO replace zalloc -> alloc. */
8163 pkt = pool_zalloc(pool_head_quic_rx_packet);
8164 if (!pkt) {
8165 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
8166 goto err;
8167 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008168
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008169 pkt->version = NULL;
8170 pkt->pn_offset = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008171
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008172 /* Set flag if pkt is the first one in dgram. */
8173 if (pos == dgram->buf)
8174 pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008175
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008176 LIST_INIT(&pkt->qc_rx_pkt_list);
8177 pkt->time_received = now_ms;
8178 quic_rx_packet_refinc(pkt);
8179 if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
8180 goto next;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008181
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008182 /* Search quic-conn instance for first packet of the datagram.
8183 * quic_rx_packet_parse() is responsible to discard packets
8184 * with different DCID as the first one in the same datagram.
8185 */
8186 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008187 int new_tid = -1;
8188
8189 qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li, &new_tid);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008190 /* qc is NULL if receiving a non Initial packet for an
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008191 * unknown connection or on connection affinity rebind.
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008192 */
8193 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008194 if (new_tid >= 0) {
8195 MT_LIST_APPEND(&quic_dghdlrs[new_tid].dgrams,
8196 &dgram->handler_list);
8197 tasklet_wakeup(quic_dghdlrs[new_tid].task);
8198 goto out;
8199 }
8200
Amaury Denoyelle98289692022-10-19 15:37:44 +02008201 /* Skip the entire datagram. */
8202 pkt->len = end - pos;
8203 goto next;
8204 }
8205
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008206 dgram->qc = qc;
8207 }
Amaury Denoyelle98289692022-10-19 15:37:44 +02008208
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008209 if (qc_rx_check_closing(qc, pkt)) {
8210 /* Skip the entire datagram. */
8211 pkt->len = end - pos;
8212 goto next;
8213 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008214
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008215 /* Detect QUIC connection migration. */
Frédéric Lécaillef6769542023-01-12 10:36:26 +01008216 if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008217 if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
8218 /* Skip the entire datagram. */
8219 TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
8220 pkt->len = end - pos;
8221 goto next;
8222 }
8223 }
8224
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008225 qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008226
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008227 next:
8228 pos += pkt->len;
8229 quic_rx_packet_refdec(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008230
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008231 /* Free rejected packets */
8232 if (!pkt->refcnt) {
8233 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
8234 pool_free(pool_head_quic_rx_packet, pkt);
8235 }
8236 } while (pos < end);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008237
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008238 /* Increasing the received bytes counter by the UDP datagram length
8239 * if this datagram could be associated to a connection.
8240 */
8241 if (dgram->qc)
8242 dgram->qc->rx.bytes += dgram->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008243
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008244 /* This must never happen. */
8245 BUG_ON(pos > end);
8246 BUG_ON(pos < end || pos > dgram->buf + dgram->len);
8247 /* Mark this datagram as consumed */
8248 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008249
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008250 out:
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008251 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
8252 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008253
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008254 err:
Amaury Denoyellea65dd3a2023-04-19 14:26:16 +02008255 /* Mark this datagram as consumed as maybe at least some packets were parsed. */
8256 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008257 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008258 return -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008259}
8260
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008261/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
8262 * CIDs. This can be used to determine if a datagram is addressed to the right
8263 * connection instance.
8264 *
8265 * Returns a boolean value.
8266 */
8267int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len)
8268{
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008269 const uchar idx = _quic_cid_tree_idx(dcid);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008270 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008271 struct ebmb_node *node = NULL;
8272 struct quic_cid_tree *tree = &quic_cid_trees[idx];
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008273
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008274 /* Test against our default CID or client ODCID. */
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008275 if ((qc->scid.len == dcid_len &&
8276 memcmp(qc->scid.data, dcid, dcid_len) == 0) ||
8277 (qc->odcid.len == dcid_len &&
Frédéric Lécaille07846cb2023-02-13 16:14:24 +01008278 memcmp(qc->odcid.data, dcid, dcid_len) == 0)) {
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008279 return 1;
8280 }
8281
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008282 /* Test against our other CIDs. This can happen if the client has
8283 * decided to switch to a new one.
8284 *
8285 * TODO to avoid locking, loop through qc.cids as an alternative.
8286 *
8287 * TODO set it to our default CID to avoid this operation next time.
8288 */
8289 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
8290 node = ebmb_lookup(&tree->root, dcid, dcid_len);
8291 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
8292
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008293 if (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008294 conn_id = ebmb_entry(node, struct quic_connection_id, node);
8295 if (qc == conn_id->qc)
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008296 return 1;
8297 }
8298
8299 return 0;
8300}
8301
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008302/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
8303 * Returns 1 if succeeded, 0 if not.
8304 */
8305int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
8306 unsigned char **dcid, size_t *dcid_len)
8307{
8308 int ret = 0, long_header;
8309 size_t minlen, skip;
8310
8311 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
8312
8313 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
8314 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
8315 goto err;
8316 }
8317
8318 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
8319 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
8320 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
8321 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
8322 if (end - buf < minlen)
8323 goto err;
8324
8325 buf += skip;
8326 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
8327 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
8328 goto err;
8329
8330 *dcid = buf;
8331
8332 ret = 1;
8333 leave:
8334 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
8335 return ret;
8336
8337 err:
8338 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
8339 goto leave;
8340}
8341
8342/* Notify the MUX layer if alive about an imminent close of <qc>. */
8343void qc_notify_close(struct quic_conn *qc)
8344{
8345 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
8346
8347 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
8348 goto leave;
8349
8350 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
8351 /* wake up the MUX */
8352 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
8353 TRACE_STATE("connection closure notidfied to mux",
8354 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8355 qc->conn->mux->wake(qc->conn);
8356 }
8357 else
8358 TRACE_STATE("connection closure not notidfied to mux",
8359 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
8360 leave:
8361 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
8362}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008363
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008364/* Wake-up upper layer for sending if all conditions are met :
8365 * - room in congestion window or probe packet to sent
8366 * - socket FD ready to sent or listener socket used
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008367 *
8368 * Returns 1 if upper layer has been woken up else 0.
8369 */
8370int qc_notify_send(struct quic_conn *qc)
8371{
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008372 const struct quic_pktns *pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8373
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008374 if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008375 /* RFC 9002 7.5. Probe Timeout
8376 *
8377 * Probe packets MUST NOT be blocked by the congestion controller.
8378 */
8379 if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
Amaury Denoyellecaa16542023-02-28 15:11:26 +01008380 (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008381 tasklet_wakeup(qc->subs->tasklet);
8382 qc->subs->events &= ~SUB_RETRY_SEND;
8383 if (!qc->subs->events)
8384 qc->subs = NULL;
8385
8386 return 1;
8387 }
8388 }
8389
8390 return 0;
8391}
8392
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008393/* Move a <qc> QUIC connection and its resources from the current thread to the
8394 * new one <new_tid>. After this call, the connection cannot be dereferenced
8395 * anymore on the current thread.
8396 *
8397 * Returns 0 on success else non-zero.
8398 */
8399int qc_set_tid_affinity(struct quic_conn *qc, uint new_tid)
8400{
8401 struct task *t1 = NULL, *t2 = NULL;
8402 struct tasklet *t3 = NULL;
8403
8404 struct quic_connection_id *conn_id;
8405 struct eb64_node *node;
8406
8407 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8408
8409 /* Pre-allocate all required resources. This ensures we do not left a
8410 * connection with only some of its field rebinded.
8411 */
8412 if (((t1 = task_new_on(new_tid)) == NULL) ||
8413 (qc->timer_task && (t2 = task_new_on(new_tid)) == NULL) ||
8414 (t3 = tasklet_new()) == NULL) {
8415 goto err;
8416 }
8417
8418 /* Reinit idle timer task. */
8419 task_kill(qc->idle_timer_task);
8420 t1->expire = qc->idle_timer_task->expire;
8421 qc->idle_timer_task = t1;
8422 qc->idle_timer_task->process = qc_idle_timer_task;
8423 qc->idle_timer_task->context = qc;
8424
8425 /* Reinit timer task if allocated. */
8426 if (qc->timer_task) {
8427 task_kill(qc->timer_task);
8428 qc->timer_task = t2;
8429 qc->timer_task->process = qc_process_timer;
8430 qc->timer_task->context = qc;
8431 }
8432
8433 /* Reinit IO tasklet. */
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008434 if (qc->wait_event.tasklet->state & TASK_IN_LIST)
8435 qc->flags |= QUIC_FL_CONN_IO_TO_REQUEUE;
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008436 tasklet_kill(qc->wait_event.tasklet);
8437 /* In most cases quic_conn_app_io_cb is used but for 0-RTT quic_conn_io_cb can be still activated. */
8438 t3->process = qc->wait_event.tasklet->process;
8439 qc->wait_event.tasklet = t3;
8440 qc->wait_event.tasklet->tid = new_tid;
8441 qc->wait_event.tasklet->context = qc;
8442 qc->wait_event.events = 0;
8443
8444 /* Rebind the connection FD. */
8445 if (qc_test_fd(qc)) {
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008446 /* Reading is reactivated by the new thread. */
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008447 fd_migrate_on(qc->fd, new_tid);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008448 }
8449
8450 /* Remove conn from per-thread list instance. */
8451 qc_detach_th_ctx_list(qc, 0);
8452 /* Connection must not be closing or else it must be inserted in quic_conns_clo list instance instead. */
8453 BUG_ON(qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING));
8454 LIST_APPEND(&ha_thread_ctx[new_tid].quic_conns, &qc->el_th_ctx);
8455 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
8456
8457 node = eb64_first(&qc->cids);
8458 BUG_ON(!node || eb64_next(node)); /* One and only one CID must be present before affinity rebind. */
8459 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
8460 /* Rebinding is considered done when CID points to the new thread. No
8461 * access should be done to quic-conn instance after it.
8462 */
8463 HA_ATOMIC_STORE(&conn_id->tid, new_tid);
8464 qc = NULL;
8465
8466 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, NULL);
8467 return 0;
8468
8469 err:
8470 task_destroy(t1);
8471 task_destroy(t2);
8472 if (t3)
8473 tasklet_free(t3);
8474
8475 TRACE_DEVEL("leaving on error", QUIC_EV_CONN_SET_AFFINITY, qc);
8476 return 1;
8477}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008478
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008479/* Must be called after qc_set_tid_affinity() on the new thread. */
8480void qc_finalize_affinity_rebind(struct quic_conn *qc)
8481{
8482 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8483
8484 /* Reactivate FD polling if connection socket is active. */
8485 qc_want_recv(qc);
8486
8487 /* Reactivate timer task if needed. */
8488 qc_set_timer(qc);
8489
8490 /* Idle timer task is always active. */
8491 task_queue(qc->idle_timer_task);
8492
8493 /* Reactivate IO tasklet if needed. */
8494 if (qc->flags & QUIC_FL_CONN_IO_TO_REQUEUE) {
8495 tasklet_wakeup(qc->wait_event.tasklet);
8496 qc->flags &= ~QUIC_FL_CONN_IO_TO_REQUEUE;
8497 }
8498
8499 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, qc);
8500}
8501
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008502/* appctx context used by "show quic" command */
8503struct show_quic_ctx {
8504 unsigned int epoch;
8505 struct bref bref; /* back-reference to the quic-conn being dumped */
8506 unsigned int thr;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008507 int flags;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008508};
8509
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008510#define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */
8511
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008512static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private)
8513{
8514 struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
8515
8516 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
8517 return 1;
8518
8519 ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1);
8520 ctx->thr = 0;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008521 ctx->flags = 0;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008522
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01008523 if (*args[2] && strcmp(args[2], "all") == 0)
8524 ctx->flags |= QC_CLI_FL_SHOW_ALL;
8525
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008526 LIST_INIT(&ctx->bref.users);
8527
8528 return 0;
8529}
8530
8531static int cli_io_handler_dump_quic(struct appctx *appctx)
8532{
8533 struct show_quic_ctx *ctx = appctx->svcctx;
8534 struct stconn *sc = appctx_sc(appctx);
8535 struct quic_conn *qc;
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008536 struct quic_pktns *pktns;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008537 struct eb64_node *node;
8538 struct qc_stream_desc *stream;
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008539 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008540 int expire;
8541 unsigned char cid_len;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008542
8543 thread_isolate();
8544
8545 if (ctx->thr >= global.nbthread)
8546 goto done;
8547
Christopher Faulet87633c32023-04-03 18:32:50 +02008548 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02008549 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008550 /* If we're forced to shut down, we might have to remove our
8551 * reference to the last stream being dumped.
8552 */
8553 if (!LIST_ISEMPTY(&ctx->bref.users))
8554 LIST_DEL_INIT(&ctx->bref.users);
8555 goto done;
8556 }
8557
8558 chunk_reset(&trash);
8559
8560 if (!LIST_ISEMPTY(&ctx->bref.users)) {
8561 /* Remove show_quic_ctx from previous quic_conn instance. */
8562 LIST_DEL_INIT(&ctx->bref.users);
8563 }
8564 else if (!ctx->bref.ref) {
8565 /* First invocation. */
8566 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8567 }
8568
8569 while (1) {
8570 int done = 0;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008571 int i;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008572
8573 if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008574 /* If closing connections requested through "all", move
8575 * to quic_conns_clo list after browsing quic_conns.
8576 * Else move directly to the next quic_conns thread.
8577 */
8578 if (ctx->flags & QC_CLI_FL_SHOW_ALL) {
8579 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns_clo.n;
8580 continue;
8581 }
8582
8583 done = 1;
8584 }
8585 else if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns_clo) {
8586 /* Closing list entirely browsed, go to next quic_conns
8587 * thread.
8588 */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008589 done = 1;
8590 }
8591 else {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008592 /* Retrieve next element of the current list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008593 qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx);
8594 if ((int)(qc->qc_epoch - ctx->epoch) > 0)
8595 done = 1;
8596 }
8597
8598 if (done) {
8599 ++ctx->thr;
8600 if (ctx->thr >= global.nbthread)
8601 break;
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008602 /* Switch to next thread quic_conns list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008603 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8604 continue;
8605 }
8606
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008607 /* CIDs */
Amaury Denoyelle66947282023-04-13 11:48:38 +02008608 chunk_appendf(&trash, "* %p[%02u]: scid=", qc, ctx->thr);
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008609 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
8610 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
8611 while (cid_len++ < 20)
8612 chunk_appendf(&trash, "..");
8613
8614 chunk_appendf(&trash, " dcid=");
8615 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
8616 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
8617 while (cid_len++ < 20)
8618 chunk_appendf(&trash, "..");
8619
8620 chunk_appendf(&trash, "\n");
8621
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008622 chunk_appendf(&trash, " loc. TPs:");
8623 quic_transport_params_dump(&trash, qc, &qc->rx.params);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008624 chunk_appendf(&trash, "\n");
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008625 chunk_appendf(&trash, " rem. TPs:");
8626 quic_transport_params_dump(&trash, qc, &qc->tx.params);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008627 chunk_appendf(&trash, "\n");
Frédéric Lécaille5e3201e2023-03-07 15:18:02 +01008628
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008629 /* Connection state */
8630 if (qc->flags & QUIC_FL_CONN_CLOSING)
8631 chunk_appendf(&trash, " st=closing ");
8632 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8633 chunk_appendf(&trash, " st=draining ");
8634 else if (qc->state < QUIC_HS_ST_CONFIRMED)
8635 chunk_appendf(&trash, " st=handshake ");
8636 else
8637 chunk_appendf(&trash, " st=opened ");
8638
8639 if (qc->mux_state == QC_MUX_NULL)
8640 chunk_appendf(&trash, "mux=null ");
8641 else if (qc->mux_state == QC_MUX_READY)
8642 chunk_appendf(&trash, "mux=ready ");
8643 else
8644 chunk_appendf(&trash, "mux=released ");
8645
Frédéric Lécaille92f4a7c2023-04-04 14:31:49 +02008646 expire = qc->idle_expire;
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008647 chunk_appendf(&trash, "expire=%02ds ",
Frédéric Lécaille92f4a7c2023-04-04 14:31:49 +02008648 TICKS_TO_MS(tick_remain(now_ms, expire)) / 1000);
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008649
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008650 chunk_appendf(&trash, "\n");
8651
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008652 /* Socket */
8653 chunk_appendf(&trash, " fd=%d", qc->fd);
8654 if (qc->local_addr.ss_family == AF_INET ||
8655 qc->local_addr.ss_family == AF_INET6) {
8656 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8657 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
8658 chunk_appendf(&trash, " from=%s:%s", bufaddr, bufport);
8659
8660 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8661 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
8662 chunk_appendf(&trash, " to=%s:%s", bufaddr, bufport);
8663 }
8664
8665 chunk_appendf(&trash, "\n");
8666
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008667 /* Packet number spaces information */
8668 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008669 chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008670 pktns->rx.arngs.sz, pktns->tx.in_flight);
8671 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008672 chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008673 pktns->rx.arngs.sz, pktns->tx.in_flight);
8674 pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8675 chunk_appendf(&trash, " [01rtt] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
8676 pktns->rx.arngs.sz, pktns->tx.in_flight);
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008677
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +02008678 chunk_appendf(&trash, " srtt=%-4u rttvar=%-4u rttmin=%-4u ptoc=%-4u cwnd=%-6llu"
8679 " mcwnd=%-6llu lostpkts=%-6llu\n",
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008680 qc->path->loss.srtt >> 3, qc->path->loss.rtt_var >> 2,
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +02008681 qc->path->loss.rtt_min, qc->path->loss.pto_count, (ullong)qc->path->cwnd,
8682 (ullong)qc->path->mcwnd, (ullong)qc->path->loss.nb_lost_pkt);
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008683
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01008684
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008685 /* Streams */
8686 node = eb64_first(&qc->streams_by_id);
8687 i = 0;
8688 while (node) {
8689 stream = eb64_entry(node, struct qc_stream_desc, by_id);
8690 node = eb64_next(node);
8691
Amaury Denoyellea9de25a2023-02-10 09:25:22 +01008692 chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key);
8693 chunk_appendf(&trash, " off=%-8llu ack=%-8llu",
8694 (unsigned long long)stream->buf_offset,
8695 (unsigned long long)stream->ack_offset);
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008696
8697 if (!(++i % 3)) {
8698 chunk_appendf(&trash, "\n");
8699 i = 0;
8700 }
8701 }
8702
8703 chunk_appendf(&trash, "\n");
8704
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008705 if (applet_putchk(appctx, &trash) == -1) {
8706 /* Register show_quic_ctx to quic_conn instance. */
8707 LIST_APPEND(&qc->back_refs, &ctx->bref.users);
8708 goto full;
8709 }
8710
8711 ctx->bref.ref = qc->el_th_ctx.n;
8712 }
8713
8714 done:
8715 thread_release();
8716 return 1;
8717
8718 full:
8719 thread_release();
8720 return 0;
8721}
8722
8723static void cli_release_show_quic(struct appctx *appctx)
8724{
8725 struct show_quic_ctx *ctx = appctx->svcctx;
8726
8727 if (ctx->thr < global.nbthread) {
8728 thread_isolate();
8729 if (!LIST_ISEMPTY(&ctx->bref.users))
8730 LIST_DEL_INIT(&ctx->bref.users);
8731 thread_release();
8732 }
8733}
8734
8735static struct cli_kw_list cli_kws = {{ }, {
8736 { { "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 +01008737 {{},}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008738}};
8739
8740INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
8741
8742static void init_quic()
8743{
8744 int thr;
8745
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008746 for (thr = 0; thr < MAX_THREADS; ++thr) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008747 LIST_INIT(&ha_thread_ctx[thr].quic_conns);
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01008748 LIST_INIT(&ha_thread_ctx[thr].quic_conns_clo);
8749 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008750}
8751INITCALL0(STG_INIT, init_quic);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008752
8753/*
8754 * Local variables:
8755 * c-indent-level: 8
8756 * c-basic-offset: 8
8757 * End:
8758 */