blob: 443dcbc637ea399c29df8014156c3b95f3852642 [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,
Frédéric Lécaillea9461252023-04-24 18:20:44 +0200220 "quic_connection_id", sizeof(struct quic_connection_id));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200221DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
222DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet));
223DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet));
224DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm));
225DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf", sizeof(struct quic_crypto_buf));
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +0200226DECLARE_STATIC_POOL(pool_head_quic_cstream, "quic_cstream", sizeof(struct quic_cstream));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200227DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame));
228DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node));
229
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +0100230static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +0200231 struct quic_conn *qc,
232 const struct quic_cid *odcid,
233 const struct sockaddr_storage *saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200234static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
235 struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
236 struct list *frms, struct quic_conn *qc,
237 const struct quic_version *ver, size_t dglen, int pkt_type,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +0200238 int must_ack, int padding, int probe, int cc, int *err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200239struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécailled7215712023-03-24 18:13:37 +0100240static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack);
241static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200242static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
243static int quic_conn_init_timer(struct quic_conn *qc);
244static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
245
246/* Only for debug purpose */
247struct enc_debug_info {
248 unsigned char *payload;
249 size_t payload_len;
250 unsigned char *aad;
251 size_t aad_len;
252 uint64_t pn;
253};
254
255/* Initializes a enc_debug_info struct (only for debug purpose) */
256static inline void enc_debug_info_init(struct enc_debug_info *edi,
257 unsigned char *payload, size_t payload_len,
258 unsigned char *aad, size_t aad_len, uint64_t pn)
259{
260 edi->payload = payload;
261 edi->payload_len = payload_len;
262 edi->aad = aad;
263 edi->aad_len = aad_len;
264 edi->pn = pn;
265}
266
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100267/* Used only for QUIC TLS key phase traces */
268struct quic_kp_trace {
269 const unsigned char *rx_sec;
270 size_t rx_seclen;
271 const struct quic_tls_kp *rx;
272 const unsigned char *tx_sec;
273 size_t tx_seclen;
274 const struct quic_tls_kp *tx;
275};
276
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200277/* Trace callback for QUIC.
278 * These traces always expect that arg1, if non-null, is of type connection.
279 */
280static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
281 const struct ist where, const struct ist func,
282 const void *a1, const void *a2, const void *a3, const void *a4)
283{
284 const struct quic_conn *qc = a1;
285
286 if (qc) {
287 const struct quic_tls_ctx *tls_ctx;
288
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +0200289 chunk_appendf(&trace_buf, " : qc@%p flags=0x%x", qc, qc->flags);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200290 if (mask & QUIC_EV_CONN_INIT) {
291 chunk_appendf(&trace_buf, "\n odcid");
292 quic_cid_dump(&trace_buf, &qc->odcid);
293 chunk_appendf(&trace_buf, "\n dcid");
294 quic_cid_dump(&trace_buf, &qc->dcid);
295 chunk_appendf(&trace_buf, "\n scid");
296 quic_cid_dump(&trace_buf, &qc->scid);
297 }
298
299 if (mask & QUIC_EV_TRANSP_PARAMS) {
300 const struct quic_transport_params *p = a2;
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100301
302 if (p)
303 quic_transport_params_dump(&trace_buf, qc, p);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200304 }
305
306 if (mask & QUIC_EV_CONN_ADDDATA) {
307 const enum ssl_encryption_level_t *level = a2;
308 const size_t *len = a3;
309
310 if (level) {
311 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
312
313 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
314 }
315 if (len)
316 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
317 }
318 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
319 /* Initial read & write secrets. */
320 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
321 const unsigned char *rx_sec = a2;
322 const unsigned char *tx_sec = a3;
323
324 tls_ctx = &qc->els[level].tls_ctx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200325 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
326 if (rx_sec)
327 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
328 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
329 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
330 if (tx_sec)
331 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
332 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200333 }
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100334
335 if ((mask & QUIC_EV_CONN_KP) && qc) {
336 /* Initial read & write secrets. */
337 const struct quic_kp_trace *kp = a2;
338
339 if (kp) {
340 if (kp->rx) {
341 chunk_appendf(&trace_buf, "\n RX kp");
342 if (kp->rx_sec)
343 quic_tls_secret_hexdump(&trace_buf, kp->rx_sec, kp->rx_seclen);
344 quic_tls_kp_keys_hexdump(&trace_buf, kp->rx);
345 }
346 if (kp->tx) {
347 chunk_appendf(&trace_buf, "\n TX kp");
348 if (kp->tx_sec)
349 quic_tls_secret_hexdump(&trace_buf, kp->tx_sec, kp->tx_seclen);
350 quic_tls_kp_keys_hexdump(&trace_buf, kp->tx);
351 }
352 }
353 }
354
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200355 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
356 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200357
358 if (level) {
359 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
360
361 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200362 if (quic_tls_has_rx_sec(&qc->els[lvl])) {
363 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200364 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200365 }
366 else
367 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200368 }
369 }
370
371 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
372 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200373
374 if (level) {
375 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
376
377 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200378 if (quic_tls_has_tx_sec(&qc->els[lvl])) {
379 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200380 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200381 }
382 else
383 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200384 }
385
386 }
387
388 if (mask & QUIC_EV_CONN_FRMLIST) {
389 const struct list *l = a2;
390
391 if (l) {
392 const struct quic_frame *frm;
393 list_for_each_entry(frm, l, list) {
394 chunk_appendf(&trace_buf, " frm@%p", frm);
395 chunk_frm_appendf(&trace_buf, frm);
396 }
397 }
398 }
399
400 if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) {
401 const struct quic_tx_packet *pkt = a2;
402 const struct quic_enc_level *qel = a3;
403 const ssize_t *room = a4;
404
405 if (qel) {
406 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200407 chunk_appendf(&trace_buf, " qel=%c flags=0x%x pto_count=%d cwnd=%llu ppif=%lld pif=%llu "
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200408 "if=%llu pp=%u",
409 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200410 qel->pktns->flags,
Frédéric Lécaille45400532023-02-13 18:39:19 +0100411 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200412 (unsigned long long)qc->path->cwnd,
413 (unsigned long long)qc->path->prep_in_flight,
414 (unsigned long long)qc->path->in_flight,
415 (unsigned long long)pktns->tx.in_flight,
416 pktns->tx.pto_probe);
417 }
418 if (pkt) {
419 const struct quic_frame *frm;
420 if (pkt->pn_node.key != (uint64_t)-1)
421 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
422 list_for_each_entry(frm, &pkt->frms, list) {
423 chunk_appendf(&trace_buf, " frm@%p", frm);
424 chunk_frm_appendf(&trace_buf, frm);
425 }
426 }
427
428 if (room) {
429 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
430 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
431 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
432 }
433 }
434
435 if (mask & QUIC_EV_CONN_IO_CB) {
436 const enum quic_handshake_state *state = a2;
437 const int *err = a3;
438
439 if (state)
440 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
441 if (err)
442 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
443 }
444
445 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
446 const struct quic_rx_packet *pkt = a2;
447 const unsigned long *pktlen = a3;
448 const SSL *ssl = a4;
449
450 if (pkt) {
451 chunk_appendf(&trace_buf, " pkt@%p", pkt);
452 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
453 chunk_appendf(&trace_buf, " kp=%d",
454 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
455 chunk_appendf(&trace_buf, " el=%c",
456 quic_packet_type_enc_level_char(pkt->type));
457 if (pkt->pnl)
458 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
459 (unsigned long long)pkt->pn);
460 if (pkt->token_len)
461 chunk_appendf(&trace_buf, " toklen=%llu",
462 (unsigned long long)pkt->token_len);
463 if (pkt->aad_len)
464 chunk_appendf(&trace_buf, " aadlen=%llu",
465 (unsigned long long)pkt->aad_len);
466 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
467 pkt->flags, (unsigned long long)pkt->len);
468 }
469 if (pktlen)
470 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
471 if (ssl) {
472 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
473 chunk_appendf(&trace_buf, " el=%c",
474 quic_enc_level_char(ssl_to_quic_enc_level(level)));
475 }
476 }
477
478 if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
479 const struct quic_rx_packet *pkt = a2;
480 const struct quic_rx_crypto_frm *cf = a3;
481 const SSL *ssl = a4;
482
483 if (pkt)
484 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
485 quic_packet_type_enc_level_char(pkt->type),
486 (unsigned long long)pkt->pn);
487 if (cf)
488 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
489 (unsigned long long)cf->offset_node.key,
490 (unsigned long long)cf->len);
491 if (ssl) {
492 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
493 chunk_appendf(&trace_buf, " rel=%c",
494 quic_enc_level_char(ssl_to_quic_enc_level(level)));
495 }
496
497 if (qc->err.code)
498 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code);
499 }
500
501 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
502 const struct quic_frame *frm = a2;
503
504 if (frm)
505 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
506 }
507
508 if (mask & QUIC_EV_CONN_PHPKTS) {
509 const struct quic_enc_level *qel = a2;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +0200510 const struct list *l = a3;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200511
512 if (qel) {
513 const struct quic_pktns *pktns = qel->pktns;
514 chunk_appendf(&trace_buf,
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200515 " qel=%c flags=0x%x state=%s ack?%d pto_count=%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u off=%llu",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200516 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille91369cf2023-04-13 15:55:49 +0200517 qel->pktns->flags,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200518 quic_hdshk_state_str(qc->state),
519 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille45400532023-02-13 18:39:19 +0100520 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200521 (unsigned long long)qc->path->cwnd,
522 (unsigned long long)qc->path->prep_in_flight,
523 (unsigned long long)qc->path->in_flight,
524 (unsigned long long)pktns->tx.in_flight,
Amaury Denoyelle2f668f02022-11-18 15:24:08 +0100525 pktns->tx.pto_probe,
526 qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200527 }
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +0200528
529 if (l) {
530 const struct quic_frame *frm;
531 list_for_each_entry(frm, l, list) {
532 chunk_appendf(&trace_buf, " frm@%p", frm);
533 chunk_frm_appendf(&trace_buf, frm);
534 }
535 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200536 }
537
538 if (mask & QUIC_EV_CONN_ENCPKT) {
539 const struct enc_debug_info *edi = a2;
540
541 if (edi)
542 chunk_appendf(&trace_buf,
543 " payload=@%p payload_len=%llu"
544 " aad=@%p aad_len=%llu pn=%llu",
545 edi->payload, (unsigned long long)edi->payload_len,
546 edi->aad, (unsigned long long)edi->aad_len,
547 (unsigned long long)edi->pn);
548 }
549
550 if (mask & QUIC_EV_CONN_RMHP) {
551 const struct quic_rx_packet *pkt = a2;
552
553 if (pkt) {
554 const int *ret = a3;
555
556 chunk_appendf(&trace_buf, " pkt@%p", pkt);
557 if (ret && *ret)
558 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
559 pkt->pnl, (unsigned long long)pkt->pn);
560 }
561 }
562
563 if (mask & QUIC_EV_CONN_PRSAFRM) {
564 const struct quic_frame *frm = a2;
565 const unsigned long *val1 = a3;
566 const unsigned long *val2 = a4;
567
568 if (frm) {
569 chunk_appendf(&trace_buf, " frm@%p", frm);
570 chunk_frm_appendf(&trace_buf, frm);
571 }
572 if (val1)
573 chunk_appendf(&trace_buf, " %lu", *val1);
574 if (val2)
575 chunk_appendf(&trace_buf, "..%lu", *val2);
576 }
577
578 if (mask & QUIC_EV_CONN_ACKSTRM) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +0200579 const struct qf_stream *strm_frm = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200580 const struct qc_stream_desc *stream = a3;
581
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +0200582 if (strm_frm)
583 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)strm_frm->offset.key, (ull)strm_frm->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200584 if (stream)
585 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
586 }
587
588 if (mask & QUIC_EV_CONN_RTTUPDT) {
589 const unsigned int *rtt_sample = a2;
590 const unsigned int *ack_delay = a3;
591 const struct quic_loss *ql = a4;
592
593 if (rtt_sample)
594 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
595 if (ack_delay)
596 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
597 if (ql)
598 chunk_appendf(&trace_buf,
599 " srtt=%ums rttvar=%ums min_rtt=%ums",
600 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
601 }
602 if (mask & QUIC_EV_CONN_CC) {
603 const struct quic_cc_event *ev = a2;
604 const struct quic_cc *cc = a3;
605
606 if (a2)
607 quic_cc_event_trace(&trace_buf, ev);
608 if (a3)
609 quic_cc_state_trace(&trace_buf, cc);
610 }
611
612 if (mask & QUIC_EV_CONN_PKTLOSS) {
613 const struct quic_pktns *pktns = a2;
614 const struct list *lost_pkts = a3;
615
616 if (pktns) {
617 chunk_appendf(&trace_buf, " pktns=%s",
618 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
619 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
620 if (pktns->tx.loss_time)
621 chunk_appendf(&trace_buf, " loss_time=%dms",
622 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
623 }
624 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
625 struct quic_tx_packet *pkt;
626
627 chunk_appendf(&trace_buf, " lost_pkts:");
628 list_for_each_entry(pkt, lost_pkts, list)
629 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
630 }
631 }
632
633 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
634 const struct quic_pktns *pktns = a2;
635 const int *duration = a3;
636 const uint64_t *ifae_pkts = a4;
637
638 if (ifae_pkts)
639 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
640 (unsigned long long)*ifae_pkts);
641 if (pktns) {
642 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
643 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
644 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
645 pktns->tx.pto_probe);
646 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
647 if (pktns->tx.in_flight)
648 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
649 if (pktns->tx.loss_time)
650 chunk_appendf(&trace_buf, " loss_time=%dms",
651 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
652 }
653 if (mask & QUIC_EV_CONN_SPTO) {
654 if (pktns->tx.time_of_last_eliciting)
655 chunk_appendf(&trace_buf, " tole=%dms",
656 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
657 if (duration)
658 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
659 }
660 }
661
662 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
663 chunk_appendf(&trace_buf,
664 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
665 }
666 }
667
668 if (mask & QUIC_EV_CONN_SPPKTS) {
669 const struct quic_tx_packet *pkt = a2;
670
Frédéric Lécaille45400532023-02-13 18:39:19 +0100671 chunk_appendf(&trace_buf, " pto_count=%d cwnd=%llu ppif=%llu pif=%llu",
672 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200673 (unsigned long long)qc->path->cwnd,
674 (unsigned long long)qc->path->prep_in_flight,
675 (unsigned long long)qc->path->in_flight);
676 if (pkt) {
677 const struct quic_frame *frm;
Frédéric Lécaille6fd25762023-04-07 19:01:33 +0200678 if (pkt->flags & QUIC_FL_TX_PACKET_ACK)
679 chunk_appendf(&trace_buf, " ack");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200680 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
681 (unsigned long)pkt->pn_node.key,
682 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
683 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
684 (unsigned long long)pkt->in_flight_len);
685 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
686 (unsigned long long)qc->rx.bytes,
687 (unsigned long long)qc->tx.bytes);
688 list_for_each_entry(frm, &pkt->frms, list) {
689 chunk_appendf(&trace_buf, " frm@%p", frm);
690 chunk_frm_appendf(&trace_buf, frm);
691 }
Frédéric Lécaillebc09f742023-02-13 17:45:36 +0100692
693 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
694 chunk_appendf(&trace_buf, " with scid");
695 quic_cid_dump(&trace_buf, &qc->scid);
696 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200697 }
698 }
699
700 if (mask & QUIC_EV_CONN_SSLALERT) {
701 const uint8_t *alert = a2;
702 const enum ssl_encryption_level_t *level = a3;
703
704 if (alert)
705 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
706 if (level)
707 chunk_appendf(&trace_buf, " el=%c",
708 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
709 }
710
711 if (mask & QUIC_EV_CONN_BCFRMS) {
712 const size_t *sz1 = a2;
713 const size_t *sz2 = a3;
714 const size_t *sz3 = a4;
715
716 if (sz1)
717 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
718 if (sz2)
719 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
720 if (sz3)
721 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
722 }
723
724 if (mask & QUIC_EV_CONN_PSTRM) {
725 const struct quic_frame *frm = a2;
726
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100727 if (frm)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200728 chunk_frm_appendf(&trace_buf, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200729 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +0200730
731 if (mask & QUIC_EV_CONN_ELEVELSEL) {
732 const enum quic_handshake_state *state = a2;
733 const enum quic_tls_enc_level *level = a3;
734 const enum quic_tls_enc_level *next_level = a4;
735
736 if (state)
737 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state));
738 if (level)
739 chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level));
740 if (next_level)
741 chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level));
742
743 }
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200744
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200745 if (mask & QUIC_EV_CONN_IDLE_TIMER) {
746 if (tick_isset(qc->ack_expire))
747 chunk_appendf(&trace_buf, " ack_expire=%ums",
748 TICKS_TO_MS(tick_remain(now_ms, qc->ack_expire)));
749 if (tick_isset(qc->idle_expire))
750 chunk_appendf(&trace_buf, " idle_expire=%ums",
751 TICKS_TO_MS(tick_remain(now_ms, qc->idle_expire)));
Frédéric Lécaillece5c1452023-04-05 09:44:21 +0200752 if (qc->idle_timer_task && tick_isset(qc->idle_timer_task->expire))
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200753 chunk_appendf(&trace_buf, " expire=%ums",
754 TICKS_TO_MS(tick_remain(now_ms, qc->idle_timer_task->expire)));
755 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200756 }
Frédéric Lécailleaaf32f02023-05-12 17:37:29 +0200757
758 if (mask & QUIC_EV_CONN_RCV) {
759 int i;
760 const struct quic_dgram *dgram = a2;
761 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
762
763 if (qc) {
764 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
765 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
766 chunk_appendf(&trace_buf, " peer_addr=%s:%s ", bufaddr, bufport);
767 }
768
769 if (dgram) {
770 chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len);
771 /* Socket */
772 if (dgram->saddr.ss_family == AF_INET ||
773 dgram->saddr.ss_family == AF_INET6) {
774 addr_to_str(&dgram->saddr, bufaddr, sizeof(bufaddr));
775 port_to_str(&dgram->saddr, bufport, sizeof(bufport));
776 chunk_appendf(&trace_buf, "saddr=%s:%s ", bufaddr, bufport);
777
778 addr_to_str(&dgram->daddr, bufaddr, sizeof(bufaddr));
779 port_to_str(&dgram->daddr, bufport, sizeof(bufport));
780 chunk_appendf(&trace_buf, "daddr=%s:%s ", bufaddr, bufport);
781 }
782 /* DCID */
783 for (i = 0; i < dgram->dcid_len; ++i)
784 chunk_appendf(&trace_buf, "%02x", dgram->dcid[i]);
785
786 }
787 }
788
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200789 if (mask & QUIC_EV_CONN_LPKT) {
790 const struct quic_rx_packet *pkt = a2;
791 const uint64_t *len = a3;
792 const struct quic_version *ver = a4;
793
794 if (pkt) {
795 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
796 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
797 if (pkt->pn_node.key != (uint64_t)-1)
798 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
799 }
800
801 if (len)
802 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
803
804 if (ver)
805 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
806 }
807
808 if (mask & QUIC_EV_STATELESS_RST) {
809 const struct quic_cid *cid = a2;
810
811 if (cid)
812 quic_cid_dump(&trace_buf, cid);
813 }
814
815}
816
817/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
818static inline int quic_peer_validated_addr(struct quic_conn *qc)
819{
820 struct quic_pktns *hdshk_pktns, *app_pktns;
821
822 if (!qc_is_listener(qc))
823 return 1;
824
825 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
826 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
827 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
828 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
829 qc->state >= QUIC_HS_ST_COMPLETE)
830 return 1;
831
832 return 0;
833}
834
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100835/* To be called to kill a connection as soon as possible (without sending any packet). */
836void qc_kill_conn(struct quic_conn *qc)
837{
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100838 TRACE_ENTER(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille495968e2023-04-03 17:42:05 +0200839 TRACE_PROTO("killing the connection", QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100840 qc->flags |= QUIC_FL_CONN_TO_KILL;
841 task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER);
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +0200842
843 qc_notify_err(qc);
844
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100845 TRACE_LEAVE(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100846}
847
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200848/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
849 * both loss detection and PTO and schedule the task assiated to this timer if needed.
850 */
851static inline void qc_set_timer(struct quic_conn *qc)
852{
853 struct quic_pktns *pktns;
854 unsigned int pto;
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100855 int handshake_confirmed;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200856
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100857 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc);
858 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200859
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100860 pktns = NULL;
861 if (!qc->timer_task) {
862 TRACE_PROTO("already released timer task", QUIC_EV_CONN_STIMER, qc);
863 goto leave;
864 }
865
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200866 pktns = quic_loss_pktns(qc);
867 if (tick_isset(pktns->tx.loss_time)) {
868 qc->timer = pktns->tx.loss_time;
869 goto out;
870 }
871
872 /* anti-amplification: the timer must be
873 * cancelled for a server which reached the anti-amplification limit.
874 */
875 if (!quic_peer_validated_addr(qc) &&
876 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
877 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
878 qc->timer = TICK_ETERNITY;
879 goto out;
880 }
881
882 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
883 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
884 /* Timer cancellation. */
885 qc->timer = TICK_ETERNITY;
886 goto out;
887 }
888
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100889 handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED;
890 pktns = quic_pto_pktns(qc, handshake_confirmed, &pto);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200891 if (tick_isset(pto))
892 qc->timer = pto;
893 out:
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100894 if (qc->timer == TICK_ETERNITY) {
895 qc->timer_task->expire = TICK_ETERNITY;
896 }
897 else if (tick_is_expired(qc->timer, now_ms)) {
898 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
899 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
900 }
901 else {
902 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
903 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200904 }
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100905 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100906 TRACE_PROTO("set timer", QUIC_EV_CONN_STIMER, qc, pktns);
907 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200908}
909
910/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
911 * connection.
912 * Return 1 if succeeded, 0 if not.
913 */
914static int quic_tls_key_update(struct quic_conn *qc)
915{
916 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 +0100917 struct quic_tls_secrets *rx = &tls_ctx->rx;
918 struct quic_tls_secrets *tx = &tls_ctx->tx;
919 /* Used only for the traces */
920 struct quic_kp_trace kp_trace = {
921 .rx_sec = rx->secret,
922 .rx_seclen = rx->secretlen,
923 .tx_sec = tx->secret,
924 .tx_seclen = tx->secretlen,
925 };
926 /* The next key phase secrets to be derived */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200927 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
928 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
929 const struct quic_version *ver =
930 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
931 int ret = 0;
932
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100933 TRACE_ENTER(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200934
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200935 nxt_rx = &qc->ku.nxt_rx;
936 nxt_tx = &qc->ku.nxt_tx;
937
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100938 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
939 "nxt_rx->secretlen=%llu rx->secretlen=%llu",
940 (ull)nxt_rx->secretlen, (ull)rx->secretlen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200941 /* Prepare new RX secrets */
942 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
943 rx->secret, rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100944 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200945 goto leave;
946 }
947
948 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
949 nxt_rx->key, nxt_rx->keylen,
950 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
951 nxt_rx->secret, nxt_rx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100952 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200953 goto leave;
954 }
955
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100956 kp_trace.rx = nxt_rx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200957 /* Prepare new TX secrets */
958 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
959 tx->secret, tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100960 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200961 goto leave;
962 }
963
964 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
965 nxt_tx->key, nxt_tx->keylen,
966 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
967 nxt_tx->secret, nxt_tx->secretlen)) {
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100968 TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200969 goto leave;
970 }
971
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +0100972 kp_trace.tx = nxt_tx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200973 if (nxt_rx->ctx) {
974 EVP_CIPHER_CTX_free(nxt_rx->ctx);
975 nxt_rx->ctx = NULL;
976 }
977
978 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
Frédéric Lécaille7a01ff72023-05-02 20:03:19 +0200979 TRACE_ERROR("could not initialize RX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200980 goto leave;
981 }
982
983 if (nxt_tx->ctx) {
984 EVP_CIPHER_CTX_free(nxt_tx->ctx);
985 nxt_tx->ctx = NULL;
986 }
987
Frédéric Lécaille7a01ff72023-05-02 20:03:19 +0200988 if (!quic_tls_tx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
989 TRACE_ERROR("could not initialize TX TLS cipher context", QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200990 goto leave;
991 }
992
993 ret = 1;
994 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100995 TRACE_PROTO("key update", QUIC_EV_CONN_KP, qc, &kp_trace);
996 TRACE_LEAVE(QUIC_EV_CONN_KP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200997 return ret;
998}
999
1000/* Rotate the Key Update information for <qc> QUIC connection.
1001 * Must be used after having updated them.
1002 * Always succeeds.
1003 */
1004static void quic_tls_rotate_keys(struct quic_conn *qc)
1005{
1006 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
1007 unsigned char *curr_secret, *curr_iv, *curr_key;
1008 EVP_CIPHER_CTX *curr_ctx;
1009
1010 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1011
1012 /* Rotate the RX secrets */
1013 curr_ctx = tls_ctx->rx.ctx;
1014 curr_secret = tls_ctx->rx.secret;
1015 curr_iv = tls_ctx->rx.iv;
1016 curr_key = tls_ctx->rx.key;
1017
1018 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
1019 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
1020 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
1021 tls_ctx->rx.key = qc->ku.nxt_rx.key;
1022
1023 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
1024 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
1025 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
1026 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
1027
1028 qc->ku.prv_rx.ctx = curr_ctx;
1029 qc->ku.prv_rx.secret = curr_secret;
1030 qc->ku.prv_rx.iv = curr_iv;
1031 qc->ku.prv_rx.key = curr_key;
1032 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
1033
1034 /* Update the TX secrets */
1035 curr_ctx = tls_ctx->tx.ctx;
1036 curr_secret = tls_ctx->tx.secret;
1037 curr_iv = tls_ctx->tx.iv;
1038 curr_key = tls_ctx->tx.key;
1039
1040 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
1041 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
1042 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
1043 tls_ctx->tx.key = qc->ku.nxt_tx.key;
1044
1045 qc->ku.nxt_tx.ctx = curr_ctx;
1046 qc->ku.nxt_tx.secret = curr_secret;
1047 qc->ku.nxt_tx.iv = curr_iv;
1048 qc->ku.nxt_tx.key = curr_key;
1049
1050 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1051}
1052
1053/* returns 0 on error, 1 on success */
1054int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
1055 const uint8_t *read_secret,
1056 const uint8_t *write_secret, size_t secret_len)
1057{
1058 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1059 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
1060 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001061 struct quic_tls_secrets *rx = NULL, *tx = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001062 const struct quic_version *ver =
1063 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
1064 int ret = 0;
1065
1066 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
1067 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001068
1069 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1070 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1071 goto out;
1072 }
1073
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001074 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1075 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001076 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001077 }
1078
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001079 if (!read_secret)
1080 goto write;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001081
1082 rx = &tls_ctx->rx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001083 if (!quic_tls_secrets_keys_alloc(rx)) {
1084 TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1085 goto leave;
1086 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001087
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001088 rx->aead = tls_aead(cipher);
1089 rx->md = tls_md(cipher);
1090 rx->hp = tls_hp(cipher);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001091
1092 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen,
1093 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
1094 read_secret, secret_len)) {
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001095 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001096 goto leave;
1097 }
1098
1099 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
1100 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1101 goto leave;
1102 }
1103
1104 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
1105 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1106 goto leave;
1107 }
1108
1109 /* Enqueue this connection asap if we could derive O-RTT secrets as
1110 * listener. Note that a listener derives only RX secrets for this
1111 * level.
1112 */
1113 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
1114 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
1115 quic_accept_push_qc(qc);
1116 }
1117
1118write:
1119
1120 if (!write_secret)
1121 goto out;
1122
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001123 tx = &tls_ctx->tx;
1124 if (!quic_tls_secrets_keys_alloc(tx)) {
1125 TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1126 goto leave;
1127 }
1128
1129 tx->aead = tls_aead(cipher);
1130 tx->md = tls_md(cipher);
1131 tx->hp = tls_hp(cipher);
1132
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001133 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen,
1134 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
1135 write_secret, secret_len)) {
1136 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
1137 goto leave;
1138 }
1139
1140 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
1141 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1142 goto leave;
1143 }
1144
1145 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
1146 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1147 goto leave;
1148 }
1149
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01001150 if (level == ssl_encryption_handshake && qc_is_listener(qc)) {
1151 qc->enc_params_len =
1152 quic_transport_params_encode(qc->enc_params,
1153 qc->enc_params + sizeof qc->enc_params,
1154 &qc->rx.params, ver, 1);
1155 if (!qc->enc_params_len) {
1156 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC);
1157 goto leave;
1158 }
1159
1160 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
1161 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC);
1162 goto leave;
1163 }
1164 }
1165
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001166 if (level == ssl_encryption_application) {
1167 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
1168 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
1169 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
1170
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001171 if (rx) {
1172 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1173 TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1174 goto leave;
1175 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001176
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001177 memcpy(rx->secret, read_secret, secret_len);
1178 rx->secretlen = secret_len;
1179 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001180
1181 if (tx) {
1182 if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1183 TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1184 goto leave;
1185 }
1186
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001187 memcpy(tx->secret, write_secret, secret_len);
1188 tx->secretlen = secret_len;
1189 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001190
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001191 /* Initialize all the secret keys lengths */
1192 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001193 }
1194
1195 out:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001196 ret = 1;
1197 leave:
1198 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
1199 return ret;
1200}
1201
1202/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1203 * with <len> as size in CRYPTO buffers dedicated to store the information about
1204 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1205 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
1206 * buffers to store all the data.
1207 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1208 */
1209static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
1210 const unsigned char *data, size_t len)
1211{
1212 struct quic_crypto_buf **qcb;
1213 /* The remaining byte to store in CRYPTO buffers. */
1214 size_t cf_offset, cf_len, *nb_buf;
1215 unsigned char *pos;
1216 int ret = 0;
1217
1218 nb_buf = &qel->tx.crypto.nb_buf;
1219 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1220 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1221 cf_len = len;
1222
1223 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1224
1225 while (len) {
1226 size_t to_copy, room;
1227
1228 pos = (*qcb)->data + (*qcb)->sz;
1229 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1230 to_copy = len > room ? room : len;
1231 if (to_copy) {
1232 memcpy(pos, data, to_copy);
1233 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1234 qel->tx.crypto.sz += to_copy;
1235 (*qcb)->sz += to_copy;
1236 len -= to_copy;
1237 data += to_copy;
1238 }
1239 else {
1240 struct quic_crypto_buf **tmp;
1241
1242 // FIXME: realloc!
1243 tmp = realloc(qel->tx.crypto.bufs,
1244 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1245 if (tmp) {
1246 qel->tx.crypto.bufs = tmp;
1247 qcb = &qel->tx.crypto.bufs[*nb_buf];
1248 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1249 if (!*qcb) {
1250 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1251 goto leave;
1252 }
1253
1254 (*qcb)->sz = 0;
1255 ++*nb_buf;
1256 }
1257 else {
1258 break;
1259 }
1260 }
1261 }
1262
1263 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1264 * have been buffered.
1265 */
1266 if (!len) {
1267 struct quic_frame *frm;
1268 struct quic_frame *found = NULL;
1269
1270 /* There is at most one CRYPTO frame in this packet number
1271 * space. Let's look for it.
1272 */
1273 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
1274 if (frm->type != QUIC_FT_CRYPTO)
1275 continue;
1276
1277 /* Found */
1278 found = frm;
1279 break;
1280 }
1281
1282 if (found) {
1283 found->crypto.len += cf_len;
1284 }
1285 else {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001286 frm = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001287 if (!frm) {
1288 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1289 goto leave;
1290 }
1291
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001292 frm->crypto.offset = cf_offset;
1293 frm->crypto.len = cf_len;
1294 frm->crypto.qel = qel;
1295 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
1296 }
1297 }
1298 ret = len == 0;
1299 leave:
1300 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1301 return ret;
1302}
1303
1304/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1305 * activity for <qc> will be interrupted.
1306 */
1307void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
1308{
1309 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1310 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
1311 goto leave;
1312
1313 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
1314 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
1315 qc->err.code = err.code;
1316 qc->err.app = err.app;
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02001317
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001318 leave:
1319 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1320}
1321
1322/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1323void quic_set_tls_alert(struct quic_conn *qc, int alert)
1324{
1325 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1326
1327 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1328 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
1329 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
1330 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1331 }
1332 quic_set_connection_close(qc, quic_err_tls(alert));
1333 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
1334 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1335
1336 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1337}
1338
1339/* Set the application for <qc> QUIC connection.
1340 * Return 1 if succeeded, 0 if not.
1341 */
1342int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1343{
1344 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1345 qc->app_ops = &h3_ops;
1346 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1347 qc->app_ops = &hq_interop_ops;
1348 else
1349 return 0;
1350
1351 return 1;
1352}
1353
1354/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1355 * wants to provide the QUIC layer with CRYPTO data.
1356 * Returns 1 if succeeded, 0 if not.
1357 */
1358int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1359 const uint8_t *data, size_t len)
1360{
1361 struct quic_conn *qc;
1362 enum quic_tls_enc_level tel;
1363 struct quic_enc_level *qel;
1364 int ret = 0;
1365
1366 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1367 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1368
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001369 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1370 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1371 goto out;
1372 }
1373
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001374 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1375 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
1376 goto out;
1377 }
1378
1379 tel = ssl_to_quic_enc_level(level);
1380 if (tel == -1) {
1381 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1382 goto leave;
1383 }
1384
1385 qel = &qc->els[tel];
1386 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1387 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1388 goto leave;
1389 }
1390
1391 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
1392 qc, &level, &len);
1393 out:
1394 ret = 1;
1395 leave:
1396 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1397 return ret;
1398}
1399
1400int ha_quic_flush_flight(SSL *ssl)
1401{
1402 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1403
1404 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1405 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
1406
1407 return 1;
1408}
1409
1410int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1411{
1412 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1413
1414 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1415
1416 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1417
1418 quic_set_tls_alert(qc, alert);
1419 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1420 return 1;
1421}
1422
1423/* QUIC TLS methods */
1424static SSL_QUIC_METHOD ha_quic_method = {
1425 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1426 .add_handshake_data = ha_quic_add_handshake_data,
1427 .flush_flight = ha_quic_flush_flight,
1428 .send_alert = ha_quic_send_alert,
1429};
1430
1431/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1432 * Returns an error count.
1433 */
1434int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1435{
1436 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1437 int cfgerr = 0;
1438
1439 long options =
1440 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1441 SSL_OP_SINGLE_ECDH_USE |
1442 SSL_OP_CIPHER_SERVER_PREFERENCE;
1443 SSL_CTX *ctx;
1444
1445 ctx = SSL_CTX_new(TLS_server_method());
1446 bind_conf->initial_ctx = ctx;
1447
1448 SSL_CTX_set_options(ctx, options);
1449 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1450 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1451 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1452
1453#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1454# if defined(HAVE_SSL_CLIENT_HELLO_CB)
1455# if defined(SSL_OP_NO_ANTI_REPLAY)
1456 if (bind_conf->ssl_conf.early_data) {
1457 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
1458 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
1459 }
1460# endif /* !SSL_OP_NO_ANTI_REPLAY */
1461 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1462 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1463# else /* ! HAVE_SSL_CLIENT_HELLO_CB */
1464 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1465# endif
1466 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1467#endif
1468 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1469
1470 return cfgerr;
1471}
1472
1473/* Decode an expected packet number from <truncated_on> its truncated value,
1474 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1475 * the number of bits used to encode this packet number (its length in bytes * 8).
1476 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1477 */
1478static uint64_t decode_packet_number(uint64_t largest_pn,
1479 uint32_t truncated_pn, unsigned int pn_nbits)
1480{
1481 uint64_t expected_pn = largest_pn + 1;
1482 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1483 uint64_t pn_hwin = pn_win / 2;
1484 uint64_t pn_mask = pn_win - 1;
1485 uint64_t candidate_pn;
1486
1487
1488 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1489 /* Note that <pn_win> > <pn_hwin>. */
1490 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1491 candidate_pn + pn_hwin <= expected_pn)
1492 return candidate_pn + pn_win;
1493
1494 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1495 return candidate_pn - pn_win;
1496
1497 return candidate_pn;
1498}
1499
1500/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1501 * cryptographic context.
1502 * <largest_pn> is the largest received packet number and <pn> the address of
1503 * the packet number field for this packet with <byte0> address of its first byte.
1504 * <end> points to one byte past the end of this packet.
1505 * Returns 1 if succeeded, 0 if not.
1506 */
1507static int qc_do_rm_hp(struct quic_conn *qc,
1508 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1509 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
1510{
1511 int ret, i, pnlen;
1512 uint64_t packet_number;
1513 uint32_t truncated_pn = 0;
1514 unsigned char mask[5] = {0};
1515 unsigned char *sample;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001516
1517 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1518
1519 ret = 0;
1520
1521 /* Check there is enough data in this packet. */
1522 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1523 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1524 goto leave;
1525 }
1526
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001527 sample = pn + QUIC_PACKET_PN_MAXLEN;
1528
1529 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1530 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
1531 goto leave;
1532 }
1533
1534 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1535 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1536 for (i = 0; i < pnlen; i++) {
1537 pn[i] ^= mask[i + 1];
1538 truncated_pn = (truncated_pn << 8) | pn[i];
1539 }
1540
1541 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1542 /* Store remaining information for this unprotected header */
1543 pkt->pn = packet_number;
1544 pkt->pnl = pnlen;
1545
1546 ret = 1;
1547 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001548 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
1549 return ret;
1550}
1551
1552/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1553 * address, with <payload_len> as payload length, <aad> as address of
1554 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1555 * context.
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02001556 *
1557 * TODO no error is expected as encryption is done in place but encryption
1558 * manual is unclear. <fail> will be set to true if an error is detected.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001559 */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02001560static void quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1561 unsigned char *aad, size_t aad_len, uint64_t pn,
1562 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc,
1563 int *fail)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001564{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001565 unsigned char iv[QUIC_TLS_IV_LEN];
1566 unsigned char *tx_iv = tls_ctx->tx.iv;
1567 size_t tx_iv_sz = tls_ctx->tx.ivlen;
1568 struct enc_debug_info edi;
1569
1570 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02001571 *fail = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001572
Amaury Denoyelle5eadc272023-05-16 18:11:01 +02001573 quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001574
1575 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1576 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1577 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02001578 *fail = 1;
1579 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001580 }
1581
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001582 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001583}
1584
Frédéric Lécaille72027782023-02-22 16:20:09 +01001585/* Select the correct TLS cipher context to used to decipher <pkt> packet
1586 * attached to <qc> connection from <qel> encryption level.
1587 */
1588static inline struct quic_tls_ctx *qc_select_tls_ctx(struct quic_conn *qc,
1589 struct quic_enc_level *qel,
1590 struct quic_rx_packet *pkt)
1591{
1592 return pkt->type != QUIC_PACKET_TYPE_INITIAL ? &qel->tls_ctx :
1593 pkt->version == qc->negotiated_version ? &qc->negotiated_ictx : &qel->tls_ctx;
1594}
1595
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001596/* Decrypt <pkt> packet using encryption level <qel> for <qc> connection.
1597 * Decryption is done in place in packet buffer.
1598 *
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +05001599 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001600 */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001601static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel,
1602 struct quic_rx_packet *pkt)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001603{
1604 int ret, kp_changed;
1605 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaille72027782023-02-22 16:20:09 +01001606 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001607 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
1608 unsigned char *rx_iv = tls_ctx->rx.iv;
1609 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1610 unsigned char *rx_key = tls_ctx->rx.key;
1611
1612 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1613
1614 ret = 0;
1615 kp_changed = 0;
1616
1617 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1618 /* The two tested bits are not at the same position,
1619 * this is why they are first both inversed.
1620 */
1621 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1622 if (pkt->pn < tls_ctx->rx.pn) {
1623 /* The lowest packet number of a previous key phase
1624 * cannot be null if it really stores previous key phase
1625 * secrets.
1626 */
1627 // TODO: check if BUG_ON() more suitable
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001628 if (!qc->ku.prv_rx.pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001629 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1630 goto leave;
1631 }
1632
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001633 rx_ctx = qc->ku.prv_rx.ctx;
1634 rx_iv = qc->ku.prv_rx.iv;
1635 rx_key = qc->ku.prv_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001636 }
1637 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1638 /* Next key phase */
Frédéric Lécaille51a7caf2023-02-23 20:38:23 +01001639 TRACE_PROTO("Key phase changed", QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001640 kp_changed = 1;
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001641 rx_ctx = qc->ku.nxt_rx.ctx;
1642 rx_iv = qc->ku.nxt_rx.iv;
1643 rx_key = qc->ku.nxt_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001644 }
1645 }
1646 }
1647
Amaury Denoyelle5eadc272023-05-16 18:11:01 +02001648 quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001649
1650 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1651 pkt->data, pkt->aad_len,
1652 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
1653 if (!ret) {
1654 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1655 goto leave;
1656 }
1657
1658 /* Update the keys only if the packet decryption succeeded. */
1659 if (kp_changed) {
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001660 quic_tls_rotate_keys(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001661 /* Toggle the Key Phase bit */
1662 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1663 /* Store the lowest packet number received for the current key phase */
1664 tls_ctx->rx.pn = pkt->pn;
1665 /* Prepare the next key update */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001666 if (!quic_tls_key_update(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001667 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1668 goto leave;
1669 }
1670 }
1671
1672 /* Update the packet length (required to parse the frames). */
1673 pkt->len -= QUIC_TLS_TAG_LEN;
1674 ret = 1;
1675 leave:
1676 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1677 return ret;
1678}
1679
1680
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001681/* Release <frm> frame and mark its copies as acknowledged */
1682void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
1683{
1684 uint64_t pn;
1685 struct quic_frame *origin, *f, *tmp;
1686
1687 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1688
1689 /* Identify this frame: a frame copy or one of its copies */
1690 origin = frm->origin ? frm->origin : frm;
1691 /* Ensure the source of the copies is flagged as acked, <frm> being
1692 * possibly a copy of <origin>
1693 */
1694 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1695 /* Mark all the copy of <origin> as acknowledged. We must
1696 * not release the packets (releasing the frames) at this time as
1697 * they are possibly also to be acknowledged alongside the
1698 * the current one.
1699 */
1700 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1701 if (f->pkt) {
1702 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1703 f->origin = NULL;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001704 LIST_DEL_INIT(&f->ref);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001705 pn = f->pkt->pn_node.key;
1706 TRACE_DEVEL("mark frame as acked from packet",
1707 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1708 }
1709 else {
1710 TRACE_DEVEL("freeing unsent frame",
1711 QUIC_EV_CONN_PRSAFRM, qc, f);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001712 LIST_DEL_INIT(&f->ref);
1713 qc_frm_free(&f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001714 }
1715 }
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001716 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001717 pn = frm->pkt->pn_node.key;
1718 quic_tx_packet_refdec(frm->pkt);
1719 TRACE_DEVEL("freeing frame from packet",
1720 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001721 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001722
1723 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1724}
1725
1726/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1727 * and all STREAM data are acknowledged. The MUX is responsible to have set
1728 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1729 *
1730 * TODO this should also be called on lost packet detection
1731 */
1732void qc_check_close_on_released_mux(struct quic_conn *qc)
1733{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001734 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1735
1736 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1737 /* Reuse errcode which should have been previously set by the MUX on release. */
1738 quic_set_connection_close(qc, qc->err);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02001739 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001740 }
1741
1742 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1743}
1744
1745/* Remove from <stream> the acknowledged frames.
1746 *
1747 * Returns 1 if at least one frame was removed else 0.
1748 */
1749static int quic_stream_try_to_consume(struct quic_conn *qc,
1750 struct qc_stream_desc *stream)
1751{
1752 int ret;
1753 struct eb64_node *frm_node;
1754
1755 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1756
1757 ret = 0;
1758 frm_node = eb64_first(&stream->acked_frms);
1759 while (frm_node) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001760 struct qf_stream *strm_frm;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001761 struct quic_frame *frm;
1762 size_t offset, len;
1763
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001764 strm_frm = eb64_entry(frm_node, struct qf_stream, offset);
1765 offset = strm_frm->offset.key;
1766 len = strm_frm->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001767
1768 if (offset > stream->ack_offset)
1769 break;
1770
1771 if (qc_stream_desc_ack(&stream, offset, len)) {
1772 /* cf. next comment : frame may be freed at this stage. */
1773 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001774 qc, stream ? strm_frm : NULL, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001775 ret = 1;
1776 }
1777
1778 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1779 * has been freed. with the stream frames tree. Nothing to do
1780 * anymore in here.
1781 */
1782 if (!stream) {
1783 qc_check_close_on_released_mux(qc);
1784 ret = 1;
1785 goto leave;
1786 }
1787
1788 frm_node = eb64_next(frm_node);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001789 eb64_delete(&strm_frm->offset);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001790
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02001791 frm = container_of(strm_frm, struct quic_frame, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001792 qc_release_frm(qc, frm);
1793 }
1794
1795 leave:
1796 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
1797 return ret;
1798}
1799
1800/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1801static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1802 struct quic_frame *frm)
1803{
Frédéric Lécaille8f991942023-03-24 15:14:45 +01001804 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1805 TRACE_PROTO("RX ack TX frm", QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001806
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001807 switch (frm->type) {
1808 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1809 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02001810 struct qf_stream *strm_frm = &frm->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001811 struct eb64_node *node = NULL;
1812 struct qc_stream_desc *stream = NULL;
1813 const size_t offset = strm_frm->offset.key;
1814 const size_t len = strm_frm->len;
1815
1816 /* do not use strm_frm->stream as the qc_stream_desc instance
1817 * might be freed at this stage. Use the id to do a proper
1818 * lookup.
1819 *
1820 * TODO if lookup operation impact on the perf is noticeable,
1821 * implement a refcount on qc_stream_desc instances.
1822 */
1823 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1824 if (!node) {
1825 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
1826 qc_release_frm(qc, frm);
1827 /* early return */
1828 goto leave;
1829 }
1830 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1831
1832 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
1833 if (offset <= stream->ack_offset) {
1834 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001835 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1836 qc, strm_frm, stream);
1837 }
1838
1839 if (!stream) {
1840 /* no need to continue if stream freed. */
1841 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
1842 qc_release_frm(qc, frm);
1843 qc_check_close_on_released_mux(qc);
1844 break;
1845 }
1846
1847 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1848 qc, strm_frm, stream);
1849 qc_release_frm(qc, frm);
1850 }
1851 else {
1852 eb64_insert(&stream->acked_frms, &strm_frm->offset);
1853 }
1854
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01001855 quic_stream_try_to_consume(qc, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001856 }
1857 break;
1858 default:
1859 qc_release_frm(qc, frm);
1860 }
1861
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001862 leave:
1863 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1864}
1865
1866/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1867 * deallocating them, and their TX frames.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001868 * May be NULL if <largest> node could not be found.
1869 */
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001870static inline void qc_ackrng_pkts(struct quic_conn *qc,
1871 struct eb_root *pkts,
1872 unsigned int *pkt_flags,
1873 struct list *newly_acked_pkts,
1874 struct eb64_node *largest_node,
1875 uint64_t largest, uint64_t smallest)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001876{
1877 struct eb64_node *node;
1878 struct quic_tx_packet *pkt;
1879
1880 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1881
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001882 node = eb64_lookup_ge(pkts, smallest);
1883 if (!node)
1884 goto leave;
1885
1886 largest_node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
1887 if (!largest_node)
1888 goto leave;
1889
1890 while (node && node->key <= largest_node->key) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001891 struct quic_frame *frm, *frmbak;
1892
1893 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
1894 *pkt_flags |= pkt->flags;
1895 LIST_INSERT(newly_acked_pkts, &pkt->list);
1896 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
1897 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1898 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaille814645f2022-11-18 18:15:28 +01001899 /* If there are others packet in the same datagram <pkt> is attached to,
1900 * detach the previous one and the next one from <pkt>.
1901 */
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01001902 quic_tx_packet_dgram_detach(pkt);
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001903 node = eb64_next(node);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001904 eb64_delete(&pkt->pn_node);
1905 }
1906
Frédéric Lécaillec664e642023-03-15 17:21:13 +01001907 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001908 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001909}
1910
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001911/* Remove all frames from <pkt_frm_list> and reinsert them in the same order
1912 * they have been sent into <pktns_frm_list>. The loss counter of each frame is
1913 * incremented and checked if it does not exceed retransmission limit.
1914 *
1915 * Returns 1 on success, 0 if a frame loss limit is exceeded. A
1916 * CONNECTION_CLOSE is scheduled in this case.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001917 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001918static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1919 struct quic_tx_packet *pkt,
1920 struct list *pktns_frm_list)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001921{
1922 struct quic_frame *frm, *frmbak;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001923 struct list *pkt_frm_list = &pkt->frms;
1924 uint64_t pn = pkt->pn_node.key;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001925 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001926
1927 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1928
1929 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1930 /* First remove this frame from the packet it was attached to */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001931 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001932 quic_tx_packet_refdec(pkt);
1933 /* At this time, this frame is not freed but removed from its packet */
1934 frm->pkt = NULL;
1935 /* Remove any reference to this frame */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001936 qc_frm_unref(frm, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001937 switch (frm->type) {
1938 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1939 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02001940 struct qf_stream *strm_frm = &frm->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001941 struct eb64_node *node = NULL;
1942 struct qc_stream_desc *stream_desc;
1943
1944 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1945 if (!node) {
1946 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1947 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1948 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001949 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001950 continue;
1951 }
1952
1953 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1954 /* Do not resend this frame if in the "already acked range" */
1955 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1956 TRACE_DEVEL("ignored frame in already acked range",
1957 QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001958 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001959 continue;
1960 }
1961 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01001962 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
1963
Frédéric Lécaillec425e032023-03-20 14:32:59 +01001964 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001965 TRACE_DEVEL("updated partially acked frame",
1966 QUIC_EV_CONN_PRSAFRM, qc, frm);
1967 }
1968 break;
1969 }
1970
1971 default:
1972 break;
1973 }
1974
1975 /* Do not resend probing packet with old data */
1976 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1977 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1978 qc, frm, &pn);
1979 if (frm->origin)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001980 LIST_DEL_INIT(&frm->ref);
1981 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001982 continue;
1983 }
1984
1985 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1986 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1987 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1988 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001989 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001990 }
1991 else {
Amaury Denoyelle24d5b722023-01-31 11:44:50 +01001992 if (++frm->loss_count >= global.tune.quic_max_frame_loss) {
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001993 TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc);
1994 quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR));
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02001995 qc_notify_err(qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001996 close = 1;
1997 }
1998
Frédéric Lécaillebe795ce2023-03-08 18:23:13 +01001999 LIST_APPEND(pktns_frm_list, &frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002000 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
2001 }
2002 }
2003
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002004 end:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002005 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002006 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002007}
2008
2009/* Free <pkt> TX packet and its attached frames.
2010 * This is the responsibility of the caller to remove this packet of
2011 * any data structure it was possibly attached to.
2012 */
2013static inline void free_quic_tx_packet(struct quic_conn *qc,
2014 struct quic_tx_packet *pkt)
2015{
2016 struct quic_frame *frm, *frmbak;
2017
2018 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
2019
2020 if (!pkt)
2021 goto leave;
2022
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002023 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
2024 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002025 pool_free(pool_head_quic_tx_packet, pkt);
2026
2027 leave:
2028 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
2029}
2030
2031/* Free the TX packets of <pkts> list */
2032static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
2033{
2034 struct quic_tx_packet *pkt, *tmp;
2035
2036 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
2037
2038 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2039 LIST_DELETE(&pkt->list);
2040 eb64_delete(&pkt->pn_node);
2041 free_quic_tx_packet(qc, pkt);
2042 }
2043
2044 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
2045}
2046
2047/* Remove already sent ranges of acknowledged packet numbers from
2048 * <pktns> packet number space tree below <largest_acked_pn> possibly
2049 * updating the range which contains <largest_acked_pn>.
2050 * Never fails.
2051 */
2052static void qc_treat_ack_of_ack(struct quic_conn *qc,
2053 struct quic_pktns *pktns,
2054 int64_t largest_acked_pn)
2055{
2056 struct eb64_node *ar, *next_ar;
2057 struct quic_arngs *arngs = &pktns->rx.arngs;
2058
2059 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2060
2061 ar = eb64_first(&arngs->root);
2062 while (ar) {
2063 struct quic_arng_node *ar_node;
2064
2065 next_ar = eb64_next(ar);
2066 ar_node = eb64_entry(ar, struct quic_arng_node, first);
2067
2068 if ((int64_t)ar_node->first.key > largest_acked_pn) {
2069 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
2070 break;
2071 }
2072
2073 if (largest_acked_pn < ar_node->last) {
2074 eb64_delete(ar);
2075 ar_node->first.key = largest_acked_pn + 1;
2076 eb64_insert(&arngs->root, ar);
2077 break;
2078 }
2079
Frédéric Lécaille0dd4fa52023-05-02 08:57:37 +02002080 /* Do not empty the tree: the first ACK range contains the
2081 * largest acknowledged packet number.
2082 */
2083 if (arngs->sz == 1)
2084 break;
2085
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002086 eb64_delete(ar);
2087 pool_free(pool_head_quic_arng, ar_node);
2088 arngs->sz--;
2089 ar = next_ar;
2090 }
2091
2092 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2093}
2094
2095/* Send a packet ack event nofication for each newly acked packet of
2096 * <newly_acked_pkts> list and free them.
2097 * Always succeeds.
2098 */
2099static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
2100 struct list *newly_acked_pkts)
2101{
2102 struct quic_tx_packet *pkt, *tmp;
2103 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
2104
2105 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2106
2107 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
2108 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2109 qc->path->prep_in_flight -= pkt->in_flight_len;
2110 qc->path->in_flight -= pkt->in_flight_len;
2111 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2112 qc->path->ifae_pkts--;
2113 /* If this packet contained an ACK frame, proceed to the
2114 * acknowledging of range of acks from the largest acknowledged
2115 * packet number which was sent in an ACK frame by this packet.
2116 */
2117 if (pkt->largest_acked_pn != -1)
2118 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
2119 ev.ack.acked = pkt->in_flight_len;
2120 ev.ack.time_sent = pkt->time_sent;
2121 quic_cc_event(&qc->path->cc, &ev);
2122 LIST_DELETE(&pkt->list);
2123 eb64_delete(&pkt->pn_node);
2124 quic_tx_packet_refdec(pkt);
2125 }
2126
2127 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2128
2129}
2130
2131/* Release all the frames attached to <pktns> packet number space */
2132static inline void qc_release_pktns_frms(struct quic_conn *qc,
2133 struct quic_pktns *pktns)
2134{
2135 struct quic_frame *frm, *frmbak;
2136
2137 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2138
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002139 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list)
2140 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002141
2142 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2143}
2144
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002145/* Handle <pkts> list of lost packets detected at <now_us> handling their TX
2146 * frames. Send a packet loss event to the congestion controller if in flight
2147 * packet have been lost. Also frees the packet in <pkts> list.
2148 *
2149 * Returns 1 on success else 0 if loss limit has been exceeded. A
2150 * CONNECTION_CLOSE was prepared to close the connection ASAP.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002151 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002152static inline int qc_release_lost_pkts(struct quic_conn *qc,
2153 struct quic_pktns *pktns,
2154 struct list *pkts,
2155 uint64_t now_us)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002156{
2157 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002158 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002159
2160 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2161
2162 if (LIST_ISEMPTY(pkts))
2163 goto leave;
2164
2165 oldest_lost = newest_lost = NULL;
2166 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2167 struct list tmp = LIST_HEAD_INIT(tmp);
2168
2169 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2170 qc->path->prep_in_flight -= pkt->in_flight_len;
2171 qc->path->in_flight -= pkt->in_flight_len;
2172 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2173 qc->path->ifae_pkts--;
2174 /* Treat the frames of this lost packet. */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002175 if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms))
2176 close = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002177 LIST_DELETE(&pkt->list);
2178 if (!oldest_lost) {
2179 oldest_lost = newest_lost = pkt;
2180 }
2181 else {
2182 if (newest_lost != oldest_lost)
2183 quic_tx_packet_refdec(newest_lost);
2184 newest_lost = pkt;
2185 }
2186 }
2187
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002188 if (!close) {
2189 if (newest_lost) {
2190 /* Sent a congestion event to the controller */
2191 struct quic_cc_event ev = { };
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002192
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002193 ev.type = QUIC_CC_EVT_LOSS;
2194 ev.loss.time_sent = newest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002195
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002196 quic_cc_event(&qc->path->cc, &ev);
2197 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002198
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002199 /* If an RTT have been already sampled, <rtt_min> has been set.
2200 * We must check if we are experiencing a persistent congestion.
2201 * If this is the case, the congestion controller must re-enter
2202 * slow start state.
2203 */
2204 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2205 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002206
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002207 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2208 now_ms, qc->max_ack_delay))
2209 qc->path->cc.algo->slow_start(&qc->path->cc);
2210 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002211 }
2212
Amaury Denoyelle3a72ba22022-11-14 11:41:34 +01002213 /* <oldest_lost> cannot be NULL at this stage because we have ensured
2214 * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a
2215 * possible overflow on a 0 byte region with O2 optimization.
2216 */
2217 ALREADY_CHECKED(oldest_lost);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002218 quic_tx_packet_refdec(oldest_lost);
2219 if (newest_lost != oldest_lost)
2220 quic_tx_packet_refdec(newest_lost);
2221
2222 leave:
2223 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002224 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002225}
2226
2227/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2228 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
2229 * if the largest acked packet was newly acked and if there was at least one newly
2230 * acked ack-eliciting packet.
2231 * Return 1, if succeeded, 0 if not.
2232 */
2233static inline int qc_parse_ack_frm(struct quic_conn *qc,
2234 struct quic_frame *frm,
2235 struct quic_enc_level *qel,
2236 unsigned int *rtt_sample,
2237 const unsigned char **pos, const unsigned char *end)
2238{
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002239 struct qf_ack *ack_frm = &frm->ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002240 uint64_t smallest, largest;
2241 struct eb_root *pkts;
2242 struct eb64_node *largest_node;
2243 unsigned int time_sent, pkt_flags;
2244 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2245 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2246 int ret = 0;
2247
2248 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2249
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002250 if (ack_frm->largest_ack > qel->pktns->tx.next_pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002251 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002252 qc, NULL, &ack_frm->largest_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002253 goto err;
2254 }
2255
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002256 if (ack_frm->first_ack_range > ack_frm->largest_ack) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002257 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002258 qc, NULL, &ack_frm->first_ack_range);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002259 goto err;
2260 }
2261
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002262 largest = ack_frm->largest_ack;
2263 smallest = largest - ack_frm->first_ack_range;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002264 pkts = &qel->pktns->tx.pkts;
2265 pkt_flags = 0;
2266 largest_node = NULL;
2267 time_sent = 0;
2268
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002269 if ((int64_t)ack_frm->largest_ack > qel->pktns->rx.largest_acked_pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002270 largest_node = eb64_lookup(pkts, largest);
2271 if (!largest_node) {
2272 TRACE_DEVEL("Largest acked packet not found",
2273 QUIC_EV_CONN_PRSAFRM, qc);
2274 }
2275 else {
2276 time_sent = eb64_entry(largest_node,
2277 struct quic_tx_packet, pn_node)->time_sent;
2278 }
2279 }
2280
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002281 TRACE_PROTO("RX ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002282 qc, NULL, &largest, &smallest);
2283 do {
2284 uint64_t gap, ack_range;
2285
2286 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2287 largest_node, largest, smallest);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002288 if (!ack_frm->ack_range_num--)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002289 break;
2290
2291 if (!quic_dec_int(&gap, pos, end)) {
2292 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
2293 goto err;
2294 }
2295
2296 if (smallest < gap + 2) {
2297 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
2298 qc, NULL, &gap, &smallest);
2299 goto err;
2300 }
2301
2302 largest = smallest - gap - 2;
2303 if (!quic_dec_int(&ack_range, pos, end)) {
2304 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
2305 goto err;
2306 }
2307
2308 if (largest < ack_range) {
2309 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
2310 qc, NULL, &largest, &ack_range);
2311 goto err;
2312 }
2313
2314 /* Do not use this node anymore. */
2315 largest_node = NULL;
2316 /* Next range */
2317 smallest = largest - ack_range;
2318
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002319 TRACE_PROTO("RX next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002320 qc, NULL, &largest, &smallest);
2321 } while (1);
2322
2323 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2324 *rtt_sample = tick_remain(time_sent, now_ms);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002325 qel->pktns->rx.largest_acked_pn = ack_frm->largest_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002326 }
2327
2328 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2329 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
2330 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002331 if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms))
2332 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002333 }
2334 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2335 if (quic_peer_validated_addr(qc))
2336 qc->path->loss.pto_count = 0;
2337 qc_set_timer(qc);
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01002338 qc_notify_send(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002339 }
2340
2341 ret = 1;
2342 leave:
2343 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2344 return ret;
2345
2346 err:
2347 free_quic_tx_pkts(qc, &newly_acked_pkts);
2348 goto leave;
2349}
2350
2351/* This function gives the detail of the SSL error. It is used only
2352 * if the debug mode and the verbose mode are activated. It dump all
2353 * the SSL error until the stack was empty.
2354 */
2355static forceinline void qc_ssl_dump_errors(struct connection *conn)
2356{
2357 if (unlikely(global.mode & MODE_DEBUG)) {
2358 while (1) {
2359 const char *func = NULL;
2360 unsigned long ret;
2361
2362 ERR_peek_error_func(&func);
2363 ret = ERR_get_error();
2364 if (!ret)
2365 return;
2366
2367 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
2368 func, ERR_reason_error_string(ret));
2369 }
2370 }
2371}
2372
2373int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2374 const char **str, int *len);
2375
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002376/* Finalize <qc> QUIC connection:
2377 * - initialize the Initial QUIC TLS context for negotiated version,
2378 * - derive the secrets for this context,
2379 * - set them into the TLS stack,
2380 *
2381 * MUST be called after having received the remote transport parameters which
2382 * are parsed when the TLS callback for the ClientHello message is called upon
2383 * SSL_do_handshake() calls, not necessarily at the first time as this TLS
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002384 * message may be split between packets
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002385 * Return 1 if succeeded, 0 if not.
2386 */
2387static int qc_conn_finalize(struct quic_conn *qc, int server)
2388{
2389 int ret = 0;
2390
2391 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
2392
2393 if (qc->flags & QUIC_FL_CONN_FINALIZED)
2394 goto finalized;
2395
2396 if (qc->negotiated_version &&
2397 !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
2398 qc->odcid.data, qc->odcid.len, server))
2399 goto out;
2400
2401 /* This connection is functional (ready to send/receive) */
2402 qc->flags |= QUIC_FL_CONN_FINALIZED;
2403
2404 finalized:
2405 ret = 1;
2406 out:
2407 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
2408 return ret;
2409}
2410
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002411/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2412 * from <qel> encryption level with <ctx> as QUIC connection context.
2413 * Remaining parameter are there for debugging purposes.
2414 * Return 1 if succeeded, 0 if not.
2415 */
2416static inline int qc_provide_cdata(struct quic_enc_level *el,
2417 struct ssl_sock_ctx *ctx,
2418 const unsigned char *data, size_t len,
2419 struct quic_rx_packet *pkt,
2420 struct quic_rx_crypto_frm *cf)
2421{
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002422#ifdef DEBUG_STRICT
2423 enum ncb_ret ncb_ret;
2424#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002425 int ssl_err, state;
2426 struct quic_conn *qc;
2427 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002428 struct ncbuf *ncbuf = &el->cstream->rx.ncbuf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002429
2430 ssl_err = SSL_ERROR_NONE;
2431 qc = ctx->qc;
2432
2433 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2434
2435 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2436 TRACE_ERROR("SSL_provide_quic_data() error",
2437 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
2438 goto leave;
2439 }
2440
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002441 TRACE_PROTO("in order CRYPTO data",
2442 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
2443
2444 state = qc->state;
2445 if (state < QUIC_HS_ST_COMPLETE) {
2446 ssl_err = SSL_do_handshake(ctx->ssl);
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002447
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01002448 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
2449 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
2450 goto leave;
2451 }
2452
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002453 /* Finalize the connection as soon as possible if the peer transport parameters
2454 * have been received. This may be useful to send packets even if this
2455 * handshake fails.
2456 */
2457 if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) {
2458 TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state);
2459 goto leave;
2460 }
2461
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002462 if (ssl_err != 1) {
2463 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2464 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2465 TRACE_PROTO("SSL handshake in progress",
2466 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2467 goto out;
2468 }
2469
2470 /* TODO: Should close the connection asap */
2471 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2472 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2473 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2474 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2475 }
2476 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2477 qc_ssl_dump_errors(ctx->conn);
2478 ERR_clear_error();
2479 goto leave;
2480 }
2481
2482 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
2483
2484 /* Check the alpn could be negotiated */
2485 if (!qc->app_ops) {
2486 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2487 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2488 goto leave;
2489 }
2490
2491 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2492 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
2493 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2494 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2495 }
2496 /* I/O callback switch */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02002497 qc->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002498 if (qc_is_listener(ctx->qc)) {
Amaury Denoyelle1304d192023-04-11 16:46:03 +02002499 qc->flags |= QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002500 qc->state = QUIC_HS_ST_CONFIRMED;
2501 /* The connection is ready to be accepted. */
2502 quic_accept_push_qc(qc);
2503 }
2504 else {
2505 qc->state = QUIC_HS_ST_COMPLETE;
2506 }
2507
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02002508 /* Prepare the next key update */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002509 if (!quic_tls_key_update(qc)) {
2510 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc);
2511 goto leave;
2512 }
2513 } else {
2514 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2515 if (ssl_err != 1) {
2516 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2517 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2518 TRACE_PROTO("SSL post handshake in progress",
2519 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2520 goto out;
2521 }
2522
2523 TRACE_ERROR("SSL post handshake error",
2524 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2525 goto leave;
2526 }
2527
2528 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
2529 }
2530
2531 out:
2532 ret = 1;
2533 leave:
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002534 /* The CRYPTO data are consumed even in case of an error to release
2535 * the memory asap.
2536 */
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002537 if (!ncb_is_null(ncbuf)) {
2538#ifdef DEBUG_STRICT
2539 ncb_ret = ncb_advance(ncbuf, len);
2540 /* ncb_advance() must always succeed. This is guaranteed as
2541 * this is only done inside a data block. If false, this will
2542 * lead to handshake failure with quic_enc_level offset shifted
2543 * from buffer data.
2544 */
2545 BUG_ON(ncb_ret != NCB_RET_OK);
2546#else
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002547 ncb_advance(ncbuf, len);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002548#endif
2549 }
2550
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002551 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
2552 return ret;
2553}
2554
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002555/* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc>
2556 * connection. <fin> is true if FIN bit is set on frame type.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002557 *
2558 * Return 1 on success. On error, 0 is returned. In this case, the packet
2559 * containing the frame must not be acknowledged.
2560 */
2561static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02002562 struct qf_stream *strm_frm,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002563 struct quic_conn *qc, char fin)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002564{
2565 int ret;
2566
2567 /* RFC9000 13.1. Packet Processing
2568 *
2569 * A packet MUST NOT be acknowledged until packet protection has been
2570 * successfully removed and all frames contained in the packet have
2571 * been processed. For STREAM frames, this means the data has been
2572 * enqueued in preparation to be received by the application protocol,
2573 * but it does not require that data be delivered and consumed.
2574 */
2575 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2576
2577 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002578 strm_frm->offset.key, fin, (char *)strm_frm->data);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002579
2580 /* frame rejected - packet must not be acknowledeged */
2581 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2582 return !ret;
2583}
2584
2585/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2586 * for <qc> QUIC connection.
2587 * This is a best effort function which never fails even if no memory could be
2588 * allocated to duplicate these frames.
2589 */
2590static void qc_dup_pkt_frms(struct quic_conn *qc,
2591 struct list *pkt_frm_list, struct list *out_frm_list)
2592{
2593 struct quic_frame *frm, *frmbak;
2594 struct list tmp = LIST_HEAD_INIT(tmp);
2595
2596 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2597
2598 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2599 struct quic_frame *dup_frm, *origin;
2600
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002601 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
2602 TRACE_DEVEL("already acknowledged frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2603 continue;
2604 }
2605
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002606 switch (frm->type) {
2607 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2608 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +02002609 struct qf_stream *strm_frm = &frm->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002610 struct eb64_node *node = NULL;
2611 struct qc_stream_desc *stream_desc;
2612
2613 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2614 if (!node) {
2615 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
2616 continue;
2617 }
2618
2619 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2620 /* Do not resend this frame if in the "already acked range" */
2621 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2622 TRACE_DEVEL("ignored frame in already acked range",
2623 QUIC_EV_CONN_PRSAFRM, qc, frm);
2624 continue;
2625 }
2626 else if (strm_frm->offset.key < stream_desc->ack_offset) {
Frédéric Lécailleca079792023-03-17 08:56:50 +01002627 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
2628
Frédéric Lécaillec425e032023-03-20 14:32:59 +01002629 qc_stream_frm_mv_fwd(frm, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002630 TRACE_DEVEL("updated partially acked frame",
2631 QUIC_EV_CONN_PRSAFRM, qc, frm);
2632 }
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01002633
2634 strm_frm->dup = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002635 break;
2636 }
2637
2638 default:
2639 break;
2640 }
2641
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002642 /* If <frm> is already a copy of another frame, we must take
2643 * its original frame as source for the copy.
2644 */
2645 origin = frm->origin ? frm->origin : frm;
2646 dup_frm = qc_frm_dup(origin);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002647 if (!dup_frm) {
2648 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2649 break;
2650 }
2651
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002652 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002653 if (origin->pkt) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002654 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
2655 qc, NULL, &origin->pkt->pn_node.key);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002656 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002657 else {
2658 /* <origin> is a frame which was sent from a packet detected as lost. */
2659 TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
2660 }
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002661
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002662 LIST_APPEND(&tmp, &dup_frm->list);
2663 }
2664
2665 LIST_SPLICE(out_frm_list, &tmp);
2666
2667 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2668}
2669
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002670/* Boolean function which return 1 if <pkt> TX packet is only made of
2671 * already acknowledged frame.
2672 */
2673static inline int qc_pkt_with_only_acked_frms(struct quic_tx_packet *pkt)
2674{
2675 struct quic_frame *frm;
2676
2677 list_for_each_entry(frm, &pkt->frms, list)
2678 if (!(frm->flags & QUIC_FL_TX_FRAME_ACKED))
2679 return 0;
2680
2681 return 1;
2682}
2683
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002684/* Prepare a fast retransmission from <qel> encryption level */
2685static void qc_prep_fast_retrans(struct quic_conn *qc,
2686 struct quic_enc_level *qel,
2687 struct list *frms1, struct list *frms2)
2688{
2689 struct eb_root *pkts = &qel->pktns->tx.pkts;
2690 struct list *frms = frms1;
2691 struct eb64_node *node;
2692 struct quic_tx_packet *pkt;
2693
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002694 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002695
2696 BUG_ON(frms1 == frms2);
2697
2698 pkt = NULL;
2699 node = eb64_first(pkts);
2700 start:
2701 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002702 struct quic_tx_packet *p;
2703
2704 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002705 node = eb64_next(node);
2706 /* Skip the empty and coalesced packets */
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002707 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002708 "--> pn=%llu (%d %d %d)", (ull)p->pn_node.key,
2709 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED),
2710 qc_pkt_with_only_acked_frms(p));
2711 if (!LIST_ISEMPTY(&p->frms) && !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002712 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002713 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002714 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002715 }
2716
2717 if (!pkt)
2718 goto leave;
2719
2720 /* When building a packet from another one, the field which may increase the
2721 * packet size is the packet number. And the maximum increase is 4 bytes.
2722 */
2723 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2724 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002725 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002726 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2727 goto leave;
2728 }
2729
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002730 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002731 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2732 if (frms == frms1 && frms2) {
2733 frms = frms2;
2734 goto start;
2735 }
2736 leave:
2737 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
2738}
2739
2740/* Prepare a fast retransmission during a handshake after a client
2741 * has resent Initial packets. According to the RFC a server may retransmit
2742 * Initial packets send them coalescing with others (Handshake here).
2743 * (Listener only function).
2744 */
2745static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2746 struct list *ifrms, struct list *hfrms)
2747{
2748 struct list itmp = LIST_HEAD_INIT(itmp);
2749 struct list htmp = LIST_HEAD_INIT(htmp);
2750
2751 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2752 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2753 struct quic_enc_level *qel = iqel;
2754 struct eb_root *pkts;
2755 struct eb64_node *node;
2756 struct quic_tx_packet *pkt;
2757 struct list *tmp = &itmp;
2758
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002759 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002760 start:
2761 pkt = NULL;
2762 pkts = &qel->pktns->tx.pkts;
2763 node = eb64_first(pkts);
2764 /* Skip the empty packet (they have already been retransmitted) */
2765 while (node) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002766 struct quic_tx_packet *p;
2767
2768 p = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002769 TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002770 "--> pn=%llu (%d %d)", (ull)p->pn_node.key,
2771 LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED));
Frédéric Lécaillee6359b62023-03-02 14:49:22 +01002772 if (!LIST_ISEMPTY(&p->frms) && !(p->flags & QUIC_FL_TX_PACKET_COALESCED) &&
2773 !qc_pkt_with_only_acked_frms(p)) {
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002774 pkt = p;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002775 break;
Frédéric Lécaille21564be2023-03-02 11:53:43 +01002776 }
2777
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002778 node = eb64_next(node);
2779 }
2780
2781 if (!pkt)
2782 goto end;
2783
2784 /* When building a packet from another one, the field which may increase the
2785 * packet size is the packet number. And the maximum increase is 4 bytes.
2786 */
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002787 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2788 size_t dglen = pkt->len + 4;
2789
2790 dglen += pkt->next ? pkt->next->len + 4 : 0;
2791 if (dglen > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01002792 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002793 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2794 if (pkt->next)
2795 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt->next);
2796 goto end;
2797 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002798 }
2799
2800 qel->pktns->tx.pto_probe += 1;
2801
2802 /* No risk to loop here, #packet per datagram is bounded */
2803 requeue:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01002804 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002805 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
2806 if (qel == iqel) {
2807 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2808 pkt = pkt->next;
2809 tmp = &htmp;
2810 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002811 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002812 goto requeue;
2813 }
2814 }
2815
2816 end:
2817 LIST_SPLICE(ifrms, &itmp);
2818 LIST_SPLICE(hfrms, &htmp);
2819
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01002820 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002821}
2822
2823static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
2824{
2825 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2826
2827 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2828 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2829 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2830 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
2831 goto out;
2832
2833 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2834 }
2835
2836 out:
2837 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
2838}
2839
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002840/* Cancel a request on connection <qc> for stream id <id>. This is useful when
2841 * the client opens a new stream but the MUX has already been released. A
Amaury Denoyelle75463012023-02-20 10:31:27 +01002842 * STOP_SENDING + RESET_STREAM frames are prepared for emission.
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002843 *
2844 * TODO this function is closely related to H3. Its place should be in H3 layer
2845 * instead of quic-conn but this requires an architecture adjustment.
2846 *
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02002847 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002848 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002849static int qc_h3_request_reject(struct quic_conn *qc, uint64_t id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002850{
2851 int ret = 0;
Amaury Denoyelle75463012023-02-20 10:31:27 +01002852 struct quic_frame *ss, *rs;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002853 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002854 const uint64_t app_error_code = H3_REQUEST_REJECTED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002855
2856 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2857
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002858 /* Do not emit rejection for unknown unidirectional stream as it is
2859 * forbidden to close some of them (H3 control stream and QPACK
2860 * encoder/decoder streams).
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002861 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002862 if (quic_stream_is_uni(id)) {
2863 ret = 1;
2864 goto out;
2865 }
2866
Amaury Denoyelle75463012023-02-20 10:31:27 +01002867 ss = qc_frm_alloc(QUIC_FT_STOP_SENDING);
2868 if (!ss) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002869 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2870 goto out;
2871 }
2872
Amaury Denoyelle75463012023-02-20 10:31:27 +01002873 ss->stop_sending.id = id;
2874 ss->stop_sending.app_error_code = app_error_code;
2875
2876 rs = qc_frm_alloc(QUIC_FT_RESET_STREAM);
2877 if (!rs) {
2878 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2879 qc_frm_free(&ss);
2880 goto out;
2881 }
2882
2883 rs->reset_stream.id = id;
2884 rs->reset_stream.app_error_code = app_error_code;
2885 rs->reset_stream.final_size = 0;
2886
2887 LIST_APPEND(&qel->pktns->tx.frms, &ss->list);
2888 LIST_APPEND(&qel->pktns->tx.frms, &rs->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002889 ret = 1;
2890 out:
2891 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2892 return ret;
2893}
2894
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002895/* Release the underlying memory use by <ncbuf> non-contiguous buffer */
2896static void quic_free_ncbuf(struct ncbuf *ncbuf)
2897{
2898 struct buffer buf;
2899
2900 if (ncb_is_null(ncbuf))
2901 return;
2902
2903 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
2904 b_free(&buf);
2905 offer_buffers(NULL, 1);
2906
2907 *ncbuf = NCBUF_NULL;
2908}
2909
2910/* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */
2911static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf)
2912{
2913 struct buffer buf = BUF_NULL;
2914
2915 if (!ncb_is_null(ncbuf))
2916 return ncbuf;
2917
2918 b_alloc(&buf);
2919 BUG_ON(b_is_null(&buf));
2920
2921 *ncbuf = ncb_make(buf.area, buf.size, 0);
2922 ncb_init(ncbuf, 0);
2923
2924 return ncbuf;
2925}
2926
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002927/* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn.
2928 * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the
2929 * speed up handshake completion may be run after having received duplicated
2930 * CRYPTO data.
2931 */
2932static int qc_handle_crypto_frm(struct quic_conn *qc,
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002933 struct qf_crypto *crypto_frm, struct quic_rx_packet *pkt,
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002934 struct quic_enc_level *qel, int *fast_retrans)
2935{
2936 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002937 enum ncb_ret ncb_ret;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002938 /* XXX TO DO: <cfdebug> is used only for the traces. */
2939 struct quic_rx_crypto_frm cfdebug = {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002940 .offset_node.key = crypto_frm->offset,
2941 .len = crypto_frm->len,
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002942 };
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002943 struct quic_cstream *cstream = qel->cstream;
2944 struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002945
2946 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2947 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2948 TRACE_PROTO("CRYPTO data discarded",
2949 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2950 goto done;
2951 }
2952
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002953 if (unlikely(crypto_frm->offset < cstream->rx.offset)) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002954 size_t diff;
2955
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002956 if (crypto_frm->offset + crypto_frm->len <= cstream->rx.offset) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002957 /* Nothing to do */
2958 TRACE_PROTO("Already received CRYPTO data",
2959 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2960 if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
2961 !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP))
2962 *fast_retrans = 1;
2963 goto done;
2964 }
2965
2966 TRACE_PROTO("Partially already received CRYPTO data",
2967 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2968
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002969 diff = cstream->rx.offset - crypto_frm->offset;
2970 crypto_frm->len -= diff;
2971 crypto_frm->data += diff;
2972 crypto_frm->offset = cstream->rx.offset;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002973 }
2974
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002975 if (crypto_frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) {
2976 if (!qc_provide_cdata(qel, qc->xprt_ctx, crypto_frm->data, crypto_frm->len,
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002977 pkt, &cfdebug)) {
2978 // trace already emitted by function above
2979 goto leave;
2980 }
2981
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002982 cstream->rx.offset += crypto_frm->len;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002983 TRACE_DEVEL("increment crypto level offset", QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002984 goto done;
2985 }
2986
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002987 if (!quic_get_ncbuf(ncbuf) ||
2988 ncb_is_null(ncbuf)) {
2989 TRACE_ERROR("CRYPTO ncbuf allocation failed", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002990 goto leave;
2991 }
2992
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02002993 /* crypto_frm->offset > cstream-trx.offset */
2994 ncb_ret = ncb_add(ncbuf, crypto_frm->offset - cstream->rx.offset,
2995 (const char *)crypto_frm->data, crypto_frm->len, NCB_ADD_COMPARE);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002996 if (ncb_ret != NCB_RET_OK) {
2997 if (ncb_ret == NCB_RET_DATA_REJ) {
2998 TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc);
2999 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02003000 qc_notify_err(qc);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02003001 }
3002 else if (ncb_ret == NCB_RET_GAP_SIZE) {
3003 TRACE_ERROR("cannot bufferize frame due to gap size limit",
3004 QUIC_EV_CONN_PRSHPKT, qc);
3005 }
3006 goto leave;
3007 }
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003008
3009 done:
3010 ret = 1;
3011 leave:
3012 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3013 return ret;
3014}
3015
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003016/* Build a NEW_CONNECTION_ID frame for <conn_id> CID of <qc> connection.
3017 *
3018 * Returns 1 on success else 0.
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003019 */
3020static int qc_build_new_connection_id_frm(struct quic_conn *qc,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003021 struct quic_connection_id *conn_id)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003022{
3023 int ret = 0;
3024 struct quic_frame *frm;
3025 struct quic_enc_level *qel;
3026
3027 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3028
3029 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3030 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
3031 if (!frm) {
3032 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3033 goto leave;
3034 }
3035
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003036 quic_connection_id_to_frm_cpy(frm, conn_id);
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003037 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
3038 ret = 1;
3039 leave:
3040 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3041 return ret;
3042}
3043
3044
3045/* Handle RETIRE_CONNECTION_ID frame from <frm> frame.
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003046 * Return 1 if succeeded, 0 if not. If succeeded, also set <to_retire>
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003047 * to the CID to be retired if not already retired.
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003048 */
3049static int qc_handle_retire_connection_id_frm(struct quic_conn *qc,
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003050 struct quic_frame *frm,
3051 struct quic_cid *dcid,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003052 struct quic_connection_id **to_retire)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003053{
3054 int ret = 0;
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003055 struct qf_retire_connection_id *rcid_frm = &frm->retire_connection_id;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003056 struct eb64_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003057 struct quic_connection_id *conn_id;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003058
3059 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3060
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003061 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3062 * Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number greater
3063 * than any previously sent to the peer MUST be treated as a connection error
3064 * of type PROTOCOL_VIOLATION.
3065 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003066 if (rcid_frm->seq_num >= qc->next_cid_seq_num) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003067 TRACE_PROTO("CID seq. number too big", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003068 goto protocol_violation;
3069 }
3070
3071 /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
3072 * The sequence number specified in a RETIRE_CONNECTION_ID frame MUST NOT refer to
3073 * the Destination Connection ID field of the packet in which the frame is contained.
3074 * The peer MAY treat this as a connection error of type PROTOCOL_VIOLATION.
3075 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003076 node = eb64_lookup(&qc->cids, rcid_frm->seq_num);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003077 if (!node) {
3078 TRACE_PROTO("CID already retired", QUIC_EV_CONN_PSTRM, qc, frm);
3079 goto out;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003080 }
3081
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003082 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003083 /* Note that the length of <dcid> has already been checked. It must match the
3084 * length of the CIDs which have been provided to the peer.
3085 */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003086 if (!memcmp(dcid->data, conn_id->cid.data, QUIC_HAP_CID_LEN)) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003087 TRACE_PROTO("cannot retire the current CID", QUIC_EV_CONN_PSTRM, qc, frm);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003088 goto protocol_violation;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003089 }
3090
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003091 *to_retire = conn_id;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003092 out:
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003093 ret = 1;
3094 leave:
3095 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3096 return ret;
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003097 protocol_violation:
3098 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02003099 qc_notify_err(qc);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003100 goto leave;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003101}
3102
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003103/* Remove a <qc> quic-conn from its ha_thread_ctx list. If <closing> is true,
3104 * it will immediately be reinserted in the ha_thread_ctx quic_conns_clo list.
3105 */
3106static void qc_detach_th_ctx_list(struct quic_conn *qc, int closing)
3107{
3108 struct bref *bref, *back;
3109
3110 /* Detach CLI context watchers currently dumping this connection.
3111 * Reattach them to the next quic_conn instance.
3112 */
3113 list_for_each_entry_safe(bref, back, &qc->back_refs, users) {
3114 /* Remove watcher from this quic_conn instance. */
3115 LIST_DEL_INIT(&bref->users);
3116
3117 /* Attach it to next instance unless it was the last list element. */
3118 if (qc->el_th_ctx.n != &th_ctx->quic_conns &&
3119 qc->el_th_ctx.n != &th_ctx->quic_conns_clo) {
3120 struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx,
3121 struct quic_conn *,
3122 el_th_ctx);
3123 LIST_APPEND(&next->back_refs, &bref->users);
3124 }
3125 bref->ref = qc->el_th_ctx.n;
3126 __ha_barrier_store();
3127 }
3128
3129 /* Remove quic_conn from global ha_thread_ctx list. */
3130 LIST_DEL_INIT(&qc->el_th_ctx);
3131
3132 if (closing)
3133 LIST_APPEND(&th_ctx->quic_conns_clo, &qc->el_th_ctx);
3134}
3135
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003136/* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel>
3137 * as encryption level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003138 * Returns 1 if succeeded, 0 if failed.
3139 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003140static int qc_parse_pkt_frms(struct quic_conn *qc, struct quic_rx_packet *pkt,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003141 struct quic_enc_level *qel)
3142{
3143 struct quic_frame frm;
3144 const unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003145 int fast_retrans = 0, ret = 0;
3146
3147 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
3148 /* Skip the AAD */
3149 pos = pkt->data + pkt->aad_len;
3150 end = pkt->data + pkt->len;
3151
3152 while (pos < end) {
3153 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
3154 // trace already emitted by function above
3155 goto leave;
3156 }
3157
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003158 switch (frm.type) {
3159 case QUIC_FT_PADDING:
3160 break;
3161 case QUIC_FT_PING:
3162 break;
3163 case QUIC_FT_ACK:
3164 {
3165 unsigned int rtt_sample;
3166
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003167 rtt_sample = UINT_MAX;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003168 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
3169 // trace already emitted by function above
3170 goto leave;
3171 }
3172
Frédéric Lécaille809bd9f2023-04-06 13:13:08 +02003173 if (rtt_sample != UINT_MAX) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003174 unsigned int ack_delay;
3175
3176 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
3177 qc->state >= QUIC_HS_ST_CONFIRMED ?
3178 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
3179 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
3180 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
3181 }
3182 break;
3183 }
3184 case QUIC_FT_RESET_STREAM:
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01003185 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003186 struct qf_reset_stream *rs_frm = &frm.reset_stream;
3187 qcc_recv_reset_stream(qc->qcc, rs_frm->id, rs_frm->app_error_code, rs_frm->final_size);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01003188 }
3189 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003190 case QUIC_FT_STOP_SENDING:
3191 {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003192 struct qf_stop_sending *ss_frm = &frm.stop_sending;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003193 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003194 if (qcc_recv_stop_sending(qc->qcc, ss_frm->id,
3195 ss_frm->app_error_code)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003196 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
3197 goto leave;
3198 }
3199 }
3200 break;
3201 }
3202 case QUIC_FT_CRYPTO:
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02003203 if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003204 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003205 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003206 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
3207 {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003208 struct qf_stream *strm_frm = &frm.stream;
3209 unsigned nb_streams = qc->rx.strms[qcs_id_type(strm_frm->id)].nb_streams;
Amaury Denoyelle2216b082023-02-02 14:59:36 +01003210 const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003211
3212 /* The upper layer may not be allocated. */
3213 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003214 if ((strm_frm->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003215 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003216 }
3217 else {
3218 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle38836b62023-02-07 14:24:54 +01003219 if (qc->app_ops == &h3_ops) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003220 if (!qc_h3_request_reject(qc, strm_frm->id)) {
Amaury Denoyelle156a89a2023-02-20 10:32:16 +01003221 TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc);
3222 /* This packet will not be acknowledged */
3223 goto leave;
3224 }
3225 }
3226 else {
3227 /* This packet will not be acknowledged */
3228 goto leave;
Frédéric Lécailled18025e2023-01-20 15:33:50 +01003229 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003230 }
Amaury Denoyelle315a4f62023-03-06 09:10:53 +01003231
3232 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003233 }
3234
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003235 if (!qc_handle_strm_frm(pkt, strm_frm, qc, fin)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003236 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
3237 goto leave;
3238 }
3239
3240 break;
3241 }
3242 case QUIC_FT_MAX_DATA:
3243 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003244 struct qf_max_data *md_frm = &frm.max_data;
3245 qcc_recv_max_data(qc->qcc, md_frm->max_data);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003246 }
3247 break;
3248 case QUIC_FT_MAX_STREAM_DATA:
3249 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02003250 struct qf_max_stream_data *msd_frm = &frm.max_stream_data;
3251 if (qcc_recv_max_stream_data(qc->qcc, msd_frm->id,
3252 msd_frm->max_stream_data)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003253 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
3254 goto leave;
3255 }
3256 }
3257 break;
3258 case QUIC_FT_MAX_STREAMS_BIDI:
3259 case QUIC_FT_MAX_STREAMS_UNI:
3260 break;
3261 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02003262 qc->cntrs.data_blocked++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003263 break;
3264 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02003265 qc->cntrs.stream_data_blocked++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003266 break;
3267 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Amaury Denoyelle6d6ee0d2023-05-25 10:36:04 +02003268 qc->cntrs.streams_blocked_bidi++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003269 break;
3270 case QUIC_FT_STREAMS_BLOCKED_UNI:
Amaury Denoyelle6d6ee0d2023-05-25 10:36:04 +02003271 qc->cntrs.streams_blocked_uni++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003272 break;
3273 case QUIC_FT_NEW_CONNECTION_ID:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003274 /* XXX TO DO XXX */
3275 break;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003276 case QUIC_FT_RETIRE_CONNECTION_ID:
3277 {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003278 struct quic_connection_id *conn_id = NULL;
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003279
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003280 if (!qc_handle_retire_connection_id_frm(qc, &frm, &pkt->dcid, &conn_id))
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003281 goto leave;
3282
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003283 if (!conn_id)
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003284 break;
3285
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003286 ebmb_delete(&conn_id->node);
3287 eb64_delete(&conn_id->seq_num);
3288 pool_free(pool_head_quic_connection_id, conn_id);
Frédéric Lécaillecc101cd2023-03-08 11:01:58 +01003289 TRACE_PROTO("CID retired", QUIC_EV_CONN_PSTRM, qc);
3290
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003291 conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
3292 if (!conn_id) {
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003293 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
3294 }
3295 else {
Amaury Denoyellee83f9372023-04-18 11:10:54 +02003296 quic_cid_insert(conn_id);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003297 qc_build_new_connection_id_frm(qc, conn_id);
Frédéric Lécaille8ac8a872023-03-06 18:16:34 +01003298 }
3299 break;
3300 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003301 case QUIC_FT_CONNECTION_CLOSE:
3302 case QUIC_FT_CONNECTION_CLOSE_APP:
3303 /* Increment the error counters */
3304 qc_cc_err_count_inc(qc, &frm);
3305 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
3306 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
3307 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
3308 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
3309 }
3310 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
3311 /* RFC 9000 10.2. Immediate Close:
3312 * The closing and draining connection states exist to ensure
3313 * that connections close cleanly and that delayed or reordered
3314 * packets are properly discarded. These states SHOULD persist
3315 * for at least three times the current PTO interval...
3316 *
3317 * Rearm the idle timeout only one time when entering draining
3318 * state.
3319 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003320 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003321 qc_detach_th_ctx_list(qc, 1);
Frédéric Lécailled7215712023-03-24 18:13:37 +01003322 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02003323 qc_notify_err(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003324 }
3325 break;
3326 case QUIC_FT_HANDSHAKE_DONE:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003327 if (qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003328 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
3329 QUIC_EV_CONN_PRSHPKT, qc);
3330 goto leave;
3331 }
3332
3333 qc->state = QUIC_HS_ST_CONFIRMED;
3334 break;
3335 default:
3336 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
3337 goto leave;
3338 }
3339 }
3340
3341 /* Flag this packet number space as having received a packet. */
3342 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
3343
3344 if (fast_retrans) {
3345 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3346 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3347
3348 TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc);
3349 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
3350 qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP;
3351 }
3352
3353 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
3354 * has successfully parse a Handshake packet. The Initial encryption must also
3355 * be discarded.
3356 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003357 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003358 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
3359 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
3360 QUIC_FL_TLS_SECRETS_DCD)) {
3361 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3362 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
3363 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003364 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003365 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3366 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3367 }
3368 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
3369 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
3370 }
3371 }
3372
3373 ret = 1;
3374 leave:
3375 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3376 return ret;
3377}
3378
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003379
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003380/* Allocate Tx buffer from <qc> quic-conn if needed.
3381 *
3382 * Returns allocated buffer or NULL on error.
3383 */
3384static struct buffer *qc_txb_alloc(struct quic_conn *qc)
3385{
3386 struct buffer *buf = &qc->tx.buf;
3387 if (!b_alloc(buf))
3388 return NULL;
3389
3390 return buf;
3391}
3392
3393/* Free Tx buffer from <qc> if it is empty. */
3394static void qc_txb_release(struct quic_conn *qc)
3395{
3396 struct buffer *buf = &qc->tx.buf;
3397
3398 /* For the moment sending function is responsible to purge the buffer
3399 * entirely. It may change in the future but this requires to be able
3400 * to reuse old data.
Frédéric Lécaillebbf86be2023-02-20 09:28:58 +01003401 * For the momemt we do not care to leave data in the buffer for
3402 * a connection which is supposed to be killed asap.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003403 */
3404 BUG_ON_HOT(buf && b_data(buf));
3405
3406 if (!b_data(buf)) {
3407 b_free(buf);
3408 offer_buffers(NULL, 1);
3409 }
3410}
3411
3412/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
3413 * must contains the address of the first packet stored in the payload.
3414 *
3415 * Caller is responsible that there is enough space in the buffer.
3416 */
3417static void qc_txb_store(struct buffer *buf, uint16_t length,
3418 struct quic_tx_packet *first_pkt)
3419{
3420 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
3421 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
3422
3423 write_u16(b_tail(buf), length);
3424 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
3425 b_add(buf, hdlen + length);
3426}
3427
3428/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
3429 * with <frms> as ack-eliciting frame list to send, 0 if not.
3430 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
3431 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003432 * Also set <*must_ack> to inform the caller if an acknowledgement should be sent.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003433 */
3434static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003435 struct quic_enc_level *qel, int cc, int probe,
3436 int *must_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003437{
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003438 int force_ack =
3439 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3440 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3441 int nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
3442
3443 /* An acknowledgement must be sent if this has been forced by the caller,
3444 * typically during the handshake when the packets must be acknowledged as
3445 * soon as possible. This is also the case when the ack delay timer has been
3446 * triggered, or at least every QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK packets.
3447 */
3448 *must_ack = (qc->flags & QUIC_FL_CONN_ACK_TIMER_FIRED) ||
3449 ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3450 (force_ack || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003451
3452 /* Do not build any more packet if the TX secrets are not available or
3453 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
3454 * and if there is no more packets to send upon PTO expiration
3455 * and if there is no more ack-eliciting frames to send or in flight
3456 * congestion control limit is reached for prepared data
3457 */
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02003458 if (!quic_tls_has_tx_sec(qel) ||
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003459 (!cc && !probe && !*must_ack &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003460 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
3461 return 0;
3462 }
3463
3464 return 1;
3465}
3466
3467/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3468 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
3469 *
3470 * Each datagram is prepended by a two fields header : the datagram length and
3471 * the address of the packet contained in the datagram.
3472 *
3473 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3474 * -1 if something wrong happened.
3475 */
3476static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
3477 struct list *frms)
3478{
3479 int ret = -1;
3480 struct quic_enc_level *qel;
3481 unsigned char *end, *pos;
3482 struct quic_tx_packet *pkt;
3483 size_t total;
3484 /* Each datagram is prepended with its length followed by the address
3485 * of the first packet in the datagram.
3486 */
3487 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
3488
3489 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3490
3491 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3492 total = 0;
3493 pos = (unsigned char *)b_tail(buf);
3494 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003495 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003496
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02003497 TRACE_PROTO("TX prep app pkts", QUIC_EV_CONN_PHPKTS, qc, qel, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003498 probe = 0;
3499 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3500 /* We do not probe if an immediate close was asked */
3501 if (!cc)
3502 probe = qel->pktns->tx.pto_probe;
3503
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003504 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003505 break;
3506
3507 /* Leave room for the datagram header */
3508 pos += dg_headlen;
3509 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3510 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3511 }
3512 else {
3513 end = pos + qc->path->mtu;
3514 }
3515
3516 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003517 QUIC_PACKET_TYPE_SHORT, must_ack, 0, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003518 switch (err) {
3519 case -2:
3520 // trace already emitted by function above
3521 goto leave;
3522 case -1:
3523 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
3524 * MTU, we are here because of the congestion control window. There is
3525 * no need to try to reuse this buffer.
3526 */
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003527 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003528 goto out;
3529 default:
3530 break;
3531 }
3532
3533 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
3534 BUG_ON(!pkt);
3535
3536 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3537 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3538
3539 total += pkt->len;
3540
3541 /* Write datagram header. */
3542 qc_txb_store(buf, pkt->len, pkt);
3543 }
3544
3545 out:
3546 ret = total;
3547 leave:
3548 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3549 return ret;
3550}
3551
3552/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3553 * <frms>. Several packets can be regrouped in a single datagram. The result is
3554 * written into <buf>.
3555 *
3556 * Each datagram is prepended by a two fields header : the datagram length and
3557 * the address of first packet in the datagram.
3558 *
3559 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3560 * -1 if something wrong happened.
3561 */
3562static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
3563 enum quic_tls_enc_level tel, struct list *tel_frms,
3564 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3565{
3566 struct quic_enc_level *qel;
3567 unsigned char *end, *pos;
3568 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3569 /* length of datagrams */
3570 uint16_t dglen;
3571 size_t total;
3572 int ret = -1, padding;
3573 /* Each datagram is prepended with its length followed by the address
3574 * of the first packet in the datagram.
3575 */
3576 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
3577 struct list *frms;
3578
3579 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3580
3581 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05003582 * caller must ensure that buf is reset.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003583 */
3584 BUG_ON_HOT(buf->head || buf->data);
3585
3586 total = 0;
3587 qel = &qc->els[tel];
3588 frms = tel_frms;
3589 dglen = 0;
3590 padding = 0;
3591 pos = (unsigned char *)b_head(buf);
3592 first_pkt = prv_pkt = NULL;
3593 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003594 int err, probe, cc, must_ack;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003595 enum quic_pkt_type pkt_type;
3596 struct quic_tls_ctx *tls_ctx;
3597 const struct quic_version *ver;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003598
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003599 TRACE_PROTO("TX prep pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003600 probe = 0;
3601 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3602 /* We do not probe if an immediate close was asked */
3603 if (!cc)
3604 probe = qel->pktns->tx.pto_probe;
3605
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003606 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003607 if (prv_pkt)
3608 qc_txb_store(buf, dglen, first_pkt);
3609 /* Let's select the next encryption level */
3610 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3611 tel = next_tel;
3612 frms = next_tel_frms;
3613 qel = &qc->els[tel];
3614 /* Build a new datagram */
3615 prv_pkt = NULL;
3616 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
3617 continue;
3618 }
3619 break;
3620 }
3621
3622 pkt_type = quic_tls_level_pkt_type(tel);
3623 if (!prv_pkt) {
3624 /* Leave room for the datagram header */
3625 pos += dg_headlen;
3626 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3627 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3628 }
3629 else {
3630 end = pos + qc->path->mtu;
3631 }
3632 }
3633
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003634 /* RFC 9000 14.1 Initial datagram size
3635 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3636 * Initial packets to at least the smallest allowed maximum datagram size of
3637 * 1200 bytes.
3638 *
3639 * Ensure that no ack-eliciting packets are sent into too small datagrams
3640 */
3641 if (pkt_type == QUIC_PACKET_TYPE_INITIAL && !LIST_ISEMPTY(tel_frms)) {
3642 if (end - pos < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécailled30a04a2023-02-21 16:44:05 +01003643 TRACE_PROTO("No more enough room to build an Initial packet",
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003644 QUIC_EV_CONN_PHPKTS, qc);
3645 goto out;
3646 }
3647
3648 /* Pad this Initial packet if there is no ack-eliciting frames to send from
3649 * the next packet number space.
3650 */
Frédéric Lécailleec937212023-03-03 17:34:41 +01003651 if (!next_tel_frms || LIST_ISEMPTY(next_tel_frms))
Frédéric Lécaille69e71182023-02-20 14:39:41 +01003652 padding = 1;
3653 }
3654
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003655 if (qc->negotiated_version) {
3656 ver = qc->negotiated_version;
3657 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3658 tls_ctx = &qc->negotiated_ictx;
3659 else
3660 tls_ctx = &qel->tls_ctx;
3661 }
3662 else {
3663 ver = qc->original_version;
3664 tls_ctx = &qel->tls_ctx;
3665 }
3666
3667 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
3668 qc, ver, dglen, pkt_type,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02003669 must_ack, padding, probe, cc, &err);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003670 switch (err) {
3671 case -2:
3672 // trace already emitted by function above
3673 goto leave;
3674 case -1:
3675 /* If there was already a correct packet present, set the
3676 * current datagram as prepared into <cbuf>.
3677 */
3678 if (prv_pkt)
3679 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillee47adca2023-04-07 18:12:00 +02003680 TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003681 goto out;
3682 default:
3683 break;
3684 }
3685
3686 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
3687 BUG_ON(!cur_pkt);
3688
3689 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3690 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3691
3692 total += cur_pkt->len;
3693 /* keep trace of the first packet in the datagram */
3694 if (!first_pkt)
3695 first_pkt = cur_pkt;
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01003696 /* Attach the current one to the previous one and vice versa */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003697 if (prv_pkt) {
3698 prv_pkt->next = cur_pkt;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01003699 cur_pkt->prev = prv_pkt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003700 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3701 }
3702 /* Let's say we have to build a new dgram */
3703 prv_pkt = NULL;
3704 dglen += cur_pkt->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003705 /* If the data for the current encryption level have all been sent,
3706 * select the next level.
3707 */
3708 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillea576c1b2023-04-13 15:59:02 +02003709 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms))) {
Frédéric Lécailleb5461932023-06-14 08:54:51 +02003710 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3711 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003712 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3713 tel = next_tel;
3714 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3715 frms = &qc->els[tel].pktns->tx.frms;
3716 else
3717 frms = next_tel_frms;
3718 qel = &qc->els[tel];
3719 if (!LIST_ISEMPTY(frms)) {
3720 /* If there is data for the next level, do not
3721 * consume a datagram.
3722 */
3723 prv_pkt = cur_pkt;
3724 }
3725 }
3726
3727 /* If we have to build a new datagram, set the current datagram as
3728 * prepared into <cbuf>.
3729 */
3730 if (!prv_pkt) {
3731 qc_txb_store(buf, dglen, first_pkt);
3732 first_pkt = NULL;
3733 dglen = 0;
3734 padding = 0;
3735 }
3736 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
3737 (!qc_is_listener(qc) ||
3738 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3739 padding = 1;
3740 }
3741 }
3742
3743 out:
3744 ret = total;
3745 leave:
3746 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3747 return ret;
3748}
3749
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003750/* Free all frames in <l> list. In addition also remove all these frames
3751 * from the original ones if they are the results of duplications.
3752 */
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003753static inline void qc_free_frm_list(struct list *l)
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003754{
3755 struct quic_frame *frm, *frmbak;
3756
3757 list_for_each_entry_safe(frm, frmbak, l, list) {
3758 LIST_DEL_INIT(&frm->ref);
3759 qc_frm_free(&frm);
3760 }
3761}
3762
3763/* Free <pkt> TX packet and all the packets coalesced to it. */
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003764static inline void qc_free_tx_coalesced_pkts(struct quic_tx_packet *p)
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003765{
3766 struct quic_tx_packet *pkt, *nxt_pkt;
3767
3768 for (pkt = p; pkt; pkt = nxt_pkt) {
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003769 qc_free_frm_list(&pkt->frms);
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003770 nxt_pkt = pkt->next;
3771 pool_free(pool_head_quic_tx_packet, pkt);
3772 }
3773}
3774
3775/* Purge <buf> TX buffer from its prepare packets. */
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003776static void qc_purge_tx_buf(struct buffer *buf)
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003777{
3778 while (b_contig_data(buf, 0)) {
3779 uint16_t dglen;
3780 struct quic_tx_packet *pkt;
3781 size_t headlen = sizeof dglen + sizeof pkt;
3782
3783 dglen = read_u16(b_head(buf));
3784 pkt = read_ptr(b_head(buf) + sizeof dglen);
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003785 qc_free_tx_coalesced_pkts(pkt);
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003786 b_del(buf, dglen + headlen);
3787 }
3788
3789 BUG_ON(b_data(buf));
3790}
3791
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003792/* Send datagrams stored in <buf>.
3793 *
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003794 * This function returns 1 for success. On error, there is several behavior
3795 * depending on underlying sendto() error :
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003796 * - for an unrecoverable error, 0 is returned and connection is killed.
3797 * - a transient error is handled differently if connection has its owned
3798 * socket. If this is the case, 0 is returned and socket is subscribed on the
3799 * poller. The other case is assimilated to a success case with 1 returned.
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003800 * Remaining data are purged from the buffer and will eventually be detected
3801 * as lost which gives the opportunity to retry sending.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003802 */
3803int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
3804{
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003805 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003806 struct quic_conn *qc;
3807 char skip_sendto = 0;
3808
3809 qc = ctx->qc;
3810 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
3811 while (b_contig_data(buf, 0)) {
3812 unsigned char *pos;
3813 struct buffer tmpbuf = { };
3814 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3815 uint16_t dglen;
3816 size_t headlen = sizeof dglen + sizeof first_pkt;
3817 unsigned int time_sent;
3818
3819 pos = (unsigned char *)b_head(buf);
3820 dglen = read_u16(pos);
3821 BUG_ON_HOT(!dglen); /* this should not happen */
3822
3823 pos += sizeof dglen;
3824 first_pkt = read_ptr(pos);
3825 pos += sizeof first_pkt;
3826 tmpbuf.area = (char *)pos;
3827 tmpbuf.size = tmpbuf.data = dglen;
3828
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003829 TRACE_PROTO("TX dgram", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003830 /* If sendto is on error just skip the call to it for the rest
3831 * of the loop but continue to purge the buffer. Data will be
3832 * transmitted when QUIC packets are detected as lost on our
3833 * side.
3834 *
3835 * TODO use fd-monitoring to detect when send operation can be
3836 * retry. This should improve the bandwidth without relying on
3837 * retransmission timer. However, it requires a major rework on
3838 * quic-conn fd management.
3839 */
3840 if (!skip_sendto) {
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003841 int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0);
3842 if (ret < 0) {
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003843 TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc, first_pkt);
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003844 qc_kill_conn(qc);
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003845 qc_free_tx_coalesced_pkts(first_pkt);
Frédéric Lécaillece0bb332023-04-24 11:11:55 +02003846 b_del(buf, dglen + headlen);
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02003847 qc_purge_tx_buf(buf);
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003848 goto leave;
3849 }
3850 else if (!ret) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01003851 /* Connection owned socket : poller will wake us up when transient error is cleared. */
3852 if (qc_test_fd(qc)) {
3853 TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc);
3854 goto leave;
3855 }
3856
3857 /* No connection owned-socket : rely on retransmission to retry sending. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003858 skip_sendto = 1;
3859 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
3860 }
3861 }
3862
3863 b_del(buf, dglen + headlen);
3864 qc->tx.bytes += tmpbuf.data;
3865 time_sent = now_ms;
3866
3867 for (pkt = first_pkt; pkt; pkt = next_pkt) {
Frédéric Lécailleceb88b82023-02-20 14:43:55 +01003868 /* RFC 9000 14.1 Initial datagram size
3869 * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
3870 * Initial packets to at least the smallest allowed maximum datagram size of
3871 * 1200 bytes.
3872 */
Frédéric Lécaille12a815a2023-05-24 15:55:14 +02003873 qc->cntrs.sent_pkt++;
Frédéric Lécailleceb88b82023-02-20 14:43:55 +01003874 BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3875 (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
3876 dglen < QUIC_INITIAL_PACKET_MINLEN);
3877
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003878 pkt->time_sent = time_sent;
3879 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3880 pkt->pktns->tx.time_of_last_eliciting = time_sent;
3881 qc->path->ifae_pkts++;
3882 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
Frédéric Lécailled7215712023-03-24 18:13:37 +01003883 qc_idle_timer_rearm(qc, 0, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003884 }
3885 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3886 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3887 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01003888 qc_detach_th_ctx_list(qc, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003889
3890 /* RFC 9000 10.2. Immediate Close:
3891 * The closing and draining connection states exist to ensure
3892 * that connections close cleanly and that delayed or reordered
3893 * packets are properly discarded. These states SHOULD persist
3894 * for at least three times the current PTO interval...
3895 *
3896 * Rearm the idle timeout only one time when entering closing
3897 * state.
3898 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01003899 qc_idle_timer_do_rearm(qc, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003900 if (qc->timer_task) {
3901 task_destroy(qc->timer_task);
3902 qc->timer_task = NULL;
3903 }
3904 }
3905 qc->path->in_flight += pkt->in_flight_len;
3906 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3907 if (pkt->in_flight_len)
3908 qc_set_timer(qc);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01003909 TRACE_PROTO("TX pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003910 next_pkt = pkt->next;
3911 quic_tx_packet_refinc(pkt);
3912 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
3913 }
3914 }
3915
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003916 ret = 1;
3917leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003918 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3919
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003920 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003921}
3922
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003923/* Copy at <pos> position a stateless reset token depending on the
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003924 * <salt> salt input. This is the cluster secret which will be derived
3925 * as HKDF input secret to generate this token.
3926 * Return 1 if succeeded, 0 if not.
3927 */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003928static int quic_stateless_reset_token_cpy(unsigned char *pos, size_t len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003929 const unsigned char *salt, size_t saltlen)
3930{
3931 /* Input secret */
3932 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3933 size_t keylen = strlen(global.cluster_secret);
3934 /* Info */
3935 const unsigned char label[] = "stateless token";
3936 size_t labellen = sizeof label - 1;
3937 int ret;
3938
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003939 ret = quic_hkdf_extract_and_expand(EVP_sha256(), pos, len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003940 key, keylen, salt, saltlen, label, labellen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003941 return ret;
3942}
3943
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003944/* Initialize the stateless reset token attached to <conn_id> connection ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003945 * Returns 1 if succeeded, 0 if not.
3946 */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003947static int quic_stateless_reset_token_init(struct quic_connection_id *conn_id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003948{
3949 int ret;
3950
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003951 if (global.cluster_secret) {
3952 /* Output secret */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003953 unsigned char *token = conn_id->stateless_reset_token;
3954 size_t tokenlen = sizeof conn_id->stateless_reset_token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003955 /* Salt */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003956 const unsigned char *cid = conn_id->cid.data;
3957 size_t cidlen = conn_id->cid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003958
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02003959 ret = quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003960 }
3961 else {
3962 /* TODO: RAND_bytes() should be replaced */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02003963 ret = RAND_bytes(conn_id->stateless_reset_token,
3964 sizeof conn_id->stateless_reset_token) == 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003965 }
3966
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003967 return ret;
3968}
3969
Amaury Denoyelle1e959ad2023-04-13 17:34:56 +02003970/* Generate a CID directly derived from <orig> CID and <addr> address.
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003971 *
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003972 * Returns the derived CID.
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003973 */
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003974struct quic_cid quic_derive_cid(const struct quic_cid *orig,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003975 const struct sockaddr_storage *addr)
3976{
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003977 struct quic_cid cid;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003978 const struct sockaddr_in *in;
3979 const struct sockaddr_in6 *in6;
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003980 char *pos = trash.area;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003981 size_t idx = 0;
3982 uint64_t hash;
Amaury Denoyellec2a92642023-04-13 15:26:18 +02003983 int i;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003984
3985 /* Prepare buffer for hash using original CID first. */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003986 memcpy(pos, orig->data, orig->len);
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003987 idx += orig->len;
3988
3989 /* Concatenate client address. */
3990 switch (addr->ss_family) {
3991 case AF_INET:
3992 in = (struct sockaddr_in *)addr;
3993
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003994 memcpy(&pos[idx], &in->sin_addr, sizeof(in->sin_addr));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003995 idx += sizeof(in->sin_addr);
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02003996 memcpy(&pos[idx], &in->sin_port, sizeof(in->sin_port));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02003997 idx += sizeof(in->sin_port);
3998 break;
3999
4000 case AF_INET6:
4001 in6 = (struct sockaddr_in6 *)addr;
4002
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004003 memcpy(&pos[idx], &in6->sin6_addr, sizeof(in6->sin6_addr));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004004 idx += sizeof(in6->sin6_addr);
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004005 memcpy(&pos[idx], &in6->sin6_port, sizeof(in6->sin6_port));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004006 idx += sizeof(in6->sin6_port);
4007 break;
4008
4009 default:
4010 /* TODO to implement */
4011 ABORT_NOW();
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004012 }
4013
4014 /* Avoid similar values between multiple haproxy process. */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004015 memcpy(&pos[idx], boot_seed, sizeof(boot_seed));
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004016 idx += sizeof(boot_seed);
4017
4018 /* Hash the final buffer content. */
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004019 hash = XXH64(pos, idx, 0);
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004020
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004021 for (i = 0; i < sizeof(hash); ++i)
4022 cid.data[i] = hash >> ((sizeof(hash) * 7) - (8 * i));
4023 cid.len = sizeof(hash);
4024
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004025 return cid;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004026}
4027
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004028/* Retrieve the thread ID associated to QUIC connection ID <cid> of length
4029 * <cid_len>. CID may be not found on the CID tree because it is an ODCID. In
4030 * this case, it will derived using client address <cli_addr> as hash
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004031 * parameter. However, this is done only if <pos> points to an INITIAL or 0RTT
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004032 * packet of length <len>.
4033 *
4034 * Returns the thread ID or a negative error code.
4035 */
4036int quic_get_cid_tid(const unsigned char *cid, size_t cid_len,
4037 const struct sockaddr_storage *cli_addr,
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004038 unsigned char *pos, size_t len)
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004039{
4040 struct quic_cid_tree *tree;
4041 struct quic_connection_id *conn_id;
4042 struct ebmb_node *node;
4043
4044 tree = &quic_cid_trees[_quic_cid_tree_idx(cid)];
4045 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
4046 node = ebmb_lookup(&tree->root, cid, cid_len);
4047 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
4048
4049 if (!node) {
4050 struct quic_cid orig, derive_cid;
4051 struct quic_rx_packet pkt;
4052
Frédéric Lécaille81a02b52023-04-24 15:29:56 +02004053 if (!qc_parse_hd_form(&pkt, &pos, pos + len))
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004054 goto not_found;
4055
4056 if (pkt.type != QUIC_PACKET_TYPE_INITIAL &&
4057 pkt.type != QUIC_PACKET_TYPE_0RTT) {
4058 goto not_found;
4059 }
4060
4061 memcpy(orig.data, cid, cid_len);
4062 orig.len = cid_len;
4063 derive_cid = quic_derive_cid(&orig, cli_addr);
4064
4065 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
4066 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
4067 node = ebmb_lookup(&tree->root, cid, cid_len);
4068 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
4069 }
4070
4071 if (!node)
4072 goto not_found;
4073
4074 conn_id = ebmb_entry(node, struct quic_connection_id, node);
4075 return HA_ATOMIC_LOAD(&conn_id->tid);
4076
4077 not_found:
4078 return -1;
4079}
4080
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004081/* Allocate a new CID and attach it to <root> ebtree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004082 *
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004083 * If <orig> and <addr> params are non null, the new CID value is directly
4084 * derived from them. Else a random value is generated. The CID is then marked
4085 * with the current thread ID.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004086 *
4087 * Returns the new CID if succeeded, NULL if not.
4088 */
4089static struct quic_connection_id *new_quic_cid(struct eb_root *root,
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004090 struct quic_conn *qc,
4091 const struct quic_cid *orig,
4092 const struct sockaddr_storage *addr)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004093{
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004094 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004095
4096 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4097
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004098 /* Caller must set either none or both values. */
4099 BUG_ON(!!orig != !!addr);
4100
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004101 conn_id = pool_alloc(pool_head_quic_connection_id);
4102 if (!conn_id) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004103 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
4104 goto err;
4105 }
4106
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004107 conn_id->cid.len = QUIC_HAP_CID_LEN;
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004108
4109 if (!orig) {
4110 /* TODO: RAND_bytes() should be replaced */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004111 if (RAND_bytes(conn_id->cid.data, conn_id->cid.len) != 1) {
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004112 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
4113 goto err;
4114 }
Amaury Denoyelle162baaf2023-04-03 18:49:39 +02004115 }
4116 else {
4117 /* Derive the new CID value from original CID. */
Amaury Denoyellec2a92642023-04-13 15:26:18 +02004118 conn_id->cid = quic_derive_cid(orig, addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004119 }
4120
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004121 if (quic_stateless_reset_token_init(conn_id) != 1) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004122 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
4123 goto err;
4124 }
4125
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004126 conn_id->qc = qc;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004127 HA_ATOMIC_STORE(&conn_id->tid, tid);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004128
Amaury Denoyellef16ec342023-04-13 17:42:34 +02004129 conn_id->seq_num.key = qc ? qc->next_cid_seq_num++ : 0;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004130 conn_id->retire_prior_to = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004131 /* insert the allocated CID in the quic_conn tree */
Amaury Denoyellef16ec342023-04-13 17:42:34 +02004132 if (root)
4133 eb64_insert(root, &conn_id->seq_num);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004134
4135 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004136 return conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004137
4138 err:
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004139 pool_free(pool_head_quic_connection_id, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004140 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4141 return NULL;
4142}
4143
4144/* Build all the frames which must be sent just after the handshake have succeeded.
4145 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
4146 * a HANDSHAKE_DONE frame.
4147 * Return 1 if succeeded, 0 if not.
4148 */
4149static int quic_build_post_handshake_frames(struct quic_conn *qc)
4150{
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004151 int ret = 0, max;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004152 struct quic_enc_level *qel;
4153 struct quic_frame *frm, *frmbak;
4154 struct list frm_list = LIST_HEAD_INIT(frm_list);
4155 struct eb64_node *node;
4156
4157 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4158
4159 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4160 /* Only servers must send a HANDSHAKE_DONE frame. */
4161 if (qc_is_listener(qc)) {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004162 frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004163 if (!frm) {
4164 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4165 goto leave;
4166 }
4167
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004168 LIST_APPEND(&frm_list, &frm->list);
4169 }
4170
4171 /* Initialize <max> connection IDs minus one: there is
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004172 * already one connection ID used for the current connection. Also limit
4173 * the number of connection IDs sent to the peer to 4 (3 from this function
4174 * plus 1 for the current connection.
4175 * Note that active_connection_id_limit >= 2: this has been already checked
4176 * when receiving this parameter.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004177 */
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004178 max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3);
4179 while (max--) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004180 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004181
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01004182 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004183 if (!frm) {
4184 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
4185 goto err;
4186 }
4187
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004188 conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
4189 if (!conn_id) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004190 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004191 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
4192 goto err;
4193 }
4194
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004195 /* TODO To prevent CID tree locking, all CIDs created here
4196 * could be allocated at the same time as the first one.
4197 */
4198 quic_cid_insert(conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004199
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004200 quic_connection_id_to_frm_cpy(frm, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004201 LIST_APPEND(&frm_list, &frm->list);
4202 }
4203
4204 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004205 qc->flags &= ~QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004206
4207 ret = 1;
4208 leave:
4209 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4210 return ret;
4211
4212 err:
4213 /* free the frames */
4214 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01004215 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004216
Frédéric Lécailleb4c54712023-03-06 14:07:59 +01004217 /* The first CID sequence number value used to allocated CIDs by this function is 1,
4218 * 0 being the sequence number of the CID for this connection.
4219 */
4220 node = eb64_lookup_ge(&qc->cids, 1);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004221 while (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004222 struct quic_connection_id *conn_id;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004223
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004224 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
4225 if (conn_id->seq_num.key >= max)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004226 break;
4227
4228 node = eb64_next(node);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02004229 quic_cid_delete(conn_id);
4230
Amaury Denoyelle591e7982023-04-12 10:04:49 +02004231 eb64_delete(&conn_id->seq_num);
4232 pool_free(pool_head_quic_connection_id, conn_id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004233 }
4234 goto leave;
4235}
4236
4237/* Deallocate <l> list of ACK ranges. */
4238void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
4239{
4240 struct eb64_node *n;
4241 struct quic_arng_node *ar;
4242
4243 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4244
4245 n = eb64_first(&arngs->root);
4246 while (n) {
4247 struct eb64_node *next;
4248
4249 ar = eb64_entry(n, struct quic_arng_node, first);
4250 next = eb64_next(n);
4251 eb64_delete(n);
4252 pool_free(pool_head_quic_arng, ar);
4253 n = next;
4254 }
4255
4256 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4257}
4258
4259/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
4260 * descending order.
4261 */
4262static inline size_t sack_gap(struct quic_arng_node *p,
4263 struct quic_arng_node *q)
4264{
4265 return p->first.key - q->last - 2;
4266}
4267
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004268/* Set the encoded size of <arngs> QUIC ack ranges. */
4269static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
4270{
4271 struct eb64_node *node, *next;
4272 struct quic_arng_node *ar, *ar_next;
4273
4274 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4275
4276 node = eb64_last(&arngs->root);
4277 if (!node)
4278 goto leave;
4279
4280 ar = eb64_entry(node, struct quic_arng_node, first);
4281 arngs->enc_sz = quic_int_getsize(ar->last) +
4282 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
4283
4284 while ((next = eb64_prev(node))) {
4285 ar_next = eb64_entry(next, struct quic_arng_node, first);
4286 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
4287 quic_int_getsize(ar_next->last - ar_next->first.key);
4288 node = next;
4289 ar = eb64_entry(node, struct quic_arng_node, first);
4290 }
4291
4292 leave:
4293 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4294}
4295
4296/* Insert <ar> ack range into <argns> tree of ack ranges.
4297 * Returns the ack range node which has been inserted if succeeded, NULL if not.
4298 */
4299static inline
4300struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
4301 struct quic_arngs *arngs,
4302 struct quic_arng *ar)
4303{
4304 struct quic_arng_node *new_ar;
4305
4306 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4307
Frédéric Lécaille0ed94032023-04-17 14:10:14 +02004308 if (arngs->sz >= QUIC_MAX_ACK_RANGES) {
4309 struct eb64_node *last;
4310
4311 last = eb64_last(&arngs->root);
4312 BUG_ON(last == NULL);
4313 eb64_delete(last);
4314 pool_free(pool_head_quic_arng, last);
4315 arngs->sz--;
4316 }
4317
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004318 new_ar = pool_alloc(pool_head_quic_arng);
4319 if (!new_ar) {
4320 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
4321 goto leave;
4322 }
4323
4324 new_ar->first.key = ar->first;
4325 new_ar->last = ar->last;
4326 eb64_insert(&arngs->root, &new_ar->first);
4327 arngs->sz++;
4328
4329 leave:
4330 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4331 return new_ar;
4332}
4333
4334/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
4335 * Note that this function computes the number of bytes required to encode
4336 * this tree of ACK ranges in descending order.
4337 *
4338 * Descending order
4339 * ------------->
4340 * range1 range2
4341 * ..........|--------|..............|--------|
4342 * ^ ^ ^ ^
4343 * | | | |
4344 * last1 first1 last2 first2
4345 * ..........+--------+--------------+--------+......
4346 * diff1 gap12 diff2
4347 *
4348 * To encode the previous list of ranges we must encode integers as follows in
4349 * descending order:
4350 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
4351 * with diff1 = last1 - first1
4352 * diff2 = last2 - first2
4353 * gap12 = first1 - last2 - 2 (>= 0)
4354 *
4355
4356returns 0 on error
4357
4358 */
4359int quic_update_ack_ranges_list(struct quic_conn *qc,
4360 struct quic_arngs *arngs,
4361 struct quic_arng *ar)
4362{
4363 int ret = 0;
4364 struct eb64_node *le;
4365 struct quic_arng_node *new_node;
4366 struct eb64_node *new;
4367
4368 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4369
4370 new = NULL;
4371 if (eb_is_empty(&arngs->root)) {
4372 new_node = quic_insert_new_range(qc, arngs, ar);
4373 if (new_node)
4374 ret = 1;
4375
4376 goto leave;
4377 }
4378
4379 le = eb64_lookup_le(&arngs->root, ar->first);
4380 if (!le) {
4381 new_node = quic_insert_new_range(qc, arngs, ar);
4382 if (!new_node)
4383 goto leave;
4384
4385 new = &new_node->first;
4386 }
4387 else {
4388 struct quic_arng_node *le_ar =
4389 eb64_entry(le, struct quic_arng_node, first);
4390
4391 /* Already existing range */
4392 if (le_ar->last >= ar->last) {
4393 ret = 1;
4394 }
4395 else if (le_ar->last + 1 >= ar->first) {
4396 le_ar->last = ar->last;
4397 new = le;
4398 new_node = le_ar;
4399 }
4400 else {
4401 new_node = quic_insert_new_range(qc, arngs, ar);
4402 if (!new_node)
4403 goto leave;
4404
4405 new = &new_node->first;
4406 }
4407 }
4408
4409 /* Verify that the new inserted node does not overlap the nodes
4410 * which follow it.
4411 */
4412 if (new) {
4413 struct eb64_node *next;
4414 struct quic_arng_node *next_node;
4415
4416 while ((next = eb64_next(new))) {
4417 next_node =
4418 eb64_entry(next, struct quic_arng_node, first);
4419 if (new_node->last + 1 < next_node->first.key)
4420 break;
4421
4422 if (next_node->last > new_node->last)
4423 new_node->last = next_node->last;
4424 eb64_delete(next);
4425 pool_free(pool_head_quic_arng, next_node);
4426 /* Decrement the size of these ranges. */
4427 arngs->sz--;
4428 }
4429 }
4430
4431 ret = 1;
4432 leave:
4433 quic_arngs_set_enc_sz(qc, arngs);
4434 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
4435 return ret;
4436}
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004437
4438/* Detect the value of the spin bit to be used. */
4439static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt,
4440 struct quic_enc_level *qel)
4441{
4442 uint64_t largest_pn = qel->pktns->rx.largest_pn;
4443
4444 if (qel != &qc->els[QUIC_TLS_ENC_LEVEL_APP] || largest_pn == -1 ||
4445 pkt->pn <= largest_pn)
4446 return;
4447
4448 if (qc_is_listener(qc)) {
4449 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4450 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4451 else
4452 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4453 }
4454 else {
4455 if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
4456 qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
4457 else
4458 qc->flags |= QUIC_FL_CONN_SPIN_BIT;
4459 }
4460}
4461
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004462/* Remove the header protection of packets at <el> encryption level.
4463 * Always succeeds.
4464 */
4465static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
4466{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004467 struct quic_rx_packet *pqpkt, *pkttmp;
4468 struct quic_enc_level *app_qel;
4469
4470 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
4471 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4472 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
4473 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004474 TRACE_PROTO("RX hp not removed (handshake not completed)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004475 QUIC_EV_CONN_ELRMHP, qc);
4476 goto out;
4477 }
Frédéric Lécaille72027782023-02-22 16:20:09 +01004478
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004479 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01004480 struct quic_tls_ctx *tls_ctx;
4481
4482 tls_ctx = qc_select_tls_ctx(qc, el, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004483 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
4484 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004485 TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004486 }
4487 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01004488 qc_handle_spin_bit(qc, pqpkt, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004489 /* The AAD includes the packet number field */
4490 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
4491 /* Store the packet into the tree of packets to decrypt. */
4492 pqpkt->pn_node.key = pqpkt->pn;
4493 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
4494 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004495 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004496 }
4497 LIST_DELETE(&pqpkt->list);
4498 quic_rx_packet_refdec(pqpkt);
4499 }
4500
4501 out:
4502 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
4503}
4504
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004505/* Process all the CRYPTO frame at <el> encryption level. This is the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004506 * responsibility of the called to ensure there exists a CRYPTO data
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004507 * stream for this level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004508 * Return 1 if succeeded, 0 if not.
4509 */
4510static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
4511 struct quic_enc_level *el,
4512 struct ssl_sock_ctx *ctx)
4513{
4514 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004515 struct ncbuf *ncbuf;
4516 struct quic_cstream *cstream = el->cstream;
4517 ncb_sz_t data;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004518
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004519 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004520
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004521 BUG_ON(!cstream);
4522 ncbuf = &cstream->rx.ncbuf;
4523 if (ncb_is_null(ncbuf))
4524 goto done;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004525
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004526 /* TODO not working if buffer is wrapping */
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004527 while ((data = ncb_data(ncbuf, 0))) {
4528 const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004529
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004530 if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004531 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004532
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004533 cstream->rx.offset += data;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004534 TRACE_DEVEL("buffered crypto data were provided to TLS stack",
4535 QUIC_EV_CONN_PHPKTS, qc, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004536 }
4537
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004538 done:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004539 ret = 1;
4540 leave:
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004541 if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
4542 TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004543 quic_free_ncbuf(ncbuf);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004544 }
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004545 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004546 return ret;
4547}
4548
4549/* Process all the packets at <el> and <next_el> encryption level.
4550 * This is the caller responsibility to check that <cur_el> is different of <next_el>
4551 * as pointer value.
4552 * Return 1 if succeeded, 0 if not.
4553 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004554int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el,
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004555 struct quic_enc_level *next_el)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004556{
4557 int ret = 0;
4558 struct eb64_node *node;
4559 int64_t largest_pn = -1;
4560 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004561 struct quic_enc_level *qel = cur_el;
4562
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004563 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004564 qel = cur_el;
4565 next_tel:
4566 if (!qel)
4567 goto out;
4568
4569 node = eb64_first(&qel->rx.pkts);
4570 while (node) {
4571 struct quic_rx_packet *pkt;
4572
4573 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
4574 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004575 qc, pkt, NULL, qc->xprt_ctx->ssl);
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01004576 if (!qc_pkt_decrypt(qc, qel, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004577 /* Drop the packet */
4578 TRACE_ERROR("packet decryption failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004579 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004580 }
4581 else {
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004582 if (!qc_parse_pkt_frms(qc, pkt, qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004583 /* Drop the packet */
4584 TRACE_ERROR("packet parsing failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004585 QUIC_EV_CONN_RXPKT, qc, pkt);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02004586 qc->cntrs.dropped_parsing++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004587 }
4588 else {
4589 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
4590
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01004591 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
Frédéric Lécailleb5efe792023-04-14 09:56:17 +02004592 int arm_ack_timer =
4593 qc->state >= QUIC_HS_ST_COMPLETE &&
4594 qel->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT];
4595
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004596 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
4597 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécailleb5efe792023-04-14 09:56:17 +02004598 qc_idle_timer_rearm(qc, 1, arm_ack_timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004599 }
4600 if (pkt->pn > largest_pn) {
4601 largest_pn = pkt->pn;
4602 largest_pn_time_received = pkt->time_received;
4603 }
4604 /* Update the list of ranges to acknowledge. */
4605 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
4606 TRACE_ERROR("Could not update ack range list",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004607 QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004608 }
4609 }
4610 node = eb64_next(node);
4611 eb64_delete(&pkt->pn_node);
4612 quic_rx_packet_refdec(pkt);
4613 }
4614
4615 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
4616 /* Update the largest packet number. */
4617 qel->pktns->rx.largest_pn = largest_pn;
4618 /* Update the largest acknowledged packet timestamps */
4619 qel->pktns->rx.largest_time_received = largest_pn_time_received;
4620 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
4621 }
4622
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004623 if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004624 // trace already emitted by function above
4625 goto leave;
4626 }
4627
4628 if (qel == cur_el) {
4629 BUG_ON(qel == next_el);
4630 qel = next_el;
4631 largest_pn = -1;
4632 goto next_tel;
4633 }
4634
4635 out:
4636 ret = 1;
4637 leave:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004638 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004639 return ret;
4640}
4641
4642/* Check if it's possible to remove header protection for packets related to
4643 * encryption level <qel>. If <qel> is NULL, assume it's false.
4644 *
4645 * Return true if the operation is possible else false.
4646 */
4647static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
4648{
4649 int ret = 0;
4650 enum quic_tls_enc_level tel;
4651
4652 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
4653
4654 if (!qel)
4655 goto cant_rm_hp;
4656
4657 tel = ssl_to_quic_enc_level(qel->level);
4658
4659 /* check if tls secrets are available */
4660 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004661 TRACE_PROTO("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004662 goto cant_rm_hp;
4663 }
4664
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004665 if (!quic_tls_has_rx_sec(qel)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004666 TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004667 goto cant_rm_hp;
4668 }
4669
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004670 if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004671 TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004672 goto cant_rm_hp;
4673 }
4674
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004675 /* check if the connection layer is ready before using app level */
4676 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
4677 qc->mux_state == QC_MUX_NULL) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01004678 TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004679 goto cant_rm_hp;
4680 }
4681
4682 ret = 1;
4683 cant_rm_hp:
4684 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
4685 return ret;
4686}
4687
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004688/* Flush txbuf for <qc> connection. This must be called prior to a packet
4689 * preparation when txbuf contains older data. A send will be conducted for
4690 * these data.
4691 *
4692 * Returns 1 on success : buffer is empty and can be use for packet
4693 * preparation. On error 0 is returned.
4694 */
4695static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
4696{
4697 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4698
4699 /* This operation can only be conducted if txbuf is not empty. This
4700 * case only happens for connection with their owned socket due to an
4701 * older transient sendto() error.
4702 */
4703 BUG_ON(!qc_test_fd(qc));
4704
4705 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4706 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4707 qc_txb_release(qc);
4708 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4709 return 0;
4710 }
4711
4712 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4713 return 1;
4714}
4715
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004716/* Try to send application frames from list <frms> on connection <qc>.
4717 *
4718 * Use qc_send_app_probing wrapper when probing with old data.
4719 *
4720 * Returns 1 on success. Some data might not have been sent due to congestion,
4721 * in this case they are left in <frms> input list. The caller may subscribe on
4722 * quic-conn to retry later.
4723 *
4724 * Returns 0 on critical error.
4725 * TODO review and classify more distinctly transient from definitive errors to
4726 * allow callers to properly handle it.
4727 */
4728static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
4729{
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004730 int status = 0, ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004731 struct buffer *buf;
4732
4733 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4734
4735 buf = qc_txb_alloc(qc);
4736 if (!buf) {
4737 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004738 goto err;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004739 }
4740
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004741 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4742 goto err;
4743
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004744 /* Prepare and send packets until we could not further prepare packets. */
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004745 do {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004746 /* Currently buf cannot be non-empty at this stage. Even if a
4747 * previous sendto() has failed it is emptied to simulate
4748 * packet emission and rely on QUIC lost detection to try to
4749 * emit it.
4750 */
4751 BUG_ON_HOT(b_data(buf));
4752 b_reset(buf);
4753
4754 ret = qc_prep_app_pkts(qc, buf, frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004755
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004756 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004757 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4758 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004759 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004760 }
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004761 } while (ret > 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004762
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004763 qc_txb_release(qc);
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02004764 if (ret < 0)
4765 goto err;
4766
4767 status = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004768 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4769 return status;
4770
4771 err:
Amaury Denoyelle37333862023-02-28 11:53:48 +01004772 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4773 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004774}
4775
4776/* Try to send application frames from list <frms> on connection <qc>. Use this
4777 * function when probing is required.
4778 *
4779 * Returns the result from qc_send_app_pkts function.
4780 */
4781static forceinline int qc_send_app_probing(struct quic_conn *qc,
4782 struct list *frms)
4783{
4784 int ret;
4785
4786 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4787
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02004788 TRACE_PROTO("preparing old data (probing)", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004789 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4790 ret = qc_send_app_pkts(qc, frms);
4791 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
4792
4793 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4794 return ret;
4795}
4796
4797/* Try to send application frames from list <frms> on connection <qc>. This
4798 * function is provided for MUX upper layer usage only.
4799 *
4800 * Returns the result from qc_send_app_pkts function.
4801 */
4802int qc_send_mux(struct quic_conn *qc, struct list *frms)
4803{
4804 int ret;
4805
4806 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4807 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
4808
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02004809 if (qc->conn->flags & CO_FL_SOCK_WR_SH) {
4810 qc->conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH;
4811 TRACE_DEVEL("connection on error", QUIC_EV_CONN_TXPKT, qc);
4812 return 0;
4813 }
4814
Amaury Denoyelle1304d192023-04-11 16:46:03 +02004815 /* Try to send post handshake frames first unless on 0-RTT. */
4816 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) &&
4817 qc->state >= QUIC_HS_ST_COMPLETE) {
4818 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4819 quic_build_post_handshake_frames(qc);
4820 qc_send_app_pkts(qc, &qel->pktns->tx.frms);
4821 }
4822
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004823 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4824 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
4825 ret = qc_send_app_pkts(qc, frms);
4826 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
4827
4828 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4829 return ret;
4830}
4831
4832/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4833 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
4834 * QUIC connection. <old_data> is used as boolean to send data already sent but
4835 * not already acknowledged (in flight).
4836 * Returns 1 if succeeded, 0 if not.
4837 */
4838int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4839 enum quic_tls_enc_level tel, struct list *tel_frms,
4840 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4841{
4842 int ret, status = 0;
4843 struct buffer *buf = qc_txb_alloc(qc);
4844
4845 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4846
4847 if (!buf) {
4848 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4849 goto leave;
4850 }
4851
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004852 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4853 goto out;
4854
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004855 /* Currently buf cannot be non-empty at this stage. Even if a previous
4856 * sendto() has failed it is emptied to simulate packet emission and
4857 * rely on QUIC lost detection to try to emit it.
4858 */
4859 BUG_ON_HOT(b_data(buf));
4860 b_reset(buf);
4861
4862 if (old_data) {
4863 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
4864 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4865 }
4866
4867 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004868 if (ret == -1) {
4869 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004870 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004871 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004872
Amaury Denoyelle37333862023-02-28 11:53:48 +01004873 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01004874 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4875 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004876 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004877 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004878
Amaury Denoyelle37333862023-02-28 11:53:48 +01004879 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004880 status = 1;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004881
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004882 out:
4883 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
4884 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004885 leave:
4886 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4887 return status;
4888}
4889
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004890/* Retransmit up to two datagrams depending on packet number space.
4891 * Return 0 when failed, 0 if not.
4892 */
4893static int qc_dgrams_retransmit(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004894{
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004895 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004896 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4897 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4898 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4899
4900 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4901
4902 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004903 int i;
4904
4905 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4906 struct list ifrms = LIST_HEAD_INIT(ifrms);
4907 struct list hfrms = LIST_HEAD_INIT(hfrms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004908
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004909 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
4910 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4911 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
4912 if (!LIST_ISEMPTY(&ifrms)) {
4913 iqel->pktns->tx.pto_probe = 1;
4914 if (!LIST_ISEMPTY(&hfrms))
4915 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004916 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4917 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms))
4918 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004919 /* Put back unsent frames in their packet number spaces */
4920 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4921 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
4922 }
Frédéric Lécailleec937212023-03-03 17:34:41 +01004923 else {
4924 if (!(qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
4925 iqel->pktns->tx.pto_probe = 1;
4926 if (!qc_send_hdshk_pkts(qc, 0, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4927 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4928 goto leave;
4929 }
4930 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004931 }
4932 TRACE_STATE("no more need to probe Initial packet number space",
4933 QUIC_EV_CONN_TXPKT, qc);
4934 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004935 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004936 }
4937 else {
4938 int i;
4939
4940 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004941 hqel->pktns->tx.pto_probe = 0;
4942 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
Frédéric Lécaille7b5d9b12022-11-28 17:21:45 +01004943 struct list frms1 = LIST_HEAD_INIT(frms1);
4944
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004945 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
4946 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4947 if (!LIST_ISEMPTY(&frms1)) {
4948 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004949 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4950 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4951 goto leave;
4952
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004953 /* Put back unsent frames into their packet number spaces */
4954 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
4955 }
4956 }
4957 TRACE_STATE("no more need to probe Handshake packet number space",
4958 QUIC_EV_CONN_TXPKT, qc);
4959 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4960 }
4961 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4962 struct list frms2 = LIST_HEAD_INIT(frms2);
4963 struct list frms1 = LIST_HEAD_INIT(frms1);
4964
4965 aqel->pktns->tx.pto_probe = 0;
4966 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4967 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4968 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4969 if (!LIST_ISEMPTY(&frms1)) {
4970 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillec6bec2a2023-04-24 11:20:32 +02004971 if (!qc_send_app_probing(qc, &frms1)) {
Frédéric Lécaillee66d67a2023-04-24 12:05:46 +02004972 qc_free_frm_list(&frms2);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004973 goto leave;
Frédéric Lécaillec6bec2a2023-04-24 11:20:32 +02004974 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004975
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004976 /* Put back unsent frames into their packet number spaces */
4977 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
4978 }
4979 if (!LIST_ISEMPTY(&frms2)) {
4980 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004981 if (!qc_send_app_probing(qc, &frms2))
4982 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004983 /* Put back unsent frames into their packet number spaces */
4984 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
4985 }
4986 TRACE_STATE("no more need to probe 01RTT packet number space",
4987 QUIC_EV_CONN_TXPKT, qc);
4988 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4989 }
4990 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004991
4992 ret = 1;
4993 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004994 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004995 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004996}
4997
4998/* QUIC connection packet handler task (post handshake) */
4999struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
5000{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005001 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005002 struct quic_enc_level *qel;
5003
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005004 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02005005
5006 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005007 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
5008
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01005009 if (qc_test_fd(qc))
5010 qc_rcv_buf(qc);
5011
Amaury Denoyelle1304d192023-04-11 16:46:03 +02005012 /* Prepare post-handshake frames
5013 * - after connection is instantiated (accept is done)
5014 * - handshake state is completed (may not be the case here in 0-RTT)
5015 */
5016 if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) && qc->conn &&
5017 qc->state >= QUIC_HS_ST_COMPLETE) {
5018 quic_build_post_handshake_frames(qc);
5019 }
5020
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005021 /* Retranmissions */
5022 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
5023 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
5024 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005025 if (!qc_dgrams_retransmit(qc))
5026 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005027 }
5028
5029 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
5030 qc_rm_hp_pkts(qc, qel);
5031
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005032 if (!qc_treat_rx_pkts(qc, qel, NULL)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005033 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
5034 goto out;
5035 }
5036
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005037 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5038 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
5039 goto out;
5040 }
5041
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005042 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5043 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
5044 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
5045 goto out;
5046 }
5047
5048 /* XXX TODO: how to limit the list frames to send */
5049 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
5050 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
5051 goto out;
5052 }
5053
5054 out:
5055 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
5056 return t;
5057}
5058
5059/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
5060static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
5061{
5062 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
5063 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
5064 qel->pktns->tx.pto_probe ||
5065 !LIST_ISEMPTY(&qel->pktns->tx.frms);
5066}
5067
5068/* QUIC connection packet handler task. */
5069struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
5070{
5071 int ret, ssl_err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005072 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005073 enum quic_tls_enc_level tel, next_tel;
5074 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005075 /* Early-data encryption level */
5076 struct quic_enc_level *eqel;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005077 struct buffer *buf = NULL;
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005078 int st, zero_rtt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005079
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005080 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02005081
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005082 eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005083 st = qc->state;
5084 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
5085
5086 /* Retranmissions */
5087 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
5088 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
5089 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01005090 if (!qc_dgrams_retransmit(qc))
5091 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005092 }
5093
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005094 ssl_err = SSL_ERROR_NONE;
5095 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005096 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005097 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01005098
5099 if (qc_test_fd(qc))
5100 qc_rcv_buf(qc);
5101
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005102 if (st >= QUIC_HS_ST_COMPLETE &&
5103 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
5104 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
5105 /* There may be remaining Handshake packets to treat and acknowledge. */
5106 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
5107 next_tel = QUIC_TLS_ENC_LEVEL_APP;
5108 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005109 else if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, zero_rtt))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005110 goto out;
5111
5112 qel = &qc->els[tel];
5113 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
5114
5115 next_level:
5116 /* Treat packets waiting for header packet protection decryption */
5117 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
5118 qc_rm_hp_pkts(qc, qel);
5119
Frédéric Lécailleb3562a32023-02-25 11:27:34 +01005120 if (!qc_treat_rx_pkts(qc, qel, next_qel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005121 goto out;
5122
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01005123 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
5124 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
5125 goto out;
5126 }
5127
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005128 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
5129 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
5130 goto out;
5131
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005132 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005133 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005134 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
5135 if (next_qel && next_qel == eqel && zero_rtt) {
5136 TRACE_DEVEL("select 0RTT as next encryption level",
5137 QUIC_EV_CONN_PHPKTS, qc);
5138 qel = next_qel;
5139 next_qel = NULL;
5140 goto next_level;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005141 }
5142
5143 st = qc->state;
5144 if (st >= QUIC_HS_ST_COMPLETE) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005145 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
5146 QUIC_FL_TLS_SECRETS_DCD)) {
5147 /* Discard the Handshake keys. */
5148 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5149 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
5150 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
5151 qc_set_timer(qc);
5152 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
5153 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
5154 }
5155
5156 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
5157 /* There may be remaining handshake to build (acks) */
5158 st = QUIC_HS_ST_SERVER_HANDSHAKE;
5159 }
5160 }
5161
5162 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
5163 * be considered.
5164 */
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02005165 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005166 goto out;
5167
5168 if (!qc_need_sending(qc, qel) &&
5169 (!next_qel || !qc_need_sending(qc, next_qel))) {
5170 goto skip_send;
5171 }
5172
5173 buf = qc_txb_alloc(qc);
5174 if (!buf)
5175 goto out;
5176
Amaury Denoyelle147862d2023-02-28 15:10:00 +01005177 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
5178 goto skip_send;
5179
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005180 /* Currently buf cannot be non-empty at this stage. Even if a previous
5181 * sendto() has failed it is emptied to simulate packet emission and
5182 * rely on QUIC lost detection to try to emit it.
5183 */
5184 BUG_ON_HOT(b_data(buf));
5185 b_reset(buf);
5186
5187 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
5188 next_tel, &qc->els[next_tel].pktns->tx.frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01005189 if (ret == -1) {
5190 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005191 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005192 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005193
Amaury Denoyelle37333862023-02-28 11:53:48 +01005194 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
Amaury Denoyellee1a0ee32023-02-28 15:11:09 +01005195 if (qc->flags & QUIC_FL_CONN_TO_KILL)
5196 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005197 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01005198 }
5199
5200 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005201
5202 skip_send:
5203 /* Check if there is something to do for the next level.
5204 */
5205 if (next_qel && next_qel != qel &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005206 quic_tls_has_rx_sec(next_qel) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005207 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
5208 qel = next_qel;
5209 next_qel = NULL;
5210 goto next_level;
5211 }
5212
5213 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005214 TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
5215 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005216 return t;
5217}
5218
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005219/* Release the memory allocated for <cs> CRYPTO stream */
5220void quic_cstream_free(struct quic_cstream *cs)
5221{
5222 if (!cs) {
5223 /* This is the case for ORTT encryption level */
5224 return;
5225 }
5226
Amaury Denoyellebc174b22022-11-17 10:12:52 +01005227 quic_free_ncbuf(&cs->rx.ncbuf);
5228
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005229 qc_stream_desc_release(cs->desc);
5230 pool_free(pool_head_quic_cstream, cs);
5231}
5232
5233/* Allocate a new QUIC stream for <qc>.
5234 * Return it if succeeded, NULL if not.
5235 */
5236struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
5237{
5238 struct quic_cstream *cs, *ret_cs = NULL;
5239
5240 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5241 cs = pool_alloc(pool_head_quic_cstream);
5242 if (!cs) {
5243 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5244 goto leave;
5245 }
5246
5247 cs->rx.offset = 0;
5248 cs->rx.ncbuf = NCBUF_NULL;
5249 cs->rx.offset = 0;
5250
5251 cs->tx.offset = 0;
5252 cs->tx.sent_offset = 0;
5253 cs->tx.buf = BUF_NULL;
5254 cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
5255 if (!cs->desc) {
5256 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
5257 goto err;
5258 }
5259
5260 ret_cs = cs;
5261 leave:
5262 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5263 return ret_cs;
5264
5265 err:
5266 pool_free(pool_head_quic_cstream, cs);
5267 goto leave;
5268}
5269
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005270/* Uninitialize <qel> QUIC encryption level. Never fails. */
5271static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
5272{
5273 int i;
5274
5275 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5276
5277 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
5278 if (qel->tx.crypto.bufs[i]) {
5279 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
5280 qel->tx.crypto.bufs[i] = NULL;
5281 }
5282 }
5283 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005284 quic_cstream_free(qel->cstream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005285
5286 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5287}
5288
5289/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
5290 * connection allocating everything needed.
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005291 *
5292 * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use
5293 * quic_conn_enc_level_uninit() to cleanup partially allocated content.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005294 */
5295static int quic_conn_enc_level_init(struct quic_conn *qc,
5296 enum quic_tls_enc_level level)
5297{
5298 int ret = 0;
5299 struct quic_enc_level *qel;
5300
5301 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5302
5303 qel = &qc->els[level];
5304 qel->level = quic_to_ssl_enc_level(level);
5305 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
5306 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
5307 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
5308 qel->tls_ctx.flags = 0;
5309
5310 qel->rx.pkts = EB_ROOT;
5311 LIST_INIT(&qel->rx.pqpkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005312
5313 /* Allocate only one buffer. */
5314 /* TODO: use a pool */
5315 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
5316 if (!qel->tx.crypto.bufs)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005317 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005318
5319 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
5320 if (!qel->tx.crypto.bufs[0])
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005321 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005322
5323 qel->tx.crypto.bufs[0]->sz = 0;
5324 qel->tx.crypto.nb_buf = 1;
5325
5326 qel->tx.crypto.sz = 0;
5327 qel->tx.crypto.offset = 0;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005328 /* No CRYPTO data for early data TLS encryption level */
5329 if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA)
5330 qel->cstream = NULL;
5331 else {
5332 qel->cstream = quic_cstream_new(qc);
5333 if (!qel->cstream)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005334 goto leave;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02005335 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005336
5337 ret = 1;
5338 leave:
5339 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5340 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005341}
5342
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005343/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
5344 * This is not the case if the remote peer address is not validated and if
5345 * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
5346 */
5347static int qc_may_probe_ipktns(struct quic_conn *qc)
5348{
5349 return quic_peer_validated_addr(qc) ||
5350 (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
5351}
5352
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005353/* Callback called upon loss detection and PTO timer expirations. */
5354struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
5355{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005356 struct quic_conn *qc = ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005357 struct quic_pktns *pktns;
5358
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005359 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
5360 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005361 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005362
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005363 task->expire = TICK_ETERNITY;
5364 pktns = quic_loss_pktns(qc);
Frédéric Lécailled21c6282023-04-24 11:26:06 +02005365
5366 if (qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL)) {
5367 TRACE_PROTO("cancelled action (draining state)", QUIC_EV_CONN_PTIMER, qc);
5368 task = NULL;
5369 goto out;
5370 }
5371
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005372 if (tick_isset(pktns->tx.loss_time)) {
5373 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
5374
5375 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
5376 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005377 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01005378 if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
5379 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005380 goto out;
5381 }
5382
5383 if (qc->path->in_flight) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +01005384 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
Frédéric Lécaille68737312023-04-07 16:28:46 +02005385 if (!pktns->tx.in_flight) {
5386 TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
5387 goto out;
5388 }
5389
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005390 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
5391 if (qc_may_probe_ipktns(qc)) {
5392 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5393 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5394 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5395 }
5396 else {
5397 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5398 }
5399 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
5400 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5401 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5402 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5403 }
Frédéric Lécaillee25fce02023-03-20 17:23:19 +01005404 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005405 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
5406 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
5407 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5408 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
5409 if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005410 if (qc_may_probe_ipktns(qc)) {
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005411 qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005412 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5413 }
5414 else {
5415 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
5416 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005417 }
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005418 }
5419 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01005420 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
Amaury Denoyelle2a19b6e2023-03-20 18:29:36 +01005421 /* Wake up upper layer if waiting to send new data. */
5422 if (!qc_notify_send(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005423 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01005424 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
5425 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005426 }
5427 }
5428 }
5429 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
5430 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
5431 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
5432
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005433 if (quic_tls_has_tx_sec(hel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005434 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02005435 if (quic_tls_has_tx_sec(iel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005436 iel->pktns->tx.pto_probe = 1;
5437 }
5438
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005439 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005440 qc->path->loss.pto_count++;
5441
5442 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01005443 TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
5444 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005445
5446 return task;
5447}
5448
5449/* Parse the Retry token from buffer <token> with <end> a pointer to
5450 * one byte past the end of this buffer. This will extract the ODCID
5451 * which will be stored into <odcid>
5452 *
5453 * Returns 0 on success else non-zero.
5454 */
5455static int parse_retry_token(struct quic_conn *qc,
5456 const unsigned char *token, const unsigned char *end,
5457 struct quic_cid *odcid)
5458{
5459 int ret = 0;
5460 uint64_t odcid_len;
5461 uint32_t timestamp;
5462
5463 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5464
5465 if (!quic_dec_int(&odcid_len, &token, end)) {
5466 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
5467 goto leave;
5468 }
5469
5470 /* RFC 9000 7.2. Negotiating Connection IDs:
5471 * When an Initial packet is sent by a client that has not previously
5472 * received an Initial or Retry packet from the server, the client
5473 * populates the Destination Connection ID field with an unpredictable
5474 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5475 */
5476 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
5477 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
5478 goto leave;
5479 }
5480
5481 if (end - token < odcid_len + sizeof timestamp) {
5482 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
5483 goto leave;
5484 }
5485
5486 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécailled0742132023-04-19 17:31:28 +02005487 if (tick_is_expired(tick_add(timestamp, MS_TO_TICKS(QUIC_RETRY_DURATION_MS)), now_ms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005488 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
5489 goto leave;
5490 }
5491
5492 ret = 1;
5493 memcpy(odcid->data, token, odcid_len);
5494 odcid->len = odcid_len;
5495 leave:
5496 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5497 return !ret;
5498}
5499
5500/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
5501 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
5502 * for QUIC servers (or haproxy listeners).
Frédéric Lécaille8a586582023-06-26 10:39:56 +02005503 * <dcid> is the destination connection ID, <scid> is the source connection ID.
5504 * This latter <scid> CID as the same value on the wire as the one for <conn_id>
5505 * which is the first CID of this connection but a different internal representation used to build
5506 * NEW_CONNECTION_ID frames. This is the responsability of the caller to insert
5507 * <conn_id> in the CIDs tree for this connection (qc->cids).
5508 * <token> is the token found to be used for this connection with <token_len> as
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005509 * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005510 * Returns the connection if succeeded, NULL if not.
5511 */
5512static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
5513 struct quic_cid *dcid, struct quic_cid *scid,
5514 const struct quic_cid *token_odcid,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005515 struct quic_connection_id *conn_id,
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005516 struct sockaddr_storage *local_addr,
5517 struct sockaddr_storage *peer_addr,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005518 int server, int token, void *owner)
5519{
5520 int i;
5521 struct quic_conn *qc;
5522 /* Initial CID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005523 char *buf_area = NULL;
5524 struct listener *l = NULL;
5525 struct quic_cc_algo *cc_algo = NULL;
5526 struct quic_tls_ctx *ictx;
5527 TRACE_ENTER(QUIC_EV_CONN_INIT);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005528 /* TODO replace pool_zalloc by pool_alloc(). This requires special care
5529 * to properly initialized internal quic_conn members to safely use
5530 * quic_conn_release() on alloc failure.
5531 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005532 qc = pool_zalloc(pool_head_quic_conn);
5533 if (!qc) {
5534 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
5535 goto err;
5536 }
5537
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005538 /* Initialize in priority qc members required for a safe dealloc. */
5539
5540 /* required to use MTLIST_IN_LIST */
5541 MT_LIST_INIT(&qc->accept_list);
5542
5543 LIST_INIT(&qc->rx.pkt_list);
5544
Amaury Denoyelle42448332022-12-12 11:24:05 +01005545 qc_init_fd(qc);
5546
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005547 LIST_INIT(&qc->back_refs);
Amaury Denoyelled537ca72023-04-19 10:45:40 +02005548 LIST_INIT(&qc->el_th_ctx);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005549
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005550 /* Now proceeds to allocation of qc members. */
5551
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005552 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
5553 if (!buf_area) {
5554 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
5555 goto err;
5556 }
5557
5558 qc->cids = EB_ROOT;
5559 /* QUIC Server (or listener). */
5560 if (server) {
5561 struct proxy *prx;
5562
5563 l = owner;
5564 prx = l->bind_conf->frontend;
5565 cc_algo = l->bind_conf->quic_cc_algo;
5566
5567 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
5568 &quic_stats_module);
5569 qc->flags |= QUIC_FL_CONN_LISTENER;
5570 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005571 /* Copy the client original DCID. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005572 qc->odcid.len = dcid->len;
Amaury Denoyelle15adc4c2023-04-05 09:50:17 +02005573 memcpy(qc->odcid.data, dcid->data, dcid->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005574
5575 /* copy the packet SCID to reuse it as DCID for sending */
5576 if (scid->len)
5577 memcpy(qc->dcid.data, scid->data, scid->len);
5578 qc->dcid.len = scid->len;
5579 qc->tx.buf = BUF_NULL;
5580 qc->li = l;
5581 }
5582 /* QUIC Client (outgoing connection to servers) */
5583 else {
5584 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
5585 if (dcid->len)
5586 memcpy(qc->dcid.data, dcid->data, dcid->len);
5587 qc->dcid.len = dcid->len;
5588 }
5589 qc->mux_state = QC_MUX_NULL;
5590 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
5591
Amaury Denoyellef16ec342023-04-13 17:42:34 +02005592 conn_id->qc = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005593
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005594 if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
5595 is_addr(local_addr)) {
5596 TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
5597 qc_alloc_fd(qc, local_addr, peer_addr);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005598
5599 /* haproxy soft-stop is supported only for QUIC connections
5600 * with their owned socket.
5601 */
5602 if (qc_test_fd(qc))
5603 _HA_ATOMIC_INC(&jobs);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005604 }
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005605
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005606 /* Select our SCID which is the first CID with 0 as sequence number. */
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005607 qc->scid = conn_id->cid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005608
5609 /* Packet number spaces initialization. */
5610 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
5611 quic_pktns_init(&qc->pktns[i]);
5612 /* QUIC encryption level context initialization. */
5613 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5614 if (!quic_conn_enc_level_init(qc, i)) {
5615 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
5616 goto err;
5617 }
5618 /* Initialize the packet number space. */
5619 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
5620 }
5621
5622 qc->original_version = qv;
5623 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
5624 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
5625 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
5626 /* TX part. */
5627 LIST_INIT(&qc->tx.frms_to_send);
5628 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
5629 qc->tx.wbuf = qc->tx.rbuf = 0;
5630 qc->tx.bytes = 0;
5631 qc->tx.buf = BUF_NULL;
5632 /* RX part. */
5633 qc->rx.bytes = 0;
5634 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
5635 for (i = 0; i < QCS_MAX_TYPES; i++)
5636 qc->rx.strms[i].nb_streams = 0;
5637
5638 qc->nb_pkt_for_cc = 1;
5639 qc->nb_pkt_since_cc = 0;
5640
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005641 if (!quic_tls_ku_init(qc)) {
5642 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
5643 goto err;
5644 }
5645
5646 /* XXX TO DO: Only one path at this time. */
5647 qc->path = &qc->paths[0];
5648 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
5649
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005650 qc->streams_by_id = EB_ROOT_UNIQUE;
5651 qc->stream_buf_count = 0;
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005652 memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
5653 memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005654
5655 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
Amaury Denoyelle591e7982023-04-12 10:04:49 +02005656 conn_id->stateless_reset_token,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005657 dcid->data, dcid->len,
5658 qc->scid.data, qc->scid.len, token_odcid))
5659 goto err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005660
Frédéric Lécailledeb97812023-03-22 11:29:45 +01005661 /* Initialize the idle timeout of the connection at the "max_idle_timeout"
5662 * value from local transport parameters.
5663 */
5664 qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005665 qc->wait_event.tasklet = tasklet_new();
5666 if (!qc->wait_event.tasklet) {
5667 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
5668 goto err;
5669 }
5670 qc->wait_event.tasklet->process = quic_conn_io_cb;
5671 qc->wait_event.tasklet->context = qc;
5672 qc->wait_event.events = 0;
Amaury Denoyellebbb1c682022-09-28 15:15:51 +02005673 qc->subs = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005674
5675 if (qc_conn_alloc_ssl_ctx(qc) ||
5676 !quic_conn_init_timer(qc) ||
5677 !quic_conn_init_idle_timer_task(qc))
5678 goto err;
5679
5680 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
5681 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
5682 goto err;
5683
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005684 /* Counters initialization */
5685 memset(&qc->cntrs, 0, sizeof qc->cntrs);
5686
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005687 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
5688 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
5689
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005690 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
5691
5692 return qc;
5693
5694 err:
5695 pool_free(pool_head_quic_conn_rxbuf, buf_area);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005696 if (qc) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005697 qc->rx.buf.area = NULL;
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005698 quic_conn_release(qc);
5699 }
5700 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005701 return NULL;
5702}
5703
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005704/* Update the proxy counters of <qc> QUIC connection from its counters */
5705static inline void quic_conn_prx_cntrs_update(struct quic_conn *qc)
5706{
Frédéric Lécaille557d30a2023-06-14 18:09:54 +02005707 if (!qc->prx_counters)
5708 return;
5709
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005710 HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt, qc->cntrs.dropped_pkt);
5711 HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt_bufoverrun, qc->cntrs.dropped_pkt_bufoverrun);
5712 HA_ATOMIC_ADD(&qc->prx_counters->dropped_parsing, qc->cntrs.dropped_parsing);
5713 HA_ATOMIC_ADD(&qc->prx_counters->socket_full, qc->cntrs.socket_full);
5714 HA_ATOMIC_ADD(&qc->prx_counters->sendto_err, qc->cntrs.sendto_err);
5715 HA_ATOMIC_ADD(&qc->prx_counters->sendto_err_unknown, qc->cntrs.sendto_err_unknown);
Frédéric Lécaille12a815a2023-05-24 15:55:14 +02005716 HA_ATOMIC_ADD(&qc->prx_counters->sent_pkt, qc->cntrs.sent_pkt);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005717 HA_ATOMIC_ADD(&qc->prx_counters->lost_pkt, qc->path->loss.nb_lost_pkt);
5718 HA_ATOMIC_ADD(&qc->prx_counters->conn_migration_done, qc->cntrs.conn_migration_done);
5719 /* Stream related counters */
5720 HA_ATOMIC_ADD(&qc->prx_counters->data_blocked, qc->cntrs.data_blocked);
5721 HA_ATOMIC_ADD(&qc->prx_counters->stream_data_blocked, qc->cntrs.stream_data_blocked);
Amaury Denoyelle6d6ee0d2023-05-25 10:36:04 +02005722 HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_bidi, qc->cntrs.streams_blocked_bidi);
5723 HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_uni, qc->cntrs.streams_blocked_uni);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005724}
5725
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005726/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
5727 * The connection tasklet is killed.
5728 *
5729 * This function must only be called by the thread responsible of the quic_conn
5730 * tasklet.
5731 */
5732void quic_conn_release(struct quic_conn *qc)
5733{
5734 int i;
5735 struct ssl_sock_ctx *conn_ctx;
5736 struct eb64_node *node;
5737 struct quic_tls_ctx *app_tls_ctx;
5738 struct quic_rx_packet *pkt, *pktback;
5739
5740 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5741
5742 /* We must not free the quic-conn if the MUX is still allocated. */
5743 BUG_ON(qc->mux_state == QC_MUX_READY);
5744
Amaury Denoyellefb375572023-02-01 09:28:32 +01005745 if (qc_test_fd(qc))
5746 _HA_ATOMIC_DEC(&jobs);
5747
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005748 /* Close quic-conn socket fd. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01005749 qc_release_fd(qc, 0);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005750
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005751 /* in the unlikely (but possible) case the connection was just added to
5752 * the accept_list we must delete it from there.
5753 */
5754 MT_LIST_DELETE(&qc->accept_list);
5755
5756 /* free remaining stream descriptors */
5757 node = eb64_first(&qc->streams_by_id);
5758 while (node) {
5759 struct qc_stream_desc *stream;
5760
5761 stream = eb64_entry(node, struct qc_stream_desc, by_id);
5762 node = eb64_next(node);
5763
5764 /* all streams attached to the quic-conn are released, so
5765 * qc_stream_desc_free will liberate the stream instance.
5766 */
5767 BUG_ON(!stream->release);
5768 qc_stream_desc_free(stream, 1);
5769 }
5770
5771 /* Purge Rx packet list. */
5772 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
5773 LIST_DELETE(&pkt->qc_rx_pkt_list);
5774 pool_free(pool_head_quic_rx_packet, pkt);
5775 }
5776
5777 if (qc->idle_timer_task) {
5778 task_destroy(qc->idle_timer_task);
5779 qc->idle_timer_task = NULL;
5780 }
5781
5782 if (qc->timer_task) {
5783 task_destroy(qc->timer_task);
5784 qc->timer_task = NULL;
5785 }
5786
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02005787 tasklet_free(qc->wait_event.tasklet);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005788
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005789 /* remove the connection from receiver cids trees */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005790 free_quic_conn_cids(qc);
5791
5792 conn_ctx = qc->xprt_ctx;
5793 if (conn_ctx) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005794 SSL_free(conn_ctx->ssl);
5795 pool_free(pool_head_quic_conn_ctx, conn_ctx);
5796 }
5797
5798 quic_tls_ku_free(qc);
5799 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5800 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
5801 quic_conn_enc_level_uninit(qc, &qc->els[i]);
5802 }
5803 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
5804
5805 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
5806 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
5807 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
5808
5809 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
5810 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
5811 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
5812 }
5813
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01005814 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005815
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02005816 quic_conn_prx_cntrs_update(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005817 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
5818 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleeb3e5172023-04-12 13:41:54 +02005819 qc = NULL;
Amaury Denoyellefb375572023-02-01 09:28:32 +01005820
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005821 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
5822
5823 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5824}
5825
5826/* Initialize the timer task of <qc> QUIC connection.
5827 * Returns 1 if succeeded, 0 if not.
5828 */
5829static int quic_conn_init_timer(struct quic_conn *qc)
5830{
5831 int ret = 0;
5832 /* Attach this task to the same thread ID used for the connection */
5833 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5834
Amaury Denoyelle66947282023-04-13 11:48:38 +02005835 qc->timer_task = task_new_here();
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005836 if (!qc->timer_task) {
5837 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5838 goto leave;
5839 }
5840
5841 qc->timer = TICK_ETERNITY;
5842 qc->timer_task->process = qc_process_timer;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005843 qc->timer_task->context = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005844
5845 ret = 1;
5846 leave:
5847 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5848 return ret;
5849}
5850
Frédéric Lécailled7215712023-03-24 18:13:37 +01005851/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
5852 * connection. */
5853static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005854{
5855 unsigned int expire;
5856
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005857 if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005858 TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005859 qc->ack_expire = TICK_ETERNITY;
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005860 task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
5861 }
5862 else {
5863 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005864 qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
5865 if (arm_ack) {
5866 /* Arm the ack timer only if not already armed. */
5867 if (!tick_isset(qc->ack_expire)) {
5868 qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
5869 qc->idle_timer_task->expire = qc->ack_expire;
5870 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005871 TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005872 }
5873 }
5874 else {
5875 qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
5876 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005877 TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005878 }
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005879 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005880}
5881
Frédéric Lécailled7215712023-03-24 18:13:37 +01005882/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
5883 * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
5884 * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
5885 * which must be rearmed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005886 */
Frédéric Lécailled7215712023-03-24 18:13:37 +01005887static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005888{
5889 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5890
5891 if (read) {
5892 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5893 }
5894 else {
5895 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5896 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01005897 qc_idle_timer_do_rearm(qc, arm_ack);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005898
5899 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5900}
5901
5902/* The task handling the idle timeout */
5903struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
5904{
5905 struct quic_conn *qc = ctx;
5906 struct quic_counters *prx_counters = qc->prx_counters;
5907 unsigned int qc_flags = qc->flags;
5908
5909 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5910
Frédéric Lécaille12eca3a2023-04-04 10:46:54 +02005911 if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
5912 goto requeue;
5913
Frédéric Lécailled7215712023-03-24 18:13:37 +01005914 if (tick_is_expired(qc->ack_expire, now_ms)) {
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005915 TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01005916 qc->ack_expire = TICK_ETERNITY;
5917 /* Note that ->idle_expire is always set. */
5918 t->expire = qc->idle_expire;
Frédéric Lécailleb73762a2023-04-24 11:32:22 +02005919 /* Do not wakeup the I/O handler in DRAINING state or if the
5920 * connection must be killed as soon as possible.
5921 */
5922 if (!(qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL))) {
5923 qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
5924 tasklet_wakeup(qc->wait_event.tasklet);
5925 }
5926
Frédéric Lécailled7215712023-03-24 18:13:37 +01005927 goto requeue;
5928 }
5929
Frédéric Lécaille495968e2023-04-03 17:42:05 +02005930 TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005931 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
5932 * might free the quic-conn too early via quic_close().
5933 */
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02005934 qc_notify_err(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005935
5936 /* If the MUX is still alive, keep the quic-conn. The MUX is
5937 * responsible to call quic_close to release it.
5938 */
5939 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005940 if (qc->mux_state != QC_MUX_READY) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005941 quic_conn_release(qc);
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005942 qc = NULL;
5943 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005944
5945 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
5946 * least clean some parts of it such as the tasklet.
5947 */
5948
5949 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
5950 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillece5c1452023-04-05 09:44:21 +02005951 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005952 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
5953 }
5954
Frédéric Lécailled7215712023-03-24 18:13:37 +01005955 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005956 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5957 return NULL;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005958
5959 requeue:
5960 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5961 return t;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005962}
5963
5964/* Initialize the idle timeout task for <qc>.
5965 * Returns 1 if succeeded, 0 if not.
5966 */
5967static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
5968{
5969 int ret = 0;
5970
5971 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5972
5973 qc->idle_timer_task = task_new_here();
5974 if (!qc->idle_timer_task) {
5975 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5976 goto leave;
5977 }
5978
5979 qc->idle_timer_task->process = qc_idle_timer_task;
5980 qc->idle_timer_task->context = qc;
Frédéric Lécailled7215712023-03-24 18:13:37 +01005981 qc->ack_expire = TICK_ETERNITY;
5982 qc_idle_timer_rearm(qc, 1, 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005983 task_queue(qc->idle_timer_task);
5984
5985 ret = 1;
5986 leave:
5987 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5988 return ret;
5989}
5990
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02005991/* Parse into <pkt> a long header located at <*pos> position, <end> begin a pointer to the end
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005992 * past one byte of this buffer.
5993 */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02005994static inline int quic_packet_read_long_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005995 struct quic_rx_packet *pkt)
5996{
5997 int ret = 0;
5998 unsigned char dcid_len, scid_len;
5999
6000 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
6001
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006002 if (end == *pos) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006003 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
6004 goto leave;
6005 }
6006
6007 /* Destination Connection ID Length */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006008 dcid_len = *(*pos)++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006009 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006010 if (dcid_len > QUIC_CID_MAXLEN || end - *pos < dcid_len + 1) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006011 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
6012 goto leave;
6013 }
6014
6015 if (dcid_len) {
6016 /* Check that the length of this received DCID matches the CID lengths
6017 * of our implementation for non Initials packets only.
6018 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006019 if (pkt->version && pkt->version->num &&
6020 pkt->type != QUIC_PACKET_TYPE_INITIAL &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006021 pkt->type != QUIC_PACKET_TYPE_0RTT &&
6022 dcid_len != QUIC_HAP_CID_LEN) {
6023 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
6024 goto leave;
6025 }
6026
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006027 memcpy(pkt->dcid.data, *pos, dcid_len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006028 }
6029
6030 pkt->dcid.len = dcid_len;
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006031 *pos += dcid_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006032
6033 /* Source Connection ID Length */
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006034 scid_len = *(*pos)++;
6035 if (scid_len > QUIC_CID_MAXLEN || end - *pos < scid_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006036 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
6037 goto leave;
6038 }
6039
6040 if (scid_len)
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006041 memcpy(pkt->scid.data, *pos, scid_len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006042 pkt->scid.len = scid_len;
Frédéric Lécaille6ff52f92023-04-24 15:41:07 +02006043 *pos += scid_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006044
6045 ret = 1;
6046 leave:
6047 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6048 return ret;
6049}
6050
6051/* Insert <pkt> RX packet in its <qel> RX packets tree */
6052static void qc_pkt_insert(struct quic_conn *qc,
6053 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
6054{
6055 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
6056
6057 pkt->pn_node.key = pkt->pn;
6058 quic_rx_packet_refinc(pkt);
6059 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
6060
6061 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6062}
6063
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006064/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
6065 * address of the packet first byte, using the keys from encryption level <el>.
6066 *
6067 * If header protection has been successfully removed, packet data are copied
6068 * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
6069 * proceeded, and the packet is inserted into <qc> protected packets tree. In
6070 * both cases, packet can now be considered handled by the <qc> connection.
6071 *
6072 * If header protection cannot be removed due to <el> secrets already
6073 * discarded, no operation is conducted.
6074 *
6075 * Returns 1 on success : packet data is now handled by the connection. On
6076 * error 0 is returned : packet should be dropped by the caller.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006077 */
6078static inline int qc_try_rm_hp(struct quic_conn *qc,
6079 struct quic_rx_packet *pkt,
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006080 unsigned char *beg,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006081 struct quic_enc_level **el)
6082{
6083 int ret = 0;
6084 unsigned char *pn = NULL; /* Packet number field */
6085 enum quic_tls_enc_level tel;
6086 struct quic_enc_level *qel;
6087 /* Only for traces. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006088
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006089 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006090 BUG_ON(!pkt->pn_offset);
6091
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006092 /* The packet number is here. This is also the start minus
6093 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
6094 * protection.
6095 */
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006096 pn = beg + pkt->pn_offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006097
6098 tel = quic_packet_type_enc_level(pkt->type);
6099 qel = &qc->els[tel];
6100
6101 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaille72027782023-02-22 16:20:09 +01006102 struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
6103
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006104 /* Note that the following function enables us to unprotect the packet
6105 * number and its length subsequently used to decrypt the entire
6106 * packets.
6107 */
Frédéric Lécaille72027782023-02-22 16:20:09 +01006108 if (!qc_do_rm_hp(qc, pkt, tls_ctx,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006109 qel->pktns->rx.largest_pn, pn, beg)) {
6110 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
6111 goto out;
6112 }
6113
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006114 qc_handle_spin_bit(qc, pkt, qel);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006115 /* The AAD includes the packet number field. */
6116 pkt->aad_len = pkt->pn_offset + pkt->pnl;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006117 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
6118 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
6119 goto out;
6120 }
6121
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006122 TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006123 }
6124 else {
6125 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
6126 /* If the packet number space has been discarded, this packet
6127 * will be not parsed.
6128 */
6129 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
6130 goto out;
6131 }
6132
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006133 TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006134 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
6135 quic_rx_packet_refinc(pkt);
6136 }
6137
6138 *el = qel;
6139 /* No reference counter incrementation here!!! */
6140 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
6141 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
6142 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
6143 b_add(&qc->rx.buf, pkt->len);
6144
6145 ret = 1;
6146 out:
Frédéric Lécaillec0aaa072023-04-07 17:58:49 +02006147 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006148 return ret;
6149}
6150
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006151/* Return the QUIC version (quic_version struct) with <version> as version number
6152 * if supported or NULL if not.
6153 */
6154static inline const struct quic_version *qc_supported_version(uint32_t version)
6155{
6156 int i;
6157
6158 if (unlikely(!version))
6159 return &quic_version_VN_reserved;
6160
6161 for (i = 0; i < quic_versions_nb; i++)
6162 if (quic_versions[i].num == version)
6163 return &quic_versions[i];
6164
6165 return NULL;
6166}
6167
Willy Tarreaudd9f9212023-05-07 07:07:44 +02006168/* Parse a QUIC packet header starting at <pos> position without exceeding <end>.
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006169 * Version and type are stored in <pkt> packet instance. Type is set to unknown
6170 * on two occasions : for unsupported version, in this case version field is
6171 * set to NULL; for Version Negotiation packet with version number set to 0.
6172 *
6173 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006174 */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006175int qc_parse_hd_form(struct quic_rx_packet *pkt,
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006176 unsigned char **pos, const unsigned char *end)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006177{
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006178 uint32_t version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006179 int ret = 0;
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006180 const unsigned char byte0 = **pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006181
6182 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006183 pkt->version = NULL;
6184 pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006185
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006186 (*pos)++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006187 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
6188 unsigned char type =
6189 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
6190
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006191 /* Version */
Frédéric Lécaillebb426aa2023-04-24 15:44:18 +02006192 if (!quic_read_uint32(&version, (const unsigned char **)pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006193 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
6194 goto out;
6195 }
6196
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02006197 pkt->version = qc_supported_version(version);
6198 if (version && pkt->version) {
6199 if (version != QUIC_PROTOCOL_VERSION_2) {
6200 pkt->type = type;
6201 }
6202 else {
6203 switch (type) {
6204 case 0:
6205 pkt->type = QUIC_PACKET_TYPE_RETRY;
6206 break;
6207 case 1:
6208 pkt->type = QUIC_PACKET_TYPE_INITIAL;
6209 break;
6210 case 2:
6211 pkt->type = QUIC_PACKET_TYPE_0RTT;
6212 break;
6213 case 3:
6214 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
6215 break;
6216 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006217 }
6218 }
6219 }
6220 else {
Frédéric Lécailleece86e62023-03-07 11:53:43 +01006221 if (byte0 & QUIC_PACKET_SPIN_BIT)
6222 pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006223 pkt->type = QUIC_PACKET_TYPE_SHORT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006224 }
6225
6226 ret = 1;
6227 out:
6228 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
6229 return ret;
6230}
6231
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006232/*
6233 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
6234 * address <addr>.
6235 * Implementation of RFC9000 6. Version Negotiation
6236 *
6237 * TODO implement a rate-limiting sending of Version Negotiation packets
6238 *
6239 * Returns 0 on success else non-zero
6240 */
6241static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
6242 struct quic_rx_packet *pkt)
6243{
6244 char buf[256];
6245 int ret = 0, i = 0, j;
6246 uint32_t version;
6247 const socklen_t addrlen = get_addr_len(addr);
6248
6249 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6250 /*
6251 * header form
6252 * long header, fixed bit to 0 for Version Negotiation
6253 */
6254 /* TODO: RAND_bytes() should be replaced? */
6255 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
6256 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
6257 goto out;
6258 }
6259
6260 buf[i++] |= '\x80';
6261 /* null version for Version Negotiation */
6262 buf[i++] = '\x00';
6263 buf[i++] = '\x00';
6264 buf[i++] = '\x00';
6265 buf[i++] = '\x00';
6266
6267 /* source connection id */
6268 buf[i++] = pkt->scid.len;
6269 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6270 i += pkt->scid.len;
6271
6272 /* destination connection id */
6273 buf[i++] = pkt->dcid.len;
6274 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
6275 i += pkt->dcid.len;
6276
6277 /* supported version */
6278 for (j = 0; j < quic_versions_nb; j++) {
6279 version = htonl(quic_versions[j].num);
6280 memcpy(&buf[i], &version, sizeof(version));
6281 i += sizeof(version);
6282 }
6283
6284 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
6285 goto out;
6286
6287 ret = 1;
6288 out:
6289 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6290 return !ret;
6291}
6292
6293/* Send a stateless reset packet depending on <pkt> RX packet information
6294 * from <fd> UDP socket to <dst>
6295 * Return 1 if succeeded, 0 if not.
6296 */
6297static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
6298 struct quic_rx_packet *rxpkt)
6299{
6300 int ret = 0, pktlen, rndlen;
6301 unsigned char pkt[64];
6302 const socklen_t addrlen = get_addr_len(dstaddr);
6303 struct proxy *prx;
6304 struct quic_counters *prx_counters;
6305
6306 TRACE_ENTER(QUIC_EV_STATELESS_RST);
6307
6308 prx = l->bind_conf->frontend;
6309 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6310 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
6311 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
6312 * Reset is difficult to distinguish from other packets if the recipient requires
6313 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
6314 * that all packets it sends are at least 22 bytes longer than the minimum
6315 * connection ID length that it requests the peer to include in its packets,
6316 * adding PADDING frames as necessary. This ensures that any Stateless Reset
6317 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
6318 * An endpoint that sends a Stateless Reset in response to a packet that is
6319 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
6320 * than the packet it responds to.
6321 */
6322
6323 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
6324 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
6325 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
6326 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
6327
6328 /* Put a header of random bytes */
6329 /* TODO: RAND_bytes() should be replaced */
6330 if (RAND_bytes(pkt, rndlen) != 1) {
6331 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
6332 goto leave;
6333 }
6334
6335 /* Clear the most significant bit, and set the second one */
6336 *pkt = (*pkt & ~0x80) | 0x40;
Amaury Denoyelle9b68b642023-04-12 15:48:51 +02006337 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006338 rxpkt->dcid.data, rxpkt->dcid.len))
6339 goto leave;
6340
6341 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
6342 goto leave;
6343
6344 ret = 1;
6345 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
6346 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
6347 leave:
6348 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
6349 return ret;
6350}
6351
6352/* QUIC server only function.
6353 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
6354 * This is the responsibility of the caller to check <aad> size is big enough
6355 * to contain these data.
6356 * Return the number of bytes copied to <aad>.
6357 */
6358static int quic_generate_retry_token_aad(unsigned char *aad,
6359 uint32_t version,
6360 const struct quic_cid *cid,
6361 const struct sockaddr_storage *addr)
6362{
6363 unsigned char *p;
6364
6365 p = aad;
Frédéric Lécaille7619aa62023-06-30 14:57:30 +02006366 *(uint32_t *)p = htonl(version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006367 p += sizeof version;
6368 p += quic_saddr_cpy(p, addr);
6369 memcpy(p, cid->data, cid->len);
6370 p += cid->len;
6371
6372 return p - aad;
6373}
6374
6375/* QUIC server only function.
6376 * Generate the token to be used in Retry packets. The token is written to
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006377 * <token> with <len> as length. <odcid> is the original destination connection
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006378 * ID and <dcid> is our side destination connection ID (or client source
6379 * connection ID).
6380 * Returns the length of the encoded token or 0 on error.
6381 */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006382static int quic_generate_retry_token(unsigned char *token, size_t len,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006383 const uint32_t version,
6384 const struct quic_cid *odcid,
6385 const struct quic_cid *dcid,
6386 struct sockaddr_storage *addr)
6387{
6388 int ret = 0;
6389 unsigned char *p;
6390 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006391 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006392 size_t aadlen;
6393 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
6394 unsigned char key[QUIC_TLS_KEY_LEN];
6395 unsigned char iv[QUIC_TLS_IV_LEN];
6396 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6397 size_t seclen = strlen(global.cluster_secret);
6398 EVP_CIPHER_CTX *ctx = NULL;
6399 const EVP_CIPHER *aead = EVP_aes_128_gcm();
6400 uint32_t timestamp = now_ms;
6401
6402 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6403
Frédéric Lécaille6d6ddb22023-05-15 17:40:00 +02006404 /* The token is made of the token format byte, the ODCID prefixed by its one byte
6405 * length, the creation timestamp, an AEAD TAG, and finally
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006406 * the random bytes used to derive the secret to encrypt the token.
6407 */
Frédéric Lécaille6d6ddb22023-05-15 17:40:00 +02006408 if (1 + odcid->len + 1 + sizeof(timestamp) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN > len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006409 goto err;
6410
6411 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
6412 /* TODO: RAND_bytes() should be replaced */
6413 if (RAND_bytes(salt, sizeof salt) != 1) {
6414 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
6415 goto err;
6416 }
6417
6418 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6419 salt, sizeof salt, sec, seclen)) {
6420 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
6421 goto err;
6422 }
6423
6424 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
6425 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
6426 goto err;
6427 }
6428
6429 /* Token build */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006430 p = token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006431 *p++ = QUIC_TOKEN_FMT_RETRY,
6432 *p++ = odcid->len;
6433 memcpy(p, odcid->data, odcid->len);
6434 p += odcid->len;
6435 write_u32(p, htonl(timestamp));
6436 p += sizeof timestamp;
6437
6438 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006439 if (!quic_tls_encrypt(token + 1, p - token - 1, aad, aadlen, ctx, aead, key, iv)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006440 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
6441 goto err;
6442 }
6443
6444 p += QUIC_TLS_TAG_LEN;
6445 memcpy(p, salt, sizeof salt);
6446 p += sizeof salt;
6447 EVP_CIPHER_CTX_free(ctx);
6448
Frédéric Lécailledad0ede2023-04-24 14:35:18 +02006449 ret = p - token;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006450 leave:
6451 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6452 return ret;
6453
6454 err:
6455 if (ctx)
6456 EVP_CIPHER_CTX_free(ctx);
6457 goto leave;
6458}
6459
6460/* QUIC server only function.
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006461 *
6462 * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
6463 * the UDP datagram containing <pkt> and <l> is the listener instance on which
6464 * it was received. If the token is valid, the ODCID of <qc> QUIC connection
6465 * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
6466 * to validate the token but it can be NULL : in this case the version will be
6467 * retrieved from the packet.
6468 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006469 * Return 1 if succeeded, 0 if not.
6470 */
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006471
6472static int quic_retry_token_check(struct quic_rx_packet *pkt,
6473 struct quic_dgram *dgram,
6474 struct listener *l,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006475 struct quic_conn *qc,
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006476 struct quic_cid *odcid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006477{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006478 struct proxy *prx;
6479 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006480 int ret = 0;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006481 unsigned char *token = pkt->token;
6482 const uint64_t tokenlen = pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006483 unsigned char buf[128];
6484 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02006485 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006486 size_t aadlen;
6487 const unsigned char *salt;
6488 unsigned char key[QUIC_TLS_KEY_LEN];
6489 unsigned char iv[QUIC_TLS_IV_LEN];
6490 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
6491 size_t seclen = strlen(global.cluster_secret);
6492 EVP_CIPHER_CTX *ctx = NULL;
6493 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006494 const struct quic_version *qv = qc ? qc->original_version :
6495 pkt->version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006496
6497 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6498
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006499 /* The caller must ensure this. */
6500 BUG_ON(!global.cluster_secret || !pkt->token_len);
6501
6502 prx = l->bind_conf->frontend;
6503 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6504
6505 if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
6506 /* TODO: New token check */
6507 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6508 goto leave;
6509 }
6510
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006511 if (sizeof buf < tokenlen) {
6512 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
6513 goto err;
6514 }
6515
Frédéric Lécaille35b63962023-05-15 18:11:21 +02006516 /* The token is made of the token format byte, the ODCID prefixed by its one byte
6517 * length, the creation timestamp, an AEAD TAG, and finally
6518 * the random bytes used to derive the secret to encrypt the token.
6519 */
6520 if (tokenlen < 2 + QUIC_ODCID_MINLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN ||
6521 tokenlen > 2 + QUIC_CID_MAXLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN) {
6522 TRACE_ERROR("invalid token length", QUIC_EV_CONN_LPKT, qc);
6523 goto err;
6524 }
6525
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006526 aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006527 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
6528 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
6529 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
6530 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
6531 goto err;
6532 }
6533
6534 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
6535 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
6536 goto err;
6537 }
6538
Frédéric Lécaille35b63962023-05-15 18:11:21 +02006539 /* The token is prefixed by a one-byte length format which is not ciphered. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006540 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
6541 ctx, aead, key, iv)) {
6542 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
6543 goto err;
6544 }
6545
6546 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
6547 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
6548 goto err;
6549 }
6550
6551 EVP_CIPHER_CTX_free(ctx);
6552
6553 ret = 1;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006554 HA_ATOMIC_INC(&prx_counters->retry_validated);
6555
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006556 leave:
6557 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6558 return ret;
6559
6560 err:
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006561 HA_ATOMIC_INC(&prx_counters->retry_error);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006562 if (ctx)
6563 EVP_CIPHER_CTX_free(ctx);
6564 goto leave;
6565}
6566
6567/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
6568 * the Initial <pkt> packet.
6569 *
6570 * Returns 0 on success else non-zero.
6571 */
6572static int send_retry(int fd, struct sockaddr_storage *addr,
6573 struct quic_rx_packet *pkt, const struct quic_version *qv)
6574{
6575 int ret = 0;
6576 unsigned char buf[128];
6577 int i = 0, token_len;
6578 const socklen_t addrlen = get_addr_len(addr);
6579 struct quic_cid scid;
6580
6581 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
6582
Frédéric Lécaille2b220542023-06-30 12:17:36 +02006583 /* long header(1) | fixed bit(1) | packet type QUIC_PACKET_TYPE_RETRY(2) | unused random bits(4)*/
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006584 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
Frédéric Lécaille2b220542023-06-30 12:17:36 +02006585 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT) |
6586 statistical_prng_range(16);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006587 /* version */
Frédéric Lécaille966e4682023-06-30 14:41:31 +02006588 *(uint32_t *)&buf[i] = htonl(qv->num);
6589 i += sizeof(uint32_t);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006590
6591 /* Use the SCID from <pkt> for Retry DCID. */
6592 buf[i++] = pkt->scid.len;
6593 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
6594 i += pkt->scid.len;
6595
6596 /* Generate a new CID to be used as SCID for the Retry packet. */
6597 scid.len = QUIC_HAP_CID_LEN;
6598 /* TODO: RAND_bytes() should be replaced */
6599 if (RAND_bytes(scid.data, scid.len) != 1) {
6600 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
6601 goto out;
6602 }
6603
6604 buf[i++] = scid.len;
6605 memcpy(&buf[i], scid.data, scid.len);
6606 i += scid.len;
6607
6608 /* token */
6609 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
6610 &pkt->dcid, &pkt->scid, addr))) {
6611 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
6612 goto out;
6613 }
6614
6615 i += token_len;
6616
6617 /* token integrity tag */
Emeric Bruna47f5cd2023-06-27 15:24:05 +02006618 if ((sizeof(buf) - i < QUIC_TLS_TAG_LEN) ||
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006619 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
6620 pkt->dcid.len, buf, i, qv)) {
6621 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6622 goto out;
6623 }
6624
6625 i += QUIC_TLS_TAG_LEN;
6626
6627 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
6628 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6629 goto out;
6630 }
6631
6632 ret = 1;
6633 out:
6634 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6635 return !ret;
6636}
6637
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006638/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
6639 * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
6640 * is used.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006641 *
6642 * Returns the instance or NULL if not found.
6643 */
6644static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
6645 struct listener *l,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006646 struct sockaddr_storage *saddr,
6647 int *new_tid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006648{
6649 struct quic_conn *qc = NULL;
6650 struct ebmb_node *node;
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006651 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006652 struct quic_cid_tree *tree;
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006653 uint conn_id_tid;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006654
6655 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006656 *new_tid = -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006657
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006658 /* First look into DCID tree. */
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006659 tree = &quic_cid_trees[_quic_cid_tree_idx(pkt->dcid.data)];
6660 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6661 node = ebmb_lookup(&tree->root, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006662
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006663 /* If not found on an Initial/0-RTT packet, it could be because an
6664 * ODCID is reused by the client. Calculate the derived CID value to
6665 * retrieve it from the DCID tree.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006666 */
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006667 if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
6668 pkt->type == QUIC_PACKET_TYPE_0RTT)) {
Amaury Denoyellec2a92642023-04-13 15:26:18 +02006669 const struct quic_cid derive_cid = quic_derive_cid(&pkt->dcid, saddr);
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006670
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006671 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
6672
Amaury Denoyellee83f9372023-04-18 11:10:54 +02006673 tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
6674 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
6675 node = ebmb_lookup(&tree->root, derive_cid.data, derive_cid.len);
Amaury Denoyelle2c982092023-04-03 18:50:58 +02006676 }
6677
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006678 if (!node)
6679 goto end;
6680
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006681 conn_id = ebmb_entry(node, struct quic_connection_id, node);
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006682 conn_id_tid = HA_ATOMIC_LOAD(&conn_id->tid);
6683 if (conn_id_tid != tid) {
6684 *new_tid = conn_id_tid;
6685 goto end;
6686 }
Amaury Denoyelle591e7982023-04-12 10:04:49 +02006687 qc = conn_id->qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006688
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006689 end:
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006690 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006691 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6692 return qc;
6693}
6694
6695/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
6696 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
6697 * parameters of this session.
6698 * This is the responsibility of the caller to check the validity of all the
6699 * pointers passed as parameter to this function.
6700 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
6701 * CO_ER_SSL_NO_MEM.
6702 */
6703static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
6704 unsigned char *params, size_t params_len)
6705{
6706 int retry, ret = -1;
6707
6708 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6709
6710 retry = 1;
6711 retry:
6712 *ssl = SSL_new(ssl_ctx);
6713 if (!*ssl) {
6714 if (!retry--)
Frédéric Lécaillee4185272023-06-02 16:56:16 +02006715 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006716
6717 pool_gc(NULL);
6718 goto retry;
6719 }
6720
6721 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
6722 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
6723 SSL_free(*ssl);
6724 *ssl = NULL;
6725 if (!retry--)
Frédéric Lécaillee4185272023-06-02 16:56:16 +02006726 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006727
6728 pool_gc(NULL);
6729 goto retry;
6730 }
6731
6732 ret = 0;
6733 leave:
6734 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6735 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006736}
6737
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006738/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
6739 * used to process <qc> received packets. The allocated context is stored in
6740 * <qc.xprt_ctx>.
6741 *
6742 * Returns 0 on success else non-zero.
6743 */
6744static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
6745{
6746 int ret = 0;
6747 struct bind_conf *bc = qc->li->bind_conf;
6748 struct ssl_sock_ctx *ctx = NULL;
6749
6750 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6751
6752 ctx = pool_zalloc(pool_head_quic_conn_ctx);
6753 if (!ctx) {
6754 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
6755 goto err;
6756 }
6757
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006758 ctx->subs = NULL;
6759 ctx->xprt_ctx = NULL;
6760 ctx->qc = qc;
6761
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006762 if (qc_is_listener(qc)) {
6763 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
6764 qc->enc_params, qc->enc_params_len) == -1) {
6765 goto err;
6766 }
6767#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6768 /* Enabling 0-RTT */
6769 if (bc->ssl_conf.early_data)
6770 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
6771#endif
6772
6773 SSL_set_accept_state(ctx->ssl);
6774 }
6775
6776 ctx->xprt = xprt_get(XPRT_QUIC);
6777
6778 /* Store the allocated context in <qc>. */
6779 qc->xprt_ctx = ctx;
6780
6781 ret = 1;
6782 leave:
6783 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6784 return !ret;
6785
6786 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006787 pool_free(pool_head_quic_conn_ctx, ctx);
6788 goto leave;
6789}
6790
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006791/* Check that all the bytes between <pos> included and <end> address
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006792 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006793 * check that there is at least one byte between <pos> end <end>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006794 * Return 1 if this all the bytes are null, 0 if not.
6795 */
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006796static inline int quic_padding_check(const unsigned char *pos,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006797 const unsigned char *end)
6798{
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006799 while (pos < end && !*pos)
6800 pos++;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006801
Frédéric Lécaille7f0b1c72023-04-24 14:38:33 +02006802 return pos == end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006803}
6804
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006805/* Find the associated connection to the packet <pkt> or create a new one if
6806 * this is an Initial packet. <dgram> is the datagram containing the packet and
6807 * <l> is the listener instance on which it was received.
6808 *
Amaury Denoyelle25174d52023-04-05 17:52:05 +02006809 * By default, <new_tid> is set to -1. However, if thread affinity has been
6810 * chanbed, it will be set to its new thread ID.
6811 *
6812 * Returns the quic-conn instance or NULL if not found or thread affinity
6813 * changed.
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006814 */
6815static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
6816 struct quic_dgram *dgram,
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006817 struct listener *l,
6818 int *new_tid)
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006819{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006820 struct quic_cid token_odcid = { .len = 0 };
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006821 struct quic_conn *qc = NULL;
6822 struct proxy *prx;
6823 struct quic_counters *prx_counters;
6824
6825 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6826
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006827 *new_tid = -1;
6828
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006829 prx = l->bind_conf->frontend;
6830 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6831
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006832 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr, new_tid);
6833
Amaury Denoyelle25174d52023-04-05 17:52:05 +02006834 /* If connection already created or rebinded on another thread. */
Frédéric Lécailleab3aa0f2023-05-24 09:06:06 +02006835 if (!qc && *new_tid != -1 && tid != *new_tid)
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006836 goto out;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006837
6838 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6839 BUG_ON(!pkt->version); /* This must not happen. */
6840
6841 if (global.cluster_secret && pkt->token_len) {
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006842 if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
6843 goto err;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006844 }
6845
6846 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006847 struct quic_cid_tree *tree;
6848 struct ebmb_node *node;
6849 struct quic_connection_id *conn_id;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006850 int ipv4;
6851
6852 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
6853 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
6854 TRACE_PROTO("Initial without token, sending retry",
6855 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6856 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
6857 TRACE_ERROR("Error during Retry generation",
6858 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6859 goto out;
6860 }
6861
6862 HA_ATOMIC_INC(&prx_counters->retry_sent);
6863 goto out;
6864 }
6865
6866 /* RFC 9000 7.2. Negotiating Connection IDs:
6867 * When an Initial packet is sent by a client that has not previously
6868 * received an Initial or Retry packet from the server, the client
6869 * populates the Destination Connection ID field with an unpredictable
6870 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6871 */
6872 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
6873 TRACE_PROTO("dropped packet",
6874 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6875 goto err;
6876 }
6877
6878 pkt->saddr = dgram->saddr;
6879 ipv4 = dgram->saddr.ss_family == AF_INET;
6880
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006881 /* Generate the first connection CID. This is derived from the client
6882 * ODCID and address. This allows to retrieve the connection from the
6883 * ODCID without storing it in the CID tree. This is an interesting
6884 * optimization as the client is expected to stop using its ODCID in
6885 * favor of our generated value.
6886 */
6887 conn_id = new_quic_cid(NULL, NULL, &pkt->dcid, &pkt->saddr);
6888 if (!conn_id)
6889 goto err;
6890
Frédéric Lécaillefd212a72023-06-16 16:10:58 +02006891 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
6892 conn_id, &dgram->daddr, &pkt->saddr, 1,
6893 !!pkt->token_len, l);
6894 if (qc == NULL) {
Frédéric Lécaillefd212a72023-06-16 16:10:58 +02006895 pool_free(pool_head_quic_connection_id, conn_id);
6896 goto err;
6897 }
6898
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006899 tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)];
6900 HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock);
6901 node = ebmb_insert(&tree->root, &conn_id->node, conn_id->cid.len);
6902 if (node != &conn_id->node) {
6903 pool_free(pool_head_quic_connection_id, conn_id);
6904
6905 conn_id = ebmb_entry(node, struct quic_connection_id, node);
6906 *new_tid = HA_ATOMIC_LOAD(&conn_id->tid);
Frédéric Lécaillefd212a72023-06-16 16:10:58 +02006907 quic_conn_release(qc);
6908 qc = NULL;
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006909 }
Frédéric Lécaille8a586582023-06-26 10:39:56 +02006910 else {
6911 /* From here, <qc> is the correct connection for this <pkt> Initial
6912 * packet. <conn_id> must be inserted in the CIDs tree for this
6913 * connection.
6914 */
6915 eb64_insert(&qc->cids, &conn_id->seq_num);
6916 /* Initialize the next CID sequence number to be used for this connection. */
6917 qc->next_cid_seq_num = 1;
6918 }
Amaury Denoyellef16ec342023-04-13 17:42:34 +02006919 HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock);
6920
6921 if (*new_tid != -1)
6922 goto out;
6923
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006924 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006925 }
6926 }
6927 else if (!qc) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01006928 TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006929 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
6930 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
6931 goto err;
6932 }
6933
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006934 out:
6935 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6936 return qc;
6937
6938 err:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02006939 if (qc)
6940 qc->cntrs.dropped_pkt++;
6941 else
6942 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006943 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
6944 return NULL;
6945}
6946
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006947/* Parse a QUIC packet starting at <pos>. Data won't be read after <end> even
Amaury Denoyelle98289692022-10-19 15:37:44 +02006948 * if the packet is incomplete. This function will populate fields of <pkt>
6949 * instance, most notably its length. <dgram> is the UDP datagram which
6950 * contains the parsed packet. <l> is the listener instance on which it was
6951 * received.
6952 *
6953 * Returns 0 on success else non-zero. Packet length is guaranteed to be set to
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006954 * the real packet value or to cover all data between <pos> and <end> : this is
Amaury Denoyelle98289692022-10-19 15:37:44 +02006955 * useful to reject a whole datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006956 */
Amaury Denoyelle98289692022-10-19 15:37:44 +02006957static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006958 unsigned char *pos, const unsigned char *end,
Amaury Denoyelle98289692022-10-19 15:37:44 +02006959 struct quic_dgram *dgram, struct listener *l)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006960{
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006961 const unsigned char *beg = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006962 struct proxy *prx;
6963 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006964
6965 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6966
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006967 prx = l->bind_conf->frontend;
6968 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6969 /* This ist only to please to traces and distinguish the
6970 * packet with parsed packet number from others.
6971 */
6972 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006973 if (end <= pos) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006974 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6975 goto drop;
6976 }
6977
6978 /* Fixed bit */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006979 if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006980 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006981 quic_padding_check(pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006982 /* Some browsers may pad the remaining datagram space with null bytes.
6983 * That is what we called add padding out of QUIC packets. Such
6984 * datagrams must be considered as valid. But we can only consume
6985 * the remaining space.
6986 */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006987 pkt->len = end - pos;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006988 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006989 }
6990
6991 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6992 goto drop;
6993 }
6994
6995 /* Header form */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02006996 if (!qc_parse_hd_form(pkt, &pos, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006997 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6998 goto drop;
6999 }
7000
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007001 if (pkt->type != QUIC_PACKET_TYPE_SHORT) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007002 uint64_t len;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007003 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007004
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007005 if (!quic_packet_read_long_header(&pos, end, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007006 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7007 goto drop;
7008 }
7009
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007010 /* When multiple QUIC packets are coalesced on the same UDP datagram,
7011 * they must have the same DCID.
7012 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007013 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007014 (pkt->dcid.len != dgram->dcid_len ||
7015 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007016 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007017 goto drop;
7018 }
7019
7020 /* Retry of Version Negotiation packets are only sent by servers */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007021 if (pkt->type == QUIC_PACKET_TYPE_RETRY ||
7022 (pkt->version && !pkt->version->num)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007023 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
7024 goto drop;
7025 }
7026
7027 /* RFC9000 6. Version Negotiation */
Amaury Denoyelle1a5cc192023-04-17 15:03:51 +02007028 if (!pkt->version) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007029 /* unsupported version, send Negotiation packet */
7030 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
7031 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007032 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007033 }
7034
7035 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007036 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007037 }
7038
7039 /* For Initial packets, and for servers (QUIC clients connections),
7040 * there is no Initial connection IDs storage.
7041 */
7042 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7043 uint64_t token_len;
7044
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007045 if (!quic_dec_int(&token_len, (const unsigned char **)&pos, end) ||
7046 end - pos < token_len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007047 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007048 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007049 goto drop;
7050 }
7051
7052 /* TODO Retry should be automatically activated if
7053 * suspect network usage is detected.
7054 */
7055 if (global.cluster_secret && !token_len) {
7056 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
7057 TRACE_PROTO("Initial without token, sending retry",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007058 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
7059 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007060 TRACE_PROTO("Error during Retry generation",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007061 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007062 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007063 }
7064
7065 HA_ATOMIC_INC(&prx_counters->retry_sent);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007066 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007067 }
7068 }
7069 else if (!global.cluster_secret && token_len) {
7070 /* Impossible case: a token was received without configured
7071 * cluster secret.
7072 */
7073 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007074 NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007075 goto drop;
7076 }
7077
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007078 pkt->token = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007079 pkt->token_len = token_len;
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007080 pos += pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007081 }
7082 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
7083 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
7084 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007085 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007086 goto drop;
7087 }
7088 }
7089
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007090 if (!quic_dec_int(&len, (const unsigned char **)&pos, end) ||
7091 end - pos < len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007092 TRACE_PROTO("Packet dropped",
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007093 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007094 goto drop;
7095 }
7096
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007097 /* Packet Number is stored here. Packet Length totalizes the
7098 * rest of the content.
7099 */
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007100 pkt->pn_offset = pos - beg;
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007101 pkt->len = pkt->pn_offset + len;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007102
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007103 /* RFC 9000. Initial Datagram Size
7104 *
7105 * A server MUST discard an Initial packet that is carried in a UDP datagram
7106 * with a payload that is smaller than the smallest allowed maximum datagram
7107 * size of 1200 bytes.
7108 */
7109 if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
7110 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007111 TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille35218c62023-02-16 11:40:11 +01007112 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
7113 goto drop;
7114 }
7115
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007116 /* Interrupt parsing after packet length retrieval : this
7117 * ensures that only the packet is dropped but not the whole
7118 * datagram.
7119 */
7120 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007121 TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007122 goto drop;
7123 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007124 }
7125 else {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007126 TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007127 if (end - pos < QUIC_HAP_CID_LEN) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007128 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007129 goto drop;
7130 }
7131
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007132 memcpy(pkt->dcid.data, pos, QUIC_HAP_CID_LEN);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007133 pkt->dcid.len = QUIC_HAP_CID_LEN;
7134
7135 /* When multiple QUIC packets are coalesced on the same UDP datagram,
7136 * they must have the same DCID.
7137 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007138 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007139 (pkt->dcid.len != dgram->dcid_len ||
7140 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007141 TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007142 goto drop;
7143 }
7144
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007145 pos += QUIC_HAP_CID_LEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007146
Frédéric Lécaillebef30982023-04-24 14:43:57 +02007147 pkt->pn_offset = pos - beg;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007148 /* A short packet is the last one of a UDP datagram. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007149 pkt->len = end - beg;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02007150 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007151
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007152 TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007153 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007154 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007155
Amaury Denoyelle98289692022-10-19 15:37:44 +02007156 drop:
7157 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007158 drop_silent:
Amaury Denoyelle98289692022-10-19 15:37:44 +02007159 if (!pkt->len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007160 pkt->len = end - beg;
Amaury Denoyelle89e48ff2023-04-19 10:04:41 +02007161 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 +01007162 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle98289692022-10-19 15:37:44 +02007163 return -1;
7164}
7165
7166/* Check if received packet <pkt> should be drop due to <qc> already in closing
7167 * state. This can be true if a CONNECTION_CLOSE has already been emitted for
7168 * this connection.
7169 *
7170 * Returns false if connection is not in closing state else true. The caller
7171 * should drop the whole datagram in the last case to not mess up <qc>
7172 * CONNECTION_CLOSE rate limit counter.
7173 */
7174static int qc_rx_check_closing(struct quic_conn *qc,
7175 struct quic_rx_packet *pkt)
7176{
7177 if (!(qc->flags & QUIC_FL_CONN_CLOSING))
7178 return 0;
7179
7180 TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
7181
7182 /* Check if CONNECTION_CLOSE rate reemission is reached. */
7183 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
7184 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
7185 qc->nb_pkt_for_cc++;
7186 qc->nb_pkt_since_cc = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007187 }
7188
Amaury Denoyelle98289692022-10-19 15:37:44 +02007189 return 1;
7190}
7191
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007192/* React to a connection migration initiated on <qc> by a client with the new
7193 * path addresses <peer_addr>/<local_addr>.
7194 *
7195 * Returns 0 on success else non-zero.
7196 */
7197static int qc_handle_conn_migration(struct quic_conn *qc,
7198 const struct sockaddr_storage *peer_addr,
7199 const struct sockaddr_storage *local_addr)
7200{
7201 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7202
Frédéric Lécaille6fc86972023-01-12 08:29:23 +01007203 /* RFC 9000. Connection Migration
7204 *
7205 * If the peer sent the disable_active_migration transport parameter,
7206 * an endpoint also MUST NOT send packets (including probing packets;
7207 * see Section 9.1) from a different local address to the address the peer
7208 * used during the handshake, unless the endpoint has acted on a
7209 * preferred_address transport parameter from the peer.
7210 */
7211 if (qc->li->bind_conf->quic_params.disable_active_migration) {
7212 TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
7213 goto err;
7214 }
7215
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007216 /* RFC 9000 9. Connection Migration
7217 *
Amaury Denoyelleeb6be982022-11-21 11:14:45 +01007218 * The design of QUIC relies on endpoints retaining a stable address for
7219 * the duration of the handshake. An endpoint MUST NOT initiate
7220 * connection migration before the handshake is confirmed, as defined in
7221 * Section 4.1.2 of [QUIC-TLS].
7222 */
7223 if (qc->state < QUIC_HS_ST_COMPLETE) {
7224 TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
7225 goto err;
7226 }
7227
7228 /* RFC 9000 9. Connection Migration
7229 *
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007230 * TODO
7231 * An endpoint MUST
7232 * perform path validation (Section 8.2) if it detects any change to a
7233 * peer's address, unless it has previously validated that address.
7234 */
7235
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007236 /* Update quic-conn owned socket if in used.
7237 * TODO try to reuse it instead of closing and opening a new one.
7238 */
7239 if (qc_test_fd(qc)) {
7240 /* TODO try to reuse socket instead of closing it and opening a new one. */
7241 TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
7242 qc_release_fd(qc, 1);
Amaury Denoyellefb375572023-02-01 09:28:32 +01007243 /* TODO need to adjust <jobs> on socket allocation failure. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01007244 qc_alloc_fd(qc, local_addr, peer_addr);
7245 }
7246
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007247 qc->local_addr = *local_addr;
7248 qc->peer_addr = *peer_addr;
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007249 qc->cntrs.conn_migration_done++;
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007250
7251 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7252 return 0;
7253
7254 err:
7255 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7256 return 1;
7257}
7258
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007259/* Release the memory for the RX packets which are no more referenced
7260 * and consume their payloads which have been copied to the RX buffer
7261 * for the connection.
7262 * Always succeeds.
7263 */
7264static inline void quic_rx_pkts_del(struct quic_conn *qc)
7265{
7266 struct quic_rx_packet *pkt, *pktback;
7267
7268 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
7269 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007270 "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007271 (long long)pkt->pn_node.key,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007272 pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007273 (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
7274 if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
7275 size_t cdata;
7276
7277 cdata = b_contig_data(&qc->rx.buf, 0);
7278 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007279 "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007280 if (cdata && !*b_head(&qc->rx.buf)) {
7281 /* Consume the remaining data */
7282 b_del(&qc->rx.buf, cdata);
7283 }
7284 break;
7285 }
7286
7287 if (pkt->refcnt)
7288 break;
7289
7290 b_del(&qc->rx.buf, pkt->raw_len);
7291 LIST_DELETE(&pkt->qc_rx_pkt_list);
7292 pool_free(pool_head_quic_rx_packet, pkt);
7293 }
7294
7295 /* In frequent cases the buffer will be emptied at this stage. */
7296 b_realign_if_empty(&qc->rx.buf);
7297}
7298
Amaury Denoyelle98289692022-10-19 15:37:44 +02007299/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
7300 * into <qc> receive buffer after header protection removal procedure.
7301 *
7302 * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
7303 * must point to packet buffer first byte.
7304 *
7305 * <tasklist_head> may be non-NULL when the caller treat several datagrams for
7306 * different quic-conn. In this case, each quic-conn tasklet will be appended
7307 * to it in order to be woken up after the current task.
7308 *
7309 * The caller can safely removed the packet data. If packet refcount was not
7310 * incremented by this function, it means that the connection did not handled
7311 * it and it should be freed by the caller.
7312 */
7313static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
7314 struct quic_dgram *dgram, unsigned char *beg,
7315 struct list **tasklist_head)
7316{
7317 const struct quic_version *qv = pkt->version;
7318 struct quic_enc_level *qel = NULL;
7319 size_t b_cspace;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007320
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007321 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7322 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Amaury Denoyelle3f474e62022-11-24 17:15:08 +01007323
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02007324 if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007325 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
7326 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
7327 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007328 TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
7329 QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007330 /* Reset the anti-amplification bit. It will be set again
7331 * when sending the next packet if reached again.
7332 */
7333 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea65b71f2023-03-03 10:16:32 +01007334 qc_set_timer(qc);
7335 if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
7336 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007337 }
7338
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007339 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
7340 TRACE_PROTO("Connection error",
7341 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7342 goto out;
7343 }
7344
7345 pkt->raw_len = pkt->len;
7346 quic_rx_pkts_del(qc);
7347 b_cspace = b_contig_space(&qc->rx.buf);
7348 if (b_cspace < pkt->len) {
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01007349 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01007350 "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007351 /* Do not consume buf if space not at the end. */
7352 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
7353 TRACE_PROTO("Packet dropped",
7354 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007355 qc->cntrs.dropped_pkt_bufoverrun++;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007356 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007357 }
7358
7359 /* Let us consume the remaining contiguous space. */
7360 if (b_cspace) {
7361 b_putchr(&qc->rx.buf, 0x00);
7362 b_cspace--;
7363 }
7364 b_add(&qc->rx.buf, b_cspace);
7365 if (b_contig_space(&qc->rx.buf) < pkt->len) {
7366 TRACE_PROTO("Too big packet",
7367 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007368 qc->cntrs.dropped_pkt_bufoverrun++;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007369 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007370 }
7371 }
7372
Amaury Denoyelle845169d2022-10-17 18:05:26 +02007373 if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007374 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
7375 goto drop;
7376 }
7377
7378 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7379 if (pkt->aad_len)
7380 qc_pkt_insert(qc, pkt, qel);
7381 out:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02007382 *tasklist_head = tasklet_wakeup_after(*tasklist_head,
7383 qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007384
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02007385 drop_silent:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007386 TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
7387 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007388 return;
7389
7390 drop:
Frédéric Lécaillebdd64fd2023-05-24 11:10:19 +02007391 qc->cntrs.dropped_pkt++;
Frédéric Lécaille464281a2023-05-24 10:24:42 +02007392 TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
7393 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007394}
7395
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007396/* This function builds into a buffer at <pos> position a QUIC long packet header,
7397 * <end> being one byte past the end of this buffer.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007398 * Return 1 if enough room to build this header, 0 if not.
7399 */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007400static int quic_build_packet_long_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007401 int type, size_t pn_len,
7402 struct quic_conn *qc, const struct quic_version *ver)
7403{
7404 int ret = 0;
7405
7406 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
7407
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007408 if (end - *pos < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007409 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7410 goto leave;
7411 }
7412
7413 type = quic_pkt_type(type, ver->num);
7414 /* #0 byte flags */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007415 *(*pos)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007416 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
7417 /* Version */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007418 quic_write_uint32(pos, end, ver->num);
7419 *(*pos)++ = qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007420 /* Destination connection ID */
7421 if (qc->dcid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007422 memcpy(*pos, qc->dcid.data, qc->dcid.len);
7423 *pos += qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007424 }
7425 /* Source connection ID */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007426 *(*pos)++ = qc->scid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007427 if (qc->scid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007428 memcpy(*pos, qc->scid.data, qc->scid.len);
7429 *pos += qc->scid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007430 }
7431
7432 ret = 1;
7433 leave:
7434 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
7435 return ret;
7436}
7437
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007438/* This function builds into a buffer at <pos> position a QUIC short packet header,
7439 * <end> being one byte past the end of this buffer.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007440 * Return 1 if enough room to build this header, 0 if not.
7441 */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007442static int quic_build_packet_short_header(unsigned char **pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007443 size_t pn_len, struct quic_conn *qc,
7444 unsigned char tls_flags)
7445{
7446 int ret = 0;
Frédéric Lécailleece86e62023-03-07 11:53:43 +01007447 unsigned char spin_bit =
7448 (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007449
7450 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7451
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007452 if (end - *pos < 1 + qc->dcid.len) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007453 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
7454 goto leave;
7455 }
7456
7457 /* #0 byte flags */
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007458 *(*pos)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007459 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
7460 /* Destination connection ID */
7461 if (qc->dcid.len) {
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007462 memcpy(*pos, qc->dcid.data, qc->dcid.len);
7463 *pos += qc->dcid.len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007464 }
7465
7466 ret = 1;
7467 leave:
7468 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7469 return ret;
7470}
7471
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007472/* Apply QUIC header protection to the packet with <pos> as first byte address,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007473 * <pn> as address of the Packet number field, <pnlen> being this field length
7474 * with <aead> as AEAD cipher and <key> as secret key.
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007475 *
7476 * TODO no error is expected as encryption is done in place but encryption
7477 * manual is unclear. <fail> will be set to true if an error is detected.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007478 */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007479void quic_apply_header_protection(struct quic_conn *qc, unsigned char *pos,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007480 unsigned char *pn, size_t pnlen,
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007481 struct quic_tls_ctx *tls_ctx, int *fail)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007482
7483{
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007484 int i;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007485 /* We need an IV of at least 5 bytes: one byte for bytes #0
7486 * and at most 4 bytes for the packet number
7487 */
7488 unsigned char mask[5] = {0};
7489 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
7490
7491 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7492
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007493 *fail = 0;
7494
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007495 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
7496 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02007497 *fail = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007498 goto out;
7499 }
7500
Frédéric Lécaille3adb9e82023-04-24 14:54:48 +02007501 *pos ^= mask[0] & (*pos & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007502 for (i = 0; i < pnlen; i++)
7503 pn[i] ^= mask[i + 1];
7504
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007505 out:
7506 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007507}
7508
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007509/* Prepare into <outlist> as most as possible ack-eliciting frame from their
7510 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
7511 * with <room> as available room, and <*len> the packet Length field initialized
7512 * with the number of bytes already present in this buffer which must be taken
7513 * into an account for the Length packet field value. <headlen> is the number of
7514 * bytes already present in this packet before building frames.
7515 *
7516 * Update consequently <*len> to reflect the size of these frames built
7517 * by this function. Also attach these frames to <l> frame list.
7518 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
7519 */
7520static inline int qc_build_frms(struct list *outlist, struct list *inlist,
7521 size_t room, size_t *len, size_t headlen,
7522 struct quic_enc_level *qel,
7523 struct quic_conn *qc)
7524{
7525 int ret;
7526 struct quic_frame *cf, *cfbak;
7527
7528 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
7529
7530 ret = 0;
7531 if (*len > room)
7532 goto leave;
7533
7534 /* If we are not probing we must take into an account the congestion
7535 * control window.
7536 */
7537 if (!qel->pktns->tx.pto_probe) {
7538 size_t remain = quic_path_prep_data(qc->path);
7539
7540 if (headlen > remain)
7541 goto leave;
7542
7543 room = QUIC_MIN(room, remain - headlen);
7544 }
7545
Frédéric Lécaille8f991942023-03-24 15:14:45 +01007546 TRACE_PROTO("TX frms build (headlen)",
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007547 QUIC_EV_CONN_BCFRMS, qc, &headlen);
7548
7549 /* NOTE: switch/case block inside a loop, a successful status must be
7550 * returned by this function only if at least one frame could be built
7551 * in the switch/case block.
7552 */
7553 list_for_each_entry_safe(cf, cfbak, inlist, list) {
7554 /* header length, data length, frame length. */
7555 size_t hlen, dlen, dlen_sz, avail_room, flen;
7556
7557 if (!room)
7558 break;
7559
7560 switch (cf->type) {
7561 case QUIC_FT_CRYPTO:
7562 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
7563 QUIC_EV_CONN_BCFRMS, qc, &room, len);
7564 /* Compute the length of this CRYPTO frame header */
7565 hlen = 1 + quic_int_getsize(cf->crypto.offset);
7566 /* Compute the data length of this CRyPTO frame. */
7567 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
7568 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
7569 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
7570 if (!dlen)
7571 continue;
7572
7573 /* CRYPTO frame length. */
7574 flen = hlen + quic_int_getsize(dlen) + dlen;
7575 TRACE_DEVEL(" CRYPTO frame length (flen)",
7576 QUIC_EV_CONN_BCFRMS, qc, &flen);
7577 /* Add the CRYPTO data length and its encoded length to the packet
7578 * length and the length of this length.
7579 */
7580 *len += flen;
7581 room -= flen;
7582 if (dlen == cf->crypto.len) {
7583 /* <cf> CRYPTO data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007584 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007585 LIST_APPEND(outlist, &cf->list);
7586 }
7587 else {
7588 struct quic_frame *new_cf;
7589
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007590 new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007591 if (!new_cf) {
7592 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
7593 continue;
7594 }
7595
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007596 new_cf->crypto.len = dlen;
7597 new_cf->crypto.offset = cf->crypto.offset;
7598 new_cf->crypto.qel = qel;
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 the remaining CRYPTO frame from its original frame */
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 /* Consume <dlen> bytes of the current frame. */
7611 cf->crypto.len -= dlen;
7612 cf->crypto.offset += dlen;
7613 }
7614 break;
7615
7616 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007617 if (cf->stream.dup) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007618 struct eb64_node *node = NULL;
7619 struct qc_stream_desc *stream_desc = NULL;
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007620 struct qf_stream *strm_frm = &cf->stream;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007621
7622 /* As this frame has been already lost, ensure the stream is always
7623 * available or the range of this frame is not consumed before
7624 * resending it.
7625 */
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007626 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007627 if (!node) {
7628 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007629 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007630 continue;
7631 }
7632
7633 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007634 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007635 TRACE_DEVEL("ignored frame frame in already acked range",
7636 QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007637 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007638 continue;
7639 }
Amaury Denoyelled5f03cd2023-04-24 15:32:23 +02007640 else if (strm_frm->offset.key < stream_desc->ack_offset) {
7641 uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
Frédéric Lécailleca079792023-03-17 08:56:50 +01007642
Frédéric Lécaillec425e032023-03-20 14:32:59 +01007643 qc_stream_frm_mv_fwd(cf, diff);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007644 TRACE_DEVEL("updated partially acked frame",
7645 QUIC_EV_CONN_PRSAFRM, qc, cf);
7646 }
7647 }
7648 /* Note that these frames are accepted in short packets only without
7649 * "Length" packet field. Here, <*len> is used only to compute the
7650 * sum of the lengths of the already built frames for this packet.
7651 *
7652 * Compute the length of this STREAM frame "header" made a all the field
7653 * excepting the variable ones. Note that +1 is for the type of this frame.
7654 */
7655 hlen = 1 + quic_int_getsize(cf->stream.id) +
7656 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
7657 /* Compute the data length of this STREAM frame. */
7658 avail_room = room - hlen - *len;
7659 if ((ssize_t)avail_room <= 0)
7660 continue;
7661
7662 TRACE_DEVEL(" New STREAM frame build (room, len)",
7663 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007664
7665 /* hlen contains STREAM id and offset. Ensure there is
7666 * enough room for length field.
7667 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007668 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007669 dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
7670 cf->stream.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007671 dlen_sz = quic_int_getsize(dlen);
7672 flen = hlen + dlen_sz + dlen;
7673 }
7674 else {
7675 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
7676 flen = hlen + dlen;
7677 }
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007678
7679 if (cf->stream.len && !dlen) {
7680 /* Only a small gap is left on buffer, not
7681 * enough to encode the STREAM data length.
7682 */
7683 continue;
7684 }
7685
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007686 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
7687 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
7688 TRACE_DEVEL(" STREAM frame length (flen)",
7689 QUIC_EV_CONN_BCFRMS, qc, &flen);
7690 /* Add the STREAM data length and its encoded length to the packet
7691 * length and the length of this length.
7692 */
7693 *len += flen;
7694 room -= flen;
7695 if (dlen == cf->stream.len) {
7696 /* <cf> STREAM data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007697 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007698 LIST_APPEND(outlist, &cf->list);
7699
7700 /* Do not notify MUX on retransmission. */
7701 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7702 qcc_streams_sent_done(cf->stream.stream->ctx,
7703 cf->stream.len,
7704 cf->stream.offset.key);
7705 }
7706 }
7707 else {
7708 struct quic_frame *new_cf;
7709 struct buffer cf_buf;
7710
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007711 new_cf = qc_frm_alloc(cf->type);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007712 if (!new_cf) {
7713 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
7714 continue;
7715 }
7716
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007717 new_cf->stream.stream = cf->stream.stream;
7718 new_cf->stream.buf = cf->stream.buf;
7719 new_cf->stream.id = cf->stream.id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01007720 new_cf->stream.offset = cf->stream.offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007721 new_cf->stream.len = dlen;
7722 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
7723 /* FIN bit reset */
7724 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
7725 new_cf->stream.data = cf->stream.data;
Amaury Denoyellec8a0efb2023-02-22 10:44:27 +01007726 new_cf->stream.dup = cf->stream.dup;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007727 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007728 if (cf->origin) {
7729 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7730 /* This <cf> frame was duplicated */
7731 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7732 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007733 /* Detach this STREAM frame from its origin */
7734 LIST_DEL_INIT(&cf->ref);
7735 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007736 }
7737 LIST_APPEND(outlist, &new_cf->list);
7738 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
7739 /* Consume <dlen> bytes of the current frame. */
7740 cf_buf = b_make(b_orig(cf->stream.buf),
7741 b_size(cf->stream.buf),
7742 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
7743 cf->stream.len -= dlen;
7744 cf->stream.offset.key += dlen;
7745 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
7746
7747 /* Do not notify MUX on retransmission. */
7748 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7749 qcc_streams_sent_done(new_cf->stream.stream->ctx,
7750 new_cf->stream.len,
7751 new_cf->stream.offset.key);
7752 }
7753 }
7754
7755 /* TODO the MUX is notified about the frame sending via
7756 * previous qcc_streams_sent_done call. However, the
7757 * sending can fail later, for example if the sendto
7758 * system call returns an error. As the MUX has been
7759 * notified, the transport layer is responsible to
7760 * bufferize and resent the announced data later.
7761 */
7762
7763 break;
7764
7765 default:
7766 flen = qc_frm_len(cf);
7767 BUG_ON(!flen);
7768 if (flen > room)
7769 continue;
7770
7771 *len += flen;
7772 room -= flen;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007773 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007774 LIST_APPEND(outlist, &cf->list);
7775 break;
7776 }
7777
7778 /* Successful status as soon as a frame could be built */
7779 ret = 1;
7780 }
7781
7782 leave:
7783 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
7784 return ret;
7785}
7786
7787/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
7788 * is used as return parameter and should be zero'ed by the caller.
7789 */
7790static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
7791 struct quic_frame *out)
7792{
7793 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
7794 *
7795 * A CONNECTION_CLOSE frame should be sent in several packets with
7796 * different encryption levels depending on the client context. This is
7797 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
7798 * more details on how to implement it.
7799 */
7800 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
7801
7802
7803 if (qc->err.app) {
7804 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
7805 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
7806 /* RFC 9000 10.2.3. Immediate Close during the Handshake
7807 *
7808 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
7809 * packet could expose application state or be used to alter application
7810 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
7811 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
7812 * Handshake packets. Otherwise, information about the application
7813 * state might be revealed. Endpoints MUST clear the value of the
7814 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
7815 * converting to a CONNECTION_CLOSE of type 0x1c.
7816 */
7817 out->type = QUIC_FT_CONNECTION_CLOSE;
7818 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
7819 out->connection_close.reason_phrase_len = 0;
7820 }
7821 else {
7822 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
7823 out->connection_close.error_code = qc->err.code;
7824 }
7825 }
7826 else {
7827 out->type = QUIC_FT_CONNECTION_CLOSE;
7828 out->connection_close.error_code = qc->err.code;
7829 }
7830 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
7831
7832}
7833
7834/* This function builds a clear packet from <pkt> information (its type)
7835 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
7836 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
7837 * filling the buffer with as much frames as possible from <frms> list of
7838 * prebuilt frames.
7839 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
7840 * reserved so that to ensure there is enough room to build this AEAD TAG after
7841 * having returned from this function.
7842 * This function also updates the value of <buf_pn> pointer to point to the packet
7843 * number field in this packet. <pn_len> will also have the packet number
7844 * length as value.
7845 *
7846 * Return 1 if succeeded (enough room to buile this packet), O if not.
7847 */
7848static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
7849 size_t dglen, struct quic_tx_packet *pkt,
7850 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007851 int must_ack, int padding, int cc, int probe,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007852 struct quic_enc_level *qel, struct quic_conn *qc,
7853 const struct quic_version *ver, struct list *frms)
7854{
7855 unsigned char *beg, *payload;
7856 size_t len, len_sz, len_frms, padding_len;
7857 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
7858 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
7859 struct quic_frame cc_frm = { };
7860 size_t ack_frm_len, head_len;
7861 int64_t rx_largest_acked_pn;
7862 int add_ping_frm;
7863 struct list frm_list = LIST_HEAD_INIT(frm_list);
7864 struct quic_frame *cf;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007865 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007866
7867 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7868
7869 /* Length field value with CRYPTO frames if present. */
7870 len_frms = 0;
7871 beg = pos;
7872 /* When not probing, and no immediate close is required, reduce the size of this
7873 * buffer to respect the congestion controller window.
7874 * This size will be limited if we have ack-eliciting frames to send from <frms>.
7875 */
7876 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
7877 size_t path_room;
7878
7879 path_room = quic_path_prep_data(qc->path);
7880 if (end - beg > path_room)
7881 end = beg + path_room;
7882 }
7883
7884 /* Ensure there is enough room for the TLS encryption tag and a zero token
7885 * length field if any.
7886 */
7887 if (end - pos < QUIC_TLS_TAG_LEN +
7888 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
7889 goto no_room;
7890
7891 end -= QUIC_TLS_TAG_LEN;
7892 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
7893 /* packet number length */
7894 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
7895 /* Build the header */
7896 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
7897 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
7898 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
7899 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
7900 goto no_room;
7901
7902 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02007903 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
7904 if (end <= pos)
7905 goto no_room;
7906
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007907 *pos++ = 0;
Frédéric Lécaille45662ef2023-04-18 14:42:40 +02007908 }
7909
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007910 head_len = pos - beg;
7911 /* Build an ACK frame if required. */
7912 ack_frm_len = 0;
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02007913 /* Do not ack and probe at the same time. */
7914 if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007915 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
7916 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
7917 ack_frm.tx_ack.arngs = arngs;
7918 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
7919 qel->pktns->tx.ack_delay =
7920 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
7921 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
7922 }
7923 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
7924 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
7925 * smallest frame (PING) and <*pn_len> more for the packet number. Note
7926 * that from here, we do not know if we will have to send a PING frame.
7927 * This will be decided after having computed the ack-eliciting frames
7928 * to be added to this packet.
7929 */
Frédéric Lécaille9d68c6a2023-04-12 20:49:29 +02007930 if (end - pos <= 1 + *pn_len)
7931 goto no_room;
7932
Frédéric Lécaille4b2627b2023-04-17 13:42:42 +02007933 ack_frm_len = qc_frm_len(&ack_frm);
7934 if (ack_frm_len > end - 1 - *pn_len - pos)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007935 goto no_room;
7936 }
7937
7938 /* Length field value without the ack-eliciting frames. */
7939 len = ack_frm_len + *pn_len;
7940 len_frms = 0;
7941 if (!cc && !LIST_ISEMPTY(frms)) {
7942 ssize_t room = end - pos;
7943
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02007944 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007945 /* Initialize the length of the frames built below to <len>.
7946 * If any frame could be successfully built by qc_build_frms(),
7947 * we will have len_frms > len.
7948 */
7949 len_frms = len;
7950 if (!qc_build_frms(&frm_list, frms,
7951 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02007952 TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007953 qc, NULL, NULL, &room);
7954 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
7955 goto no_room;
7956 }
7957 }
7958
7959 /* Length (of the remaining data). Must not fail because, the buffer size
7960 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
7961 * for the encryption tag. It must be taken into an account for the length
7962 * of this packet.
7963 */
7964 if (len_frms)
7965 len = len_frms + QUIC_TLS_TAG_LEN;
7966 else
7967 len += QUIC_TLS_TAG_LEN;
7968 /* CONNECTION_CLOSE frame */
7969 if (cc) {
7970 qc_build_cc_frm(qc, qel, &cc_frm);
7971 len += qc_frm_len(&cc_frm);
7972 }
7973 add_ping_frm = 0;
7974 padding_len = 0;
7975 len_sz = quic_int_getsize(len);
7976 /* Add this packet size to <dglen> */
7977 dglen += head_len + len_sz + len;
Frédéric Lécailleec937212023-03-03 17:34:41 +01007978 /* Note that <padding> is true only when building an Handshake packet
7979 * coalesced to an Initial packet.
7980 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007981 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
7982 /* This is a maximum padding size */
7983 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7984 /* The length field value is of this packet is <len> + <padding_len>
7985 * the size of which may be greater than the initial computed size
7986 * <len_sz>. So, let's deduce the difference between these to packet
7987 * sizes from <padding_len>.
7988 */
7989 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7990 len += padding_len;
7991 }
Frédéric Lécaille5faf5772023-02-16 17:30:53 +01007992 else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
7993 len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
7994 }
7995 else if (LIST_ISEMPTY(&frm_list)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007996 if (qel->pktns->tx.pto_probe) {
7997 /* If we cannot send a frame, we send a PING frame. */
7998 add_ping_frm = 1;
7999 len += 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01008000 dglen += 1;
8001 /* Note that only we are in the case where this Initial packet
8002 * is not coalesced to an Handshake packet. We must directly
8003 * pad the datragram.
8004 */
Frédéric Lécaille9c317b12023-03-28 15:39:11 +02008005 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
8006 if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
8007 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
8008 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
8009 len += padding_len;
8010 }
8011 }
8012 else {
8013 /* Note that +1 is for the PING frame */
8014 if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
8015 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
Frédéric Lécailleec937212023-03-03 17:34:41 +01008016 }
8017 }
8018 else {
8019 /* If there is no frame at all to follow, add at least a PADDING frame. */
8020 if (!ack_frm_len && !cc)
8021 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008022 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008023 }
8024
8025 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
8026 goto no_room;
8027
8028 /* Packet number field address. */
8029 *buf_pn = pos;
8030
8031 /* Packet number encoding. */
8032 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
8033 goto no_room;
8034
8035 /* payload building (ack-eliciting or not frames) */
8036 payload = pos;
8037 if (ack_frm_len) {
8038 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
8039 goto no_room;
8040
8041 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
8042 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
8043 }
8044
8045 /* Ack-eliciting frames */
8046 if (!LIST_ISEMPTY(&frm_list)) {
8047 struct quic_frame *tmp_cf;
8048 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
8049 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
8050 ssize_t room = end - pos;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008051 TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008052 qc, NULL, NULL, &room);
8053 /* Note that <cf> was added from <frms> to <frm_list> list by
8054 * qc_build_frms().
8055 */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01008056 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008057 LIST_INSERT(frms, &cf->list);
8058 continue;
8059 }
8060
8061 quic_tx_packet_refinc(pkt);
8062 cf->pkt = pkt;
8063 }
8064 }
8065
8066 /* Build a PING frame if needed. */
8067 if (add_ping_frm) {
8068 frm.type = QUIC_FT_PING;
8069 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
8070 goto no_room;
8071 }
8072
8073 /* Build a CONNECTION_CLOSE frame if needed. */
8074 if (cc) {
8075 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
8076 goto no_room;
8077
8078 pkt->flags |= QUIC_FL_TX_PACKET_CC;
8079 }
8080
8081 /* Build a PADDING frame if needed. */
8082 if (padding_len) {
8083 frm.type = QUIC_FT_PADDING;
8084 frm.padding.len = padding_len;
8085 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
8086 goto no_room;
8087 }
8088
8089 if (pos == payload) {
8090 /* No payload was built because of congestion control */
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008091 TRACE_PROTO("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008092 goto no_room;
8093 }
8094
8095 /* If this packet is ack-eliciting and we are probing let's
8096 * decrement the PTO probe counter.
8097 */
8098 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
8099 qel->pktns->tx.pto_probe)
8100 qel->pktns->tx.pto_probe--;
8101
8102 pkt->len = pos - beg;
8103 LIST_SPLICE(&pkt->frms, &frm_list);
8104
8105 ret = 1;
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008106 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008107 leave:
8108 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
8109 return ret;
8110
8111 no_room:
8112 /* Replace the pre-built frames which could not be add to this packet */
8113 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillee95e00e2023-04-24 10:59:33 +02008114 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008115 goto leave;
8116}
8117
8118static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
8119{
8120 pkt->type = type;
8121 pkt->len = 0;
8122 pkt->in_flight_len = 0;
8123 pkt->pn_node.key = (uint64_t)-1;
8124 LIST_INIT(&pkt->frms);
8125 pkt->time_sent = TICK_ETERNITY;
8126 pkt->next = NULL;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01008127 pkt->prev = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008128 pkt->largest_acked_pn = -1;
8129 pkt->flags = 0;
8130 pkt->refcnt = 0;
8131}
8132
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008133/* Build a packet into a buffer at <pos> position, <end> pointing to one byte past
8134 * the end of this buffer, with <pkt_type> as packet type for <qc> QUIC connection
8135 * at <qel> encryption level with <frms> list of prebuilt frames.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008136 *
8137 * Return -2 if the packet could not be allocated or encrypted for any reason,
8138 * -1 if there was not enough room to build a packet.
8139 * XXX NOTE XXX
8140 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
8141 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
8142 * control window limitation.
8143 */
8144static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008145 const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008146 struct quic_enc_level *qel,
8147 struct quic_tls_ctx *tls_ctx, struct list *frms,
8148 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008149 size_t dglen, int pkt_type, int must_ack,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008150 int padding, int probe, int cc, int *err)
8151{
8152 struct quic_tx_packet *ret_pkt = NULL;
8153 /* The pointer to the packet number field. */
8154 unsigned char *buf_pn;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008155 unsigned char *first_byte, *last_byte, *payload;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008156 int64_t pn;
8157 size_t pn_len, payload_len, aad_len;
8158 struct quic_tx_packet *pkt;
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008159 int encrypt_failure = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008160
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008161 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
8162 TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008163 *err = 0;
8164 pkt = pool_alloc(pool_head_quic_tx_packet);
8165 if (!pkt) {
8166 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
8167 *err = -2;
8168 goto err;
8169 }
8170
8171 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008172 first_byte = *pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008173 pn_len = 0;
8174 buf_pn = NULL;
8175
8176 pn = qel->pktns->tx.next_pn + 1;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008177 if (!qc_do_build_pkt(*pos, end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille45bf1a82023-04-12 18:51:49 +02008178 must_ack, padding, cc, probe, qel, qc, ver, frms)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008179 // trace already emitted by function above
8180 *err = -1;
8181 goto err;
8182 }
8183
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008184 last_byte = first_byte + pkt->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008185 payload = buf_pn + pn_len;
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008186 payload_len = last_byte - payload;
8187 aad_len = payload - first_byte;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008188
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008189 quic_packet_encrypt(payload, payload_len, first_byte, aad_len, pn, tls_ctx, qc, &encrypt_failure);
8190 if (encrypt_failure) {
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02008191 /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008192 WARN_ON("quic_packet_encrypt failure");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008193 *err = -2;
8194 goto err;
8195 }
8196
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008197 last_byte += QUIC_TLS_TAG_LEN;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008198 pkt->len += QUIC_TLS_TAG_LEN;
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008199 quic_apply_header_protection(qc, first_byte, buf_pn, pn_len, tls_ctx, &encrypt_failure);
8200 if (encrypt_failure) {
Amaury Denoyelle7385ff32023-04-19 15:56:30 +02008201 /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
Amaury Denoyellef8fbb0b2023-05-16 18:23:37 +02008202 WARN_ON("quic_apply_header_protection failure");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008203 *err = -2;
8204 goto err;
8205 }
8206
8207 /* Consume a packet number */
8208 qel->pktns->tx.next_pn++;
8209 qc->tx.prep_bytes += pkt->len;
8210 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
8211 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
8212 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
8213 }
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008214
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008215 /* Now that a correct packet is built, let us consume <*pos> buffer. */
Frédéric Lécaille1e0f8252023-04-24 15:02:34 +02008216 *pos = last_byte;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008217 /* Attach the built packet to its tree. */
8218 pkt->pn_node.key = pn;
8219 /* Set the packet in fligth length for in flight packet only. */
8220 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
8221 pkt->in_flight_len = pkt->len;
8222 qc->path->prep_in_flight += pkt->len;
8223 }
Frédéric Lécailled7215712023-03-24 18:13:37 +01008224 /* Always reset this flag */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008225 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
8226 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
8227 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
8228 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
Frédéric Lécailled7215712023-03-24 18:13:37 +01008229 qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
8230 if (tick_isset(qc->ack_expire)) {
8231 qc->ack_expire = TICK_ETERNITY;
8232 qc->idle_timer_task->expire = qc->idle_expire;
8233 task_queue(qc->idle_timer_task);
Frédéric Lécaille495968e2023-04-03 17:42:05 +02008234 TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécailled7215712023-03-24 18:13:37 +01008235 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008236 }
8237
8238 pkt->pktns = qel->pktns;
8239
8240 ret_pkt = pkt;
8241 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +01008242 TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
8243 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008244 return ret_pkt;
8245
8246 err:
8247 /* TODO: what about the frames which have been built
8248 * for this packet.
8249 */
8250 free_quic_tx_packet(qc, pkt);
8251 goto leave;
8252}
8253
8254
8255static void __quic_conn_init(void)
8256{
8257 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
8258}
8259INITCALL0(STG_REGISTER, __quic_conn_init);
8260
8261static void __quic_conn_deinit(void)
8262{
8263 BIO_meth_free(ha_quic_meth);
8264}
8265REGISTER_POST_DEINIT(__quic_conn_deinit);
8266
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008267/* Handle a new <dgram> received. Parse each QUIC packets and copied their
8268 * content to a quic-conn instance. The datagram content can be released after
8269 * this function.
8270 *
8271 * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
8272 * to the connection instance. <li> is the attached listener. The caller is
8273 * responsible to ensure that the first packet is destined to this connection
8274 * by comparing CIDs.
8275 *
8276 * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
8277 * function will thus retrieve the connection from the CID tree or allocate a
8278 * new one if possible. <li> is the listener attached to the receiver.
8279 *
8280 * Returns 0 on success else non-zero. If an error happens, some packets from
8281 * the datagram may not have been parsed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008282 */
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008283int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
8284 struct listener *li)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008285{
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008286 struct quic_rx_packet *pkt;
8287 struct quic_conn *qc = NULL;
8288 unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008289 struct list *tasklist_head = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008290
8291 TRACE_ENTER(QUIC_EV_CONN_LPKT);
8292
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008293 pos = dgram->buf;
8294 end = pos + dgram->len;
8295 do {
8296 /* TODO replace zalloc -> alloc. */
8297 pkt = pool_zalloc(pool_head_quic_rx_packet);
8298 if (!pkt) {
8299 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
8300 goto err;
8301 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008302
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008303 pkt->version = NULL;
8304 pkt->pn_offset = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008305
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008306 /* Set flag if pkt is the first one in dgram. */
8307 if (pos == dgram->buf)
8308 pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008309
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008310 LIST_INIT(&pkt->qc_rx_pkt_list);
8311 pkt->time_received = now_ms;
8312 quic_rx_packet_refinc(pkt);
8313 if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
8314 goto next;
Amaury Denoyelle98289692022-10-19 15:37:44 +02008315
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008316 /* Search quic-conn instance for first packet of the datagram.
8317 * quic_rx_packet_parse() is responsible to discard packets
8318 * with different DCID as the first one in the same datagram.
8319 */
8320 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008321 int new_tid = -1;
8322
8323 qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li, &new_tid);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008324 /* qc is NULL if receiving a non Initial packet for an
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008325 * unknown connection or on connection affinity rebind.
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008326 */
8327 if (!qc) {
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008328 if (new_tid >= 0) {
8329 MT_LIST_APPEND(&quic_dghdlrs[new_tid].dgrams,
8330 &dgram->handler_list);
8331 tasklet_wakeup(quic_dghdlrs[new_tid].task);
8332 goto out;
8333 }
8334
Amaury Denoyelle98289692022-10-19 15:37:44 +02008335 /* Skip the entire datagram. */
8336 pkt->len = end - pos;
8337 goto next;
8338 }
8339
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008340 dgram->qc = qc;
8341 }
Amaury Denoyelle98289692022-10-19 15:37:44 +02008342
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008343 if (qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED)
8344 qc_finalize_affinity_rebind(qc);
8345
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008346 if (qc_rx_check_closing(qc, pkt)) {
8347 /* Skip the entire datagram. */
8348 pkt->len = end - pos;
8349 goto next;
8350 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008351
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008352 /* Detect QUIC connection migration. */
Frédéric Lécaillef6769542023-01-12 10:36:26 +01008353 if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01008354 if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
8355 /* Skip the entire datagram. */
8356 TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
8357 pkt->len = end - pos;
8358 goto next;
8359 }
8360 }
8361
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008362 qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008363
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008364 next:
8365 pos += pkt->len;
8366 quic_rx_packet_refdec(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008367
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008368 /* Free rejected packets */
8369 if (!pkt->refcnt) {
8370 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
8371 pool_free(pool_head_quic_rx_packet, pkt);
8372 }
8373 } while (pos < end);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008374
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008375 /* Increasing the received bytes counter by the UDP datagram length
8376 * if this datagram could be associated to a connection.
8377 */
8378 if (dgram->qc)
8379 dgram->qc->rx.bytes += dgram->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008380
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008381 /* This must never happen. */
8382 BUG_ON(pos > end);
8383 BUG_ON(pos < end || pos > dgram->buf + dgram->len);
8384 /* Mark this datagram as consumed */
8385 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008386
Amaury Denoyellef16ec342023-04-13 17:42:34 +02008387 out:
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008388 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
8389 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008390
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008391 err:
Amaury Denoyellea65dd3a2023-04-19 14:26:16 +02008392 /* Mark this datagram as consumed as maybe at least some packets were parsed. */
8393 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008394 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02008395 return -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008396}
8397
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008398/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
8399 * CIDs. This can be used to determine if a datagram is addressed to the right
8400 * connection instance.
8401 *
8402 * Returns a boolean value.
8403 */
8404int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len)
8405{
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008406 const uchar idx = _quic_cid_tree_idx(dcid);
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008407 struct quic_connection_id *conn_id;
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008408 struct ebmb_node *node = NULL;
8409 struct quic_cid_tree *tree = &quic_cid_trees[idx];
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008410
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008411 /* Test against our default CID or client ODCID. */
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008412 if ((qc->scid.len == dcid_len &&
8413 memcmp(qc->scid.data, dcid, dcid_len) == 0) ||
8414 (qc->odcid.len == dcid_len &&
Frédéric Lécaille07846cb2023-02-13 16:14:24 +01008415 memcmp(qc->odcid.data, dcid, dcid_len) == 0)) {
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008416 return 1;
8417 }
8418
Amaury Denoyellee83f9372023-04-18 11:10:54 +02008419 /* Test against our other CIDs. This can happen if the client has
8420 * decided to switch to a new one.
8421 *
8422 * TODO to avoid locking, loop through qc.cids as an alternative.
8423 *
8424 * TODO set it to our default CID to avoid this operation next time.
8425 */
8426 HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
8427 node = ebmb_lookup(&tree->root, dcid, dcid_len);
8428 HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
8429
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008430 if (node) {
Amaury Denoyelle591e7982023-04-12 10:04:49 +02008431 conn_id = ebmb_entry(node, struct quic_connection_id, node);
8432 if (qc == conn_id->qc)
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01008433 return 1;
8434 }
8435
8436 return 0;
8437}
8438
Willy Tarreaudd9f9212023-05-07 07:07:44 +02008439/* Retrieve the DCID from a QUIC datagram or packet at <pos> position,
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008440 * <end> being at one byte past the end of this datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008441 * Returns 1 if succeeded, 0 if not.
8442 */
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008443int quic_get_dgram_dcid(unsigned char *pos, const unsigned char *end,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008444 unsigned char **dcid, size_t *dcid_len)
8445{
8446 int ret = 0, long_header;
8447 size_t minlen, skip;
8448
8449 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
8450
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008451 if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008452 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
8453 goto err;
8454 }
8455
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008456 long_header = *pos & QUIC_PACKET_LONG_HEADER_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008457 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
8458 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
8459 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008460 if (end - pos < minlen)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008461 goto err;
8462
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008463 pos += skip;
8464 *dcid_len = long_header ? *pos++ : QUIC_HAP_CID_LEN;
8465 if (*dcid_len > QUIC_CID_MAXLEN || end - pos <= *dcid_len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008466 goto err;
8467
Frédéric Lécaille182934d2023-04-24 15:24:58 +02008468 *dcid = pos;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008469
8470 ret = 1;
8471 leave:
8472 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
8473 return ret;
8474
8475 err:
8476 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
8477 goto leave;
8478}
8479
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008480/* Notify upper layer of a fatal error which forces to close the connection. */
8481void qc_notify_err(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008482{
8483 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
8484
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008485 if (qc->mux_state == QC_MUX_READY) {
8486 TRACE_STATE("error notified to mux", QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008487
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008488 /* Mark socket as closed. */
8489 qc->conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
8490
8491 /* TODO quic-conn layer must stay active until MUX is released.
8492 * Thus, we have to wake up directly to ensure upper stream
8493 * layer will be notified of the error. If a proper separation
8494 * is made between MUX and quic-conn layer, wake up could be
8495 * conducted only with qc.subs.
8496 */
8497 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008498 }
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02008499
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008500 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
8501}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008502
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008503/* Wake-up upper layer for sending if all conditions are met :
8504 * - room in congestion window or probe packet to sent
8505 * - socket FD ready to sent or listener socket used
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008506 *
8507 * Returns 1 if upper layer has been woken up else 0.
8508 */
8509int qc_notify_send(struct quic_conn *qc)
8510{
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008511 const struct quic_pktns *pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8512
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008513 if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle8afe4b82023-03-21 11:39:24 +01008514 /* RFC 9002 7.5. Probe Timeout
8515 *
8516 * Probe packets MUST NOT be blocked by the congestion controller.
8517 */
8518 if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
Amaury Denoyellecaa16542023-02-28 15:11:26 +01008519 (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01008520 tasklet_wakeup(qc->subs->tasklet);
8521 qc->subs->events &= ~SUB_RETRY_SEND;
8522 if (!qc->subs->events)
8523 qc->subs = NULL;
8524
8525 return 1;
8526 }
8527 }
8528
8529 return 0;
8530}
8531
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008532/* Move a <qc> QUIC connection and its resources from the current thread to the
Willy Tarreau77d37b02023-04-20 19:03:49 +02008533 * new one <new_tid> optionally in association with <new_li> (since it may need
8534 * to change when migrating to a thread from a different group, otherwise leave
8535 * it NULL). After this call, the connection cannot be dereferenced anymore on
8536 * the current thread.
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008537 *
8538 * Returns 0 on success else non-zero.
8539 */
Willy Tarreau77d37b02023-04-20 19:03:49 +02008540int qc_set_tid_affinity(struct quic_conn *qc, uint new_tid, struct listener *new_li)
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008541{
8542 struct task *t1 = NULL, *t2 = NULL;
8543 struct tasklet *t3 = NULL;
8544
8545 struct quic_connection_id *conn_id;
8546 struct eb64_node *node;
8547
8548 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8549
8550 /* Pre-allocate all required resources. This ensures we do not left a
8551 * connection with only some of its field rebinded.
8552 */
8553 if (((t1 = task_new_on(new_tid)) == NULL) ||
8554 (qc->timer_task && (t2 = task_new_on(new_tid)) == NULL) ||
8555 (t3 = tasklet_new()) == NULL) {
8556 goto err;
8557 }
8558
8559 /* Reinit idle timer task. */
8560 task_kill(qc->idle_timer_task);
8561 t1->expire = qc->idle_timer_task->expire;
8562 qc->idle_timer_task = t1;
8563 qc->idle_timer_task->process = qc_idle_timer_task;
8564 qc->idle_timer_task->context = qc;
8565
8566 /* Reinit timer task if allocated. */
8567 if (qc->timer_task) {
8568 task_kill(qc->timer_task);
8569 qc->timer_task = t2;
8570 qc->timer_task->process = qc_process_timer;
8571 qc->timer_task->context = qc;
8572 }
8573
8574 /* Reinit IO tasklet. */
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008575 if (qc->wait_event.tasklet->state & TASK_IN_LIST)
8576 qc->flags |= QUIC_FL_CONN_IO_TO_REQUEUE;
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008577 tasklet_kill(qc->wait_event.tasklet);
8578 /* In most cases quic_conn_app_io_cb is used but for 0-RTT quic_conn_io_cb can be still activated. */
8579 t3->process = qc->wait_event.tasklet->process;
8580 qc->wait_event.tasklet = t3;
8581 qc->wait_event.tasklet->tid = new_tid;
8582 qc->wait_event.tasklet->context = qc;
8583 qc->wait_event.events = 0;
8584
8585 /* Rebind the connection FD. */
8586 if (qc_test_fd(qc)) {
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008587 /* Reading is reactivated by the new thread. */
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008588 fd_migrate_on(qc->fd, new_tid);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008589 }
8590
Amaury Denoyelle7b516d32023-04-26 16:12:12 +02008591 /* Remove conn from per-thread list instance. It will be hidden from
8592 * "show quic" until rebinding is completed.
8593 */
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008594 qc_detach_th_ctx_list(qc, 0);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008595
8596 node = eb64_first(&qc->cids);
8597 BUG_ON(!node || eb64_next(node)); /* One and only one CID must be present before affinity rebind. */
8598 conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
Willy Tarreau77d37b02023-04-20 19:03:49 +02008599
8600 /* At this point no connection was accounted for yet on this
8601 * listener so it's OK to just swap the pointer.
8602 */
8603 if (new_li && new_li != qc->li)
8604 qc->li = new_li;
8605
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008606 /* Rebinding is considered done when CID points to the new thread. No
8607 * access should be done to quic-conn instance after it.
8608 */
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008609 qc->flags |= QUIC_FL_CONN_AFFINITY_CHANGED;
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008610 HA_ATOMIC_STORE(&conn_id->tid, new_tid);
8611 qc = NULL;
8612
8613 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, NULL);
8614 return 0;
8615
8616 err:
8617 task_destroy(t1);
8618 task_destroy(t2);
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02008619 tasklet_free(t3);
Amaury Denoyelle25174d52023-04-05 17:52:05 +02008620
8621 TRACE_DEVEL("leaving on error", QUIC_EV_CONN_SET_AFFINITY, qc);
8622 return 1;
8623}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008624
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008625/* Must be called after qc_set_tid_affinity() on the new thread. */
8626void qc_finalize_affinity_rebind(struct quic_conn *qc)
8627{
8628 TRACE_ENTER(QUIC_EV_CONN_SET_AFFINITY, qc);
8629
Amaury Denoyelled6646dd2023-04-26 17:15:37 +02008630 /* This function must not be called twice after an affinity rebind. */
8631 BUG_ON(!(qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED));
8632 qc->flags &= ~QUIC_FL_CONN_AFFINITY_CHANGED;
8633
Amaury Denoyelle7b516d32023-04-26 16:12:12 +02008634 /* A connection must not pass to closing state until affinity rebind
8635 * is completed. Else quic_handle_stopping() may miss it during process
8636 * stopping cleanup.
8637 */
8638 BUG_ON(qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING));
8639
8640 /* Reinsert connection in ha_thread_ctx global list. */
8641 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
8642 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
8643
Amaury Denoyelle739de3f2023-04-11 14:42:31 +02008644 /* Reactivate FD polling if connection socket is active. */
8645 qc_want_recv(qc);
8646
8647 /* Reactivate timer task if needed. */
8648 qc_set_timer(qc);
8649
8650 /* Idle timer task is always active. */
8651 task_queue(qc->idle_timer_task);
8652
8653 /* Reactivate IO tasklet if needed. */
8654 if (qc->flags & QUIC_FL_CONN_IO_TO_REQUEUE) {
8655 tasklet_wakeup(qc->wait_event.tasklet);
8656 qc->flags &= ~QUIC_FL_CONN_IO_TO_REQUEUE;
8657 }
8658
8659 TRACE_LEAVE(QUIC_EV_CONN_SET_AFFINITY, qc);
8660}
8661
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008662enum quic_dump_format {
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008663 QUIC_DUMP_FMT_ONELINE,
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008664 QUIC_DUMP_FMT_FULL,
8665};
8666
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008667/* appctx context used by "show quic" command */
8668struct show_quic_ctx {
8669 unsigned int epoch;
8670 struct bref bref; /* back-reference to the quic-conn being dumped */
8671 unsigned int thr;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008672 int flags;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008673 enum quic_dump_format format;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008674};
8675
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008676#define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */
8677
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008678static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private)
8679{
8680 struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008681 int argc = 2;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008682
8683 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
8684 return 1;
8685
8686 ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1);
8687 ctx->thr = 0;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01008688 ctx->flags = 0;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008689 ctx->format = QUIC_DUMP_FMT_ONELINE;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008690
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008691 if (strcmp(args[argc], "oneline") == 0) {
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008692 /* format already used as default value */
8693 ++argc;
8694 }
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008695 else if (strcmp(args[argc], "full") == 0) {
8696 ctx->format = QUIC_DUMP_FMT_FULL;
8697 ++argc;
8698 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008699
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008700 while (*args[argc]) {
8701 if (strcmp(args[argc], "all") == 0)
8702 ctx->flags |= QC_CLI_FL_SHOW_ALL;
8703
8704 ++argc;
8705 }
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01008706
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008707 LIST_INIT(&ctx->bref.users);
8708
8709 return 0;
8710}
8711
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008712/* Dump for "show quic" with "oneline" format. */
8713static void dump_quic_oneline(struct show_quic_ctx *ctx, struct quic_conn *qc)
8714{
8715 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008716 int ret;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008717 unsigned char cid_len;
8718
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008719 ret = chunk_appendf(&trash, "%p[%02u]/%-.12s ", qc, ctx->thr,
8720 qc->li->bind_conf->frontend->id);
8721 chunk_appendf(&trash, "%*s", 36 - ret, " "); /* align output */
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008722
8723 /* State */
8724 if (qc->flags & QUIC_FL_CONN_CLOSING)
8725 chunk_appendf(&trash, "CLOSE ");
8726 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8727 chunk_appendf(&trash, "DRAIN ");
8728 else if (qc->state < QUIC_HS_ST_COMPLETE)
8729 chunk_appendf(&trash, "HDSHK ");
8730 else
8731 chunk_appendf(&trash, "ESTAB ");
8732
8733 /* Bytes in flight / Lost packets */
8734 chunk_appendf(&trash, "%9llu %6llu %6llu ",
8735 (ullong)qc->path->in_flight,
8736 (ullong)qc->path->ifae_pkts,
8737 (ullong)qc->path->loss.nb_lost_pkt);
8738
8739 /* Socket */
8740 if (qc->local_addr.ss_family == AF_INET ||
8741 qc->local_addr.ss_family == AF_INET6) {
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02008742 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8743 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
8744 chunk_appendf(&trash, "%15s:%-5s ", bufaddr, bufport);
8745
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008746 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8747 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008748 chunk_appendf(&trash, "%15s:%-5s ", bufaddr, bufport);
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008749
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008750 }
8751
8752 /* CIDs */
8753 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
8754 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
8755
8756 chunk_appendf(&trash, " ");
8757 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
8758 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
8759
8760 chunk_appendf(&trash, "\n");
8761}
8762
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008763/* Dump for "show quic" with "full" format. */
8764static void dump_quic_full(struct show_quic_ctx *ctx, struct quic_conn *qc)
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008765{
Frédéric Lécaillea3772e12023-03-21 13:42:43 +01008766 struct quic_pktns *pktns;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01008767 struct eb64_node *node;
8768 struct qc_stream_desc *stream;
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01008769 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02008770 int expire, i, addnl;
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01008771 unsigned char cid_len;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008772
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02008773 addnl = 0;
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008774 /* CIDs */
8775 chunk_appendf(&trash, "* %p[%02u]: scid=", qc, ctx->thr);
8776 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
8777 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
8778 while (cid_len++ < 20)
8779 chunk_appendf(&trash, "..");
8780
8781 chunk_appendf(&trash, " dcid=");
8782 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
8783 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
8784 while (cid_len++ < 20)
8785 chunk_appendf(&trash, "..");
8786
8787 chunk_appendf(&trash, "\n");
8788
8789 chunk_appendf(&trash, " loc. TPs:");
8790 quic_transport_params_dump(&trash, qc, &qc->rx.params);
8791 chunk_appendf(&trash, "\n");
8792 chunk_appendf(&trash, " rem. TPs:");
8793 quic_transport_params_dump(&trash, qc, &qc->tx.params);
8794 chunk_appendf(&trash, "\n");
8795
8796 /* Connection state */
8797 if (qc->flags & QUIC_FL_CONN_CLOSING)
8798 chunk_appendf(&trash, " st=closing ");
8799 else if (qc->flags & QUIC_FL_CONN_DRAINING)
8800 chunk_appendf(&trash, " st=draining ");
8801 else if (qc->state < QUIC_HS_ST_CONFIRMED)
8802 chunk_appendf(&trash, " st=handshake ");
8803 else
8804 chunk_appendf(&trash, " st=opened ");
8805
8806 if (qc->mux_state == QC_MUX_NULL)
8807 chunk_appendf(&trash, "mux=null ");
8808 else if (qc->mux_state == QC_MUX_READY)
8809 chunk_appendf(&trash, "mux=ready ");
8810 else
8811 chunk_appendf(&trash, "mux=released ");
8812
8813 expire = qc->idle_expire;
8814 chunk_appendf(&trash, "expire=%02ds ",
8815 TICKS_TO_MS(tick_remain(now_ms, expire)) / 1000);
8816
8817 chunk_appendf(&trash, "\n");
8818
8819 /* Socket */
8820 chunk_appendf(&trash, " fd=%d", qc->fd);
8821 if (qc->local_addr.ss_family == AF_INET ||
8822 qc->local_addr.ss_family == AF_INET6) {
8823 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
8824 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02008825 chunk_appendf(&trash, " local_addr=%s:%s", bufaddr, bufport);
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008826
8827 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
8828 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02008829 chunk_appendf(&trash, " foreign_addr=%s:%s", bufaddr, bufport);
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008830 }
8831
8832 chunk_appendf(&trash, "\n");
8833
8834 /* Packet number spaces information */
8835 pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL];
8836 chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu",
8837 pktns->rx.arngs.sz, pktns->tx.in_flight);
8838 pktns = &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE];
8839 chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
8840 pktns->rx.arngs.sz, pktns->tx.in_flight);
8841 pktns = &qc->pktns[QUIC_TLS_PKTNS_01RTT];
8842 chunk_appendf(&trash, " [01rtt] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
8843 pktns->rx.arngs.sz, pktns->tx.in_flight);
8844
8845 chunk_appendf(&trash, " srtt=%-4u rttvar=%-4u rttmin=%-4u ptoc=%-4u cwnd=%-6llu"
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02008846 " mcwnd=%-6llu sentpkts=%-6llu lostpkts=%-6llu\n",
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008847 qc->path->loss.srtt >> 3, qc->path->loss.rtt_var >> 2,
8848 qc->path->loss.rtt_min, qc->path->loss.pto_count, (ullong)qc->path->cwnd,
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02008849 (ullong)qc->path->mcwnd, (ullong)qc->cntrs.sent_pkt, (ullong)qc->path->loss.nb_lost_pkt);
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008850
Frédéric Lécaillea73563b2023-05-25 16:10:03 +02008851 if (qc->cntrs.dropped_pkt) {
8852 chunk_appendf(&trash, " droppkts=%-6llu", qc->cntrs.dropped_pkt);
8853 addnl = 1;
8854 }
8855 if (qc->cntrs.dropped_pkt_bufoverrun) {
8856 chunk_appendf(&trash, " dropbuff=%-6llu", qc->cntrs.dropped_pkt_bufoverrun);
8857 addnl = 1;
8858 }
8859 if (qc->cntrs.dropped_parsing) {
8860 chunk_appendf(&trash, " droppars=%-6llu", qc->cntrs.dropped_parsing);
8861 addnl = 1;
8862 }
8863 if (qc->cntrs.socket_full) {
8864 chunk_appendf(&trash, " sockfull=%-6llu", qc->cntrs.socket_full);
8865 addnl = 1;
8866 }
8867 if (qc->cntrs.sendto_err) {
8868 chunk_appendf(&trash, " sendtoerr=%-6llu", qc->cntrs.sendto_err);
8869 addnl = 1;
8870 }
8871 if (qc->cntrs.sendto_err_unknown) {
8872 chunk_appendf(&trash, " sendtounknerr=%-6llu", qc->cntrs.sendto_err);
8873 addnl = 1;
8874 }
8875 if (qc->cntrs.conn_migration_done) {
8876 chunk_appendf(&trash, " migrdone=%-6llu", qc->cntrs.conn_migration_done);
8877 addnl = 1;
8878 }
8879 if (qc->cntrs.data_blocked) {
8880 chunk_appendf(&trash, " datablocked=%-6llu", qc->cntrs.data_blocked);
8881 addnl = 1;
8882 }
8883 if (qc->cntrs.stream_data_blocked) {
8884 chunk_appendf(&trash, " sdatablocked=%-6llu", qc->cntrs.stream_data_blocked);
8885 addnl = 1;
8886 }
8887 if (qc->cntrs.streams_blocked_bidi) {
8888 chunk_appendf(&trash, " sblockebidi=%-6llu", qc->cntrs.streams_blocked_bidi);
8889 addnl = 1;
8890 }
8891 if (qc->cntrs.streams_blocked_uni) {
8892 chunk_appendf(&trash, " sblockeduni=%-6llu", qc->cntrs.streams_blocked_uni);
8893 addnl = 1;
8894 }
8895 if (addnl)
8896 chunk_appendf(&trash, "\n");
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008897
8898 /* Streams */
8899 node = eb64_first(&qc->streams_by_id);
8900 i = 0;
8901 while (node) {
8902 stream = eb64_entry(node, struct qc_stream_desc, by_id);
8903 node = eb64_next(node);
8904
8905 chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key);
8906 chunk_appendf(&trash, " off=%-8llu ack=%-8llu",
8907 (unsigned long long)stream->buf_offset,
8908 (unsigned long long)stream->ack_offset);
8909
8910 if (!(++i % 3)) {
8911 chunk_appendf(&trash, "\n");
8912 i = 0;
8913 }
8914 }
8915
8916 chunk_appendf(&trash, "\n");
8917}
8918
8919static int cli_io_handler_dump_quic(struct appctx *appctx)
8920{
8921 struct show_quic_ctx *ctx = appctx->svcctx;
8922 struct stconn *sc = appctx_sc(appctx);
8923 struct quic_conn *qc;
8924
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008925 thread_isolate();
8926
8927 if (ctx->thr >= global.nbthread)
8928 goto done;
8929
Christopher Faulet87633c32023-04-03 18:32:50 +02008930 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02008931 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008932 /* If we're forced to shut down, we might have to remove our
8933 * reference to the last stream being dumped.
8934 */
8935 if (!LIST_ISEMPTY(&ctx->bref.users))
8936 LIST_DEL_INIT(&ctx->bref.users);
8937 goto done;
8938 }
8939
8940 chunk_reset(&trash);
8941
8942 if (!LIST_ISEMPTY(&ctx->bref.users)) {
8943 /* Remove show_quic_ctx from previous quic_conn instance. */
8944 LIST_DEL_INIT(&ctx->bref.users);
8945 }
8946 else if (!ctx->bref.ref) {
8947 /* First invocation. */
8948 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008949
8950 /* Print legend for oneline format. */
8951 if (ctx->format == QUIC_DUMP_FMT_ONELINE) {
Amaury Denoyelleaa39cc92023-05-22 10:57:56 +02008952 chunk_appendf(&trash, "# conn/frontend state "
Frédéric Lécaille8f8e3fa2023-06-14 09:17:20 +02008953 "in_flight infl_p lost_p "
8954 "Local Address Foreign Address "
Amaury Denoyelle2273af12023-05-05 16:08:34 +02008955 "local & remote CIDs\n");
8956 applet_putchk(appctx, &trash);
8957 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008958 }
8959
8960 while (1) {
8961 int done = 0;
8962
8963 if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008964 /* If closing connections requested through "all", move
8965 * to quic_conns_clo list after browsing quic_conns.
8966 * Else move directly to the next quic_conns thread.
8967 */
8968 if (ctx->flags & QC_CLI_FL_SHOW_ALL) {
8969 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns_clo.n;
8970 continue;
8971 }
8972
8973 done = 1;
8974 }
8975 else if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns_clo) {
8976 /* Closing list entirely browsed, go to next quic_conns
8977 * thread.
8978 */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008979 done = 1;
8980 }
8981 else {
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008982 /* Retrieve next element of the current list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008983 qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx);
8984 if ((int)(qc->qc_epoch - ctx->epoch) > 0)
8985 done = 1;
8986 }
8987
8988 if (done) {
8989 ++ctx->thr;
8990 if (ctx->thr >= global.nbthread)
8991 break;
Amaury Denoyelle2d376292023-03-08 09:42:31 +01008992 /* Switch to next thread quic_conns list. */
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008993 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
8994 continue;
8995 }
8996
Amaury Denoyellebc1f5fe2023-05-05 16:07:58 +02008997 switch (ctx->format) {
8998 case QUIC_DUMP_FMT_FULL:
8999 dump_quic_full(ctx, qc);
9000 break;
Amaury Denoyelle2273af12023-05-05 16:08:34 +02009001 case QUIC_DUMP_FMT_ONELINE:
9002 dump_quic_oneline(ctx, qc);
9003 break;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01009004 }
9005
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009006 if (applet_putchk(appctx, &trash) == -1) {
9007 /* Register show_quic_ctx to quic_conn instance. */
9008 LIST_APPEND(&qc->back_refs, &ctx->bref.users);
9009 goto full;
9010 }
9011
9012 ctx->bref.ref = qc->el_th_ctx.n;
9013 }
9014
9015 done:
9016 thread_release();
9017 return 1;
9018
9019 full:
9020 thread_release();
9021 return 0;
9022}
9023
9024static void cli_release_show_quic(struct appctx *appctx)
9025{
9026 struct show_quic_ctx *ctx = appctx->svcctx;
9027
9028 if (ctx->thr < global.nbthread) {
9029 thread_isolate();
9030 if (!LIST_ISEMPTY(&ctx->bref.users))
9031 LIST_DEL_INIT(&ctx->bref.users);
9032 thread_release();
9033 }
9034}
9035
9036static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreau6ccc8622023-05-31 15:54:48 +02009037 { { "show", "quic", NULL }, "show quic [oneline|full] [all] : display quic connections status", cli_parse_show_quic, cli_io_handler_dump_quic, cli_release_show_quic },
Frédéric Lécaille91376d62023-02-11 20:24:42 +01009038 {{},}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009039}};
9040
9041INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
9042
9043static void init_quic()
9044{
9045 int thr;
9046
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01009047 for (thr = 0; thr < MAX_THREADS; ++thr) {
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009048 LIST_INIT(&ha_thread_ctx[thr].quic_conns);
Amaury Denoyelleefed86c2023-03-08 09:42:04 +01009049 LIST_INIT(&ha_thread_ctx[thr].quic_conns_clo);
9050 }
Amaury Denoyelle15c74702023-02-01 10:18:26 +01009051}
9052INITCALL0(STG_INIT, init_quic);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02009053
9054/*
9055 * Local variables:
9056 * c-indent-level: 8
9057 * c-basic-offset: 8
9058 * End:
9059 */