blob: d97e82eee0a6365bcc297d820441495484b9fc3b [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>
35
Amaury Denoyelle15c74702023-02-01 10:18:26 +010036#include <haproxy/applet-t.h>
37#include <haproxy/cli.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020038#include <haproxy/connection.h>
39#include <haproxy/fd.h>
40#include <haproxy/freq_ctr.h>
41#include <haproxy/global.h>
42#include <haproxy/h3.h>
43#include <haproxy/hq_interop.h>
44#include <haproxy/log.h>
45#include <haproxy/mux_quic.h>
46#include <haproxy/ncbuf.h>
47#include <haproxy/pipe.h>
48#include <haproxy/proxy.h>
49#include <haproxy/quic_cc.h>
50#include <haproxy/quic_frame.h>
51#include <haproxy/quic_enc.h>
52#include <haproxy/quic_loss.h>
53#include <haproxy/quic_sock.h>
54#include <haproxy/quic_stats.h>
55#include <haproxy/quic_stream.h>
56#include <haproxy/quic_tp.h>
57#include <haproxy/cbuf.h>
58#include <haproxy/proto_quic.h>
59#include <haproxy/quic_tls.h>
60#include <haproxy/ssl_sock.h>
61#include <haproxy/task.h>
Amaury Denoyelle15c74702023-02-01 10:18:26 +010062#include <haproxy/thread.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020063#include <haproxy/trace.h>
64
Amaury Denoyelle15c74702023-02-01 10:18:26 +010065/* incremented by each "show quic". */
66static unsigned int qc_epoch = 0;
67
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020068/* list of supported QUIC versions by this implementation */
69const struct quic_version quic_versions[] = {
70 {
71 .num = QUIC_PROTOCOL_VERSION_DRAFT_29,
72 .initial_salt = initial_salt_draft_29,
73 .initial_salt_len = sizeof initial_salt_draft_29,
74 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
75 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
76 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
77 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
78 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
79 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
80 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
81 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
82 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT,
83 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT,
84 },
85 {
86 .num = QUIC_PROTOCOL_VERSION_1,
87 .initial_salt = initial_salt_v1,
88 .initial_salt_len = sizeof initial_salt_v1,
89 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
90 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
91 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
92 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
93 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
94 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
95 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
96 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
97 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1,
98 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1,
99 },
100 {
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +0100101 .num = QUIC_PROTOCOL_VERSION_2,
102 .initial_salt = initial_salt_v2,
103 .initial_salt_len = sizeof initial_salt_v2,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200104 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2,
105 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1,
106 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2,
107 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1,
108 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2,
109 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1,
110 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2,
111 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1,
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +0100112 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2,
113 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200114 },
115};
116
117/* The total number of supported versions */
118const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions;
119/* Listener only preferred version */
120const struct quic_version *preferred_version;
121
122/* trace source and events */
123static void quic_trace(enum trace_level level, uint64_t mask, \
124 const struct trace_source *src,
125 const struct ist where, const struct ist func,
126 const void *a1, const void *a2, const void *a3, const void *a4);
127
128static const struct trace_event quic_trace_events[] = {
129 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
130 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
131 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
132 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
133 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
134 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
135 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
136 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
137 { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" },
138 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
139 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
140 { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" },
141 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
142 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
143 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
144 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
145 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
146 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
147 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
148 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
149 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
150 { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" },
151 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
152 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
153 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
154 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
155 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
156 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
157 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
158 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
159 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
160 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
161 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
162 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
163 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
164 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
165 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
166 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
167 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
168 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
169 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
170 { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"},
171 { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"},
172 { .mask = QUIC_EV_CONN_SUB, .name = "xprt_sub", .desc = "RX/TX subcription or unsubscription to QUIC xprt"},
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200173 { .mask = QUIC_EV_CONN_RCV, .name = "conn_recv", .desc = "RX on connection" },
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200174 { /* end */ }
175};
176
177static const struct name_desc quic_trace_lockon_args[4] = {
178 /* arg1 */ { /* already used by the connection */ },
179 /* arg2 */ { .name="quic", .desc="QUIC transport" },
180 /* arg3 */ { },
181 /* arg4 */ { }
182};
183
184static const struct name_desc quic_trace_decoding[] = {
185#define QUIC_VERB_CLEAN 1
186 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
187 { /* end */ }
188};
189
190
191struct trace_source trace_quic = {
192 .name = IST("quic"),
193 .desc = "QUIC xprt",
194 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
195 .default_cb = quic_trace,
196 .known_events = quic_trace_events,
197 .lockon_args = quic_trace_lockon_args,
198 .decoding = quic_trace_decoding,
199 .report_events = ~0, /* report everything by default */
200};
201
202#define TRACE_SOURCE &trace_quic
203INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
204
205static BIO_METHOD *ha_quic_meth;
206
207DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ);
208DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
209DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
210 "quic_conn_ctx", sizeof(struct ssl_sock_ctx));
211DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
212DECLARE_POOL(pool_head_quic_connection_id,
213 "quic_connnection_id", sizeof(struct quic_connection_id));
214DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
215DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet));
216DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet));
217DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm));
218DECLARE_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 +0200219DECLARE_STATIC_POOL(pool_head_quic_cstream, "quic_cstream", sizeof(struct quic_cstream));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200220DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame));
221DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node));
222
223static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
224 struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
225 struct list *frms, struct quic_conn *qc,
226 const struct quic_version *ver, size_t dglen, int pkt_type,
227 int force_ack, int padding, int probe, int cc, int *err);
228struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
229static void qc_idle_timer_do_rearm(struct quic_conn *qc);
230static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
231static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
232static int quic_conn_init_timer(struct quic_conn *qc);
233static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
234
235/* Only for debug purpose */
236struct enc_debug_info {
237 unsigned char *payload;
238 size_t payload_len;
239 unsigned char *aad;
240 size_t aad_len;
241 uint64_t pn;
242};
243
244/* Initializes a enc_debug_info struct (only for debug purpose) */
245static inline void enc_debug_info_init(struct enc_debug_info *edi,
246 unsigned char *payload, size_t payload_len,
247 unsigned char *aad, size_t aad_len, uint64_t pn)
248{
249 edi->payload = payload;
250 edi->payload_len = payload_len;
251 edi->aad = aad;
252 edi->aad_len = aad_len;
253 edi->pn = pn;
254}
255
256/* Trace callback for QUIC.
257 * These traces always expect that arg1, if non-null, is of type connection.
258 */
259static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
260 const struct ist where, const struct ist func,
261 const void *a1, const void *a2, const void *a3, const void *a4)
262{
263 const struct quic_conn *qc = a1;
264
265 if (qc) {
266 const struct quic_tls_ctx *tls_ctx;
267
268 chunk_appendf(&trace_buf, " : qc@%p", qc);
269 if (mask & QUIC_EV_CONN_INIT) {
270 chunk_appendf(&trace_buf, "\n odcid");
271 quic_cid_dump(&trace_buf, &qc->odcid);
272 chunk_appendf(&trace_buf, "\n dcid");
273 quic_cid_dump(&trace_buf, &qc->dcid);
274 chunk_appendf(&trace_buf, "\n scid");
275 quic_cid_dump(&trace_buf, &qc->scid);
276 }
277
278 if (mask & QUIC_EV_TRANSP_PARAMS) {
279 const struct quic_transport_params *p = a2;
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100280
281 if (p)
282 quic_transport_params_dump(&trace_buf, qc, p);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200283 }
284
285 if (mask & QUIC_EV_CONN_ADDDATA) {
286 const enum ssl_encryption_level_t *level = a2;
287 const size_t *len = a3;
288
289 if (level) {
290 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
291
292 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
293 }
294 if (len)
295 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
296 }
297 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
298 /* Initial read & write secrets. */
299 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
300 const unsigned char *rx_sec = a2;
301 const unsigned char *tx_sec = a3;
302
303 tls_ctx = &qc->els[level].tls_ctx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200304 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
305 if (rx_sec)
306 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
307 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
308 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
309 if (tx_sec)
310 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
311 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200312 }
313 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
314 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200315
316 if (level) {
317 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
318
319 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200320 if (quic_tls_has_rx_sec(&qc->els[lvl])) {
321 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200322 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200323 }
324 else
325 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200326 }
327 }
328
329 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
330 const enum ssl_encryption_level_t *level = a2;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200331
332 if (level) {
333 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
334
335 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200336 if (quic_tls_has_tx_sec(&qc->els[lvl])) {
337 tls_ctx = &qc->els[lvl].tls_ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200338 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200339 }
340 else
341 chunk_appendf(&trace_buf, " (none)");
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200342 }
343
344 }
345
346 if (mask & QUIC_EV_CONN_FRMLIST) {
347 const struct list *l = a2;
348
349 if (l) {
350 const struct quic_frame *frm;
351 list_for_each_entry(frm, l, list) {
352 chunk_appendf(&trace_buf, " frm@%p", frm);
353 chunk_frm_appendf(&trace_buf, frm);
354 }
355 }
356 }
357
358 if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) {
359 const struct quic_tx_packet *pkt = a2;
360 const struct quic_enc_level *qel = a3;
361 const ssize_t *room = a4;
362
363 if (qel) {
364 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaille45400532023-02-13 18:39:19 +0100365 chunk_appendf(&trace_buf, " qel=%c pto_count=%d cwnd=%llu ppif=%lld pif=%llu "
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200366 "if=%llu pp=%u",
367 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille45400532023-02-13 18:39:19 +0100368 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200369 (unsigned long long)qc->path->cwnd,
370 (unsigned long long)qc->path->prep_in_flight,
371 (unsigned long long)qc->path->in_flight,
372 (unsigned long long)pktns->tx.in_flight,
373 pktns->tx.pto_probe);
374 }
375 if (pkt) {
376 const struct quic_frame *frm;
377 if (pkt->pn_node.key != (uint64_t)-1)
378 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
379 list_for_each_entry(frm, &pkt->frms, list) {
380 chunk_appendf(&trace_buf, " frm@%p", frm);
381 chunk_frm_appendf(&trace_buf, frm);
382 }
383 }
384
385 if (room) {
386 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
387 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
388 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
389 }
390 }
391
392 if (mask & QUIC_EV_CONN_IO_CB) {
393 const enum quic_handshake_state *state = a2;
394 const int *err = a3;
395
396 if (state)
397 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
398 if (err)
399 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
400 }
401
402 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
403 const struct quic_rx_packet *pkt = a2;
404 const unsigned long *pktlen = a3;
405 const SSL *ssl = a4;
406
407 if (pkt) {
408 chunk_appendf(&trace_buf, " pkt@%p", pkt);
409 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
410 chunk_appendf(&trace_buf, " kp=%d",
411 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
412 chunk_appendf(&trace_buf, " el=%c",
413 quic_packet_type_enc_level_char(pkt->type));
414 if (pkt->pnl)
415 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
416 (unsigned long long)pkt->pn);
417 if (pkt->token_len)
418 chunk_appendf(&trace_buf, " toklen=%llu",
419 (unsigned long long)pkt->token_len);
420 if (pkt->aad_len)
421 chunk_appendf(&trace_buf, " aadlen=%llu",
422 (unsigned long long)pkt->aad_len);
423 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
424 pkt->flags, (unsigned long long)pkt->len);
425 }
426 if (pktlen)
427 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
428 if (ssl) {
429 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
430 chunk_appendf(&trace_buf, " el=%c",
431 quic_enc_level_char(ssl_to_quic_enc_level(level)));
432 }
433 }
434
435 if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
436 const struct quic_rx_packet *pkt = a2;
437 const struct quic_rx_crypto_frm *cf = a3;
438 const SSL *ssl = a4;
439
440 if (pkt)
441 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
442 quic_packet_type_enc_level_char(pkt->type),
443 (unsigned long long)pkt->pn);
444 if (cf)
445 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
446 (unsigned long long)cf->offset_node.key,
447 (unsigned long long)cf->len);
448 if (ssl) {
449 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
450 chunk_appendf(&trace_buf, " rel=%c",
451 quic_enc_level_char(ssl_to_quic_enc_level(level)));
452 }
453
454 if (qc->err.code)
455 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code);
456 }
457
458 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
459 const struct quic_frame *frm = a2;
460
461 if (frm)
462 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
463 }
464
465 if (mask & QUIC_EV_CONN_PHPKTS) {
466 const struct quic_enc_level *qel = a2;
467
468 if (qel) {
469 const struct quic_pktns *pktns = qel->pktns;
470 chunk_appendf(&trace_buf,
Frédéric Lécaille45400532023-02-13 18:39:19 +0100471 " qel=%c 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 +0200472 quic_enc_level_char_from_qel(qel, qc),
473 quic_hdshk_state_str(qc->state),
474 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille45400532023-02-13 18:39:19 +0100475 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200476 (unsigned long long)qc->path->cwnd,
477 (unsigned long long)qc->path->prep_in_flight,
478 (unsigned long long)qc->path->in_flight,
479 (unsigned long long)pktns->tx.in_flight,
Amaury Denoyelle2f668f02022-11-18 15:24:08 +0100480 pktns->tx.pto_probe,
481 qel->cstream ? (unsigned long long)qel->cstream->rx.offset : 0);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200482 }
483 }
484
485 if (mask & QUIC_EV_CONN_ENCPKT) {
486 const struct enc_debug_info *edi = a2;
487
488 if (edi)
489 chunk_appendf(&trace_buf,
490 " payload=@%p payload_len=%llu"
491 " aad=@%p aad_len=%llu pn=%llu",
492 edi->payload, (unsigned long long)edi->payload_len,
493 edi->aad, (unsigned long long)edi->aad_len,
494 (unsigned long long)edi->pn);
495 }
496
497 if (mask & QUIC_EV_CONN_RMHP) {
498 const struct quic_rx_packet *pkt = a2;
499
500 if (pkt) {
501 const int *ret = a3;
502
503 chunk_appendf(&trace_buf, " pkt@%p", pkt);
504 if (ret && *ret)
505 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
506 pkt->pnl, (unsigned long long)pkt->pn);
507 }
508 }
509
510 if (mask & QUIC_EV_CONN_PRSAFRM) {
511 const struct quic_frame *frm = a2;
512 const unsigned long *val1 = a3;
513 const unsigned long *val2 = a4;
514
515 if (frm) {
516 chunk_appendf(&trace_buf, " frm@%p", frm);
517 chunk_frm_appendf(&trace_buf, frm);
518 }
519 if (val1)
520 chunk_appendf(&trace_buf, " %lu", *val1);
521 if (val2)
522 chunk_appendf(&trace_buf, "..%lu", *val2);
523 }
524
525 if (mask & QUIC_EV_CONN_ACKSTRM) {
526 const struct quic_stream *s = a2;
527 const struct qc_stream_desc *stream = a3;
528
529 if (s)
530 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
531 if (stream)
532 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
533 }
534
535 if (mask & QUIC_EV_CONN_RTTUPDT) {
536 const unsigned int *rtt_sample = a2;
537 const unsigned int *ack_delay = a3;
538 const struct quic_loss *ql = a4;
539
540 if (rtt_sample)
541 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
542 if (ack_delay)
543 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
544 if (ql)
545 chunk_appendf(&trace_buf,
546 " srtt=%ums rttvar=%ums min_rtt=%ums",
547 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
548 }
549 if (mask & QUIC_EV_CONN_CC) {
550 const struct quic_cc_event *ev = a2;
551 const struct quic_cc *cc = a3;
552
553 if (a2)
554 quic_cc_event_trace(&trace_buf, ev);
555 if (a3)
556 quic_cc_state_trace(&trace_buf, cc);
557 }
558
559 if (mask & QUIC_EV_CONN_PKTLOSS) {
560 const struct quic_pktns *pktns = a2;
561 const struct list *lost_pkts = a3;
562
563 if (pktns) {
564 chunk_appendf(&trace_buf, " pktns=%s",
565 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
566 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
567 if (pktns->tx.loss_time)
568 chunk_appendf(&trace_buf, " loss_time=%dms",
569 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
570 }
571 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
572 struct quic_tx_packet *pkt;
573
574 chunk_appendf(&trace_buf, " lost_pkts:");
575 list_for_each_entry(pkt, lost_pkts, list)
576 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
577 }
578 }
579
580 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
581 const struct quic_pktns *pktns = a2;
582 const int *duration = a3;
583 const uint64_t *ifae_pkts = a4;
584
585 if (ifae_pkts)
586 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
587 (unsigned long long)*ifae_pkts);
588 if (pktns) {
589 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
590 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
591 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
592 pktns->tx.pto_probe);
593 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
594 if (pktns->tx.in_flight)
595 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
596 if (pktns->tx.loss_time)
597 chunk_appendf(&trace_buf, " loss_time=%dms",
598 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
599 }
600 if (mask & QUIC_EV_CONN_SPTO) {
601 if (pktns->tx.time_of_last_eliciting)
602 chunk_appendf(&trace_buf, " tole=%dms",
603 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
604 if (duration)
605 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
606 }
607 }
608
609 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
610 chunk_appendf(&trace_buf,
611 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
612 }
613 }
614
615 if (mask & QUIC_EV_CONN_SPPKTS) {
616 const struct quic_tx_packet *pkt = a2;
617
Frédéric Lécaille45400532023-02-13 18:39:19 +0100618 chunk_appendf(&trace_buf, " pto_count=%d cwnd=%llu ppif=%llu pif=%llu",
619 qc->path->loss.pto_count,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200620 (unsigned long long)qc->path->cwnd,
621 (unsigned long long)qc->path->prep_in_flight,
622 (unsigned long long)qc->path->in_flight);
623 if (pkt) {
624 const struct quic_frame *frm;
625 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
626 (unsigned long)pkt->pn_node.key,
627 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
628 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
629 (unsigned long long)pkt->in_flight_len);
630 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
631 (unsigned long long)qc->rx.bytes,
632 (unsigned long long)qc->tx.bytes);
633 list_for_each_entry(frm, &pkt->frms, list) {
634 chunk_appendf(&trace_buf, " frm@%p", frm);
635 chunk_frm_appendf(&trace_buf, frm);
636 }
Frédéric Lécaillebc09f742023-02-13 17:45:36 +0100637
638 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
639 chunk_appendf(&trace_buf, " with scid");
640 quic_cid_dump(&trace_buf, &qc->scid);
641 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200642 }
643 }
644
645 if (mask & QUIC_EV_CONN_SSLALERT) {
646 const uint8_t *alert = a2;
647 const enum ssl_encryption_level_t *level = a3;
648
649 if (alert)
650 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
651 if (level)
652 chunk_appendf(&trace_buf, " el=%c",
653 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
654 }
655
656 if (mask & QUIC_EV_CONN_BCFRMS) {
657 const size_t *sz1 = a2;
658 const size_t *sz2 = a3;
659 const size_t *sz3 = a4;
660
661 if (sz1)
662 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
663 if (sz2)
664 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
665 if (sz3)
666 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
667 }
668
669 if (mask & QUIC_EV_CONN_PSTRM) {
670 const struct quic_frame *frm = a2;
671
672 if (frm) {
673 chunk_appendf(&trace_buf, " frm@%p", frm);
674 chunk_frm_appendf(&trace_buf, frm);
675 }
676 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +0200677
678 if (mask & QUIC_EV_CONN_ELEVELSEL) {
679 const enum quic_handshake_state *state = a2;
680 const enum quic_tls_enc_level *level = a3;
681 const enum quic_tls_enc_level *next_level = a4;
682
683 if (state)
684 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(qc->state));
685 if (level)
686 chunk_appendf(&trace_buf, " level=%c", quic_enc_level_char(*level));
687 if (next_level)
688 chunk_appendf(&trace_buf, " next_level=%c", quic_enc_level_char(*next_level));
689
690 }
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200691
692 if (mask & QUIC_EV_CONN_RCV) {
693 const struct quic_dgram *dgram = a2;
694
695 if (dgram)
696 chunk_appendf(&trace_buf, " dgram.len=%zu", dgram->len);
697 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200698 }
699 if (mask & QUIC_EV_CONN_LPKT) {
700 const struct quic_rx_packet *pkt = a2;
701 const uint64_t *len = a3;
702 const struct quic_version *ver = a4;
703
704 if (pkt) {
705 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
706 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
707 if (pkt->pn_node.key != (uint64_t)-1)
708 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
709 }
710
711 if (len)
712 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
713
714 if (ver)
715 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
716 }
717
718 if (mask & QUIC_EV_STATELESS_RST) {
719 const struct quic_cid *cid = a2;
720
721 if (cid)
722 quic_cid_dump(&trace_buf, cid);
723 }
724
725}
726
727/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
728static inline int quic_peer_validated_addr(struct quic_conn *qc)
729{
730 struct quic_pktns *hdshk_pktns, *app_pktns;
731
732 if (!qc_is_listener(qc))
733 return 1;
734
735 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
736 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
737 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
738 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
739 qc->state >= QUIC_HS_ST_COMPLETE)
740 return 1;
741
742 return 0;
743}
744
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100745/* To be called to kill a connection as soon as possible (without sending any packet). */
746void qc_kill_conn(struct quic_conn *qc)
747{
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100748 TRACE_ENTER(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100749 qc->flags |= QUIC_FL_CONN_TO_KILL;
750 task_wakeup(qc->idle_timer_task, TASK_WOKEN_OTHER);
Frédéric Lécaille2f531112023-02-10 14:44:51 +0100751 TRACE_LEAVE(QUIC_EV_CONN_KILL, qc);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100752}
753
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200754/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
755 * both loss detection and PTO and schedule the task assiated to this timer if needed.
756 */
757static inline void qc_set_timer(struct quic_conn *qc)
758{
759 struct quic_pktns *pktns;
760 unsigned int pto;
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100761 int handshake_confirmed;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200762
763 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
764 NULL, NULL, &qc->path->ifae_pkts);
765
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100766 pktns = NULL;
767 if (!qc->timer_task) {
768 TRACE_PROTO("already released timer task", QUIC_EV_CONN_STIMER, qc);
769 goto leave;
770 }
771
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200772 pktns = quic_loss_pktns(qc);
773 if (tick_isset(pktns->tx.loss_time)) {
774 qc->timer = pktns->tx.loss_time;
775 goto out;
776 }
777
778 /* anti-amplification: the timer must be
779 * cancelled for a server which reached the anti-amplification limit.
780 */
781 if (!quic_peer_validated_addr(qc) &&
782 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
783 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
784 qc->timer = TICK_ETERNITY;
785 goto out;
786 }
787
788 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
789 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
790 /* Timer cancellation. */
791 qc->timer = TICK_ETERNITY;
792 goto out;
793 }
794
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +0100795 handshake_confirmed = qc->state >= QUIC_HS_ST_CONFIRMED;
796 pktns = quic_pto_pktns(qc, handshake_confirmed, &pto);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200797 if (tick_isset(pto))
798 qc->timer = pto;
799 out:
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100800 if (qc->timer == TICK_ETERNITY) {
801 qc->timer_task->expire = TICK_ETERNITY;
802 }
803 else if (tick_is_expired(qc->timer, now_ms)) {
804 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
805 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
806 }
807 else {
808 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
809 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200810 }
Frédéric Lécailledd41a452023-02-09 07:48:33 +0100811 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200812 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
813}
814
815/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
816 * connection.
817 * Return 1 if succeeded, 0 if not.
818 */
819static int quic_tls_key_update(struct quic_conn *qc)
820{
821 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
822 struct quic_tls_secrets *rx, *tx;
823 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
824 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
825 const struct quic_version *ver =
826 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
827 int ret = 0;
828
829 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
830
831 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
832 rx = &tls_ctx->rx;
833 tx = &tls_ctx->tx;
834 nxt_rx = &qc->ku.nxt_rx;
835 nxt_tx = &qc->ku.nxt_tx;
836
837 /* Prepare new RX secrets */
838 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
839 rx->secret, rx->secretlen)) {
840 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
841 goto leave;
842 }
843
844 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
845 nxt_rx->key, nxt_rx->keylen,
846 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
847 nxt_rx->secret, nxt_rx->secretlen)) {
848 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
849 goto leave;
850 }
851
852 /* Prepare new TX secrets */
853 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
854 tx->secret, tx->secretlen)) {
855 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
856 goto leave;
857 }
858
859 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
860 nxt_tx->key, nxt_tx->keylen,
861 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
862 nxt_tx->secret, nxt_tx->secretlen)) {
863 TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
864 goto leave;
865 }
866
867 if (nxt_rx->ctx) {
868 EVP_CIPHER_CTX_free(nxt_rx->ctx);
869 nxt_rx->ctx = NULL;
870 }
871
872 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
873 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
874 goto leave;
875 }
876
877 if (nxt_tx->ctx) {
878 EVP_CIPHER_CTX_free(nxt_tx->ctx);
879 nxt_tx->ctx = NULL;
880 }
881
882 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
883 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
884 goto leave;
885 }
886
887 ret = 1;
888 leave:
889 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc);
890 return ret;
891}
892
893/* Rotate the Key Update information for <qc> QUIC connection.
894 * Must be used after having updated them.
895 * Always succeeds.
896 */
897static void quic_tls_rotate_keys(struct quic_conn *qc)
898{
899 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
900 unsigned char *curr_secret, *curr_iv, *curr_key;
901 EVP_CIPHER_CTX *curr_ctx;
902
903 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
904
905 /* Rotate the RX secrets */
906 curr_ctx = tls_ctx->rx.ctx;
907 curr_secret = tls_ctx->rx.secret;
908 curr_iv = tls_ctx->rx.iv;
909 curr_key = tls_ctx->rx.key;
910
911 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
912 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
913 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
914 tls_ctx->rx.key = qc->ku.nxt_rx.key;
915
916 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
917 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
918 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
919 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
920
921 qc->ku.prv_rx.ctx = curr_ctx;
922 qc->ku.prv_rx.secret = curr_secret;
923 qc->ku.prv_rx.iv = curr_iv;
924 qc->ku.prv_rx.key = curr_key;
925 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
926
927 /* Update the TX secrets */
928 curr_ctx = tls_ctx->tx.ctx;
929 curr_secret = tls_ctx->tx.secret;
930 curr_iv = tls_ctx->tx.iv;
931 curr_key = tls_ctx->tx.key;
932
933 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
934 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
935 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
936 tls_ctx->tx.key = qc->ku.nxt_tx.key;
937
938 qc->ku.nxt_tx.ctx = curr_ctx;
939 qc->ku.nxt_tx.secret = curr_secret;
940 qc->ku.nxt_tx.iv = curr_iv;
941 qc->ku.nxt_tx.key = curr_key;
942
943 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
944}
945
946/* returns 0 on error, 1 on success */
947int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
948 const uint8_t *read_secret,
949 const uint8_t *write_secret, size_t secret_len)
950{
951 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
952 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
953 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200954 struct quic_tls_secrets *rx = NULL, *tx = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200955 const struct quic_version *ver =
956 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
957 int ret = 0;
958
959 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
960 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille0aa79952023-02-03 16:15:08 +0100961
962 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
963 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
964 goto out;
965 }
966
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200967 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
968 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200969 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200970 }
971
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200972 if (!read_secret)
973 goto write;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200974
975 rx = &tls_ctx->rx;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200976 if (!quic_tls_secrets_keys_alloc(rx)) {
977 TRACE_ERROR("RX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
978 goto leave;
979 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200980
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200981 rx->aead = tls_aead(cipher);
982 rx->md = tls_md(cipher);
983 rx->hp = tls_hp(cipher);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200984
985 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen,
986 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
987 read_secret, secret_len)) {
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +0200988 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +0200989 goto leave;
990 }
991
992 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
993 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
994 goto leave;
995 }
996
997 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
998 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
999 goto leave;
1000 }
1001
1002 /* Enqueue this connection asap if we could derive O-RTT secrets as
1003 * listener. Note that a listener derives only RX secrets for this
1004 * level.
1005 */
1006 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
1007 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
1008 quic_accept_push_qc(qc);
1009 }
1010
1011write:
1012
1013 if (!write_secret)
1014 goto out;
1015
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001016 tx = &tls_ctx->tx;
1017 if (!quic_tls_secrets_keys_alloc(tx)) {
1018 TRACE_ERROR("TX keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
1019 goto leave;
1020 }
1021
1022 tx->aead = tls_aead(cipher);
1023 tx->md = tls_md(cipher);
1024 tx->hp = tls_hp(cipher);
1025
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001026 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen,
1027 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
1028 write_secret, secret_len)) {
1029 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
1030 goto leave;
1031 }
1032
1033 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
1034 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
1035 goto leave;
1036 }
1037
1038 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
1039 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
1040 goto leave;
1041 }
1042
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01001043 if (level == ssl_encryption_handshake && qc_is_listener(qc)) {
1044 qc->enc_params_len =
1045 quic_transport_params_encode(qc->enc_params,
1046 qc->enc_params + sizeof qc->enc_params,
1047 &qc->rx.params, ver, 1);
1048 if (!qc->enc_params_len) {
1049 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_RWSEC);
1050 goto leave;
1051 }
1052
1053 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
1054 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_RWSEC);
1055 goto leave;
1056 }
1057 }
1058
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001059 if (level == ssl_encryption_application) {
1060 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
1061 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
1062 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
1063
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001064 if (rx) {
1065 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1066 TRACE_ERROR("Could not allocate RX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1067 goto leave;
1068 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001069
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001070 memcpy(rx->secret, read_secret, secret_len);
1071 rx->secretlen = secret_len;
1072 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001073
1074 if (tx) {
1075 if (!(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
1076 TRACE_ERROR("Could not allocate TX Application secrete keys", QUIC_EV_CONN_RWSEC, qc);
1077 goto leave;
1078 }
1079
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001080 memcpy(tx->secret, write_secret, secret_len);
1081 tx->secretlen = secret_len;
1082 }
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02001083
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001084 /* Initialize all the secret keys lengths */
1085 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001086 }
1087
1088 out:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001089 ret = 1;
1090 leave:
1091 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
1092 return ret;
1093}
1094
1095/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1096 * with <len> as size in CRYPTO buffers dedicated to store the information about
1097 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1098 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
1099 * buffers to store all the data.
1100 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1101 */
1102static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
1103 const unsigned char *data, size_t len)
1104{
1105 struct quic_crypto_buf **qcb;
1106 /* The remaining byte to store in CRYPTO buffers. */
1107 size_t cf_offset, cf_len, *nb_buf;
1108 unsigned char *pos;
1109 int ret = 0;
1110
1111 nb_buf = &qel->tx.crypto.nb_buf;
1112 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1113 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1114 cf_len = len;
1115
1116 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1117
1118 while (len) {
1119 size_t to_copy, room;
1120
1121 pos = (*qcb)->data + (*qcb)->sz;
1122 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1123 to_copy = len > room ? room : len;
1124 if (to_copy) {
1125 memcpy(pos, data, to_copy);
1126 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1127 qel->tx.crypto.sz += to_copy;
1128 (*qcb)->sz += to_copy;
1129 len -= to_copy;
1130 data += to_copy;
1131 }
1132 else {
1133 struct quic_crypto_buf **tmp;
1134
1135 // FIXME: realloc!
1136 tmp = realloc(qel->tx.crypto.bufs,
1137 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1138 if (tmp) {
1139 qel->tx.crypto.bufs = tmp;
1140 qcb = &qel->tx.crypto.bufs[*nb_buf];
1141 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1142 if (!*qcb) {
1143 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1144 goto leave;
1145 }
1146
1147 (*qcb)->sz = 0;
1148 ++*nb_buf;
1149 }
1150 else {
1151 break;
1152 }
1153 }
1154 }
1155
1156 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1157 * have been buffered.
1158 */
1159 if (!len) {
1160 struct quic_frame *frm;
1161 struct quic_frame *found = NULL;
1162
1163 /* There is at most one CRYPTO frame in this packet number
1164 * space. Let's look for it.
1165 */
1166 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
1167 if (frm->type != QUIC_FT_CRYPTO)
1168 continue;
1169
1170 /* Found */
1171 found = frm;
1172 break;
1173 }
1174
1175 if (found) {
1176 found->crypto.len += cf_len;
1177 }
1178 else {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001179 frm = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001180 if (!frm) {
1181 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1182 goto leave;
1183 }
1184
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001185 frm->crypto.offset = cf_offset;
1186 frm->crypto.len = cf_len;
1187 frm->crypto.qel = qel;
1188 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
1189 }
1190 }
1191 ret = len == 0;
1192 leave:
1193 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1194 return ret;
1195}
1196
1197/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1198 * activity for <qc> will be interrupted.
1199 */
1200void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
1201{
1202 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1203 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
1204 goto leave;
1205
1206 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
1207 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
1208 qc->err.code = err.code;
1209 qc->err.app = err.app;
1210 leave:
1211 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1212}
1213
1214/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1215void quic_set_tls_alert(struct quic_conn *qc, int alert)
1216{
1217 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1218
1219 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1220 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
1221 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
1222 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1223 }
1224 quic_set_connection_close(qc, quic_err_tls(alert));
1225 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
1226 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1227
1228 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1229}
1230
1231/* Set the application for <qc> QUIC connection.
1232 * Return 1 if succeeded, 0 if not.
1233 */
1234int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1235{
1236 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1237 qc->app_ops = &h3_ops;
1238 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1239 qc->app_ops = &hq_interop_ops;
1240 else
1241 return 0;
1242
1243 return 1;
1244}
1245
1246/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1247 * wants to provide the QUIC layer with CRYPTO data.
1248 * Returns 1 if succeeded, 0 if not.
1249 */
1250int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1251 const uint8_t *data, size_t len)
1252{
1253 struct quic_conn *qc;
1254 enum quic_tls_enc_level tel;
1255 struct quic_enc_level *qel;
1256 int ret = 0;
1257
1258 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1259 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1260
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01001261 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
1262 TRACE_PROTO("connection to be killed", QUIC_EV_CONN_ADDDATA, qc);
1263 goto out;
1264 }
1265
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001266 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1267 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
1268 goto out;
1269 }
1270
1271 tel = ssl_to_quic_enc_level(level);
1272 if (tel == -1) {
1273 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1274 goto leave;
1275 }
1276
1277 qel = &qc->els[tel];
1278 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1279 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1280 goto leave;
1281 }
1282
1283 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
1284 qc, &level, &len);
1285 out:
1286 ret = 1;
1287 leave:
1288 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1289 return ret;
1290}
1291
1292int ha_quic_flush_flight(SSL *ssl)
1293{
1294 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1295
1296 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1297 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
1298
1299 return 1;
1300}
1301
1302int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1303{
1304 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1305
1306 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1307
1308 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1309
1310 quic_set_tls_alert(qc, alert);
1311 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
1312 return 1;
1313}
1314
1315/* QUIC TLS methods */
1316static SSL_QUIC_METHOD ha_quic_method = {
1317 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1318 .add_handshake_data = ha_quic_add_handshake_data,
1319 .flush_flight = ha_quic_flush_flight,
1320 .send_alert = ha_quic_send_alert,
1321};
1322
1323/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1324 * Returns an error count.
1325 */
1326int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1327{
1328 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1329 int cfgerr = 0;
1330
1331 long options =
1332 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1333 SSL_OP_SINGLE_ECDH_USE |
1334 SSL_OP_CIPHER_SERVER_PREFERENCE;
1335 SSL_CTX *ctx;
1336
1337 ctx = SSL_CTX_new(TLS_server_method());
1338 bind_conf->initial_ctx = ctx;
1339
1340 SSL_CTX_set_options(ctx, options);
1341 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1342 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1343 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1344
1345#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1346# if defined(HAVE_SSL_CLIENT_HELLO_CB)
1347# if defined(SSL_OP_NO_ANTI_REPLAY)
1348 if (bind_conf->ssl_conf.early_data) {
1349 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
1350 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
1351 }
1352# endif /* !SSL_OP_NO_ANTI_REPLAY */
1353 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1354 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1355# else /* ! HAVE_SSL_CLIENT_HELLO_CB */
1356 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1357# endif
1358 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1359#endif
1360 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1361
1362 return cfgerr;
1363}
1364
1365/* Decode an expected packet number from <truncated_on> its truncated value,
1366 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1367 * the number of bits used to encode this packet number (its length in bytes * 8).
1368 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1369 */
1370static uint64_t decode_packet_number(uint64_t largest_pn,
1371 uint32_t truncated_pn, unsigned int pn_nbits)
1372{
1373 uint64_t expected_pn = largest_pn + 1;
1374 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1375 uint64_t pn_hwin = pn_win / 2;
1376 uint64_t pn_mask = pn_win - 1;
1377 uint64_t candidate_pn;
1378
1379
1380 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1381 /* Note that <pn_win> > <pn_hwin>. */
1382 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1383 candidate_pn + pn_hwin <= expected_pn)
1384 return candidate_pn + pn_win;
1385
1386 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1387 return candidate_pn - pn_win;
1388
1389 return candidate_pn;
1390}
1391
1392/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1393 * cryptographic context.
1394 * <largest_pn> is the largest received packet number and <pn> the address of
1395 * the packet number field for this packet with <byte0> address of its first byte.
1396 * <end> points to one byte past the end of this packet.
1397 * Returns 1 if succeeded, 0 if not.
1398 */
1399static int qc_do_rm_hp(struct quic_conn *qc,
1400 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1401 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
1402{
1403 int ret, i, pnlen;
1404 uint64_t packet_number;
1405 uint32_t truncated_pn = 0;
1406 unsigned char mask[5] = {0};
1407 unsigned char *sample;
1408 EVP_CIPHER_CTX *cctx = NULL;
1409
1410 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1411
1412 ret = 0;
1413
1414 /* Check there is enough data in this packet. */
1415 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1416 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1417 goto leave;
1418 }
1419
1420 cctx = EVP_CIPHER_CTX_new();
1421 if (!cctx) {
1422 TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
1423 goto leave;
1424 }
1425
1426 sample = pn + QUIC_PACKET_PN_MAXLEN;
1427
1428 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1429 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
1430 goto leave;
1431 }
1432
1433 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1434 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1435 for (i = 0; i < pnlen; i++) {
1436 pn[i] ^= mask[i + 1];
1437 truncated_pn = (truncated_pn << 8) | pn[i];
1438 }
1439
1440 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1441 /* Store remaining information for this unprotected header */
1442 pkt->pn = packet_number;
1443 pkt->pnl = pnlen;
1444
1445 ret = 1;
1446 leave:
1447 if (cctx)
1448 EVP_CIPHER_CTX_free(cctx);
1449 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
1450 return ret;
1451}
1452
1453/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1454 * address, with <payload_len> as payload length, <aad> as address of
1455 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1456 * context.
1457 * Returns 1 if succeeded, 0 if not.
1458 */
1459static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1460 unsigned char *aad, size_t aad_len, uint64_t pn,
1461 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
1462{
1463 int ret = 0;
1464 unsigned char iv[QUIC_TLS_IV_LEN];
1465 unsigned char *tx_iv = tls_ctx->tx.iv;
1466 size_t tx_iv_sz = tls_ctx->tx.ivlen;
1467 struct enc_debug_info edi;
1468
1469 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1470
1471 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1472 TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc);
1473 goto err;
1474 }
1475
1476 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1477 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1478 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
1479 goto err;
1480 }
1481
1482 ret = 1;
1483 leave:
1484 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1485 return ret;
1486
1487 err:
1488 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1489 goto leave;
1490}
1491
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001492/* Decrypt <pkt> packet using encryption level <qel> for <qc> connection.
1493 * Decryption is done in place in packet buffer.
1494 *
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +05001495 * Returns 1 on success else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001496 */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001497static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel,
1498 struct quic_rx_packet *pkt)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001499{
1500 int ret, kp_changed;
1501 unsigned char iv[QUIC_TLS_IV_LEN];
1502 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
1503 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
1504 unsigned char *rx_iv = tls_ctx->rx.iv;
1505 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1506 unsigned char *rx_key = tls_ctx->rx.key;
1507
1508 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1509
1510 ret = 0;
1511 kp_changed = 0;
1512
1513 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1514 /* The two tested bits are not at the same position,
1515 * this is why they are first both inversed.
1516 */
1517 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1518 if (pkt->pn < tls_ctx->rx.pn) {
1519 /* The lowest packet number of a previous key phase
1520 * cannot be null if it really stores previous key phase
1521 * secrets.
1522 */
1523 // TODO: check if BUG_ON() more suitable
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001524 if (!qc->ku.prv_rx.pn) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001525 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1526 goto leave;
1527 }
1528
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001529 rx_ctx = qc->ku.prv_rx.ctx;
1530 rx_iv = qc->ku.prv_rx.iv;
1531 rx_key = qc->ku.prv_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001532 }
1533 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1534 /* Next key phase */
1535 kp_changed = 1;
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001536 rx_ctx = qc->ku.nxt_rx.ctx;
1537 rx_iv = qc->ku.nxt_rx.iv;
1538 rx_key = qc->ku.nxt_rx.key;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001539 }
1540 }
1541 }
1542
1543 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1544 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1545 goto leave;
1546 }
1547
1548 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1549 pkt->data, pkt->aad_len,
1550 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
1551 if (!ret) {
1552 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1553 goto leave;
1554 }
1555
1556 /* Update the keys only if the packet decryption succeeded. */
1557 if (kp_changed) {
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001558 quic_tls_rotate_keys(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001559 /* Toggle the Key Phase bit */
1560 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1561 /* Store the lowest packet number received for the current key phase */
1562 tls_ctx->rx.pn = pkt->pn;
1563 /* Prepare the next key update */
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01001564 if (!quic_tls_key_update(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001565 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1566 goto leave;
1567 }
1568 }
1569
1570 /* Update the packet length (required to parse the frames). */
1571 pkt->len -= QUIC_TLS_TAG_LEN;
1572 ret = 1;
1573 leave:
1574 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
1575 return ret;
1576}
1577
1578
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001579/* Release <frm> frame and mark its copies as acknowledged */
1580void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
1581{
1582 uint64_t pn;
1583 struct quic_frame *origin, *f, *tmp;
1584
1585 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1586
1587 /* Identify this frame: a frame copy or one of its copies */
1588 origin = frm->origin ? frm->origin : frm;
1589 /* Ensure the source of the copies is flagged as acked, <frm> being
1590 * possibly a copy of <origin>
1591 */
1592 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1593 /* Mark all the copy of <origin> as acknowledged. We must
1594 * not release the packets (releasing the frames) at this time as
1595 * they are possibly also to be acknowledged alongside the
1596 * the current one.
1597 */
1598 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1599 if (f->pkt) {
1600 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1601 f->origin = NULL;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001602 LIST_DEL_INIT(&f->ref);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001603 pn = f->pkt->pn_node.key;
1604 TRACE_DEVEL("mark frame as acked from packet",
1605 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1606 }
1607 else {
1608 TRACE_DEVEL("freeing unsent frame",
1609 QUIC_EV_CONN_PRSAFRM, qc, f);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001610 LIST_DEL_INIT(&f->ref);
1611 qc_frm_free(&f);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001612 }
1613 }
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001614 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001615 pn = frm->pkt->pn_node.key;
1616 quic_tx_packet_refdec(frm->pkt);
1617 TRACE_DEVEL("freeing frame from packet",
1618 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001619 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001620
1621 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1622}
1623
1624/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1625 * and all STREAM data are acknowledged. The MUX is responsible to have set
1626 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1627 *
1628 * TODO this should also be called on lost packet detection
1629 */
1630void qc_check_close_on_released_mux(struct quic_conn *qc)
1631{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001632 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1633
1634 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1635 /* Reuse errcode which should have been previously set by the MUX on release. */
1636 quic_set_connection_close(qc, qc->err);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02001637 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001638 }
1639
1640 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
1641}
1642
1643/* Remove from <stream> the acknowledged frames.
1644 *
1645 * Returns 1 if at least one frame was removed else 0.
1646 */
1647static int quic_stream_try_to_consume(struct quic_conn *qc,
1648 struct qc_stream_desc *stream)
1649{
1650 int ret;
1651 struct eb64_node *frm_node;
1652
1653 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1654
1655 ret = 0;
1656 frm_node = eb64_first(&stream->acked_frms);
1657 while (frm_node) {
1658 struct quic_stream *strm;
1659 struct quic_frame *frm;
1660 size_t offset, len;
1661
1662 strm = eb64_entry(frm_node, struct quic_stream, offset);
1663 offset = strm->offset.key;
1664 len = strm->len;
1665
1666 if (offset > stream->ack_offset)
1667 break;
1668
1669 if (qc_stream_desc_ack(&stream, offset, len)) {
1670 /* cf. next comment : frame may be freed at this stage. */
1671 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1672 qc, stream ? strm : NULL, stream);
1673 ret = 1;
1674 }
1675
1676 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1677 * has been freed. with the stream frames tree. Nothing to do
1678 * anymore in here.
1679 */
1680 if (!stream) {
1681 qc_check_close_on_released_mux(qc);
1682 ret = 1;
1683 goto leave;
1684 }
1685
1686 frm_node = eb64_next(frm_node);
1687 eb64_delete(&strm->offset);
1688
1689 frm = container_of(strm, struct quic_frame, stream);
1690 qc_release_frm(qc, frm);
1691 }
1692
1693 leave:
1694 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
1695 return ret;
1696}
1697
1698/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1699static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1700 struct quic_frame *frm)
1701{
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001702 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm);
1703
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001704 switch (frm->type) {
1705 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1706 {
1707 struct quic_stream *strm_frm = &frm->stream;
1708 struct eb64_node *node = NULL;
1709 struct qc_stream_desc *stream = NULL;
1710 const size_t offset = strm_frm->offset.key;
1711 const size_t len = strm_frm->len;
1712
1713 /* do not use strm_frm->stream as the qc_stream_desc instance
1714 * might be freed at this stage. Use the id to do a proper
1715 * lookup.
1716 *
1717 * TODO if lookup operation impact on the perf is noticeable,
1718 * implement a refcount on qc_stream_desc instances.
1719 */
1720 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1721 if (!node) {
1722 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
1723 qc_release_frm(qc, frm);
1724 /* early return */
1725 goto leave;
1726 }
1727 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1728
1729 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
1730 if (offset <= stream->ack_offset) {
1731 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001732 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1733 qc, strm_frm, stream);
1734 }
1735
1736 if (!stream) {
1737 /* no need to continue if stream freed. */
1738 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
1739 qc_release_frm(qc, frm);
1740 qc_check_close_on_released_mux(qc);
1741 break;
1742 }
1743
1744 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
1745 qc, strm_frm, stream);
1746 qc_release_frm(qc, frm);
1747 }
1748 else {
1749 eb64_insert(&stream->acked_frms, &strm_frm->offset);
1750 }
1751
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01001752 quic_stream_try_to_consume(qc, stream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001753 }
1754 break;
1755 default:
1756 qc_release_frm(qc, frm);
1757 }
1758
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001759 leave:
1760 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1761}
1762
1763/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1764 * deallocating them, and their TX frames.
1765 * Returns the last node reached to be used for the next range.
1766 * May be NULL if <largest> node could not be found.
1767 */
1768static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1769 struct eb_root *pkts,
1770 unsigned int *pkt_flags,
1771 struct list *newly_acked_pkts,
1772 struct eb64_node *largest_node,
1773 uint64_t largest, uint64_t smallest)
1774{
1775 struct eb64_node *node;
1776 struct quic_tx_packet *pkt;
1777
1778 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1779
1780 node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
1781 while (node && node->key >= smallest) {
1782 struct quic_frame *frm, *frmbak;
1783
1784 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
1785 *pkt_flags |= pkt->flags;
1786 LIST_INSERT(newly_acked_pkts, &pkt->list);
1787 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
1788 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1789 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaille814645f2022-11-18 18:15:28 +01001790 /* If there are others packet in the same datagram <pkt> is attached to,
1791 * detach the previous one and the next one from <pkt>.
1792 */
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01001793 quic_tx_packet_dgram_detach(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001794 node = eb64_prev(node);
1795 eb64_delete(&pkt->pn_node);
1796 }
1797
1798 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1799 return node;
1800}
1801
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001802/* Remove all frames from <pkt_frm_list> and reinsert them in the same order
1803 * they have been sent into <pktns_frm_list>. The loss counter of each frame is
1804 * incremented and checked if it does not exceed retransmission limit.
1805 *
1806 * Returns 1 on success, 0 if a frame loss limit is exceeded. A
1807 * CONNECTION_CLOSE is scheduled in this case.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001808 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001809static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1810 struct quic_tx_packet *pkt,
1811 struct list *pktns_frm_list)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001812{
1813 struct quic_frame *frm, *frmbak;
1814 struct list tmp = LIST_HEAD_INIT(tmp);
1815 struct list *pkt_frm_list = &pkt->frms;
1816 uint64_t pn = pkt->pn_node.key;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001817 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001818
1819 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1820
1821 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1822 /* First remove this frame from the packet it was attached to */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001823 LIST_DEL_INIT(&frm->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001824 quic_tx_packet_refdec(pkt);
1825 /* At this time, this frame is not freed but removed from its packet */
1826 frm->pkt = NULL;
1827 /* Remove any reference to this frame */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001828 qc_frm_unref(frm, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001829 switch (frm->type) {
1830 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1831 {
1832 struct quic_stream *strm_frm = &frm->stream;
1833 struct eb64_node *node = NULL;
1834 struct qc_stream_desc *stream_desc;
1835
1836 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1837 if (!node) {
1838 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1839 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1840 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001841 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001842 continue;
1843 }
1844
1845 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1846 /* Do not resend this frame if in the "already acked range" */
1847 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1848 TRACE_DEVEL("ignored frame in already acked range",
1849 QUIC_EV_CONN_PRSAFRM, qc, frm);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001850 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001851 continue;
1852 }
1853 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1854 strm_frm->offset.key = stream_desc->ack_offset;
1855 TRACE_DEVEL("updated partially acked frame",
1856 QUIC_EV_CONN_PRSAFRM, qc, frm);
1857 }
1858 break;
1859 }
1860
1861 default:
1862 break;
1863 }
1864
1865 /* Do not resend probing packet with old data */
1866 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1867 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1868 qc, frm, &pn);
1869 if (frm->origin)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001870 LIST_DEL_INIT(&frm->ref);
1871 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001872 continue;
1873 }
1874
1875 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1876 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1877 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1878 qc, frm, &pn);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001879 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001880 }
1881 else {
Amaury Denoyelle24d5b722023-01-31 11:44:50 +01001882 if (++frm->loss_count >= global.tune.quic_max_frame_loss) {
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001883 TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc);
1884 quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR));
1885 close = 1;
1886 }
1887
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001888 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1889 /* Mark this STREAM frame as lost. A look up their stream descriptor
1890 * will be performed to check the stream is not consumed or released.
1891 */
1892 frm->flags |= QUIC_FL_TX_FRAME_LOST;
1893 }
1894 LIST_APPEND(&tmp, &frm->list);
1895 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
1896 }
1897 }
1898
1899 LIST_SPLICE(pktns_frm_list, &tmp);
1900
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001901 end:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001902 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01001903 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001904}
1905
1906/* Free <pkt> TX packet and its attached frames.
1907 * This is the responsibility of the caller to remove this packet of
1908 * any data structure it was possibly attached to.
1909 */
1910static inline void free_quic_tx_packet(struct quic_conn *qc,
1911 struct quic_tx_packet *pkt)
1912{
1913 struct quic_frame *frm, *frmbak;
1914
1915 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1916
1917 if (!pkt)
1918 goto leave;
1919
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001920 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1921 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02001922 pool_free(pool_head_quic_tx_packet, pkt);
1923
1924 leave:
1925 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
1926}
1927
1928/* Free the TX packets of <pkts> list */
1929static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
1930{
1931 struct quic_tx_packet *pkt, *tmp;
1932
1933 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1934
1935 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1936 LIST_DELETE(&pkt->list);
1937 eb64_delete(&pkt->pn_node);
1938 free_quic_tx_packet(qc, pkt);
1939 }
1940
1941 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
1942}
1943
1944/* Remove already sent ranges of acknowledged packet numbers from
1945 * <pktns> packet number space tree below <largest_acked_pn> possibly
1946 * updating the range which contains <largest_acked_pn>.
1947 * Never fails.
1948 */
1949static void qc_treat_ack_of_ack(struct quic_conn *qc,
1950 struct quic_pktns *pktns,
1951 int64_t largest_acked_pn)
1952{
1953 struct eb64_node *ar, *next_ar;
1954 struct quic_arngs *arngs = &pktns->rx.arngs;
1955
1956 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1957
1958 ar = eb64_first(&arngs->root);
1959 while (ar) {
1960 struct quic_arng_node *ar_node;
1961
1962 next_ar = eb64_next(ar);
1963 ar_node = eb64_entry(ar, struct quic_arng_node, first);
1964
1965 if ((int64_t)ar_node->first.key > largest_acked_pn) {
1966 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
1967 break;
1968 }
1969
1970 if (largest_acked_pn < ar_node->last) {
1971 eb64_delete(ar);
1972 ar_node->first.key = largest_acked_pn + 1;
1973 eb64_insert(&arngs->root, ar);
1974 break;
1975 }
1976
1977 eb64_delete(ar);
1978 pool_free(pool_head_quic_arng, ar_node);
1979 arngs->sz--;
1980 ar = next_ar;
1981 }
1982
1983 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1984}
1985
1986/* Send a packet ack event nofication for each newly acked packet of
1987 * <newly_acked_pkts> list and free them.
1988 * Always succeeds.
1989 */
1990static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
1991 struct list *newly_acked_pkts)
1992{
1993 struct quic_tx_packet *pkt, *tmp;
1994 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1995
1996 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1997
1998 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1999 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2000 qc->path->prep_in_flight -= pkt->in_flight_len;
2001 qc->path->in_flight -= pkt->in_flight_len;
2002 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2003 qc->path->ifae_pkts--;
2004 /* If this packet contained an ACK frame, proceed to the
2005 * acknowledging of range of acks from the largest acknowledged
2006 * packet number which was sent in an ACK frame by this packet.
2007 */
2008 if (pkt->largest_acked_pn != -1)
2009 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
2010 ev.ack.acked = pkt->in_flight_len;
2011 ev.ack.time_sent = pkt->time_sent;
2012 quic_cc_event(&qc->path->cc, &ev);
2013 LIST_DELETE(&pkt->list);
2014 eb64_delete(&pkt->pn_node);
2015 quic_tx_packet_refdec(pkt);
2016 }
2017
2018 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2019
2020}
2021
2022/* Release all the frames attached to <pktns> packet number space */
2023static inline void qc_release_pktns_frms(struct quic_conn *qc,
2024 struct quic_pktns *pktns)
2025{
2026 struct quic_frame *frm, *frmbak;
2027
2028 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2029
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002030 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list)
2031 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002032
2033 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2034}
2035
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002036/* Handle <pkts> list of lost packets detected at <now_us> handling their TX
2037 * frames. Send a packet loss event to the congestion controller if in flight
2038 * packet have been lost. Also frees the packet in <pkts> list.
2039 *
2040 * Returns 1 on success else 0 if loss limit has been exceeded. A
2041 * CONNECTION_CLOSE was prepared to close the connection ASAP.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002042 */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002043static inline int qc_release_lost_pkts(struct quic_conn *qc,
2044 struct quic_pktns *pktns,
2045 struct list *pkts,
2046 uint64_t now_us)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002047{
2048 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002049 int close = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002050
2051 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2052
2053 if (LIST_ISEMPTY(pkts))
2054 goto leave;
2055
2056 oldest_lost = newest_lost = NULL;
2057 list_for_each_entry_safe(pkt, tmp, pkts, list) {
2058 struct list tmp = LIST_HEAD_INIT(tmp);
2059
2060 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
2061 qc->path->prep_in_flight -= pkt->in_flight_len;
2062 qc->path->in_flight -= pkt->in_flight_len;
2063 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
2064 qc->path->ifae_pkts--;
2065 /* Treat the frames of this lost packet. */
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002066 if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms))
2067 close = 1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002068 LIST_DELETE(&pkt->list);
2069 if (!oldest_lost) {
2070 oldest_lost = newest_lost = pkt;
2071 }
2072 else {
2073 if (newest_lost != oldest_lost)
2074 quic_tx_packet_refdec(newest_lost);
2075 newest_lost = pkt;
2076 }
2077 }
2078
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002079 if (!close) {
2080 if (newest_lost) {
2081 /* Sent a congestion event to the controller */
2082 struct quic_cc_event ev = { };
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002083
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002084 ev.type = QUIC_CC_EVT_LOSS;
2085 ev.loss.time_sent = newest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002086
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002087 quic_cc_event(&qc->path->cc, &ev);
2088 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002089
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002090 /* If an RTT have been already sampled, <rtt_min> has been set.
2091 * We must check if we are experiencing a persistent congestion.
2092 * If this is the case, the congestion controller must re-enter
2093 * slow start state.
2094 */
2095 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2096 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002097
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002098 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2099 now_ms, qc->max_ack_delay))
2100 qc->path->cc.algo->slow_start(&qc->path->cc);
2101 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002102 }
2103
Amaury Denoyelle3a72ba22022-11-14 11:41:34 +01002104 /* <oldest_lost> cannot be NULL at this stage because we have ensured
2105 * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a
2106 * possible overflow on a 0 byte region with O2 optimization.
2107 */
2108 ALREADY_CHECKED(oldest_lost);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002109 quic_tx_packet_refdec(oldest_lost);
2110 if (newest_lost != oldest_lost)
2111 quic_tx_packet_refdec(newest_lost);
2112
2113 leave:
2114 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002115 return !close;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002116}
2117
2118/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2119 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
2120 * if the largest acked packet was newly acked and if there was at least one newly
2121 * acked ack-eliciting packet.
2122 * Return 1, if succeeded, 0 if not.
2123 */
2124static inline int qc_parse_ack_frm(struct quic_conn *qc,
2125 struct quic_frame *frm,
2126 struct quic_enc_level *qel,
2127 unsigned int *rtt_sample,
2128 const unsigned char **pos, const unsigned char *end)
2129{
2130 struct quic_ack *ack = &frm->ack;
2131 uint64_t smallest, largest;
2132 struct eb_root *pkts;
2133 struct eb64_node *largest_node;
2134 unsigned int time_sent, pkt_flags;
2135 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2136 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2137 int ret = 0;
2138
2139 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2140
2141 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2142 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
2143 qc, NULL, &ack->largest_ack);
2144 goto err;
2145 }
2146
2147 if (ack->first_ack_range > ack->largest_ack) {
2148 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
2149 qc, NULL, &ack->first_ack_range);
2150 goto err;
2151 }
2152
2153 largest = ack->largest_ack;
2154 smallest = largest - ack->first_ack_range;
2155 pkts = &qel->pktns->tx.pkts;
2156 pkt_flags = 0;
2157 largest_node = NULL;
2158 time_sent = 0;
2159
2160 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
2161 largest_node = eb64_lookup(pkts, largest);
2162 if (!largest_node) {
2163 TRACE_DEVEL("Largest acked packet not found",
2164 QUIC_EV_CONN_PRSAFRM, qc);
2165 }
2166 else {
2167 time_sent = eb64_entry(largest_node,
2168 struct quic_tx_packet, pn_node)->time_sent;
2169 }
2170 }
2171
2172 TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM,
2173 qc, NULL, &largest, &smallest);
2174 do {
2175 uint64_t gap, ack_range;
2176
2177 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2178 largest_node, largest, smallest);
2179 if (!ack->ack_range_num--)
2180 break;
2181
2182 if (!quic_dec_int(&gap, pos, end)) {
2183 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
2184 goto err;
2185 }
2186
2187 if (smallest < gap + 2) {
2188 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
2189 qc, NULL, &gap, &smallest);
2190 goto err;
2191 }
2192
2193 largest = smallest - gap - 2;
2194 if (!quic_dec_int(&ack_range, pos, end)) {
2195 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
2196 goto err;
2197 }
2198
2199 if (largest < ack_range) {
2200 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
2201 qc, NULL, &largest, &ack_range);
2202 goto err;
2203 }
2204
2205 /* Do not use this node anymore. */
2206 largest_node = NULL;
2207 /* Next range */
2208 smallest = largest - ack_range;
2209
2210 TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM,
2211 qc, NULL, &largest, &smallest);
2212 } while (1);
2213
2214 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2215 *rtt_sample = tick_remain(time_sent, now_ms);
2216 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
2217 }
2218
2219 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2220 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
2221 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01002222 if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms))
2223 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002224 }
2225 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2226 if (quic_peer_validated_addr(qc))
2227 qc->path->loss.pto_count = 0;
2228 qc_set_timer(qc);
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01002229 qc_notify_send(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002230 }
2231
2232 ret = 1;
2233 leave:
2234 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2235 return ret;
2236
2237 err:
2238 free_quic_tx_pkts(qc, &newly_acked_pkts);
2239 goto leave;
2240}
2241
2242/* This function gives the detail of the SSL error. It is used only
2243 * if the debug mode and the verbose mode are activated. It dump all
2244 * the SSL error until the stack was empty.
2245 */
2246static forceinline void qc_ssl_dump_errors(struct connection *conn)
2247{
2248 if (unlikely(global.mode & MODE_DEBUG)) {
2249 while (1) {
2250 const char *func = NULL;
2251 unsigned long ret;
2252
2253 ERR_peek_error_func(&func);
2254 ret = ERR_get_error();
2255 if (!ret)
2256 return;
2257
2258 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
2259 func, ERR_reason_error_string(ret));
2260 }
2261 }
2262}
2263
2264int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2265 const char **str, int *len);
2266
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002267/* Finalize <qc> QUIC connection:
2268 * - initialize the Initial QUIC TLS context for negotiated version,
2269 * - derive the secrets for this context,
2270 * - set them into the TLS stack,
2271 *
2272 * MUST be called after having received the remote transport parameters which
2273 * are parsed when the TLS callback for the ClientHello message is called upon
2274 * SSL_do_handshake() calls, not necessarily at the first time as this TLS
2275 * message may be splitted between packets
2276 * Return 1 if succeeded, 0 if not.
2277 */
2278static int qc_conn_finalize(struct quic_conn *qc, int server)
2279{
2280 int ret = 0;
2281
2282 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
2283
2284 if (qc->flags & QUIC_FL_CONN_FINALIZED)
2285 goto finalized;
2286
2287 if (qc->negotiated_version &&
2288 !qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
2289 qc->odcid.data, qc->odcid.len, server))
2290 goto out;
2291
2292 /* This connection is functional (ready to send/receive) */
2293 qc->flags |= QUIC_FL_CONN_FINALIZED;
2294
2295 finalized:
2296 ret = 1;
2297 out:
2298 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
2299 return ret;
2300}
2301
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002302/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2303 * from <qel> encryption level with <ctx> as QUIC connection context.
2304 * Remaining parameter are there for debugging purposes.
2305 * Return 1 if succeeded, 0 if not.
2306 */
2307static inline int qc_provide_cdata(struct quic_enc_level *el,
2308 struct ssl_sock_ctx *ctx,
2309 const unsigned char *data, size_t len,
2310 struct quic_rx_packet *pkt,
2311 struct quic_rx_crypto_frm *cf)
2312{
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002313#ifdef DEBUG_STRICT
2314 enum ncb_ret ncb_ret;
2315#endif
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002316 int ssl_err, state;
2317 struct quic_conn *qc;
2318 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002319 struct ncbuf *ncbuf = &el->cstream->rx.ncbuf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002320
2321 ssl_err = SSL_ERROR_NONE;
2322 qc = ctx->qc;
2323
2324 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2325
2326 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2327 TRACE_ERROR("SSL_provide_quic_data() error",
2328 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
2329 goto leave;
2330 }
2331
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002332 TRACE_PROTO("in order CRYPTO data",
2333 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
2334
2335 state = qc->state;
2336 if (state < QUIC_HS_ST_COMPLETE) {
2337 ssl_err = SSL_do_handshake(ctx->ssl);
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002338
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01002339 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
2340 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
2341 goto leave;
2342 }
2343
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002344 /* Finalize the connection as soon as possible if the peer transport parameters
2345 * have been received. This may be useful to send packets even if this
2346 * handshake fails.
2347 */
2348 if ((qc->flags & QUIC_FL_CONN_TX_TP_RECEIVED) && !qc_conn_finalize(qc, 1)) {
2349 TRACE_ERROR("connection finalization failed", QUIC_EV_CONN_IO_CB, qc, &state);
2350 goto leave;
2351 }
2352
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002353 if (ssl_err != 1) {
2354 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2355 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2356 TRACE_PROTO("SSL handshake in progress",
2357 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2358 goto out;
2359 }
2360
2361 /* TODO: Should close the connection asap */
2362 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2363 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2364 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2365 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2366 }
2367 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2368 qc_ssl_dump_errors(ctx->conn);
2369 ERR_clear_error();
2370 goto leave;
2371 }
2372
2373 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
2374
2375 /* Check the alpn could be negotiated */
2376 if (!qc->app_ops) {
2377 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2378 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2379 goto leave;
2380 }
2381
2382 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2383 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
2384 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2385 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2386 }
2387 /* I/O callback switch */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02002388 qc->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002389 if (qc_is_listener(ctx->qc)) {
2390 qc->state = QUIC_HS_ST_CONFIRMED;
2391 /* The connection is ready to be accepted. */
2392 quic_accept_push_qc(qc);
2393 }
2394 else {
2395 qc->state = QUIC_HS_ST_COMPLETE;
2396 }
2397
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02002398 /* Prepare the next key update */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002399 if (!quic_tls_key_update(qc)) {
2400 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc);
2401 goto leave;
2402 }
2403 } else {
2404 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2405 if (ssl_err != 1) {
2406 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2407 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2408 TRACE_PROTO("SSL post handshake in progress",
2409 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2410 goto out;
2411 }
2412
2413 TRACE_ERROR("SSL post handshake error",
2414 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
2415 goto leave;
2416 }
2417
2418 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
2419 }
2420
2421 out:
2422 ret = 1;
2423 leave:
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002424 /* The CRYPTO data are consumed even in case of an error to release
2425 * the memory asap.
2426 */
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002427 if (!ncb_is_null(ncbuf)) {
2428#ifdef DEBUG_STRICT
2429 ncb_ret = ncb_advance(ncbuf, len);
2430 /* ncb_advance() must always succeed. This is guaranteed as
2431 * this is only done inside a data block. If false, this will
2432 * lead to handshake failure with quic_enc_level offset shifted
2433 * from buffer data.
2434 */
2435 BUG_ON(ncb_ret != NCB_RET_OK);
2436#else
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002437 ncb_advance(ncbuf, len);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002438#endif
2439 }
2440
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002441 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
2442 return ret;
2443}
2444
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002445/* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc>
2446 * connection. <fin> is true if FIN bit is set on frame type.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002447 *
2448 * Return 1 on success. On error, 0 is returned. In this case, the packet
2449 * containing the frame must not be acknowledged.
2450 */
2451static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2452 struct quic_stream *strm_frm,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002453 struct quic_conn *qc, char fin)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002454{
2455 int ret;
2456
2457 /* RFC9000 13.1. Packet Processing
2458 *
2459 * A packet MUST NOT be acknowledged until packet protection has been
2460 * successfully removed and all frames contained in the packet have
2461 * been processed. For STREAM frames, this means the data has been
2462 * enqueued in preparation to be received by the application protocol,
2463 * but it does not require that data be delivered and consumed.
2464 */
2465 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2466
2467 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002468 strm_frm->offset.key, fin, (char *)strm_frm->data);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002469
2470 /* frame rejected - packet must not be acknowledeged */
2471 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2472 return !ret;
2473}
2474
2475/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2476 * for <qc> QUIC connection.
2477 * This is a best effort function which never fails even if no memory could be
2478 * allocated to duplicate these frames.
2479 */
2480static void qc_dup_pkt_frms(struct quic_conn *qc,
2481 struct list *pkt_frm_list, struct list *out_frm_list)
2482{
2483 struct quic_frame *frm, *frmbak;
2484 struct list tmp = LIST_HEAD_INIT(tmp);
2485
2486 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2487
2488 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2489 struct quic_frame *dup_frm, *origin;
2490
2491 switch (frm->type) {
2492 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2493 {
2494 struct quic_stream *strm_frm = &frm->stream;
2495 struct eb64_node *node = NULL;
2496 struct qc_stream_desc *stream_desc;
2497
2498 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2499 if (!node) {
2500 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
2501 continue;
2502 }
2503
2504 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2505 /* Do not resend this frame if in the "already acked range" */
2506 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2507 TRACE_DEVEL("ignored frame in already acked range",
2508 QUIC_EV_CONN_PRSAFRM, qc, frm);
2509 continue;
2510 }
2511 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2512 strm_frm->offset.key = stream_desc->ack_offset;
2513 TRACE_DEVEL("updated partially acked frame",
2514 QUIC_EV_CONN_PRSAFRM, qc, frm);
2515 }
2516 break;
2517 }
2518
2519 default:
2520 break;
2521 }
2522
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002523 /* If <frm> is already a copy of another frame, we must take
2524 * its original frame as source for the copy.
2525 */
2526 origin = frm->origin ? frm->origin : frm;
2527 dup_frm = qc_frm_dup(origin);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002528 if (!dup_frm) {
2529 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2530 break;
2531 }
2532
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002533 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002534 if (origin->pkt) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002535 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
2536 qc, NULL, &origin->pkt->pn_node.key);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002537 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002538 else {
2539 /* <origin> is a frame which was sent from a packet detected as lost. */
2540 TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
2541 }
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01002542
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002543 LIST_APPEND(&tmp, &dup_frm->list);
2544 }
2545
2546 LIST_SPLICE(out_frm_list, &tmp);
2547
2548 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2549}
2550
2551/* Prepare a fast retransmission from <qel> encryption level */
2552static void qc_prep_fast_retrans(struct quic_conn *qc,
2553 struct quic_enc_level *qel,
2554 struct list *frms1, struct list *frms2)
2555{
2556 struct eb_root *pkts = &qel->pktns->tx.pkts;
2557 struct list *frms = frms1;
2558 struct eb64_node *node;
2559 struct quic_tx_packet *pkt;
2560
2561 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2562
2563 BUG_ON(frms1 == frms2);
2564
2565 pkt = NULL;
2566 node = eb64_first(pkts);
2567 start:
2568 while (node) {
2569 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2570 node = eb64_next(node);
2571 /* Skip the empty and coalesced packets */
Frédéric Lécaille6dead912023-01-30 17:27:32 +01002572 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
2573 "--> pn=%llu (%d %d)", (ull)pkt->pn_node.key,
2574 LIST_ISEMPTY(&pkt->frms), !!(pkt->flags & QUIC_FL_TX_PACKET_COALESCED));
Frédéric Lécaille055e8262023-01-31 10:10:06 +01002575 if (!LIST_ISEMPTY(&pkt->frms))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002576 break;
2577 }
2578
2579 if (!pkt)
2580 goto leave;
2581
2582 /* When building a packet from another one, the field which may increase the
2583 * packet size is the packet number. And the maximum increase is 4 bytes.
2584 */
2585 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2586 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2587 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
2588 goto leave;
2589 }
2590
2591 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
2592 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2593 if (frms == frms1 && frms2) {
2594 frms = frms2;
2595 goto start;
2596 }
2597 leave:
2598 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
2599}
2600
2601/* Prepare a fast retransmission during a handshake after a client
2602 * has resent Initial packets. According to the RFC a server may retransmit
2603 * Initial packets send them coalescing with others (Handshake here).
2604 * (Listener only function).
2605 */
2606static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2607 struct list *ifrms, struct list *hfrms)
2608{
2609 struct list itmp = LIST_HEAD_INIT(itmp);
2610 struct list htmp = LIST_HEAD_INIT(htmp);
2611
2612 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2613 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2614 struct quic_enc_level *qel = iqel;
2615 struct eb_root *pkts;
2616 struct eb64_node *node;
2617 struct quic_tx_packet *pkt;
2618 struct list *tmp = &itmp;
2619
2620 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2621 start:
2622 pkt = NULL;
2623 pkts = &qel->pktns->tx.pkts;
2624 node = eb64_first(pkts);
2625 /* Skip the empty packet (they have already been retransmitted) */
2626 while (node) {
2627 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaille6dead912023-01-30 17:27:32 +01002628 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
2629 "--> pn=%llu (%d %d)", (ull)pkt->pn_node.key,
2630 LIST_ISEMPTY(&pkt->frms), !!(pkt->flags & QUIC_FL_TX_PACKET_COALESCED));
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002631 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
2632 break;
2633 node = eb64_next(node);
2634 }
2635
2636 if (!pkt)
2637 goto end;
2638
2639 /* When building a packet from another one, the field which may increase the
2640 * packet size is the packet number. And the maximum increase is 4 bytes.
2641 */
2642 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2643 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2644 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2645 goto end;
2646 }
2647
2648 qel->pktns->tx.pto_probe += 1;
2649
2650 /* No risk to loop here, #packet per datagram is bounded */
2651 requeue:
2652 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2653 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
2654 if (qel == iqel) {
2655 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2656 pkt = pkt->next;
2657 tmp = &htmp;
2658 hqel->pktns->tx.pto_probe += 1;
2659 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc);
2660 goto requeue;
2661 }
2662 }
2663
2664 end:
2665 LIST_SPLICE(ifrms, &itmp);
2666 LIST_SPLICE(hfrms, &htmp);
2667
2668 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2669}
2670
2671static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
2672{
2673 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2674
2675 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2676 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2677 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2678 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
2679 goto out;
2680
2681 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2682 }
2683
2684 out:
2685 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
2686}
2687
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002688/* Cancel a request on connection <qc> for stream id <id>. This is useful when
2689 * the client opens a new stream but the MUX has already been released. A
Amaury Denoyelle75463012023-02-20 10:31:27 +01002690 * STOP_SENDING + RESET_STREAM frames are prepared for emission.
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002691 *
2692 * TODO this function is closely related to H3. Its place should be in H3 layer
2693 * instead of quic-conn but this requires an architecture adjustment.
2694 *
2695 * Returns 1 on sucess else 0.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002696 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002697static int qc_h3_request_reject(struct quic_conn *qc, uint64_t id)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002698{
2699 int ret = 0;
Amaury Denoyelle75463012023-02-20 10:31:27 +01002700 struct quic_frame *ss, *rs;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002701 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002702 const uint64_t app_error_code = H3_REQUEST_REJECTED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002703
2704 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2705
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002706 /* Do not emit rejection for unknown unidirectional stream as it is
2707 * forbidden to close some of them (H3 control stream and QPACK
2708 * encoder/decoder streams).
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002709 */
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002710 if (quic_stream_is_uni(id)) {
2711 ret = 1;
2712 goto out;
2713 }
2714
Amaury Denoyelle75463012023-02-20 10:31:27 +01002715 ss = qc_frm_alloc(QUIC_FT_STOP_SENDING);
2716 if (!ss) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002717 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2718 goto out;
2719 }
2720
Amaury Denoyelle75463012023-02-20 10:31:27 +01002721 ss->stop_sending.id = id;
2722 ss->stop_sending.app_error_code = app_error_code;
2723
2724 rs = qc_frm_alloc(QUIC_FT_RESET_STREAM);
2725 if (!rs) {
2726 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2727 qc_frm_free(&ss);
2728 goto out;
2729 }
2730
2731 rs->reset_stream.id = id;
2732 rs->reset_stream.app_error_code = app_error_code;
2733 rs->reset_stream.final_size = 0;
2734
2735 LIST_APPEND(&qel->pktns->tx.frms, &ss->list);
2736 LIST_APPEND(&qel->pktns->tx.frms, &rs->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002737 ret = 1;
2738 out:
2739 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2740 return ret;
2741}
2742
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002743/* Release the underlying memory use by <ncbuf> non-contiguous buffer */
2744static void quic_free_ncbuf(struct ncbuf *ncbuf)
2745{
2746 struct buffer buf;
2747
2748 if (ncb_is_null(ncbuf))
2749 return;
2750
2751 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
2752 b_free(&buf);
2753 offer_buffers(NULL, 1);
2754
2755 *ncbuf = NCBUF_NULL;
2756}
2757
2758/* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */
2759static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf)
2760{
2761 struct buffer buf = BUF_NULL;
2762
2763 if (!ncb_is_null(ncbuf))
2764 return ncbuf;
2765
2766 b_alloc(&buf);
2767 BUG_ON(b_is_null(&buf));
2768
2769 *ncbuf = ncb_make(buf.area, buf.size, 0);
2770 ncb_init(ncbuf, 0);
2771
2772 return ncbuf;
2773}
2774
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002775/* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn.
2776 * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the
2777 * speed up handshake completion may be run after having received duplicated
2778 * CRYPTO data.
2779 */
2780static int qc_handle_crypto_frm(struct quic_conn *qc,
2781 struct quic_crypto *frm, struct quic_rx_packet *pkt,
2782 struct quic_enc_level *qel, int *fast_retrans)
2783{
2784 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002785 enum ncb_ret ncb_ret;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002786 /* XXX TO DO: <cfdebug> is used only for the traces. */
2787 struct quic_rx_crypto_frm cfdebug = {
2788 .offset_node.key = frm->offset,
2789 .len = frm->len,
2790 };
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002791 struct quic_cstream *cstream = qel->cstream;
2792 struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002793
2794 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2795 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2796 TRACE_PROTO("CRYPTO data discarded",
2797 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2798 goto done;
2799 }
2800
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002801 if (unlikely(frm->offset < cstream->rx.offset)) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002802 size_t diff;
2803
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002804 if (frm->offset + frm->len <= cstream->rx.offset) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002805 /* Nothing to do */
2806 TRACE_PROTO("Already received CRYPTO data",
2807 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2808 if (qc_is_listener(qc) && qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
2809 !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP))
2810 *fast_retrans = 1;
2811 goto done;
2812 }
2813
2814 TRACE_PROTO("Partially already received CRYPTO data",
2815 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
2816
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002817 diff = cstream->rx.offset - frm->offset;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002818 frm->len -= diff;
2819 frm->data += diff;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002820 frm->offset = cstream->rx.offset;
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002821 }
2822
Amaury Denoyelleff95f2d2022-11-18 14:50:06 +01002823 if (frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) {
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002824 if (!qc_provide_cdata(qel, qc->xprt_ctx, frm->data, frm->len,
2825 pkt, &cfdebug)) {
2826 // trace already emitted by function above
2827 goto leave;
2828 }
2829
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002830 cstream->rx.offset += frm->len;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01002831 TRACE_DEVEL("increment crypto level offset", QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002832 goto done;
2833 }
2834
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002835 if (!quic_get_ncbuf(ncbuf) ||
2836 ncb_is_null(ncbuf)) {
2837 TRACE_ERROR("CRYPTO ncbuf allocation failed", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002838 goto leave;
2839 }
2840
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02002841 /* frm->offset > cstream-trx.offset */
2842 ncb_ret = ncb_add(ncbuf, frm->offset - cstream->rx.offset,
2843 (const char *)frm->data, frm->len, NCB_ADD_COMPARE);
2844 if (ncb_ret != NCB_RET_OK) {
2845 if (ncb_ret == NCB_RET_DATA_REJ) {
2846 TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc);
2847 quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
2848 }
2849 else if (ncb_ret == NCB_RET_GAP_SIZE) {
2850 TRACE_ERROR("cannot bufferize frame due to gap size limit",
2851 QUIC_EV_CONN_PRSHPKT, qc);
2852 }
2853 goto leave;
2854 }
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002855
2856 done:
2857 ret = 1;
2858 leave:
2859 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2860 return ret;
2861}
2862
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02002863/* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel>
2864 * as encryption level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002865 * Returns 1 if succeeded, 0 if failed.
2866 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02002867static int qc_parse_pkt_frms(struct quic_conn *qc, struct quic_rx_packet *pkt,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002868 struct quic_enc_level *qel)
2869{
2870 struct quic_frame frm;
2871 const unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002872 int fast_retrans = 0, ret = 0;
2873
2874 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2875 /* Skip the AAD */
2876 pos = pkt->data + pkt->aad_len;
2877 end = pkt->data + pkt->len;
2878
2879 while (pos < end) {
2880 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
2881 // trace already emitted by function above
2882 goto leave;
2883 }
2884
2885 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
2886 switch (frm.type) {
2887 case QUIC_FT_PADDING:
2888 break;
2889 case QUIC_FT_PING:
2890 break;
2891 case QUIC_FT_ACK:
2892 {
2893 unsigned int rtt_sample;
2894
2895 rtt_sample = 0;
2896 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
2897 // trace already emitted by function above
2898 goto leave;
2899 }
2900
2901 if (rtt_sample) {
2902 unsigned int ack_delay;
2903
2904 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
2905 qc->state >= QUIC_HS_ST_CONFIRMED ?
2906 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2907 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
2908 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
2909 }
2910 break;
2911 }
2912 case QUIC_FT_RESET_STREAM:
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01002913 if (qc->mux_state == QC_MUX_READY) {
2914 struct quic_reset_stream *rs = &frm.reset_stream;
2915 qcc_recv_reset_stream(qc->qcc, rs->id, rs->app_error_code, rs->final_size);
2916 }
2917 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002918 case QUIC_FT_STOP_SENDING:
2919 {
2920 struct quic_stop_sending *stop_sending = &frm.stop_sending;
2921 if (qc->mux_state == QC_MUX_READY) {
2922 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
2923 stop_sending->app_error_code)) {
2924 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
2925 goto leave;
2926 }
2927 }
2928 break;
2929 }
2930 case QUIC_FT_CRYPTO:
Frédéric Lécaillea20c93e2022-09-12 14:54:45 +02002931 if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002932 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002933 break;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002934 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2935 {
2936 struct quic_stream *stream = &frm.stream;
2937 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002938 const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002939
2940 /* The upper layer may not be allocated. */
2941 if (qc->mux_state != QC_MUX_READY) {
2942 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
2943 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
2944 break;
2945 }
2946 else {
2947 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle38836b62023-02-07 14:24:54 +01002948 if (qc->app_ops == &h3_ops) {
Amaury Denoyelle156a89a2023-02-20 10:32:16 +01002949 if (!qc_h3_request_reject(qc, stream->id)) {
2950 TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc);
2951 /* This packet will not be acknowledged */
2952 goto leave;
2953 }
2954 }
2955 else {
2956 /* This packet will not be acknowledged */
2957 goto leave;
Frédéric Lécailled18025e2023-01-20 15:33:50 +01002958 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002959 }
2960 }
2961
Amaury Denoyelle2216b082023-02-02 14:59:36 +01002962 if (!qc_handle_strm_frm(pkt, stream, qc, fin)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02002963 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
2964 goto leave;
2965 }
2966
2967 break;
2968 }
2969 case QUIC_FT_MAX_DATA:
2970 if (qc->mux_state == QC_MUX_READY) {
2971 struct quic_max_data *data = &frm.max_data;
2972 qcc_recv_max_data(qc->qcc, data->max_data);
2973 }
2974 break;
2975 case QUIC_FT_MAX_STREAM_DATA:
2976 if (qc->mux_state == QC_MUX_READY) {
2977 struct quic_max_stream_data *data = &frm.max_stream_data;
2978 if (qcc_recv_max_stream_data(qc->qcc, data->id,
2979 data->max_stream_data)) {
2980 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
2981 goto leave;
2982 }
2983 }
2984 break;
2985 case QUIC_FT_MAX_STREAMS_BIDI:
2986 case QUIC_FT_MAX_STREAMS_UNI:
2987 break;
2988 case QUIC_FT_DATA_BLOCKED:
2989 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2990 break;
2991 case QUIC_FT_STREAM_DATA_BLOCKED:
2992 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2993 break;
2994 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2995 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2996 break;
2997 case QUIC_FT_STREAMS_BLOCKED_UNI:
2998 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
2999 break;
3000 case QUIC_FT_NEW_CONNECTION_ID:
3001 case QUIC_FT_RETIRE_CONNECTION_ID:
3002 /* XXX TO DO XXX */
3003 break;
3004 case QUIC_FT_CONNECTION_CLOSE:
3005 case QUIC_FT_CONNECTION_CLOSE_APP:
3006 /* Increment the error counters */
3007 qc_cc_err_count_inc(qc, &frm);
3008 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
3009 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
3010 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
3011 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
3012 }
3013 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
3014 /* RFC 9000 10.2. Immediate Close:
3015 * The closing and draining connection states exist to ensure
3016 * that connections close cleanly and that delayed or reordered
3017 * packets are properly discarded. These states SHOULD persist
3018 * for at least three times the current PTO interval...
3019 *
3020 * Rearm the idle timeout only one time when entering draining
3021 * state.
3022 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003023 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01003024 qc_idle_timer_do_rearm(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003025 qc_notify_close(qc);
3026 }
3027 break;
3028 case QUIC_FT_HANDSHAKE_DONE:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003029 if (qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003030 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
3031 QUIC_EV_CONN_PRSHPKT, qc);
3032 goto leave;
3033 }
3034
3035 qc->state = QUIC_HS_ST_CONFIRMED;
3036 break;
3037 default:
3038 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
3039 goto leave;
3040 }
3041 }
3042
3043 /* Flag this packet number space as having received a packet. */
3044 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
3045
3046 if (fast_retrans) {
3047 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3048 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3049
3050 TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc);
3051 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
3052 qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP;
3053 }
3054
3055 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
3056 * has successfully parse a Handshake packet. The Initial encryption must also
3057 * be discarded.
3058 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003059 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003060 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
3061 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
3062 QUIC_FL_TLS_SECRETS_DCD)) {
3063 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3064 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
3065 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003066 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003067 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3068 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3069 }
3070 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
3071 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
3072 }
3073 }
3074
3075 ret = 1;
3076 leave:
3077 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
3078 return ret;
3079}
3080
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02003081
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003082/* Allocate Tx buffer from <qc> quic-conn if needed.
3083 *
3084 * Returns allocated buffer or NULL on error.
3085 */
3086static struct buffer *qc_txb_alloc(struct quic_conn *qc)
3087{
3088 struct buffer *buf = &qc->tx.buf;
3089 if (!b_alloc(buf))
3090 return NULL;
3091
3092 return buf;
3093}
3094
3095/* Free Tx buffer from <qc> if it is empty. */
3096static void qc_txb_release(struct quic_conn *qc)
3097{
3098 struct buffer *buf = &qc->tx.buf;
3099
3100 /* For the moment sending function is responsible to purge the buffer
3101 * entirely. It may change in the future but this requires to be able
3102 * to reuse old data.
Frédéric Lécaillebbf86be2023-02-20 09:28:58 +01003103 * For the momemt we do not care to leave data in the buffer for
3104 * a connection which is supposed to be killed asap.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003105 */
3106 BUG_ON_HOT(buf && b_data(buf));
3107
3108 if (!b_data(buf)) {
3109 b_free(buf);
3110 offer_buffers(NULL, 1);
3111 }
3112}
3113
3114/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
3115 * must contains the address of the first packet stored in the payload.
3116 *
3117 * Caller is responsible that there is enough space in the buffer.
3118 */
3119static void qc_txb_store(struct buffer *buf, uint16_t length,
3120 struct quic_tx_packet *first_pkt)
3121{
3122 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
3123 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
3124
3125 write_u16(b_tail(buf), length);
3126 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
3127 b_add(buf, hdlen + length);
3128}
3129
3130/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
3131 * with <frms> as ack-eliciting frame list to send, 0 if not.
3132 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
3133 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
3134 * <force_ack> may be set to 1 if you want to force an ack.
3135 */
3136static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
3137 struct quic_enc_level *qel, int cc, int probe, int force_ack)
3138{
3139 unsigned int must_ack = force_ack ||
3140 (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED));
3141
3142 /* Do not build any more packet if the TX secrets are not available or
3143 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
3144 * and if there is no more packets to send upon PTO expiration
3145 * and if there is no more ack-eliciting frames to send or in flight
3146 * congestion control limit is reached for prepared data
3147 */
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02003148 if (!quic_tls_has_tx_sec(qel) ||
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003149 (!cc && !probe && !must_ack &&
3150 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
3151 return 0;
3152 }
3153
3154 return 1;
3155}
3156
3157/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3158 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
3159 *
3160 * Each datagram is prepended by a two fields header : the datagram length and
3161 * the address of the packet contained in the datagram.
3162 *
3163 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3164 * -1 if something wrong happened.
3165 */
3166static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
3167 struct list *frms)
3168{
3169 int ret = -1;
3170 struct quic_enc_level *qel;
3171 unsigned char *end, *pos;
3172 struct quic_tx_packet *pkt;
3173 size_t total;
3174 /* Each datagram is prepended with its length followed by the address
3175 * of the first packet in the datagram.
3176 */
3177 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
3178
3179 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3180
3181 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3182 total = 0;
3183 pos = (unsigned char *)b_tail(buf);
3184 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
3185 int err, probe, cc;
3186
3187 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
3188 probe = 0;
3189 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3190 /* We do not probe if an immediate close was asked */
3191 if (!cc)
3192 probe = qel->pktns->tx.pto_probe;
3193
3194 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
3195 break;
3196
3197 /* Leave room for the datagram header */
3198 pos += dg_headlen;
3199 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3200 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3201 }
3202 else {
3203 end = pos + qc->path->mtu;
3204 }
3205
3206 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
3207 QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err);
3208 switch (err) {
3209 case -2:
3210 // trace already emitted by function above
3211 goto leave;
3212 case -1:
3213 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
3214 * MTU, we are here because of the congestion control window. There is
3215 * no need to try to reuse this buffer.
3216 */
3217 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
3218 goto out;
3219 default:
3220 break;
3221 }
3222
3223 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
3224 BUG_ON(!pkt);
3225
3226 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3227 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3228
3229 total += pkt->len;
3230
3231 /* Write datagram header. */
3232 qc_txb_store(buf, pkt->len, pkt);
3233 }
3234
3235 out:
3236 ret = total;
3237 leave:
3238 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3239 return ret;
3240}
3241
3242/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3243 * <frms>. Several packets can be regrouped in a single datagram. The result is
3244 * written into <buf>.
3245 *
3246 * Each datagram is prepended by a two fields header : the datagram length and
3247 * the address of first packet in the datagram.
3248 *
3249 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3250 * -1 if something wrong happened.
3251 */
3252static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
3253 enum quic_tls_enc_level tel, struct list *tel_frms,
3254 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3255{
3256 struct quic_enc_level *qel;
3257 unsigned char *end, *pos;
3258 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3259 /* length of datagrams */
3260 uint16_t dglen;
3261 size_t total;
3262 int ret = -1, padding;
3263 /* Each datagram is prepended with its length followed by the address
3264 * of the first packet in the datagram.
3265 */
3266 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
3267 struct list *frms;
3268
3269 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3270
3271 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05003272 * caller must ensure that buf is reset.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003273 */
3274 BUG_ON_HOT(buf->head || buf->data);
3275
3276 total = 0;
3277 qel = &qc->els[tel];
3278 frms = tel_frms;
3279 dglen = 0;
3280 padding = 0;
3281 pos = (unsigned char *)b_head(buf);
3282 first_pkt = prv_pkt = NULL;
3283 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
3284 int err, probe, cc;
3285 enum quic_pkt_type pkt_type;
3286 struct quic_tls_ctx *tls_ctx;
3287 const struct quic_version *ver;
3288 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3289 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3290 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3291
3292 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
3293 probe = 0;
3294 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
3295 /* We do not probe if an immediate close was asked */
3296 if (!cc)
3297 probe = qel->pktns->tx.pto_probe;
3298
3299 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
3300 if (prv_pkt)
3301 qc_txb_store(buf, dglen, first_pkt);
3302 /* Let's select the next encryption level */
3303 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3304 tel = next_tel;
3305 frms = next_tel_frms;
3306 qel = &qc->els[tel];
3307 /* Build a new datagram */
3308 prv_pkt = NULL;
3309 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
3310 continue;
3311 }
3312 break;
3313 }
3314
3315 pkt_type = quic_tls_level_pkt_type(tel);
3316 if (!prv_pkt) {
3317 /* Leave room for the datagram header */
3318 pos += dg_headlen;
3319 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
3320 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3321 }
3322 else {
3323 end = pos + qc->path->mtu;
3324 }
3325 }
3326
3327 if (qc->negotiated_version) {
3328 ver = qc->negotiated_version;
3329 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3330 tls_ctx = &qc->negotiated_ictx;
3331 else
3332 tls_ctx = &qel->tls_ctx;
3333 }
3334 else {
3335 ver = qc->original_version;
3336 tls_ctx = &qel->tls_ctx;
3337 }
3338
3339 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
3340 qc, ver, dglen, pkt_type,
3341 force_ack, padding, probe, cc, &err);
3342 switch (err) {
3343 case -2:
3344 // trace already emitted by function above
3345 goto leave;
3346 case -1:
3347 /* If there was already a correct packet present, set the
3348 * current datagram as prepared into <cbuf>.
3349 */
3350 if (prv_pkt)
3351 qc_txb_store(buf, dglen, first_pkt);
3352 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
3353 goto out;
3354 default:
3355 break;
3356 }
3357
3358 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
3359 BUG_ON(!cur_pkt);
3360
3361 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3362 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3363
3364 total += cur_pkt->len;
3365 /* keep trace of the first packet in the datagram */
3366 if (!first_pkt)
3367 first_pkt = cur_pkt;
Frédéric Lécaille74b5f7b2022-11-20 18:35:35 +01003368 /* Attach the current one to the previous one and vice versa */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003369 if (prv_pkt) {
3370 prv_pkt->next = cur_pkt;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01003371 cur_pkt->prev = prv_pkt;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003372 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3373 }
3374 /* Let's say we have to build a new dgram */
3375 prv_pkt = NULL;
3376 dglen += cur_pkt->len;
3377 /* Client: discard the Initial encryption keys as soon as
3378 * a handshake packet could be built.
3379 */
3380 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
3381 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3382 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3383 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3384 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3385 qc_set_timer(qc);
3386 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3387 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
3388 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
3389 }
3390 /* If the data for the current encryption level have all been sent,
3391 * select the next level.
3392 */
3393 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
3394 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) {
3395 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3396 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3397 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3398 tel = next_tel;
3399 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3400 frms = &qc->els[tel].pktns->tx.frms;
3401 else
3402 frms = next_tel_frms;
3403 qel = &qc->els[tel];
3404 if (!LIST_ISEMPTY(frms)) {
3405 /* If there is data for the next level, do not
3406 * consume a datagram.
3407 */
3408 prv_pkt = cur_pkt;
3409 }
3410 }
3411
3412 /* If we have to build a new datagram, set the current datagram as
3413 * prepared into <cbuf>.
3414 */
3415 if (!prv_pkt) {
3416 qc_txb_store(buf, dglen, first_pkt);
3417 first_pkt = NULL;
3418 dglen = 0;
3419 padding = 0;
3420 }
3421 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
3422 (!qc_is_listener(qc) ||
3423 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3424 padding = 1;
3425 }
3426 }
3427
3428 out:
3429 ret = total;
3430 leave:
3431 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3432 return ret;
3433}
3434
3435/* Send datagrams stored in <buf>.
3436 *
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003437 * This function returns 1 for success. On error, there is several behavior
3438 * depending on underlying sendto() error :
3439 * - for a fatal error, 0 is returned and connection is killed.
3440 * - a transient error is assimilated to a success case with 1 returned.
3441 * Remaining data are purged from the buffer and will eventually be detected
3442 * as lost which gives the opportunity to retry sending.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003443 */
3444int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
3445{
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003446 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003447 struct quic_conn *qc;
3448 char skip_sendto = 0;
3449
3450 qc = ctx->qc;
3451 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
3452 while (b_contig_data(buf, 0)) {
3453 unsigned char *pos;
3454 struct buffer tmpbuf = { };
3455 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3456 uint16_t dglen;
3457 size_t headlen = sizeof dglen + sizeof first_pkt;
3458 unsigned int time_sent;
3459
3460 pos = (unsigned char *)b_head(buf);
3461 dglen = read_u16(pos);
3462 BUG_ON_HOT(!dglen); /* this should not happen */
3463
3464 pos += sizeof dglen;
3465 first_pkt = read_ptr(pos);
3466 pos += sizeof first_pkt;
3467 tmpbuf.area = (char *)pos;
3468 tmpbuf.size = tmpbuf.data = dglen;
3469
3470 TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc);
3471 /* If sendto is on error just skip the call to it for the rest
3472 * of the loop but continue to purge the buffer. Data will be
3473 * transmitted when QUIC packets are detected as lost on our
3474 * side.
3475 *
3476 * TODO use fd-monitoring to detect when send operation can be
3477 * retry. This should improve the bandwidth without relying on
3478 * retransmission timer. However, it requires a major rework on
3479 * quic-conn fd management.
3480 */
3481 if (!skip_sendto) {
Amaury Denoyelle1febc2d2023-02-23 11:18:38 +01003482 int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0);
3483 if (ret < 0) {
3484 qc_kill_conn(qc);
3485 b_del(buf, buf->data);
3486 goto leave;
3487 }
3488 else if (!ret) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003489 skip_sendto = 1;
3490 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
3491 }
3492 }
3493
3494 b_del(buf, dglen + headlen);
3495 qc->tx.bytes += tmpbuf.data;
3496 time_sent = now_ms;
3497
3498 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3499 pkt->time_sent = time_sent;
3500 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3501 pkt->pktns->tx.time_of_last_eliciting = time_sent;
3502 qc->path->ifae_pkts++;
3503 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3504 qc_idle_timer_rearm(qc, 0);
3505 }
3506 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3507 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3508 qc->flags |= QUIC_FL_CONN_CLOSING;
3509 qc_notify_close(qc);
3510
3511 /* RFC 9000 10.2. Immediate Close:
3512 * The closing and draining connection states exist to ensure
3513 * that connections close cleanly and that delayed or reordered
3514 * packets are properly discarded. These states SHOULD persist
3515 * for at least three times the current PTO interval...
3516 *
3517 * Rearm the idle timeout only one time when entering closing
3518 * state.
3519 */
3520 qc_idle_timer_do_rearm(qc);
3521 if (qc->timer_task) {
3522 task_destroy(qc->timer_task);
3523 qc->timer_task = NULL;
3524 }
3525 }
3526 qc->path->in_flight += pkt->in_flight_len;
3527 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3528 if (pkt->in_flight_len)
3529 qc_set_timer(qc);
3530 TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
3531 next_pkt = pkt->next;
3532 quic_tx_packet_refinc(pkt);
3533 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
3534 }
3535 }
3536
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003537 ret = 1;
3538leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003539 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3540
Frédéric Lécaillea2c62c32023-02-10 14:13:43 +01003541 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003542}
3543
3544/* Copy into <buf> buffer a stateless reset token depending on the
3545 * <salt> salt input. This is the cluster secret which will be derived
3546 * as HKDF input secret to generate this token.
3547 * Return 1 if succeeded, 0 if not.
3548 */
3549static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3550 unsigned char *buf, size_t len,
3551 const unsigned char *salt, size_t saltlen)
3552{
3553 /* Input secret */
3554 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3555 size_t keylen = strlen(global.cluster_secret);
3556 /* Info */
3557 const unsigned char label[] = "stateless token";
3558 size_t labellen = sizeof label - 1;
3559 int ret;
3560
3561 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3562
3563 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
3564 key, keylen, salt, saltlen, label, labellen);
3565 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3566 return ret;
3567}
3568
3569/* Initialize the stateless reset token attached to <cid> connection ID.
3570 * Returns 1 if succeeded, 0 if not.
3571 */
3572static int quic_stateless_reset_token_init(struct quic_conn *qc,
3573 struct quic_connection_id *quic_cid)
3574{
3575 int ret;
3576
3577 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3578
3579 if (global.cluster_secret) {
3580 /* Output secret */
3581 unsigned char *token = quic_cid->stateless_reset_token;
3582 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3583 /* Salt */
3584 const unsigned char *cid = quic_cid->cid.data;
3585 size_t cidlen = quic_cid->cid.len;
3586
3587 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
3588 }
3589 else {
3590 /* TODO: RAND_bytes() should be replaced */
3591 ret = RAND_bytes(quic_cid->stateless_reset_token,
3592 sizeof quic_cid->stateless_reset_token) == 1;
3593 }
3594
3595 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3596 return ret;
3597}
3598
3599/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3600 * ebtree.
3601 *
3602 * The CID is randomly generated in part with the result altered to be
3603 * associated with the current thread ID. This means this function must only
3604 * be called by the quic_conn thread.
3605 *
3606 * Returns the new CID if succeeded, NULL if not.
3607 */
3608static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3609 struct quic_conn *qc,
3610 int seq_num)
3611{
3612 struct quic_connection_id *cid;
3613
3614 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3615
3616 cid = pool_alloc(pool_head_quic_connection_id);
3617 if (!cid) {
3618 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3619 goto err;
3620 }
3621
3622 cid->cid.len = QUIC_HAP_CID_LEN;
3623 /* TODO: RAND_bytes() should be replaced */
3624 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3625 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
3626 goto err;
3627 }
3628
3629 quic_pin_cid_to_tid(cid->cid.data, tid);
3630 if (quic_stateless_reset_token_init(qc, cid) != 1) {
3631 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
3632 goto err;
3633 }
3634
3635 cid->qc = qc;
3636
3637 cid->seq_num.key = seq_num;
3638 cid->retire_prior_to = 0;
3639 /* insert the allocated CID in the quic_conn tree */
3640 eb64_insert(root, &cid->seq_num);
3641
3642 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3643 return cid;
3644
3645 err:
3646 pool_free(pool_head_quic_connection_id, cid);
3647 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3648 return NULL;
3649}
3650
3651/* Build all the frames which must be sent just after the handshake have succeeded.
3652 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3653 * a HANDSHAKE_DONE frame.
3654 * Return 1 if succeeded, 0 if not.
3655 */
3656static int quic_build_post_handshake_frames(struct quic_conn *qc)
3657{
3658 int ret = 0, i, first, max;
3659 struct quic_enc_level *qel;
3660 struct quic_frame *frm, *frmbak;
3661 struct list frm_list = LIST_HEAD_INIT(frm_list);
3662 struct eb64_node *node;
3663
3664 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
3665
3666 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3667 /* Only servers must send a HANDSHAKE_DONE frame. */
3668 if (qc_is_listener(qc)) {
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01003669 frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003670 if (!frm) {
3671 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3672 goto leave;
3673 }
3674
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003675 LIST_APPEND(&frm_list, &frm->list);
3676 }
3677
3678 /* Initialize <max> connection IDs minus one: there is
3679 * already one connection ID used for the current connection.
3680 */
3681 first = 1;
3682 max = qc->tx.params.active_connection_id_limit;
3683
3684 /* TODO: check limit */
3685 for (i = first; i < max; i++) {
3686 struct quic_connection_id *cid;
3687
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01003688 frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003689 if (!frm) {
3690 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3691 goto err;
3692 }
3693
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003694 cid = new_quic_cid(&qc->cids, qc, i);
3695 if (!cid) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01003696 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003697 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
3698 goto err;
3699 }
3700
3701 /* insert the allocated CID in the receiver datagram handler tree */
3702 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
3703
3704 quic_connection_id_to_frm_cpy(frm, cid);
3705 LIST_APPEND(&frm_list, &frm->list);
3706 }
3707
3708 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
3709 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
3710
3711 ret = 1;
3712 leave:
3713 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
3714 return ret;
3715
3716 err:
3717 /* free the frames */
3718 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01003719 qc_frm_free(&frm);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02003720
3721 node = eb64_lookup_ge(&qc->cids, first);
3722 while (node) {
3723 struct quic_connection_id *cid;
3724
3725 cid = eb64_entry(node, struct quic_connection_id, seq_num);
3726 if (cid->seq_num.key >= max)
3727 break;
3728
3729 node = eb64_next(node);
3730 ebmb_delete(&cid->node);
3731 eb64_delete(&cid->seq_num);
3732 pool_free(pool_head_quic_connection_id, cid);
3733 }
3734 goto leave;
3735}
3736
3737/* Deallocate <l> list of ACK ranges. */
3738void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
3739{
3740 struct eb64_node *n;
3741 struct quic_arng_node *ar;
3742
3743 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
3744
3745 n = eb64_first(&arngs->root);
3746 while (n) {
3747 struct eb64_node *next;
3748
3749 ar = eb64_entry(n, struct quic_arng_node, first);
3750 next = eb64_next(n);
3751 eb64_delete(n);
3752 pool_free(pool_head_quic_arng, ar);
3753 n = next;
3754 }
3755
3756 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
3757}
3758
3759/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3760 * descending order.
3761 */
3762static inline size_t sack_gap(struct quic_arng_node *p,
3763 struct quic_arng_node *q)
3764{
3765 return p->first.key - q->last - 2;
3766}
3767
3768
3769/* Remove the last elements of <ack_ranges> list of ack range updating its
3770 * encoded size until it goes below <limit>.
3771 * Returns 1 if succeeded, 0 if not (no more element to remove).
3772 */
3773static int quic_rm_last_ack_ranges(struct quic_conn *qc,
3774 struct quic_arngs *arngs, size_t limit)
3775{
3776 int ret = 0;
3777 struct eb64_node *last, *prev;
3778
3779 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3780
3781 last = eb64_last(&arngs->root);
3782 while (last && arngs->enc_sz > limit) {
3783 struct quic_arng_node *last_node, *prev_node;
3784
3785 prev = eb64_prev(last);
3786 if (!prev) {
3787 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
3788 goto out;
3789 }
3790
3791 last_node = eb64_entry(last, struct quic_arng_node, first);
3792 prev_node = eb64_entry(prev, struct quic_arng_node, first);
3793 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3794 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3795 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3796 --arngs->sz;
3797 eb64_delete(last);
3798 pool_free(pool_head_quic_arng, last);
3799 last = prev;
3800 }
3801
3802 ret = 1;
3803 out:
3804 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3805 return ret;
3806}
3807
3808/* Set the encoded size of <arngs> QUIC ack ranges. */
3809static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
3810{
3811 struct eb64_node *node, *next;
3812 struct quic_arng_node *ar, *ar_next;
3813
3814 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3815
3816 node = eb64_last(&arngs->root);
3817 if (!node)
3818 goto leave;
3819
3820 ar = eb64_entry(node, struct quic_arng_node, first);
3821 arngs->enc_sz = quic_int_getsize(ar->last) +
3822 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3823
3824 while ((next = eb64_prev(node))) {
3825 ar_next = eb64_entry(next, struct quic_arng_node, first);
3826 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3827 quic_int_getsize(ar_next->last - ar_next->first.key);
3828 node = next;
3829 ar = eb64_entry(node, struct quic_arng_node, first);
3830 }
3831
3832 leave:
3833 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3834}
3835
3836/* Insert <ar> ack range into <argns> tree of ack ranges.
3837 * Returns the ack range node which has been inserted if succeeded, NULL if not.
3838 */
3839static inline
3840struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
3841 struct quic_arngs *arngs,
3842 struct quic_arng *ar)
3843{
3844 struct quic_arng_node *new_ar;
3845
3846 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3847
3848 new_ar = pool_alloc(pool_head_quic_arng);
3849 if (!new_ar) {
3850 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
3851 goto leave;
3852 }
3853
3854 new_ar->first.key = ar->first;
3855 new_ar->last = ar->last;
3856 eb64_insert(&arngs->root, &new_ar->first);
3857 arngs->sz++;
3858
3859 leave:
3860 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
3861 return new_ar;
3862}
3863
3864/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
3865 * Note that this function computes the number of bytes required to encode
3866 * this tree of ACK ranges in descending order.
3867 *
3868 * Descending order
3869 * ------------->
3870 * range1 range2
3871 * ..........|--------|..............|--------|
3872 * ^ ^ ^ ^
3873 * | | | |
3874 * last1 first1 last2 first2
3875 * ..........+--------+--------------+--------+......
3876 * diff1 gap12 diff2
3877 *
3878 * To encode the previous list of ranges we must encode integers as follows in
3879 * descending order:
3880 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
3881 * with diff1 = last1 - first1
3882 * diff2 = last2 - first2
3883 * gap12 = first1 - last2 - 2 (>= 0)
3884 *
3885
3886returns 0 on error
3887
3888 */
3889int quic_update_ack_ranges_list(struct quic_conn *qc,
3890 struct quic_arngs *arngs,
3891 struct quic_arng *ar)
3892{
3893 int ret = 0;
3894 struct eb64_node *le;
3895 struct quic_arng_node *new_node;
3896 struct eb64_node *new;
3897
3898 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3899
3900 new = NULL;
3901 if (eb_is_empty(&arngs->root)) {
3902 new_node = quic_insert_new_range(qc, arngs, ar);
3903 if (new_node)
3904 ret = 1;
3905
3906 goto leave;
3907 }
3908
3909 le = eb64_lookup_le(&arngs->root, ar->first);
3910 if (!le) {
3911 new_node = quic_insert_new_range(qc, arngs, ar);
3912 if (!new_node)
3913 goto leave;
3914
3915 new = &new_node->first;
3916 }
3917 else {
3918 struct quic_arng_node *le_ar =
3919 eb64_entry(le, struct quic_arng_node, first);
3920
3921 /* Already existing range */
3922 if (le_ar->last >= ar->last) {
3923 ret = 1;
3924 }
3925 else if (le_ar->last + 1 >= ar->first) {
3926 le_ar->last = ar->last;
3927 new = le;
3928 new_node = le_ar;
3929 }
3930 else {
3931 new_node = quic_insert_new_range(qc, arngs, ar);
3932 if (!new_node)
3933 goto leave;
3934
3935 new = &new_node->first;
3936 }
3937 }
3938
3939 /* Verify that the new inserted node does not overlap the nodes
3940 * which follow it.
3941 */
3942 if (new) {
3943 struct eb64_node *next;
3944 struct quic_arng_node *next_node;
3945
3946 while ((next = eb64_next(new))) {
3947 next_node =
3948 eb64_entry(next, struct quic_arng_node, first);
3949 if (new_node->last + 1 < next_node->first.key)
3950 break;
3951
3952 if (next_node->last > new_node->last)
3953 new_node->last = next_node->last;
3954 eb64_delete(next);
3955 pool_free(pool_head_quic_arng, next_node);
3956 /* Decrement the size of these ranges. */
3957 arngs->sz--;
3958 }
3959 }
3960
3961 ret = 1;
3962 leave:
3963 quic_arngs_set_enc_sz(qc, arngs);
3964 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
3965 return ret;
3966}
3967/* Remove the header protection of packets at <el> encryption level.
3968 * Always succeeds.
3969 */
3970static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
3971{
3972 struct quic_tls_ctx *tls_ctx;
3973 struct quic_rx_packet *pqpkt, *pkttmp;
3974 struct quic_enc_level *app_qel;
3975
3976 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3977 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3978 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
3979 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
3980 TRACE_DEVEL("hp not removed (handshake not completed)",
3981 QUIC_EV_CONN_ELRMHP, qc);
3982 goto out;
3983 }
3984 tls_ctx = &el->tls_ctx;
3985 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
3986 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
3987 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
3988 TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
3989 }
3990 else {
3991 /* The AAD includes the packet number field */
3992 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3993 /* Store the packet into the tree of packets to decrypt. */
3994 pqpkt->pn_node.key = pqpkt->pn;
3995 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3996 quic_rx_packet_refinc(pqpkt);
3997 TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
3998 }
3999 LIST_DELETE(&pqpkt->list);
4000 quic_rx_packet_refdec(pqpkt);
4001 }
4002
4003 out:
4004 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
4005}
4006
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004007/* Process all the CRYPTO frame at <el> encryption level. This is the
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004008 * responsibility of the called to ensure there exists a CRYPTO data
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004009 * stream for this level.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004010 * Return 1 if succeeded, 0 if not.
4011 */
4012static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
4013 struct quic_enc_level *el,
4014 struct ssl_sock_ctx *ctx)
4015{
4016 int ret = 0;
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004017 struct ncbuf *ncbuf;
4018 struct quic_cstream *cstream = el->cstream;
4019 ncb_sz_t data;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004020
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004021 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004022
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004023 BUG_ON(!cstream);
4024 ncbuf = &cstream->rx.ncbuf;
4025 if (ncb_is_null(ncbuf))
4026 goto done;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004027
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004028 /* TODO not working if buffer is wrapping */
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004029 while ((data = ncb_data(ncbuf, 0))) {
4030 const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004031
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004032 if (!qc_provide_cdata(el, ctx, cdata, data, NULL, NULL))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004033 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004034
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004035 cstream->rx.offset += data;
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004036 TRACE_DEVEL("buffered crypto data were provided to TLS stack",
4037 QUIC_EV_CONN_PHPKTS, qc, el);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004038 }
4039
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004040 done:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004041 ret = 1;
4042 leave:
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004043 if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
4044 TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004045 quic_free_ncbuf(ncbuf);
Amaury Denoyelle2f668f02022-11-18 15:24:08 +01004046 }
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004047 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004048 return ret;
4049}
4050
4051/* Process all the packets at <el> and <next_el> encryption level.
4052 * This is the caller responsibility to check that <cur_el> is different of <next_el>
4053 * as pointer value.
4054 * Return 1 if succeeded, 0 if not.
4055 */
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004056int qc_treat_rx_pkts(struct quic_conn *qc, struct quic_enc_level *cur_el,
4057 struct quic_enc_level *next_el, int force_ack)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004058{
4059 int ret = 0;
4060 struct eb64_node *node;
4061 int64_t largest_pn = -1;
4062 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004063 struct quic_enc_level *qel = cur_el;
4064
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004065 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004066 qel = cur_el;
4067 next_tel:
4068 if (!qel)
4069 goto out;
4070
4071 node = eb64_first(&qel->rx.pkts);
4072 while (node) {
4073 struct quic_rx_packet *pkt;
4074
4075 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
4076 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004077 qc, pkt, NULL, qc->xprt_ctx->ssl);
Amaury Denoyelle518c98f2022-11-24 17:12:25 +01004078 if (!qc_pkt_decrypt(qc, qel, pkt)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004079 /* Drop the packet */
4080 TRACE_ERROR("packet decryption failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004081 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004082 }
4083 else {
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004084 if (!qc_parse_pkt_frms(qc, pkt, qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004085 /* Drop the packet */
4086 TRACE_ERROR("packet parsing failed -> dropped",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004087 QUIC_EV_CONN_RXPKT, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004088 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
4089 }
4090 else {
4091 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
4092
4093 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
4094 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
4095 qel->pktns->rx.nb_aepkts_since_last_ack++;
4096 qc_idle_timer_rearm(qc, 1);
4097 }
4098 if (pkt->pn > largest_pn) {
4099 largest_pn = pkt->pn;
4100 largest_pn_time_received = pkt->time_received;
4101 }
4102 /* Update the list of ranges to acknowledge. */
4103 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
4104 TRACE_ERROR("Could not update ack range list",
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004105 QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004106 }
4107 }
4108 node = eb64_next(node);
4109 eb64_delete(&pkt->pn_node);
4110 quic_rx_packet_refdec(pkt);
4111 }
4112
4113 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
4114 /* Update the largest packet number. */
4115 qel->pktns->rx.largest_pn = largest_pn;
4116 /* Update the largest acknowledged packet timestamps */
4117 qel->pktns->rx.largest_time_received = largest_pn_time_received;
4118 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
4119 }
4120
Frédéric Lécaille9f9263e2022-09-13 14:36:44 +02004121 if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004122 // trace already emitted by function above
4123 goto leave;
4124 }
4125
4126 if (qel == cur_el) {
4127 BUG_ON(qel == next_el);
4128 qel = next_el;
4129 largest_pn = -1;
4130 goto next_tel;
4131 }
4132
4133 out:
4134 ret = 1;
4135 leave:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004136 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004137 return ret;
4138}
4139
4140/* Check if it's possible to remove header protection for packets related to
4141 * encryption level <qel>. If <qel> is NULL, assume it's false.
4142 *
4143 * Return true if the operation is possible else false.
4144 */
4145static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
4146{
4147 int ret = 0;
4148 enum quic_tls_enc_level tel;
4149
4150 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
4151
4152 if (!qel)
4153 goto cant_rm_hp;
4154
4155 tel = ssl_to_quic_enc_level(qel->level);
4156
4157 /* check if tls secrets are available */
4158 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
4159 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
4160 goto cant_rm_hp;
4161 }
4162
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004163 if (!quic_tls_has_rx_sec(qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004164 TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc);
4165 goto cant_rm_hp;
4166 }
4167
Frédéric Lécaille8417beb2023-02-01 10:31:35 +01004168 if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->state < QUIC_HS_ST_COMPLETE) {
4169 TRACE_DEVEL("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
4170 goto cant_rm_hp;
4171 }
4172
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004173 /* check if the connection layer is ready before using app level */
4174 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
4175 qc->mux_state == QC_MUX_NULL) {
4176 TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
4177 goto cant_rm_hp;
4178 }
4179
4180 ret = 1;
4181 cant_rm_hp:
4182 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
4183 return ret;
4184}
4185
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004186/* Flush txbuf for <qc> connection. This must be called prior to a packet
4187 * preparation when txbuf contains older data. A send will be conducted for
4188 * these data.
4189 *
4190 * Returns 1 on success : buffer is empty and can be use for packet
4191 * preparation. On error 0 is returned.
4192 */
4193static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
4194{
4195 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4196
4197 /* This operation can only be conducted if txbuf is not empty. This
4198 * case only happens for connection with their owned socket due to an
4199 * older transient sendto() error.
4200 */
4201 BUG_ON(!qc_test_fd(qc));
4202
4203 if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4204 if (qc->flags & QUIC_FL_CONN_TO_KILL)
4205 qc_txb_release(qc);
4206 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4207 return 0;
4208 }
4209
4210 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4211 return 1;
4212}
4213
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004214/* Try to send application frames from list <frms> on connection <qc>.
4215 *
4216 * Use qc_send_app_probing wrapper when probing with old data.
4217 *
4218 * Returns 1 on success. Some data might not have been sent due to congestion,
4219 * in this case they are left in <frms> input list. The caller may subscribe on
4220 * quic-conn to retry later.
4221 *
4222 * Returns 0 on critical error.
4223 * TODO review and classify more distinctly transient from definitive errors to
4224 * allow callers to properly handle it.
4225 */
4226static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
4227{
4228 int status = 0;
4229 struct buffer *buf;
4230
4231 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4232
4233 buf = qc_txb_alloc(qc);
4234 if (!buf) {
4235 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004236 goto err;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004237 }
4238
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004239 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4240 goto err;
4241
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004242 /* Prepare and send packets until we could not further prepare packets. */
4243 while (1) {
4244 int ret;
4245 /* Currently buf cannot be non-empty at this stage. Even if a
4246 * previous sendto() has failed it is emptied to simulate
4247 * packet emission and rely on QUIC lost detection to try to
4248 * emit it.
4249 */
4250 BUG_ON_HOT(b_data(buf));
4251 b_reset(buf);
4252
4253 ret = qc_prep_app_pkts(qc, buf, frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004254 if (ret == -1) {
4255 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004256 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004257 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004258
Amaury Denoyelle37333862023-02-28 11:53:48 +01004259 if (!ret)
4260 break;
4261
4262 if (!qc_send_ppkts(buf, qc->xprt_ctx)) {
4263 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004264 goto err;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004265 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004266 }
4267
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004268 status = 1;
4269 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004270 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4271 return status;
4272
4273 err:
Amaury Denoyelle37333862023-02-28 11:53:48 +01004274 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
4275 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004276}
4277
4278/* Try to send application frames from list <frms> on connection <qc>. Use this
4279 * function when probing is required.
4280 *
4281 * Returns the result from qc_send_app_pkts function.
4282 */
4283static forceinline int qc_send_app_probing(struct quic_conn *qc,
4284 struct list *frms)
4285{
4286 int ret;
4287
4288 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4289
4290 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
4291 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4292 ret = qc_send_app_pkts(qc, frms);
4293 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
4294
4295 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4296 return ret;
4297}
4298
4299/* Try to send application frames from list <frms> on connection <qc>. This
4300 * function is provided for MUX upper layer usage only.
4301 *
4302 * Returns the result from qc_send_app_pkts function.
4303 */
4304int qc_send_mux(struct quic_conn *qc, struct list *frms)
4305{
4306 int ret;
4307
4308 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4309 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
4310
4311 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4312 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
4313 ret = qc_send_app_pkts(qc, frms);
4314 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
4315
4316 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4317 return ret;
4318}
4319
4320/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4321 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
4322 * QUIC connection. <old_data> is used as boolean to send data already sent but
4323 * not already acknowledged (in flight).
4324 * Returns 1 if succeeded, 0 if not.
4325 */
4326int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4327 enum quic_tls_enc_level tel, struct list *tel_frms,
4328 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4329{
4330 int ret, status = 0;
4331 struct buffer *buf = qc_txb_alloc(qc);
4332
4333 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4334
4335 if (!buf) {
4336 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4337 goto leave;
4338 }
4339
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004340 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4341 goto out;
4342
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004343 /* Currently buf cannot be non-empty at this stage. Even if a previous
4344 * sendto() has failed it is emptied to simulate packet emission and
4345 * rely on QUIC lost detection to try to emit it.
4346 */
4347 BUG_ON_HOT(b_data(buf));
4348 b_reset(buf);
4349
4350 if (old_data) {
4351 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
4352 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
4353 }
4354
4355 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004356 if (ret == -1) {
4357 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004358 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004359 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004360
Amaury Denoyelle37333862023-02-28 11:53:48 +01004361 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4362 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004363 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004364 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004365
Amaury Denoyelle37333862023-02-28 11:53:48 +01004366 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004367 status = 1;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004368
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004369 out:
4370 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
4371 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004372 leave:
4373 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4374 return status;
4375}
4376
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004377/* Retransmit up to two datagrams depending on packet number space.
4378 * Return 0 when failed, 0 if not.
4379 */
4380static int qc_dgrams_retransmit(struct quic_conn *qc)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004381{
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004382 int ret = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004383 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4384 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4385 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4386
4387 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4388
4389 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004390 int i;
4391
4392 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4393 struct list ifrms = LIST_HEAD_INIT(ifrms);
4394 struct list hfrms = LIST_HEAD_INIT(hfrms);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004395
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004396 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
4397 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4398 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
4399 if (!LIST_ISEMPTY(&ifrms)) {
4400 iqel->pktns->tx.pto_probe = 1;
4401 if (!LIST_ISEMPTY(&hfrms))
4402 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004403 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4404 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms))
4405 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004406 /* Put back unsent frames in their packet number spaces */
4407 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4408 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
4409 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004410 }
4411 TRACE_STATE("no more need to probe Initial packet number space",
4412 QUIC_EV_CONN_TXPKT, qc);
4413 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004414 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004415 }
4416 else {
4417 int i;
4418
4419 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004420 hqel->pktns->tx.pto_probe = 0;
4421 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
Frédéric Lécaille7b5d9b12022-11-28 17:21:45 +01004422 struct list frms1 = LIST_HEAD_INIT(frms1);
4423
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004424 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
4425 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4426 if (!LIST_ISEMPTY(&frms1)) {
4427 hqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004428 if (!qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4429 QUIC_TLS_ENC_LEVEL_NONE, NULL))
4430 goto leave;
4431
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004432 /* Put back unsent frames into their packet number spaces */
4433 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
4434 }
4435 }
4436 TRACE_STATE("no more need to probe Handshake packet number space",
4437 QUIC_EV_CONN_TXPKT, qc);
4438 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4439 }
4440 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4441 struct list frms2 = LIST_HEAD_INIT(frms2);
4442 struct list frms1 = LIST_HEAD_INIT(frms1);
4443
4444 aqel->pktns->tx.pto_probe = 0;
4445 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4446 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4447 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4448 if (!LIST_ISEMPTY(&frms1)) {
4449 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004450 if (!qc_send_app_probing(qc, &frms1))
4451 goto leave;
4452
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004453 /* Put back unsent frames into their packet number spaces */
4454 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
4455 }
4456 if (!LIST_ISEMPTY(&frms2)) {
4457 aqel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004458 if (!qc_send_app_probing(qc, &frms2))
4459 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004460 /* Put back unsent frames into their packet number spaces */
4461 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
4462 }
4463 TRACE_STATE("no more need to probe 01RTT packet number space",
4464 QUIC_EV_CONN_TXPKT, qc);
4465 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4466 }
4467 }
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004468
4469 ret = 1;
4470 leave:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004471 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004472 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004473}
4474
4475/* QUIC connection packet handler task (post handshake) */
4476struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4477{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004478 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004479 struct quic_enc_level *qel;
4480
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004481 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4482
4483 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4484 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
4485
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01004486 if (qc_test_fd(qc))
4487 qc_rcv_buf(qc);
4488
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004489 /* Retranmissions */
4490 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
4491 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
4492 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004493 if (!qc_dgrams_retransmit(qc))
4494 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004495 }
4496
4497 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4498 qc_rm_hp_pkts(qc, qel);
4499
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004500 if (!qc_treat_rx_pkts(qc, qel, NULL, 0)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004501 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4502 goto out;
4503 }
4504
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01004505 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
4506 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
4507 goto out;
4508 }
4509
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004510 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4511 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4512 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
4513 goto out;
4514 }
4515
4516 /* XXX TODO: how to limit the list frames to send */
4517 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4518 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4519 goto out;
4520 }
4521
4522 out:
4523 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
4524 return t;
4525}
4526
4527/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4528static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4529{
4530 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4531 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4532 qel->pktns->tx.pto_probe ||
4533 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4534}
4535
4536/* QUIC connection packet handler task. */
4537struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
4538{
4539 int ret, ssl_err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004540 struct quic_conn *qc = context;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004541 enum quic_tls_enc_level tel, next_tel;
4542 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004543 /* Early-data encryption level */
4544 struct quic_enc_level *eqel;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004545 struct buffer *buf = NULL;
4546 int st, force_ack, zero_rtt;
4547
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004548 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004549 eqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004550 st = qc->state;
4551 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
4552
4553 /* Retranmissions */
4554 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
4555 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
4556 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
Frédéric Lécaillee1738df2023-02-10 14:46:39 +01004557 if (!qc_dgrams_retransmit(qc))
4558 goto out;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004559 }
4560
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004561 ssl_err = SSL_ERROR_NONE;
4562 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004563 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004564 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01004565
4566 if (qc_test_fd(qc))
4567 qc_rcv_buf(qc);
4568
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004569 if (st >= QUIC_HS_ST_COMPLETE &&
4570 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
4571 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
4572 /* There may be remaining Handshake packets to treat and acknowledge. */
4573 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4574 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4575 }
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004576 else if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, zero_rtt))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004577 goto out;
4578
4579 qel = &qc->els[tel];
4580 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
4581
4582 next_level:
4583 /* Treat packets waiting for header packet protection decryption */
4584 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4585 qc_rm_hp_pkts(qc, qel);
4586
4587 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4588 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004589 if (!qc_treat_rx_pkts(qc, qel, next_qel, force_ack))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004590 goto out;
4591
Frédéric Lécaille0aa79952023-02-03 16:15:08 +01004592 if (qc->flags & QUIC_FL_CONN_TO_KILL) {
4593 TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
4594 goto out;
4595 }
4596
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004597 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4598 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4599 goto out;
4600
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004601 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004602 quic_tls_has_rx_sec(eqel) &&
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004603 (!LIST_ISEMPTY(&eqel->rx.pqpkts) || qc_el_rx_pkts(eqel));
4604 if (next_qel && next_qel == eqel && zero_rtt) {
4605 TRACE_DEVEL("select 0RTT as next encryption level",
4606 QUIC_EV_CONN_PHPKTS, qc);
4607 qel = next_qel;
4608 next_qel = NULL;
4609 goto next_level;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004610 }
4611
4612 st = qc->state;
4613 if (st >= QUIC_HS_ST_COMPLETE) {
4614 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
4615 !quic_build_post_handshake_frames(qc))
4616 goto out;
4617
4618 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
4619 QUIC_FL_TLS_SECRETS_DCD)) {
4620 /* Discard the Handshake keys. */
4621 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4622 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
4623 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
4624 qc_set_timer(qc);
4625 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4626 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
4627 }
4628
4629 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
4630 /* There may be remaining handshake to build (acks) */
4631 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4632 }
4633 }
4634
4635 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4636 * be considered.
4637 */
Frédéric Lécaille4aa7d812022-09-16 10:15:58 +02004638 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc, st, 0))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004639 goto out;
4640
4641 if (!qc_need_sending(qc, qel) &&
4642 (!next_qel || !qc_need_sending(qc, next_qel))) {
4643 goto skip_send;
4644 }
4645
4646 buf = qc_txb_alloc(qc);
4647 if (!buf)
4648 goto out;
4649
Amaury Denoyelle147862d2023-02-28 15:10:00 +01004650 if (b_data(buf) && !qc_purge_txbuf(qc, buf))
4651 goto skip_send;
4652
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004653 /* Currently buf cannot be non-empty at this stage. Even if a previous
4654 * sendto() has failed it is emptied to simulate packet emission and
4655 * rely on QUIC lost detection to try to emit it.
4656 */
4657 BUG_ON_HOT(b_data(buf));
4658 b_reset(buf);
4659
4660 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
4661 next_tel, &qc->els[next_tel].pktns->tx.frms);
Amaury Denoyelle37333862023-02-28 11:53:48 +01004662 if (ret == -1) {
4663 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004664 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004665 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004666
Amaury Denoyelle37333862023-02-28 11:53:48 +01004667 if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
4668 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004669 goto out;
Amaury Denoyelle37333862023-02-28 11:53:48 +01004670 }
4671
4672 qc_txb_release(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004673
4674 skip_send:
4675 /* Check if there is something to do for the next level.
4676 */
4677 if (next_qel && next_qel != qel &&
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004678 quic_tls_has_rx_sec(next_qel) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004679 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
4680 qel = next_qel;
4681 next_qel = NULL;
4682 goto next_level;
4683 }
4684
4685 out:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004686 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
4687 return t;
4688}
4689
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02004690/* Release the memory allocated for <cs> CRYPTO stream */
4691void quic_cstream_free(struct quic_cstream *cs)
4692{
4693 if (!cs) {
4694 /* This is the case for ORTT encryption level */
4695 return;
4696 }
4697
Amaury Denoyellebc174b22022-11-17 10:12:52 +01004698 quic_free_ncbuf(&cs->rx.ncbuf);
4699
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02004700 qc_stream_desc_release(cs->desc);
4701 pool_free(pool_head_quic_cstream, cs);
4702}
4703
4704/* Allocate a new QUIC stream for <qc>.
4705 * Return it if succeeded, NULL if not.
4706 */
4707struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
4708{
4709 struct quic_cstream *cs, *ret_cs = NULL;
4710
4711 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
4712 cs = pool_alloc(pool_head_quic_cstream);
4713 if (!cs) {
4714 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
4715 goto leave;
4716 }
4717
4718 cs->rx.offset = 0;
4719 cs->rx.ncbuf = NCBUF_NULL;
4720 cs->rx.offset = 0;
4721
4722 cs->tx.offset = 0;
4723 cs->tx.sent_offset = 0;
4724 cs->tx.buf = BUF_NULL;
4725 cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
4726 if (!cs->desc) {
4727 TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
4728 goto err;
4729 }
4730
4731 ret_cs = cs;
4732 leave:
4733 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
4734 return ret_cs;
4735
4736 err:
4737 pool_free(pool_head_quic_cstream, cs);
4738 goto leave;
4739}
4740
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004741/* Uninitialize <qel> QUIC encryption level. Never fails. */
4742static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
4743{
4744 int i;
4745
4746 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4747
4748 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4749 if (qel->tx.crypto.bufs[i]) {
4750 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4751 qel->tx.crypto.bufs[i] = NULL;
4752 }
4753 }
4754 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02004755 quic_cstream_free(qel->cstream);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004756
4757 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4758}
4759
4760/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
4761 * connection allocating everything needed.
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01004762 *
4763 * Returns 1 if succeeded, 0 if not. On error the caller is responsible to use
4764 * quic_conn_enc_level_uninit() to cleanup partially allocated content.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004765 */
4766static int quic_conn_enc_level_init(struct quic_conn *qc,
4767 enum quic_tls_enc_level level)
4768{
4769 int ret = 0;
4770 struct quic_enc_level *qel;
4771
4772 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4773
4774 qel = &qc->els[level];
4775 qel->level = quic_to_ssl_enc_level(level);
4776 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4777 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4778 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
4779 qel->tls_ctx.flags = 0;
4780
4781 qel->rx.pkts = EB_ROOT;
4782 LIST_INIT(&qel->rx.pqpkts);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004783
4784 /* Allocate only one buffer. */
4785 /* TODO: use a pool */
4786 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4787 if (!qel->tx.crypto.bufs)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01004788 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004789
4790 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4791 if (!qel->tx.crypto.bufs[0])
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01004792 goto leave;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004793
4794 qel->tx.crypto.bufs[0]->sz = 0;
4795 qel->tx.crypto.nb_buf = 1;
4796
4797 qel->tx.crypto.sz = 0;
4798 qel->tx.crypto.offset = 0;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02004799 /* No CRYPTO data for early data TLS encryption level */
4800 if (level == QUIC_TLS_ENC_LEVEL_EARLY_DATA)
4801 qel->cstream = NULL;
4802 else {
4803 qel->cstream = quic_cstream_new(qc);
4804 if (!qel->cstream)
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01004805 goto leave;
Frédéric Lécaille7e3f7c42022-09-09 18:05:45 +02004806 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004807
4808 ret = 1;
4809 leave:
4810 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4811 return ret;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004812}
4813
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01004814/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
4815 * This is not the case if the remote peer address is not validated and if
4816 * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
4817 */
4818static int qc_may_probe_ipktns(struct quic_conn *qc)
4819{
4820 return quic_peer_validated_addr(qc) ||
4821 (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
4822}
4823
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004824/* Callback called upon loss detection and PTO timer expirations. */
4825struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
4826{
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004827 struct quic_conn *qc = ctx;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004828 struct quic_pktns *pktns;
4829
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004830 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
4831 NULL, NULL, &qc->path->ifae_pkts);
4832 task->expire = TICK_ETERNITY;
4833 pktns = quic_loss_pktns(qc);
4834 if (tick_isset(pktns->tx.loss_time)) {
4835 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4836
4837 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
4838 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004839 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyellee4abb1f2023-01-27 17:54:15 +01004840 if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
4841 qc_set_timer(qc);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004842 goto out;
4843 }
4844
4845 if (qc->path->in_flight) {
Frédéric Lécailleb75eecc2023-01-26 15:18:17 +01004846 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01004847 if (!qc_notify_send(qc)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004848 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01004849 if (qc_may_probe_ipktns(qc)) {
4850 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4851 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4852 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4853 }
4854 else {
4855 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4856 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004857 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01004858 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004859 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4860 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4861 }
4862 }
4863 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
4864 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01004865 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4866 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004867 if (qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight) {
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01004868 if (qc_may_probe_ipktns(qc)) {
4869 qc->pktns[QUIC_TLS_PKTNS_INITIAL].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4870 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4871 }
4872 else {
4873 TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4874 }
Frédéric Lécaille37ed4a32023-01-31 17:32:06 +01004875 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004876 }
4877 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
4878 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille7c6d8f82023-02-14 16:00:18 +01004879 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4880 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004881 }
4882 }
4883 }
4884 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
4885 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4886 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4887
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004888 if (quic_tls_has_tx_sec(hel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004889 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillee1a49cf2022-09-16 16:24:47 +02004890 if (quic_tls_has_tx_sec(iel))
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004891 iel->pktns->tx.pto_probe = 1;
4892 }
4893
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02004894 tasklet_wakeup(qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004895 qc->path->loss.pto_count++;
4896
4897 out:
4898 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
4899
4900 return task;
4901}
4902
4903/* Parse the Retry token from buffer <token> with <end> a pointer to
4904 * one byte past the end of this buffer. This will extract the ODCID
4905 * which will be stored into <odcid>
4906 *
4907 * Returns 0 on success else non-zero.
4908 */
4909static int parse_retry_token(struct quic_conn *qc,
4910 const unsigned char *token, const unsigned char *end,
4911 struct quic_cid *odcid)
4912{
4913 int ret = 0;
4914 uint64_t odcid_len;
4915 uint32_t timestamp;
4916
4917 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
4918
4919 if (!quic_dec_int(&odcid_len, &token, end)) {
4920 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
4921 goto leave;
4922 }
4923
4924 /* RFC 9000 7.2. Negotiating Connection IDs:
4925 * When an Initial packet is sent by a client that has not previously
4926 * received an Initial or Retry packet from the server, the client
4927 * populates the Destination Connection ID field with an unpredictable
4928 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4929 */
4930 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
4931 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
4932 goto leave;
4933 }
4934
4935 if (end - token < odcid_len + sizeof timestamp) {
4936 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
4937 goto leave;
4938 }
4939
4940 timestamp = ntohl(read_u32(token + odcid_len));
4941 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
4942 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
4943 goto leave;
4944 }
4945
4946 ret = 1;
4947 memcpy(odcid->data, token, odcid_len);
4948 odcid->len = odcid_len;
4949 leave:
4950 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
4951 return !ret;
4952}
4953
4954/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4955 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4956 * for QUIC servers (or haproxy listeners).
4957 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4958 * <token> the token found to be used for this connection with <token_len> as
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02004959 * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004960 * Returns the connection if succeeded, NULL if not.
4961 */
4962static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
4963 struct quic_cid *dcid, struct quic_cid *scid,
4964 const struct quic_cid *token_odcid,
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02004965 struct sockaddr_storage *local_addr,
4966 struct sockaddr_storage *peer_addr,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004967 int server, int token, void *owner)
4968{
4969 int i;
4970 struct quic_conn *qc;
4971 /* Initial CID. */
4972 struct quic_connection_id *icid;
4973 char *buf_area = NULL;
4974 struct listener *l = NULL;
4975 struct quic_cc_algo *cc_algo = NULL;
4976 struct quic_tls_ctx *ictx;
4977 TRACE_ENTER(QUIC_EV_CONN_INIT);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01004978 /* TODO replace pool_zalloc by pool_alloc(). This requires special care
4979 * to properly initialized internal quic_conn members to safely use
4980 * quic_conn_release() on alloc failure.
4981 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02004982 qc = pool_zalloc(pool_head_quic_conn);
4983 if (!qc) {
4984 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
4985 goto err;
4986 }
4987
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01004988 /* Initialize in priority qc members required for a safe dealloc. */
4989
4990 /* required to use MTLIST_IN_LIST */
4991 MT_LIST_INIT(&qc->accept_list);
4992
4993 LIST_INIT(&qc->rx.pkt_list);
4994
Amaury Denoyelle42448332022-12-12 11:24:05 +01004995 qc_init_fd(qc);
4996
Amaury Denoyelle15c74702023-02-01 10:18:26 +01004997 LIST_INIT(&qc->back_refs);
4998
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01004999 /* Now proceeds to allocation of qc members. */
5000
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005001 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
5002 if (!buf_area) {
5003 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
5004 goto err;
5005 }
5006
5007 qc->cids = EB_ROOT;
5008 /* QUIC Server (or listener). */
5009 if (server) {
5010 struct proxy *prx;
5011
5012 l = owner;
5013 prx = l->bind_conf->frontend;
5014 cc_algo = l->bind_conf->quic_cc_algo;
5015
5016 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
5017 &quic_stats_module);
5018 qc->flags |= QUIC_FL_CONN_LISTENER;
5019 qc->state = QUIC_HS_ST_SERVER_INITIAL;
5020 /* Copy the initial DCID with the address. */
5021 qc->odcid.len = dcid->len;
5022 qc->odcid.addrlen = dcid->addrlen;
5023 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
5024
5025 /* copy the packet SCID to reuse it as DCID for sending */
5026 if (scid->len)
5027 memcpy(qc->dcid.data, scid->data, scid->len);
5028 qc->dcid.len = scid->len;
5029 qc->tx.buf = BUF_NULL;
5030 qc->li = l;
5031 }
5032 /* QUIC Client (outgoing connection to servers) */
5033 else {
5034 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
5035 if (dcid->len)
5036 memcpy(qc->dcid.data, dcid->data, dcid->len);
5037 qc->dcid.len = dcid->len;
5038 }
5039 qc->mux_state = QC_MUX_NULL;
5040 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
5041
5042 icid = new_quic_cid(&qc->cids, qc, 0);
5043 if (!icid) {
5044 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
5045 goto err;
5046 }
5047
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005048 if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
5049 is_addr(local_addr)) {
5050 TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
5051 qc_alloc_fd(qc, local_addr, peer_addr);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005052
5053 /* haproxy soft-stop is supported only for QUIC connections
5054 * with their owned socket.
5055 */
5056 if (qc_test_fd(qc))
5057 _HA_ATOMIC_INC(&jobs);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005058 }
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005059
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005060 /* insert the allocated CID in the receiver datagram handler tree */
5061 if (server)
5062 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
5063
5064 /* Select our SCID which is the first CID with 0 as sequence number. */
5065 qc->scid = icid->cid;
5066
5067 /* Packet number spaces initialization. */
5068 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
5069 quic_pktns_init(&qc->pktns[i]);
5070 /* QUIC encryption level context initialization. */
5071 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5072 if (!quic_conn_enc_level_init(qc, i)) {
5073 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
5074 goto err;
5075 }
5076 /* Initialize the packet number space. */
5077 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
5078 }
5079
5080 qc->original_version = qv;
5081 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
5082 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
5083 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
5084 /* TX part. */
5085 LIST_INIT(&qc->tx.frms_to_send);
5086 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
5087 qc->tx.wbuf = qc->tx.rbuf = 0;
5088 qc->tx.bytes = 0;
5089 qc->tx.buf = BUF_NULL;
5090 /* RX part. */
5091 qc->rx.bytes = 0;
5092 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
5093 for (i = 0; i < QCS_MAX_TYPES; i++)
5094 qc->rx.strms[i].nb_streams = 0;
5095
5096 qc->nb_pkt_for_cc = 1;
5097 qc->nb_pkt_since_cc = 0;
5098
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005099 if (!quic_tls_ku_init(qc)) {
5100 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
5101 goto err;
5102 }
5103
5104 /* XXX TO DO: Only one path at this time. */
5105 qc->path = &qc->paths[0];
5106 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
5107
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005108 qc->streams_by_id = EB_ROOT_UNIQUE;
5109 qc->stream_buf_count = 0;
Amaury Denoyelle97ecc7a2022-09-23 17:15:58 +02005110 memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
5111 memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005112
5113 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
5114 icid->stateless_reset_token,
5115 dcid->data, dcid->len,
5116 qc->scid.data, qc->scid.len, token_odcid))
5117 goto err;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005118
5119 qc->wait_event.tasklet = tasklet_new();
5120 if (!qc->wait_event.tasklet) {
5121 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
5122 goto err;
5123 }
5124 qc->wait_event.tasklet->process = quic_conn_io_cb;
5125 qc->wait_event.tasklet->context = qc;
5126 qc->wait_event.events = 0;
5127 /* Set tasklet tid based on the SCID selected by us for this
5128 * connection. The upper layer will also be binded on the same thread.
5129 */
Willy Tarreaueed78262022-12-21 09:09:19 +01005130 qc->tid = quic_get_cid_tid(qc->scid.data, &l->rx);
Willy Tarreauf5a0c8a2022-10-13 16:14:11 +02005131 qc->wait_event.tasklet->tid = qc->tid;
Amaury Denoyellebbb1c682022-09-28 15:15:51 +02005132 qc->subs = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005133
5134 if (qc_conn_alloc_ssl_ctx(qc) ||
5135 !quic_conn_init_timer(qc) ||
5136 !quic_conn_init_idle_timer_task(qc))
5137 goto err;
5138
5139 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
5140 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
5141 goto err;
5142
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005143 LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
5144 qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
5145
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005146 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
5147
5148 return qc;
5149
5150 err:
5151 pool_free(pool_head_quic_conn_rxbuf, buf_area);
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005152 if (qc) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005153 qc->rx.buf.area = NULL;
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005154 quic_conn_release(qc);
5155 }
5156 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005157 return NULL;
5158}
5159
5160/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
5161 * The connection tasklet is killed.
5162 *
5163 * This function must only be called by the thread responsible of the quic_conn
5164 * tasklet.
5165 */
5166void quic_conn_release(struct quic_conn *qc)
5167{
5168 int i;
5169 struct ssl_sock_ctx *conn_ctx;
5170 struct eb64_node *node;
5171 struct quic_tls_ctx *app_tls_ctx;
5172 struct quic_rx_packet *pkt, *pktback;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005173 struct bref *bref, *back;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005174
5175 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
5176
5177 /* We must not free the quic-conn if the MUX is still allocated. */
5178 BUG_ON(qc->mux_state == QC_MUX_READY);
5179
Amaury Denoyellefb375572023-02-01 09:28:32 +01005180 if (qc_test_fd(qc))
5181 _HA_ATOMIC_DEC(&jobs);
5182
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005183 /* Close quic-conn socket fd. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01005184 qc_release_fd(qc, 0);
Amaury Denoyelle40909df2022-10-24 17:08:43 +02005185
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005186 /* in the unlikely (but possible) case the connection was just added to
5187 * the accept_list we must delete it from there.
5188 */
5189 MT_LIST_DELETE(&qc->accept_list);
5190
5191 /* free remaining stream descriptors */
5192 node = eb64_first(&qc->streams_by_id);
5193 while (node) {
5194 struct qc_stream_desc *stream;
5195
5196 stream = eb64_entry(node, struct qc_stream_desc, by_id);
5197 node = eb64_next(node);
5198
5199 /* all streams attached to the quic-conn are released, so
5200 * qc_stream_desc_free will liberate the stream instance.
5201 */
5202 BUG_ON(!stream->release);
5203 qc_stream_desc_free(stream, 1);
5204 }
5205
5206 /* Purge Rx packet list. */
5207 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
5208 LIST_DELETE(&pkt->qc_rx_pkt_list);
5209 pool_free(pool_head_quic_rx_packet, pkt);
5210 }
5211
5212 if (qc->idle_timer_task) {
5213 task_destroy(qc->idle_timer_task);
5214 qc->idle_timer_task = NULL;
5215 }
5216
5217 if (qc->timer_task) {
5218 task_destroy(qc->timer_task);
5219 qc->timer_task = NULL;
5220 }
5221
Amaury Denoyelledbf6ad42022-12-12 11:22:42 +01005222 if (qc->wait_event.tasklet)
5223 tasklet_free(qc->wait_event.tasklet);
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005224
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005225 /* remove the connection from receiver cids trees */
5226 ebmb_delete(&qc->odcid_node);
5227 ebmb_delete(&qc->scid_node);
5228 free_quic_conn_cids(qc);
5229
5230 conn_ctx = qc->xprt_ctx;
5231 if (conn_ctx) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005232 SSL_free(conn_ctx->ssl);
5233 pool_free(pool_head_quic_conn_ctx, conn_ctx);
5234 }
5235
5236 quic_tls_ku_free(qc);
5237 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
5238 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
5239 quic_conn_enc_level_uninit(qc, &qc->els[i]);
5240 }
5241 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
5242
5243 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
5244 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
5245 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
5246
5247 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
5248 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
5249 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
5250 }
5251
Amaury Denoyelle15c74702023-02-01 10:18:26 +01005252 /* Detach CLI context watchers currently dumping this connection.
5253 * Reattach them to the next quic_conn instance.
5254 */
5255 list_for_each_entry_safe(bref, back, &qc->back_refs, users) {
5256 /* Remove watcher from this quic_conn instance. */
5257 LIST_DEL_INIT(&bref->users);
5258
5259 /* Attach it to next instance unless it was the last list element. */
5260 if (qc->el_th_ctx.n != &th_ctx->quic_conns) {
5261 struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx,
5262 struct quic_conn *,
5263 el_th_ctx);
5264 LIST_APPEND(&next->back_refs, &bref->users);
5265 }
5266 bref->ref = qc->el_th_ctx.n;
5267 __ha_barrier_store();
5268 }
5269 /* Remove quic_conn from global ha_thread_ctx list. */
5270 LIST_DELETE(&qc->el_th_ctx);
5271
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005272 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
5273 pool_free(pool_head_quic_conn, qc);
Amaury Denoyellefb375572023-02-01 09:28:32 +01005274
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005275 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
5276
5277 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
5278}
5279
5280/* Initialize the timer task of <qc> QUIC connection.
5281 * Returns 1 if succeeded, 0 if not.
5282 */
5283static int quic_conn_init_timer(struct quic_conn *qc)
5284{
5285 int ret = 0;
5286 /* Attach this task to the same thread ID used for the connection */
5287 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5288
5289 qc->timer_task = task_new_on(qc->tid);
5290 if (!qc->timer_task) {
5291 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5292 goto leave;
5293 }
5294
5295 qc->timer = TICK_ETERNITY;
5296 qc->timer_task->process = qc_process_timer;
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02005297 qc->timer_task->context = qc;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005298
5299 ret = 1;
5300 leave:
5301 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5302 return ret;
5303}
5304
5305/* Rearm the idle timer for <qc> QUIC connection. */
5306static void qc_idle_timer_do_rearm(struct quic_conn *qc)
5307{
5308 unsigned int expire;
5309
Amaury Denoyelle77ed6312023-02-01 09:28:55 +01005310 if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
5311 TRACE_STATE("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
5312 task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
5313 }
5314 else {
5315 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
5316 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
5317 task_queue(qc->idle_timer_task);
5318 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005319}
5320
5321/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
5322 * which is set to 1 when receiving a packet , and 0 when sending packet
5323 */
5324static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
5325{
5326 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5327
5328 if (read) {
5329 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5330 }
5331 else {
5332 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
5333 }
5334 qc_idle_timer_do_rearm(qc);
5335
5336 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5337}
5338
5339/* The task handling the idle timeout */
5340struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
5341{
5342 struct quic_conn *qc = ctx;
5343 struct quic_counters *prx_counters = qc->prx_counters;
5344 unsigned int qc_flags = qc->flags;
5345
5346 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
5347
5348 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
5349 * might free the quic-conn too early via quic_close().
5350 */
5351 qc_notify_close(qc);
5352
5353 /* If the MUX is still alive, keep the quic-conn. The MUX is
5354 * responsible to call quic_close to release it.
5355 */
5356 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
5357 if (qc->mux_state != QC_MUX_READY)
5358 quic_conn_release(qc);
5359
5360 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
5361 * least clean some parts of it such as the tasklet.
5362 */
5363
5364 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
5365 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
5366 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
5367 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
5368 }
5369
5370 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
5371 return NULL;
5372}
5373
5374/* Initialize the idle timeout task for <qc>.
5375 * Returns 1 if succeeded, 0 if not.
5376 */
5377static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
5378{
5379 int ret = 0;
5380
5381 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5382
5383 qc->idle_timer_task = task_new_here();
5384 if (!qc->idle_timer_task) {
5385 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
5386 goto leave;
5387 }
5388
5389 qc->idle_timer_task->process = qc_idle_timer_task;
5390 qc->idle_timer_task->context = qc;
5391 qc_idle_timer_rearm(qc, 1);
5392 task_queue(qc->idle_timer_task);
5393
5394 ret = 1;
5395 leave:
5396 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5397 return ret;
5398}
5399
5400/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
5401 * past one byte of this buffer.
5402 */
5403static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
5404 struct quic_rx_packet *pkt)
5405{
5406 int ret = 0;
5407 unsigned char dcid_len, scid_len;
5408
5409 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5410
5411 if (end == *buf) {
5412 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
5413 goto leave;
5414 }
5415
5416 /* Destination Connection ID Length */
5417 dcid_len = *(*buf)++;
5418 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
5419 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
5420 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
5421 goto leave;
5422 }
5423
5424 if (dcid_len) {
5425 /* Check that the length of this received DCID matches the CID lengths
5426 * of our implementation for non Initials packets only.
5427 */
5428 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
5429 pkt->type != QUIC_PACKET_TYPE_0RTT &&
5430 dcid_len != QUIC_HAP_CID_LEN) {
5431 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
5432 goto leave;
5433 }
5434
5435 memcpy(pkt->dcid.data, *buf, dcid_len);
5436 }
5437
5438 pkt->dcid.len = dcid_len;
5439 *buf += dcid_len;
5440
5441 /* Source Connection ID Length */
5442 scid_len = *(*buf)++;
5443 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
5444 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
5445 goto leave;
5446 }
5447
5448 if (scid_len)
5449 memcpy(pkt->scid.data, *buf, scid_len);
5450 pkt->scid.len = scid_len;
5451 *buf += scid_len;
5452
5453 ret = 1;
5454 leave:
5455 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
5456 return ret;
5457}
5458
5459/* Insert <pkt> RX packet in its <qel> RX packets tree */
5460static void qc_pkt_insert(struct quic_conn *qc,
5461 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
5462{
5463 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
5464
5465 pkt->pn_node.key = pkt->pn;
5466 quic_rx_packet_refinc(pkt);
5467 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
5468
5469 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
5470}
5471
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005472/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
5473 * address of the packet first byte, using the keys from encryption level <el>.
5474 *
5475 * If header protection has been successfully removed, packet data are copied
5476 * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
5477 * proceeded, and the packet is inserted into <qc> protected packets tree. In
5478 * both cases, packet can now be considered handled by the <qc> connection.
5479 *
5480 * If header protection cannot be removed due to <el> secrets already
5481 * discarded, no operation is conducted.
5482 *
5483 * Returns 1 on success : packet data is now handled by the connection. On
5484 * error 0 is returned : packet should be dropped by the caller.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005485 */
5486static inline int qc_try_rm_hp(struct quic_conn *qc,
5487 struct quic_rx_packet *pkt,
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005488 unsigned char *beg,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005489 struct quic_enc_level **el)
5490{
5491 int ret = 0;
5492 unsigned char *pn = NULL; /* Packet number field */
5493 enum quic_tls_enc_level tel;
5494 struct quic_enc_level *qel;
5495 /* Only for traces. */
5496 struct quic_rx_packet *qpkt_trace;
5497
5498 qpkt_trace = NULL;
5499 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005500 BUG_ON(!pkt->pn_offset);
5501
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005502 /* The packet number is here. This is also the start minus
5503 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5504 * protection.
5505 */
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005506 pn = beg + pkt->pn_offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005507
5508 tel = quic_packet_type_enc_level(pkt->type);
5509 qel = &qc->els[tel];
5510
5511 if (qc_qel_may_rm_hp(qc, qel)) {
5512 /* Note that the following function enables us to unprotect the packet
5513 * number and its length subsequently used to decrypt the entire
5514 * packets.
5515 */
5516 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
5517 qel->pktns->rx.largest_pn, pn, beg)) {
5518 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
5519 goto out;
5520 }
5521
Amaury Denoyelle845169d2022-10-17 18:05:26 +02005522 /* The AAD includes the packet number field. */
5523 pkt->aad_len = pkt->pn_offset + pkt->pnl;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005524 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5525 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
5526 goto out;
5527 }
5528
5529 qpkt_trace = pkt;
5530 }
5531 else {
5532 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
5533 /* If the packet number space has been discarded, this packet
5534 * will be not parsed.
5535 */
5536 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
5537 goto out;
5538 }
5539
5540 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005541 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
5542 quic_rx_packet_refinc(pkt);
5543 }
5544
5545 *el = qel;
5546 /* No reference counter incrementation here!!! */
5547 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
5548 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
5549 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
5550 b_add(&qc->rx.buf, pkt->len);
5551
5552 ret = 1;
5553 out:
5554 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
5555 return ret;
5556}
5557
5558/* Parse the header form from <byte0> first byte of <pkt> packet to set its type.
5559 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
5560 * of this packet into <*version>.
5561 */
5562static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
5563 unsigned char **buf, const unsigned char *end,
5564 int *long_header, uint32_t *version)
5565{
5566 int ret = 0;
5567 const unsigned char byte0 = **buf;
5568
5569 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5570
5571 (*buf)++;
5572 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
5573 unsigned char type =
5574 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
5575
5576 *long_header = 1;
5577 /* Version */
5578 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
5579 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
5580 goto out;
5581 }
5582
Frédéric Lécaille21c4c9b2023-01-13 16:37:02 +01005583 if (*version != QUIC_PROTOCOL_VERSION_2) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005584 pkt->type = type;
5585 }
5586 else {
5587 switch (type) {
5588 case 0:
5589 pkt->type = QUIC_PACKET_TYPE_RETRY;
5590 break;
5591 case 1:
5592 pkt->type = QUIC_PACKET_TYPE_INITIAL;
5593 break;
5594 case 2:
5595 pkt->type = QUIC_PACKET_TYPE_0RTT;
5596 break;
5597 case 3:
5598 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
5599 break;
5600 }
5601 }
5602 }
5603 else {
5604 pkt->type = QUIC_PACKET_TYPE_SHORT;
5605 *long_header = 0;
5606 }
5607
5608 ret = 1;
5609 out:
5610 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
5611 return ret;
5612}
5613
5614/* Return the QUIC version (quic_version struct) with <version> as version number
5615 * if supported or NULL if not.
5616 */
5617static inline const struct quic_version *qc_supported_version(uint32_t version)
5618{
5619 int i;
5620
5621 for (i = 0; i < quic_versions_nb; i++)
5622 if (quic_versions[i].num == version)
5623 return &quic_versions[i];
5624
5625 return NULL;
5626}
5627
5628/*
5629 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
5630 * address <addr>.
5631 * Implementation of RFC9000 6. Version Negotiation
5632 *
5633 * TODO implement a rate-limiting sending of Version Negotiation packets
5634 *
5635 * Returns 0 on success else non-zero
5636 */
5637static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
5638 struct quic_rx_packet *pkt)
5639{
5640 char buf[256];
5641 int ret = 0, i = 0, j;
5642 uint32_t version;
5643 const socklen_t addrlen = get_addr_len(addr);
5644
5645 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5646 /*
5647 * header form
5648 * long header, fixed bit to 0 for Version Negotiation
5649 */
5650 /* TODO: RAND_bytes() should be replaced? */
5651 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
5652 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
5653 goto out;
5654 }
5655
5656 buf[i++] |= '\x80';
5657 /* null version for Version Negotiation */
5658 buf[i++] = '\x00';
5659 buf[i++] = '\x00';
5660 buf[i++] = '\x00';
5661 buf[i++] = '\x00';
5662
5663 /* source connection id */
5664 buf[i++] = pkt->scid.len;
5665 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5666 i += pkt->scid.len;
5667
5668 /* destination connection id */
5669 buf[i++] = pkt->dcid.len;
5670 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
5671 i += pkt->dcid.len;
5672
5673 /* supported version */
5674 for (j = 0; j < quic_versions_nb; j++) {
5675 version = htonl(quic_versions[j].num);
5676 memcpy(&buf[i], &version, sizeof(version));
5677 i += sizeof(version);
5678 }
5679
5680 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
5681 goto out;
5682
5683 ret = 1;
5684 out:
5685 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5686 return !ret;
5687}
5688
5689/* Send a stateless reset packet depending on <pkt> RX packet information
5690 * from <fd> UDP socket to <dst>
5691 * Return 1 if succeeded, 0 if not.
5692 */
5693static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
5694 struct quic_rx_packet *rxpkt)
5695{
5696 int ret = 0, pktlen, rndlen;
5697 unsigned char pkt[64];
5698 const socklen_t addrlen = get_addr_len(dstaddr);
5699 struct proxy *prx;
5700 struct quic_counters *prx_counters;
5701
5702 TRACE_ENTER(QUIC_EV_STATELESS_RST);
5703
5704 prx = l->bind_conf->frontend;
5705 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
5706 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
5707 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
5708 * Reset is difficult to distinguish from other packets if the recipient requires
5709 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
5710 * that all packets it sends are at least 22 bytes longer than the minimum
5711 * connection ID length that it requests the peer to include in its packets,
5712 * adding PADDING frames as necessary. This ensures that any Stateless Reset
5713 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
5714 * An endpoint that sends a Stateless Reset in response to a packet that is
5715 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
5716 * than the packet it responds to.
5717 */
5718
5719 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
5720 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
5721 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
5722 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
5723
5724 /* Put a header of random bytes */
5725 /* TODO: RAND_bytes() should be replaced */
5726 if (RAND_bytes(pkt, rndlen) != 1) {
5727 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
5728 goto leave;
5729 }
5730
5731 /* Clear the most significant bit, and set the second one */
5732 *pkt = (*pkt & ~0x80) | 0x40;
5733 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
5734 rxpkt->dcid.data, rxpkt->dcid.len))
5735 goto leave;
5736
5737 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
5738 goto leave;
5739
5740 ret = 1;
5741 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
5742 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
5743 leave:
5744 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
5745 return ret;
5746}
5747
5748/* QUIC server only function.
5749 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
5750 * This is the responsibility of the caller to check <aad> size is big enough
5751 * to contain these data.
5752 * Return the number of bytes copied to <aad>.
5753 */
5754static int quic_generate_retry_token_aad(unsigned char *aad,
5755 uint32_t version,
5756 const struct quic_cid *cid,
5757 const struct sockaddr_storage *addr)
5758{
5759 unsigned char *p;
5760
5761 p = aad;
5762 memcpy(p, &version, sizeof version);
5763 p += sizeof version;
5764 p += quic_saddr_cpy(p, addr);
5765 memcpy(p, cid->data, cid->len);
5766 p += cid->len;
5767
5768 return p - aad;
5769}
5770
5771/* QUIC server only function.
5772 * Generate the token to be used in Retry packets. The token is written to
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05005773 * <buf> with <len> as length. <odcid> is the original destination connection
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005774 * ID and <dcid> is our side destination connection ID (or client source
5775 * connection ID).
5776 * Returns the length of the encoded token or 0 on error.
5777 */
5778static int quic_generate_retry_token(unsigned char *buf, size_t len,
5779 const uint32_t version,
5780 const struct quic_cid *odcid,
5781 const struct quic_cid *dcid,
5782 struct sockaddr_storage *addr)
5783{
5784 int ret = 0;
5785 unsigned char *p;
5786 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02005787 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005788 size_t aadlen;
5789 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
5790 unsigned char key[QUIC_TLS_KEY_LEN];
5791 unsigned char iv[QUIC_TLS_IV_LEN];
5792 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5793 size_t seclen = strlen(global.cluster_secret);
5794 EVP_CIPHER_CTX *ctx = NULL;
5795 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5796 uint32_t timestamp = now_ms;
5797
5798 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5799
5800 /* We copy the odcid into the token, prefixed by its one byte
5801 * length, the format token byte. It is followed by an AEAD TAG, and finally
5802 * the random bytes used to derive the secret to encrypt the token.
5803 */
5804 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
5805 goto err;
5806
5807 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
5808 /* TODO: RAND_bytes() should be replaced */
5809 if (RAND_bytes(salt, sizeof salt) != 1) {
5810 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
5811 goto err;
5812 }
5813
5814 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
5815 salt, sizeof salt, sec, seclen)) {
5816 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
5817 goto err;
5818 }
5819
5820 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
5821 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
5822 goto err;
5823 }
5824
5825 /* Token build */
5826 p = buf;
5827 *p++ = QUIC_TOKEN_FMT_RETRY,
5828 *p++ = odcid->len;
5829 memcpy(p, odcid->data, odcid->len);
5830 p += odcid->len;
5831 write_u32(p, htonl(timestamp));
5832 p += sizeof timestamp;
5833
5834 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
5835 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
5836 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
5837 goto err;
5838 }
5839
5840 p += QUIC_TLS_TAG_LEN;
5841 memcpy(p, salt, sizeof salt);
5842 p += sizeof salt;
5843 EVP_CIPHER_CTX_free(ctx);
5844
5845 ret = p - buf;
5846 leave:
5847 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5848 return ret;
5849
5850 err:
5851 if (ctx)
5852 EVP_CIPHER_CTX_free(ctx);
5853 goto leave;
5854}
5855
5856/* QUIC server only function.
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005857 *
5858 * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
5859 * the UDP datagram containing <pkt> and <l> is the listener instance on which
5860 * it was received. If the token is valid, the ODCID of <qc> QUIC connection
5861 * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
5862 * to validate the token but it can be NULL : in this case the version will be
5863 * retrieved from the packet.
5864 *
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005865 * Return 1 if succeeded, 0 if not.
5866 */
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005867
5868static int quic_retry_token_check(struct quic_rx_packet *pkt,
5869 struct quic_dgram *dgram,
5870 struct listener *l,
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005871 struct quic_conn *qc,
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005872 struct quic_cid *odcid)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005873{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005874 struct proxy *prx;
5875 struct quic_counters *prx_counters;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005876 int ret = 0;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005877 unsigned char *token = pkt->token;
5878 const uint64_t tokenlen = pkt->token_len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005879 unsigned char buf[128];
5880 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
Amaury Denoyelle6c940562022-10-18 11:05:02 +02005881 sizeof(struct in6_addr) + QUIC_CID_MAXLEN];
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005882 size_t aadlen;
5883 const unsigned char *salt;
5884 unsigned char key[QUIC_TLS_KEY_LEN];
5885 unsigned char iv[QUIC_TLS_IV_LEN];
5886 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5887 size_t seclen = strlen(global.cluster_secret);
5888 EVP_CIPHER_CTX *ctx = NULL;
5889 const EVP_CIPHER *aead = EVP_aes_128_gcm();
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005890 const struct quic_version *qv = qc ? qc->original_version :
5891 pkt->version;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005892
5893 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5894
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005895 /* The caller must ensure this. */
5896 BUG_ON(!global.cluster_secret || !pkt->token_len);
5897
5898 prx = l->bind_conf->frontend;
5899 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
5900
5901 if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
5902 /* TODO: New token check */
5903 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
5904 goto leave;
5905 }
5906
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005907 if (sizeof buf < tokenlen) {
5908 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
5909 goto err;
5910 }
5911
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005912 aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->scid, &dgram->saddr);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005913 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
5914 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
5915 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
5916 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
5917 goto err;
5918 }
5919
5920 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
5921 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
5922 goto err;
5923 }
5924
5925 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
5926 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
5927 ctx, aead, key, iv)) {
5928 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
5929 goto err;
5930 }
5931
5932 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
5933 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
5934 goto err;
5935 }
5936
5937 EVP_CIPHER_CTX_free(ctx);
5938
5939 ret = 1;
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005940 HA_ATOMIC_INC(&prx_counters->retry_validated);
5941
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005942 leave:
5943 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5944 return ret;
5945
5946 err:
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02005947 HA_ATOMIC_INC(&prx_counters->retry_error);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02005948 if (ctx)
5949 EVP_CIPHER_CTX_free(ctx);
5950 goto leave;
5951}
5952
5953/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
5954 * the Initial <pkt> packet.
5955 *
5956 * Returns 0 on success else non-zero.
5957 */
5958static int send_retry(int fd, struct sockaddr_storage *addr,
5959 struct quic_rx_packet *pkt, const struct quic_version *qv)
5960{
5961 int ret = 0;
5962 unsigned char buf[128];
5963 int i = 0, token_len;
5964 const socklen_t addrlen = get_addr_len(addr);
5965 struct quic_cid scid;
5966
5967 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5968
5969 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
5970 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
5971 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
5972 /* version */
5973 buf[i++] = *((unsigned char *)&qv->num + 3);
5974 buf[i++] = *((unsigned char *)&qv->num + 2);
5975 buf[i++] = *((unsigned char *)&qv->num + 1);
5976 buf[i++] = *(unsigned char *)&qv->num;
5977
5978 /* Use the SCID from <pkt> for Retry DCID. */
5979 buf[i++] = pkt->scid.len;
5980 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5981 i += pkt->scid.len;
5982
5983 /* Generate a new CID to be used as SCID for the Retry packet. */
5984 scid.len = QUIC_HAP_CID_LEN;
5985 /* TODO: RAND_bytes() should be replaced */
5986 if (RAND_bytes(scid.data, scid.len) != 1) {
5987 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
5988 goto out;
5989 }
5990
5991 buf[i++] = scid.len;
5992 memcpy(&buf[i], scid.data, scid.len);
5993 i += scid.len;
5994
5995 /* token */
5996 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
5997 &pkt->dcid, &pkt->scid, addr))) {
5998 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
5999 goto out;
6000 }
6001
6002 i += token_len;
6003
6004 /* token integrity tag */
6005 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
6006 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
6007 pkt->dcid.len, buf, i, qv)) {
6008 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6009 goto out;
6010 }
6011
6012 i += QUIC_TLS_TAG_LEN;
6013
6014 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
6015 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
6016 goto out;
6017 }
6018
6019 ret = 1;
6020 out:
6021 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
6022 return !ret;
6023}
6024
6025/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
6026 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
6027 * concatenated to the <pkt> DCID field.
6028 *
6029 * Returns the instance or NULL if not found.
6030 */
6031static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
6032 struct listener *l,
6033 struct sockaddr_storage *saddr)
6034{
6035 struct quic_conn *qc = NULL;
6036 struct ebmb_node *node;
6037 struct quic_connection_id *id;
6038 /* set if the quic_conn is found in the second DCID tree */
6039
6040 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
6041
6042 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
6043 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
6044 pkt->type == QUIC_PACKET_TYPE_0RTT) {
6045 /* DCIDs of first packets coming from multiple clients may have
6046 * the same values. Let's distinguish them by concatenating the
6047 * socket addresses.
6048 */
6049 quic_cid_saddr_cat(&pkt->dcid, saddr);
6050 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
6051 pkt->dcid.len + pkt->dcid.addrlen);
6052 if (node) {
6053 qc = ebmb_entry(node, struct quic_conn, odcid_node);
6054 goto end;
6055 }
6056 }
6057
6058 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
6059 * also for INITIAL/0-RTT non-first packets with the final DCID in
6060 * used.
6061 */
6062 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
6063 if (!node)
6064 goto end;
6065
6066 id = ebmb_entry(node, struct quic_connection_id, node);
6067 qc = id->qc;
6068
6069 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
6070 * If already done, this is a noop.
6071 */
6072 if (qc)
6073 ebmb_delete(&qc->odcid_node);
6074
6075 end:
6076 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
6077 return qc;
6078}
6079
6080/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
6081 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
6082 * parameters of this session.
6083 * This is the responsibility of the caller to check the validity of all the
6084 * pointers passed as parameter to this function.
6085 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
6086 * CO_ER_SSL_NO_MEM.
6087 */
6088static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
6089 unsigned char *params, size_t params_len)
6090{
6091 int retry, ret = -1;
6092
6093 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6094
6095 retry = 1;
6096 retry:
6097 *ssl = SSL_new(ssl_ctx);
6098 if (!*ssl) {
6099 if (!retry--)
6100 goto err;
6101
6102 pool_gc(NULL);
6103 goto retry;
6104 }
6105
6106 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
6107 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
6108 SSL_free(*ssl);
6109 *ssl = NULL;
6110 if (!retry--)
6111 goto err;
6112
6113 pool_gc(NULL);
6114 goto retry;
6115 }
6116
6117 ret = 0;
6118 leave:
6119 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6120 return ret;
6121
6122 err:
6123 qc->conn->err_code = CO_ER_SSL_NO_MEM;
6124 goto leave;
6125}
6126
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006127/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
6128 * used to process <qc> received packets. The allocated context is stored in
6129 * <qc.xprt_ctx>.
6130 *
6131 * Returns 0 on success else non-zero.
6132 */
6133static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
6134{
6135 int ret = 0;
6136 struct bind_conf *bc = qc->li->bind_conf;
6137 struct ssl_sock_ctx *ctx = NULL;
6138
6139 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
6140
6141 ctx = pool_zalloc(pool_head_quic_conn_ctx);
6142 if (!ctx) {
6143 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
6144 goto err;
6145 }
6146
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006147 ctx->subs = NULL;
6148 ctx->xprt_ctx = NULL;
6149 ctx->qc = qc;
6150
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006151 if (qc_is_listener(qc)) {
6152 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
6153 qc->enc_params, qc->enc_params_len) == -1) {
6154 goto err;
6155 }
6156#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6157 /* Enabling 0-RTT */
6158 if (bc->ssl_conf.early_data)
6159 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
6160#endif
6161
6162 SSL_set_accept_state(ctx->ssl);
6163 }
6164
6165 ctx->xprt = xprt_get(XPRT_QUIC);
6166
6167 /* Store the allocated context in <qc>. */
6168 qc->xprt_ctx = ctx;
6169
6170 ret = 1;
6171 leave:
6172 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
6173 return !ret;
6174
6175 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006176 pool_free(pool_head_quic_conn_ctx, ctx);
6177 goto leave;
6178}
6179
6180/* Check that all the bytes between <buf> included and <end> address
6181 * excluded are null. This is the responsibility of the caller to
6182 * check that there is at least one byte between <buf> end <end>.
6183 * Return 1 if this all the bytes are null, 0 if not.
6184 */
6185static inline int quic_padding_check(const unsigned char *buf,
6186 const unsigned char *end)
6187{
6188 while (buf < end && !*buf)
6189 buf++;
6190
6191 return buf == end;
6192}
6193
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006194/* Find the associated connection to the packet <pkt> or create a new one if
6195 * this is an Initial packet. <dgram> is the datagram containing the packet and
6196 * <l> is the listener instance on which it was received.
6197 *
6198 * Returns the quic-conn instance or NULL.
6199 */
6200static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
6201 struct quic_dgram *dgram,
6202 struct listener *l)
6203{
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006204 struct quic_cid token_odcid = { .len = 0 };
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006205 struct quic_conn *qc = NULL;
6206 struct proxy *prx;
6207 struct quic_counters *prx_counters;
6208
6209 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6210
6211 prx = l->bind_conf->frontend;
6212 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6213
6214 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
6215
6216 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6217 BUG_ON(!pkt->version); /* This must not happen. */
6218
6219 if (global.cluster_secret && pkt->token_len) {
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006220 if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
6221 goto err;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006222 }
6223
6224 if (!qc) {
6225 int ipv4;
6226
6227 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
6228 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
6229 TRACE_PROTO("Initial without token, sending retry",
6230 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6231 if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
6232 TRACE_ERROR("Error during Retry generation",
6233 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6234 goto out;
6235 }
6236
6237 HA_ATOMIC_INC(&prx_counters->retry_sent);
6238 goto out;
6239 }
6240
6241 /* RFC 9000 7.2. Negotiating Connection IDs:
6242 * When an Initial packet is sent by a client that has not previously
6243 * received an Initial or Retry packet from the server, the client
6244 * populates the Destination Connection ID field with an unpredictable
6245 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6246 */
6247 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
6248 TRACE_PROTO("dropped packet",
6249 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6250 goto err;
6251 }
6252
6253 pkt->saddr = dgram->saddr;
6254 ipv4 = dgram->saddr.ss_family == AF_INET;
6255
Amaury Denoyelle9e3026c2022-10-17 11:13:07 +02006256 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006257 &dgram->daddr, &pkt->saddr, 1,
6258 !!pkt->token_len, l);
6259 if (qc == NULL)
6260 goto err;
6261
6262 HA_ATOMIC_INC(&prx_counters->half_open_conn);
6263 /* Insert the DCID the QUIC client has chosen (only for listeners) */
6264 ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
6265 qc->odcid.len + qc->odcid.addrlen);
6266 }
6267 }
6268 else if (!qc) {
6269 TRACE_PROTO("No connection on a non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
6270 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
6271 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
6272 goto err;
6273 }
6274
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006275 out:
6276 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6277 return qc;
6278
6279 err:
6280 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
6281 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
6282 return NULL;
6283}
6284
Amaury Denoyelle98289692022-10-19 15:37:44 +02006285/* Parse a QUIC packet starting at <buf>. Data won't be read after <end> even
6286 * if the packet is incomplete. This function will populate fields of <pkt>
6287 * instance, most notably its length. <dgram> is the UDP datagram which
6288 * contains the parsed packet. <l> is the listener instance on which it was
6289 * received.
6290 *
6291 * Returns 0 on success else non-zero. Packet length is guaranteed to be set to
6292 * the real packet value or to cover all data between <buf> and <end> : this is
6293 * useful to reject a whole datagram.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006294 */
Amaury Denoyelle98289692022-10-19 15:37:44 +02006295static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
6296 unsigned char *buf, const unsigned char *end,
6297 struct quic_dgram *dgram, struct listener *l)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006298{
Amaury Denoyelle98289692022-10-19 15:37:44 +02006299 const unsigned char *beg = buf;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006300 struct proxy *prx;
6301 struct quic_counters *prx_counters;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006302 int long_header = 0;
Willy Tarreau33a68702022-11-24 09:16:41 +01006303 uint32_t version = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006304 const struct quic_version *qv = NULL;
6305
6306 TRACE_ENTER(QUIC_EV_CONN_LPKT);
6307
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006308 prx = l->bind_conf->frontend;
6309 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
6310 /* This ist only to please to traces and distinguish the
6311 * packet with parsed packet number from others.
6312 */
6313 pkt->pn_node.key = (uint64_t)-1;
6314 if (end <= buf) {
6315 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6316 goto drop;
6317 }
6318
6319 /* Fixed bit */
6320 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006321 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
6322 quic_padding_check(buf, end)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006323 /* Some browsers may pad the remaining datagram space with null bytes.
6324 * That is what we called add padding out of QUIC packets. Such
6325 * datagrams must be considered as valid. But we can only consume
6326 * the remaining space.
6327 */
6328 pkt->len = end - buf;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006329 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006330 }
6331
6332 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6333 goto drop;
6334 }
6335
6336 /* Header form */
6337 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
6338 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6339 goto drop;
6340 }
6341
6342 if (long_header) {
6343 uint64_t len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006344
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006345 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006346 if (!quic_packet_read_long_header(&buf, end, pkt)) {
6347 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6348 goto drop;
6349 }
6350
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006351 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6352 * they must have the same DCID.
6353 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006354 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006355 (pkt->dcid.len != dgram->dcid_len ||
6356 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006357 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006358 goto drop;
6359 }
6360
6361 /* Retry of Version Negotiation packets are only sent by servers */
6362 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
6363 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6364 goto drop;
6365 }
6366
6367 /* RFC9000 6. Version Negotiation */
6368 qv = qc_supported_version(version);
6369 if (!qv) {
6370 /* unsupported version, send Negotiation packet */
6371 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
6372 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006373 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006374 }
6375
6376 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006377 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006378 }
Amaury Denoyelle0eae5722022-10-17 18:05:18 +02006379 pkt->version = qv;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006380
6381 /* For Initial packets, and for servers (QUIC clients connections),
6382 * there is no Initial connection IDs storage.
6383 */
6384 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
6385 uint64_t token_len;
6386
6387 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
6388 end - buf < token_len) {
6389 TRACE_PROTO("Packet dropped",
6390 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6391 goto drop;
6392 }
6393
6394 /* TODO Retry should be automatically activated if
6395 * suspect network usage is detected.
6396 */
6397 if (global.cluster_secret && !token_len) {
6398 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
6399 TRACE_PROTO("Initial without token, sending retry",
Amaury Denoyelle90121b32022-09-27 10:35:29 +02006400 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006401 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
6402 TRACE_PROTO("Error during Retry generation",
Amaury Denoyelle90121b32022-09-27 10:35:29 +02006403 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006404 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006405 }
6406
6407 HA_ATOMIC_INC(&prx_counters->retry_sent);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006408 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006409 }
6410 }
6411 else if (!global.cluster_secret && token_len) {
6412 /* Impossible case: a token was received without configured
6413 * cluster secret.
6414 */
6415 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
6416 NULL, NULL, NULL, qv);
6417 goto drop;
6418 }
6419
6420 pkt->token = buf;
6421 pkt->token_len = token_len;
6422 buf += pkt->token_len;
6423 }
6424 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
6425 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
6426 TRACE_PROTO("Packet dropped",
6427 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6428 goto drop;
6429 }
6430 }
6431
6432 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
6433 end - buf < len) {
6434 TRACE_PROTO("Packet dropped",
6435 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
6436 goto drop;
6437 }
6438
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006439 /* Packet Number is stored here. Packet Length totalizes the
6440 * rest of the content.
6441 */
6442 pkt->pn_offset = buf - beg;
6443 pkt->len = pkt->pn_offset + len;
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006444
Frédéric Lécaille35218c62023-02-16 11:40:11 +01006445 /* RFC 9000. Initial Datagram Size
6446 *
6447 * A server MUST discard an Initial packet that is carried in a UDP datagram
6448 * with a payload that is smaller than the smallest allowed maximum datagram
6449 * size of 1200 bytes.
6450 */
6451 if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
6452 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
6453 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
6454 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
6455 goto drop;
6456 }
6457
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006458 /* Interrupt parsing after packet length retrieval : this
6459 * ensures that only the packet is dropped but not the whole
6460 * datagram.
6461 */
6462 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
6463 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT);
6464 goto drop;
6465 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006466 }
6467 else {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006468 TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006469 if (end - buf < QUIC_HAP_CID_LEN) {
6470 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
6471 goto drop;
6472 }
6473
6474 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
6475 pkt->dcid.len = QUIC_HAP_CID_LEN;
6476
6477 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6478 * they must have the same DCID.
6479 */
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006480 if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006481 (pkt->dcid.len != dgram->dcid_len ||
6482 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006483 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006484 goto drop;
6485 }
6486
6487 buf += QUIC_HAP_CID_LEN;
6488
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006489 pkt->pn_offset = buf - beg;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006490 /* A short packet is the last one of a UDP datagram. */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006491 pkt->len = end - beg;
Amaury Denoyelle449b1a82022-10-19 15:28:44 +02006492 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006493
Amaury Denoyelle98289692022-10-19 15:37:44 +02006494 TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv);
6495 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006496
Amaury Denoyelle98289692022-10-19 15:37:44 +02006497 drop:
6498 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006499 drop_silent:
Amaury Denoyelle98289692022-10-19 15:37:44 +02006500 if (!pkt->len)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006501 pkt->len = end - beg;
Amaury Denoyelle98289692022-10-19 15:37:44 +02006502 TRACE_LEAVE(QUIC_EV_CONN_LPKT, NULL, pkt, NULL, qv);
6503 return -1;
6504}
6505
6506/* Check if received packet <pkt> should be drop due to <qc> already in closing
6507 * state. This can be true if a CONNECTION_CLOSE has already been emitted for
6508 * this connection.
6509 *
6510 * Returns false if connection is not in closing state else true. The caller
6511 * should drop the whole datagram in the last case to not mess up <qc>
6512 * CONNECTION_CLOSE rate limit counter.
6513 */
6514static int qc_rx_check_closing(struct quic_conn *qc,
6515 struct quic_rx_packet *pkt)
6516{
6517 if (!(qc->flags & QUIC_FL_CONN_CLOSING))
6518 return 0;
6519
6520 TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
6521
6522 /* Check if CONNECTION_CLOSE rate reemission is reached. */
6523 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6524 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6525 qc->nb_pkt_for_cc++;
6526 qc->nb_pkt_since_cc = 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006527 }
6528
Amaury Denoyelle98289692022-10-19 15:37:44 +02006529 return 1;
6530}
6531
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01006532/* React to a connection migration initiated on <qc> by a client with the new
6533 * path addresses <peer_addr>/<local_addr>.
6534 *
6535 * Returns 0 on success else non-zero.
6536 */
6537static int qc_handle_conn_migration(struct quic_conn *qc,
6538 const struct sockaddr_storage *peer_addr,
6539 const struct sockaddr_storage *local_addr)
6540{
6541 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6542
Frédéric Lécaille6fc86972023-01-12 08:29:23 +01006543 /* RFC 9000. Connection Migration
6544 *
6545 * If the peer sent the disable_active_migration transport parameter,
6546 * an endpoint also MUST NOT send packets (including probing packets;
6547 * see Section 9.1) from a different local address to the address the peer
6548 * used during the handshake, unless the endpoint has acted on a
6549 * preferred_address transport parameter from the peer.
6550 */
6551 if (qc->li->bind_conf->quic_params.disable_active_migration) {
6552 TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
6553 goto err;
6554 }
6555
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01006556 /* RFC 9000 9. Connection Migration
6557 *
Amaury Denoyelleeb6be982022-11-21 11:14:45 +01006558 * The design of QUIC relies on endpoints retaining a stable address for
6559 * the duration of the handshake. An endpoint MUST NOT initiate
6560 * connection migration before the handshake is confirmed, as defined in
6561 * Section 4.1.2 of [QUIC-TLS].
6562 */
6563 if (qc->state < QUIC_HS_ST_COMPLETE) {
6564 TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
6565 goto err;
6566 }
6567
6568 /* RFC 9000 9. Connection Migration
6569 *
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01006570 * TODO
6571 * An endpoint MUST
6572 * perform path validation (Section 8.2) if it detects any change to a
6573 * peer's address, unless it has previously validated that address.
6574 */
6575
Amaury Denoyelled3083c92022-12-01 16:20:06 +01006576 /* Update quic-conn owned socket if in used.
6577 * TODO try to reuse it instead of closing and opening a new one.
6578 */
6579 if (qc_test_fd(qc)) {
6580 /* TODO try to reuse socket instead of closing it and opening a new one. */
6581 TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
6582 qc_release_fd(qc, 1);
Amaury Denoyellefb375572023-02-01 09:28:32 +01006583 /* TODO need to adjust <jobs> on socket allocation failure. */
Amaury Denoyelled3083c92022-12-01 16:20:06 +01006584 qc_alloc_fd(qc, local_addr, peer_addr);
6585 }
6586
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01006587 qc->local_addr = *local_addr;
6588 qc->peer_addr = *peer_addr;
6589 HA_ATOMIC_INC(&qc->prx_counters->conn_migration_done);
6590
6591 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6592 return 0;
6593
6594 err:
6595 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6596 return 1;
6597}
6598
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01006599/* Release the memory for the RX packets which are no more referenced
6600 * and consume their payloads which have been copied to the RX buffer
6601 * for the connection.
6602 * Always succeeds.
6603 */
6604static inline void quic_rx_pkts_del(struct quic_conn *qc)
6605{
6606 struct quic_rx_packet *pkt, *pktback;
6607
6608 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
6609 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01006610 "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01006611 (long long)pkt->pn_node.key,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01006612 pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01006613 (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
6614 if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
6615 size_t cdata;
6616
6617 cdata = b_contig_data(&qc->rx.buf, 0);
6618 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01006619 "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01006620 if (cdata && !*b_head(&qc->rx.buf)) {
6621 /* Consume the remaining data */
6622 b_del(&qc->rx.buf, cdata);
6623 }
6624 break;
6625 }
6626
6627 if (pkt->refcnt)
6628 break;
6629
6630 b_del(&qc->rx.buf, pkt->raw_len);
6631 LIST_DELETE(&pkt->qc_rx_pkt_list);
6632 pool_free(pool_head_quic_rx_packet, pkt);
6633 }
6634
6635 /* In frequent cases the buffer will be emptied at this stage. */
6636 b_realign_if_empty(&qc->rx.buf);
6637}
6638
Amaury Denoyelle98289692022-10-19 15:37:44 +02006639/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
6640 * into <qc> receive buffer after header protection removal procedure.
6641 *
6642 * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
6643 * must point to packet buffer first byte.
6644 *
6645 * <tasklist_head> may be non-NULL when the caller treat several datagrams for
6646 * different quic-conn. In this case, each quic-conn tasklet will be appended
6647 * to it in order to be woken up after the current task.
6648 *
6649 * The caller can safely removed the packet data. If packet refcount was not
6650 * incremented by this function, it means that the connection did not handled
6651 * it and it should be freed by the caller.
6652 */
6653static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
6654 struct quic_dgram *dgram, unsigned char *beg,
6655 struct list **tasklist_head)
6656{
6657 const struct quic_version *qv = pkt->version;
6658 struct quic_enc_level *qel = NULL;
6659 size_t b_cspace;
Frédéric Lécaille8f7d2242023-02-15 11:55:21 +01006660 int io_cb_wakeup = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02006661
Amaury Denoyelle3f474e62022-11-24 17:15:08 +01006662 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
6663
Amaury Denoyelledeb7c872022-10-19 17:14:28 +02006664 if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
6665 !quic_peer_validated_addr(qc) &&
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006666 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
6667 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
6668 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
6669 /* Reset the anti-amplification bit. It will be set again
6670 * when sending the next packet if reached again.
6671 */
6672 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006673 io_cb_wakeup = 1;
6674 }
6675
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006676 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
6677 TRACE_PROTO("Connection error",
6678 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
6679 goto out;
6680 }
6681
6682 pkt->raw_len = pkt->len;
6683 quic_rx_pkts_del(qc);
6684 b_cspace = b_contig_space(&qc->rx.buf);
6685 if (b_cspace < pkt->len) {
Frédéric Lécaille1dbeb352023-02-07 11:40:21 +01006686 TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
Frédéric Lécailleb7a13be2023-02-22 17:24:23 +01006687 "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006688 /* Do not consume buf if space not at the end. */
6689 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
6690 TRACE_PROTO("Packet dropped",
6691 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02006692 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006693 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006694 }
6695
6696 /* Let us consume the remaining contiguous space. */
6697 if (b_cspace) {
6698 b_putchr(&qc->rx.buf, 0x00);
6699 b_cspace--;
6700 }
6701 b_add(&qc->rx.buf, b_cspace);
6702 if (b_contig_space(&qc->rx.buf) < pkt->len) {
6703 TRACE_PROTO("Too big packet",
6704 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Amaury Denoyelle98289692022-10-19 15:37:44 +02006705 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt_bufoverrun);
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006706 goto drop_silent;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006707 }
6708 }
6709
Amaury Denoyelle845169d2022-10-17 18:05:26 +02006710 if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006711 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
6712 goto drop;
6713 }
6714
6715 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
6716 if (pkt->aad_len)
6717 qc_pkt_insert(qc, pkt, qel);
6718 out:
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02006719 *tasklist_head = tasklet_wakeup_after(*tasklist_head,
6720 qc->wait_event.tasklet);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006721
Amaury Denoyelle6e56a9e2022-10-17 12:04:49 +02006722 drop_silent:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006723 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006724 return;
6725
6726 drop:
Amaury Denoyelle98289692022-10-19 15:37:44 +02006727 HA_ATOMIC_INC(&qc->prx_counters->dropped_pkt);
Frédéric Lécaille75c8ad52023-02-08 16:08:28 +01006728 if (io_cb_wakeup) {
6729 TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
6730 QUIC_EV_CONN_LPKT, qc);
6731 qc_set_timer(qc);
6732 if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
6733 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
6734 }
Amaury Denoyelle2ed84002022-09-26 14:53:59 +02006735
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006736 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
6737}
6738
6739/* This function builds into <buf> buffer a QUIC long packet header.
6740 * Return 1 if enough room to build this header, 0 if not.
6741 */
6742static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
6743 int type, size_t pn_len,
6744 struct quic_conn *qc, const struct quic_version *ver)
6745{
6746 int ret = 0;
6747
6748 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6749
6750 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
6751 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6752 goto leave;
6753 }
6754
6755 type = quic_pkt_type(type, ver->num);
6756 /* #0 byte flags */
6757 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
6758 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
6759 /* Version */
6760 quic_write_uint32(buf, end, ver->num);
6761 *(*buf)++ = qc->dcid.len;
6762 /* Destination connection ID */
6763 if (qc->dcid.len) {
6764 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6765 *buf += qc->dcid.len;
6766 }
6767 /* Source connection ID */
6768 *(*buf)++ = qc->scid.len;
6769 if (qc->scid.len) {
6770 memcpy(*buf, qc->scid.data, qc->scid.len);
6771 *buf += qc->scid.len;
6772 }
6773
6774 ret = 1;
6775 leave:
6776 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6777 return ret;
6778}
6779
6780/* This function builds into <buf> buffer a QUIC short packet header.
6781 * Return 1 if enough room to build this header, 0 if not.
6782 */
6783static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
6784 size_t pn_len, struct quic_conn *qc,
6785 unsigned char tls_flags)
6786{
6787 int ret = 0;
6788
6789 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6790
6791 if (end - *buf < 1 + qc->dcid.len) {
6792 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6793 goto leave;
6794 }
6795
6796 /* #0 byte flags */
6797 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
6798 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
6799 /* Destination connection ID */
6800 if (qc->dcid.len) {
6801 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6802 *buf += qc->dcid.len;
6803 }
6804
6805 ret = 1;
6806 leave:
6807 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6808 return ret;
6809}
6810
6811/* Apply QUIC header protection to the packet with <buf> as first byte address,
6812 * <pn> as address of the Packet number field, <pnlen> being this field length
6813 * with <aead> as AEAD cipher and <key> as secret key.
6814 * Returns 1 if succeeded or 0 if failed.
6815 */
6816static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
6817 unsigned char *pn, size_t pnlen,
6818 struct quic_tls_ctx *tls_ctx)
6819
6820{
6821 int i, ret = 0;
6822 /* We need an IV of at least 5 bytes: one byte for bytes #0
6823 * and at most 4 bytes for the packet number
6824 */
6825 unsigned char mask[5] = {0};
6826 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
6827
6828 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6829
6830 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
6831 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
6832 goto out;
6833 }
6834
6835 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
6836 for (i = 0; i < pnlen; i++)
6837 pn[i] ^= mask[i + 1];
6838
6839 ret = 1;
6840 out:
6841 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6842 return ret;
6843}
6844
6845/* Reduce the encoded size of <ack_frm> ACK frame removing the last
6846 * ACK ranges if needed to a value below <limit> in bytes.
6847 * Return 1 if succeeded, 0 if not.
6848 */
6849static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
6850 struct quic_frame *ack_frm, size_t limit)
6851{
6852 size_t room, ack_delay_sz;
6853 int ret = 0;
6854
6855 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6856
6857 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
6858 /* A frame is made of 1 byte for the frame type. */
6859 room = limit - ack_delay_sz - 1;
6860 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
6861 goto leave;
6862
6863 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
6864 leave:
6865 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6866 return ret;
6867}
6868
6869/* Prepare into <outlist> as most as possible ack-eliciting frame from their
6870 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
6871 * with <room> as available room, and <*len> the packet Length field initialized
6872 * with the number of bytes already present in this buffer which must be taken
6873 * into an account for the Length packet field value. <headlen> is the number of
6874 * bytes already present in this packet before building frames.
6875 *
6876 * Update consequently <*len> to reflect the size of these frames built
6877 * by this function. Also attach these frames to <l> frame list.
6878 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
6879 */
6880static inline int qc_build_frms(struct list *outlist, struct list *inlist,
6881 size_t room, size_t *len, size_t headlen,
6882 struct quic_enc_level *qel,
6883 struct quic_conn *qc)
6884{
6885 int ret;
6886 struct quic_frame *cf, *cfbak;
6887
6888 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
6889
6890 ret = 0;
6891 if (*len > room)
6892 goto leave;
6893
6894 /* If we are not probing we must take into an account the congestion
6895 * control window.
6896 */
6897 if (!qel->pktns->tx.pto_probe) {
6898 size_t remain = quic_path_prep_data(qc->path);
6899
6900 if (headlen > remain)
6901 goto leave;
6902
6903 room = QUIC_MIN(room, remain - headlen);
6904 }
6905
6906 TRACE_PROTO("************** frames build (headlen)",
6907 QUIC_EV_CONN_BCFRMS, qc, &headlen);
6908
6909 /* NOTE: switch/case block inside a loop, a successful status must be
6910 * returned by this function only if at least one frame could be built
6911 * in the switch/case block.
6912 */
6913 list_for_each_entry_safe(cf, cfbak, inlist, list) {
6914 /* header length, data length, frame length. */
6915 size_t hlen, dlen, dlen_sz, avail_room, flen;
6916
6917 if (!room)
6918 break;
6919
6920 switch (cf->type) {
6921 case QUIC_FT_CRYPTO:
6922 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
6923 QUIC_EV_CONN_BCFRMS, qc, &room, len);
6924 /* Compute the length of this CRYPTO frame header */
6925 hlen = 1 + quic_int_getsize(cf->crypto.offset);
6926 /* Compute the data length of this CRyPTO frame. */
6927 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
6928 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
6929 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
6930 if (!dlen)
6931 continue;
6932
6933 /* CRYPTO frame length. */
6934 flen = hlen + quic_int_getsize(dlen) + dlen;
6935 TRACE_DEVEL(" CRYPTO frame length (flen)",
6936 QUIC_EV_CONN_BCFRMS, qc, &flen);
6937 /* Add the CRYPTO data length and its encoded length to the packet
6938 * length and the length of this length.
6939 */
6940 *len += flen;
6941 room -= flen;
6942 if (dlen == cf->crypto.len) {
6943 /* <cf> CRYPTO data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01006944 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006945 LIST_APPEND(outlist, &cf->list);
6946 }
6947 else {
6948 struct quic_frame *new_cf;
6949
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01006950 new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006951 if (!new_cf) {
6952 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
6953 continue;
6954 }
6955
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006956 new_cf->crypto.len = dlen;
6957 new_cf->crypto.offset = cf->crypto.offset;
6958 new_cf->crypto.qel = qel;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05006959 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006960 if (cf->origin) {
6961 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6962 /* This <cf> frame was duplicated */
6963 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6964 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01006965 /* Detach the remaining CRYPTO frame from its original frame */
6966 LIST_DEL_INIT(&cf->ref);
6967 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006968 }
6969 LIST_APPEND(outlist, &new_cf->list);
6970 /* Consume <dlen> bytes of the current frame. */
6971 cf->crypto.len -= dlen;
6972 cf->crypto.offset += dlen;
6973 }
6974 break;
6975
6976 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
6977 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
6978 struct eb64_node *node = NULL;
6979 struct qc_stream_desc *stream_desc = NULL;
6980 struct quic_stream *strm = &cf->stream;
6981
6982 /* As this frame has been already lost, ensure the stream is always
6983 * available or the range of this frame is not consumed before
6984 * resending it.
6985 */
6986 node = eb64_lookup(&qc->streams_by_id, strm->id);
6987 if (!node) {
6988 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01006989 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006990 continue;
6991 }
6992
6993 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
6994 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
6995 TRACE_DEVEL("ignored frame frame in already acked range",
6996 QUIC_EV_CONN_PRSAFRM, qc, cf);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01006997 qc_frm_free(&cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02006998 continue;
6999 }
7000 else if (strm->offset.key < stream_desc->ack_offset) {
7001 strm->offset.key = stream_desc->ack_offset;
7002 TRACE_DEVEL("updated partially acked frame",
7003 QUIC_EV_CONN_PRSAFRM, qc, cf);
7004 }
7005 }
7006 /* Note that these frames are accepted in short packets only without
7007 * "Length" packet field. Here, <*len> is used only to compute the
7008 * sum of the lengths of the already built frames for this packet.
7009 *
7010 * Compute the length of this STREAM frame "header" made a all the field
7011 * excepting the variable ones. Note that +1 is for the type of this frame.
7012 */
7013 hlen = 1 + quic_int_getsize(cf->stream.id) +
7014 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
7015 /* Compute the data length of this STREAM frame. */
7016 avail_room = room - hlen - *len;
7017 if ((ssize_t)avail_room <= 0)
7018 continue;
7019
7020 TRACE_DEVEL(" New STREAM frame build (room, len)",
7021 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007022
7023 /* hlen contains STREAM id and offset. Ensure there is
7024 * enough room for length field.
7025 */
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007026 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007027 dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
7028 cf->stream.len);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007029 dlen_sz = quic_int_getsize(dlen);
7030 flen = hlen + dlen_sz + dlen;
7031 }
7032 else {
7033 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
7034 flen = hlen + dlen;
7035 }
Amaury Denoyellef2f08f82023-02-03 18:39:06 +01007036
7037 if (cf->stream.len && !dlen) {
7038 /* Only a small gap is left on buffer, not
7039 * enough to encode the STREAM data length.
7040 */
7041 continue;
7042 }
7043
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007044 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
7045 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
7046 TRACE_DEVEL(" STREAM frame length (flen)",
7047 QUIC_EV_CONN_BCFRMS, qc, &flen);
7048 /* Add the STREAM data length and its encoded length to the packet
7049 * length and the length of this length.
7050 */
7051 *len += flen;
7052 room -= flen;
7053 if (dlen == cf->stream.len) {
7054 /* <cf> STREAM data have been consumed. */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007055 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007056 LIST_APPEND(outlist, &cf->list);
7057
7058 /* Do not notify MUX on retransmission. */
7059 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7060 qcc_streams_sent_done(cf->stream.stream->ctx,
7061 cf->stream.len,
7062 cf->stream.offset.key);
7063 }
7064 }
7065 else {
7066 struct quic_frame *new_cf;
7067 struct buffer cf_buf;
7068
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01007069 new_cf = qc_frm_alloc(cf->type);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007070 if (!new_cf) {
7071 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
7072 continue;
7073 }
7074
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007075 new_cf->stream.stream = cf->stream.stream;
7076 new_cf->stream.buf = cf->stream.buf;
7077 new_cf->stream.id = cf->stream.id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01007078 new_cf->stream.offset = cf->stream.offset;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007079 new_cf->stream.len = dlen;
7080 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
7081 /* FIN bit reset */
7082 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
7083 new_cf->stream.data = cf->stream.data;
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05007084 TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007085 if (cf->origin) {
7086 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
7087 /* This <cf> frame was duplicated */
7088 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
7089 new_cf->origin = cf->origin;
Frédéric Lécailledd419462023-01-26 15:07:39 +01007090 /* Detach this STREAM frame from its origin */
7091 LIST_DEL_INIT(&cf->ref);
7092 cf->origin = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007093 }
7094 LIST_APPEND(outlist, &new_cf->list);
7095 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
7096 /* Consume <dlen> bytes of the current frame. */
7097 cf_buf = b_make(b_orig(cf->stream.buf),
7098 b_size(cf->stream.buf),
7099 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
7100 cf->stream.len -= dlen;
7101 cf->stream.offset.key += dlen;
7102 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
7103
7104 /* Do not notify MUX on retransmission. */
7105 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
7106 qcc_streams_sent_done(new_cf->stream.stream->ctx,
7107 new_cf->stream.len,
7108 new_cf->stream.offset.key);
7109 }
7110 }
7111
7112 /* TODO the MUX is notified about the frame sending via
7113 * previous qcc_streams_sent_done call. However, the
7114 * sending can fail later, for example if the sendto
7115 * system call returns an error. As the MUX has been
7116 * notified, the transport layer is responsible to
7117 * bufferize and resent the announced data later.
7118 */
7119
7120 break;
7121
7122 default:
7123 flen = qc_frm_len(cf);
7124 BUG_ON(!flen);
7125 if (flen > room)
7126 continue;
7127
7128 *len += flen;
7129 room -= flen;
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007130 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007131 LIST_APPEND(outlist, &cf->list);
7132 break;
7133 }
7134
7135 /* Successful status as soon as a frame could be built */
7136 ret = 1;
7137 }
7138
7139 leave:
7140 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
7141 return ret;
7142}
7143
7144/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
7145 * is used as return parameter and should be zero'ed by the caller.
7146 */
7147static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
7148 struct quic_frame *out)
7149{
7150 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
7151 *
7152 * A CONNECTION_CLOSE frame should be sent in several packets with
7153 * different encryption levels depending on the client context. This is
7154 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
7155 * more details on how to implement it.
7156 */
7157 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
7158
7159
7160 if (qc->err.app) {
7161 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
7162 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
7163 /* RFC 9000 10.2.3. Immediate Close during the Handshake
7164 *
7165 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
7166 * packet could expose application state or be used to alter application
7167 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
7168 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
7169 * Handshake packets. Otherwise, information about the application
7170 * state might be revealed. Endpoints MUST clear the value of the
7171 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
7172 * converting to a CONNECTION_CLOSE of type 0x1c.
7173 */
7174 out->type = QUIC_FT_CONNECTION_CLOSE;
7175 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
7176 out->connection_close.reason_phrase_len = 0;
7177 }
7178 else {
7179 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
7180 out->connection_close.error_code = qc->err.code;
7181 }
7182 }
7183 else {
7184 out->type = QUIC_FT_CONNECTION_CLOSE;
7185 out->connection_close.error_code = qc->err.code;
7186 }
7187 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
7188
7189}
7190
7191/* This function builds a clear packet from <pkt> information (its type)
7192 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
7193 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
7194 * filling the buffer with as much frames as possible from <frms> list of
7195 * prebuilt frames.
7196 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
7197 * reserved so that to ensure there is enough room to build this AEAD TAG after
7198 * having returned from this function.
7199 * This function also updates the value of <buf_pn> pointer to point to the packet
7200 * number field in this packet. <pn_len> will also have the packet number
7201 * length as value.
7202 *
7203 * Return 1 if succeeded (enough room to buile this packet), O if not.
7204 */
7205static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
7206 size_t dglen, struct quic_tx_packet *pkt,
7207 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
7208 int force_ack, int padding, int cc, int probe,
7209 struct quic_enc_level *qel, struct quic_conn *qc,
7210 const struct quic_version *ver, struct list *frms)
7211{
7212 unsigned char *beg, *payload;
7213 size_t len, len_sz, len_frms, padding_len;
7214 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
7215 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
7216 struct quic_frame cc_frm = { };
7217 size_t ack_frm_len, head_len;
7218 int64_t rx_largest_acked_pn;
7219 int add_ping_frm;
7220 struct list frm_list = LIST_HEAD_INIT(frm_list);
7221 struct quic_frame *cf;
7222 int must_ack, ret = 0;
7223 int nb_aepkts_since_last_ack;
7224
7225 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
7226
7227 /* Length field value with CRYPTO frames if present. */
7228 len_frms = 0;
7229 beg = pos;
7230 /* When not probing, and no immediate close is required, reduce the size of this
7231 * buffer to respect the congestion controller window.
7232 * This size will be limited if we have ack-eliciting frames to send from <frms>.
7233 */
7234 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
7235 size_t path_room;
7236
7237 path_room = quic_path_prep_data(qc->path);
7238 if (end - beg > path_room)
7239 end = beg + path_room;
7240 }
7241
7242 /* Ensure there is enough room for the TLS encryption tag and a zero token
7243 * length field if any.
7244 */
7245 if (end - pos < QUIC_TLS_TAG_LEN +
7246 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
7247 goto no_room;
7248
7249 end -= QUIC_TLS_TAG_LEN;
7250 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
7251 /* packet number length */
7252 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
7253 /* Build the header */
7254 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
7255 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
7256 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
7257 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
7258 goto no_room;
7259
7260 /* Encode the token length (0) for an Initial packet. */
7261 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
7262 *pos++ = 0;
7263 head_len = pos - beg;
7264 /* Build an ACK frame if required. */
7265 ack_frm_len = 0;
7266 nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
7267 must_ack = !qel->pktns->tx.pto_probe &&
7268 (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
7269 (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK)));
7270 if (must_ack) {
7271 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
7272 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
7273 ack_frm.tx_ack.arngs = arngs;
7274 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
7275 qel->pktns->tx.ack_delay =
7276 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
7277 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
7278 }
7279 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
7280 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
7281 * smallest frame (PING) and <*pn_len> more for the packet number. Note
7282 * that from here, we do not know if we will have to send a PING frame.
7283 * This will be decided after having computed the ack-eliciting frames
7284 * to be added to this packet.
7285 */
7286 ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos);
7287 if (!ack_frm_len)
7288 goto no_room;
7289 }
7290
7291 /* Length field value without the ack-eliciting frames. */
7292 len = ack_frm_len + *pn_len;
7293 len_frms = 0;
7294 if (!cc && !LIST_ISEMPTY(frms)) {
7295 ssize_t room = end - pos;
7296
7297 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
7298 /* Initialize the length of the frames built below to <len>.
7299 * If any frame could be successfully built by qc_build_frms(),
7300 * we will have len_frms > len.
7301 */
7302 len_frms = len;
7303 if (!qc_build_frms(&frm_list, frms,
7304 end - pos, &len_frms, pos - beg, qel, qc)) {
7305 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7306 qc, NULL, NULL, &room);
7307 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
7308 goto no_room;
7309 }
7310 }
7311
7312 /* Length (of the remaining data). Must not fail because, the buffer size
7313 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
7314 * for the encryption tag. It must be taken into an account for the length
7315 * of this packet.
7316 */
7317 if (len_frms)
7318 len = len_frms + QUIC_TLS_TAG_LEN;
7319 else
7320 len += QUIC_TLS_TAG_LEN;
7321 /* CONNECTION_CLOSE frame */
7322 if (cc) {
7323 qc_build_cc_frm(qc, qel, &cc_frm);
7324 len += qc_frm_len(&cc_frm);
7325 }
7326 add_ping_frm = 0;
7327 padding_len = 0;
7328 len_sz = quic_int_getsize(len);
7329 /* Add this packet size to <dglen> */
7330 dglen += head_len + len_sz + len;
7331 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
7332 /* This is a maximum padding size */
7333 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
7334 /* The length field value is of this packet is <len> + <padding_len>
7335 * the size of which may be greater than the initial computed size
7336 * <len_sz>. So, let's deduce the difference between these to packet
7337 * sizes from <padding_len>.
7338 */
7339 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
7340 len += padding_len;
7341 }
Frédéric Lécaille5faf5772023-02-16 17:30:53 +01007342 else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
7343 len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
7344 }
7345 else if (LIST_ISEMPTY(&frm_list)) {
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007346 if (qel->pktns->tx.pto_probe) {
7347 /* If we cannot send a frame, we send a PING frame. */
7348 add_ping_frm = 1;
7349 len += 1;
7350 }
7351 /* If there is no frame at all to follow, add at least a PADDING frame. */
7352 if (!ack_frm_len && !cc)
7353 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
7354 }
7355
7356 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
7357 goto no_room;
7358
7359 /* Packet number field address. */
7360 *buf_pn = pos;
7361
7362 /* Packet number encoding. */
7363 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
7364 goto no_room;
7365
7366 /* payload building (ack-eliciting or not frames) */
7367 payload = pos;
7368 if (ack_frm_len) {
7369 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
7370 goto no_room;
7371
7372 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
7373 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
7374 }
7375
7376 /* Ack-eliciting frames */
7377 if (!LIST_ISEMPTY(&frm_list)) {
7378 struct quic_frame *tmp_cf;
7379 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
7380 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
7381 ssize_t room = end - pos;
7382 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
7383 qc, NULL, NULL, &room);
7384 /* Note that <cf> was added from <frms> to <frm_list> list by
7385 * qc_build_frms().
7386 */
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01007387 LIST_DEL_INIT(&cf->list);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007388 LIST_INSERT(frms, &cf->list);
7389 continue;
7390 }
7391
7392 quic_tx_packet_refinc(pkt);
7393 cf->pkt = pkt;
7394 }
7395 }
7396
7397 /* Build a PING frame if needed. */
7398 if (add_ping_frm) {
7399 frm.type = QUIC_FT_PING;
7400 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
7401 goto no_room;
7402 }
7403
7404 /* Build a CONNECTION_CLOSE frame if needed. */
7405 if (cc) {
7406 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
7407 goto no_room;
7408
7409 pkt->flags |= QUIC_FL_TX_PACKET_CC;
7410 }
7411
7412 /* Build a PADDING frame if needed. */
7413 if (padding_len) {
7414 frm.type = QUIC_FT_PADDING;
7415 frm.padding.len = padding_len;
7416 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
7417 goto no_room;
7418 }
7419
7420 if (pos == payload) {
7421 /* No payload was built because of congestion control */
7422 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
7423 goto no_room;
7424 }
7425
7426 /* If this packet is ack-eliciting and we are probing let's
7427 * decrement the PTO probe counter.
7428 */
7429 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
7430 qel->pktns->tx.pto_probe)
7431 qel->pktns->tx.pto_probe--;
7432
7433 pkt->len = pos - beg;
7434 LIST_SPLICE(&pkt->frms, &frm_list);
7435
7436 ret = 1;
7437 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
7438 leave:
7439 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
7440 return ret;
7441
7442 no_room:
7443 /* Replace the pre-built frames which could not be add to this packet */
7444 LIST_SPLICE(frms, &frm_list);
7445 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
7446 goto leave;
7447}
7448
7449static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
7450{
7451 pkt->type = type;
7452 pkt->len = 0;
7453 pkt->in_flight_len = 0;
7454 pkt->pn_node.key = (uint64_t)-1;
7455 LIST_INIT(&pkt->frms);
7456 pkt->time_sent = TICK_ETERNITY;
7457 pkt->next = NULL;
Frédéric Lécaille814645f2022-11-18 18:15:28 +01007458 pkt->prev = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007459 pkt->largest_acked_pn = -1;
7460 pkt->flags = 0;
7461 pkt->refcnt = 0;
7462}
7463
7464/* Build a packet into <buf> packet buffer with <pkt_type> as packet
7465 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
7466 * of prebuilt frames.
7467 *
7468 * Return -2 if the packet could not be allocated or encrypted for any reason,
7469 * -1 if there was not enough room to build a packet.
7470 * XXX NOTE XXX
7471 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
7472 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
7473 * control window limitation.
7474 */
7475static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
7476 const unsigned char *buf_end,
7477 struct quic_enc_level *qel,
7478 struct quic_tls_ctx *tls_ctx, struct list *frms,
7479 struct quic_conn *qc, const struct quic_version *ver,
7480 size_t dglen, int pkt_type, int force_ack,
7481 int padding, int probe, int cc, int *err)
7482{
7483 struct quic_tx_packet *ret_pkt = NULL;
7484 /* The pointer to the packet number field. */
7485 unsigned char *buf_pn;
7486 unsigned char *beg, *end, *payload;
7487 int64_t pn;
7488 size_t pn_len, payload_len, aad_len;
7489 struct quic_tx_packet *pkt;
7490
7491 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel);
7492 *err = 0;
7493 pkt = pool_alloc(pool_head_quic_tx_packet);
7494 if (!pkt) {
7495 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
7496 *err = -2;
7497 goto err;
7498 }
7499
7500 quic_tx_packet_init(pkt, pkt_type);
7501 beg = *pos;
7502 pn_len = 0;
7503 buf_pn = NULL;
7504
7505 pn = qel->pktns->tx.next_pn + 1;
7506 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
7507 force_ack, padding, cc, probe, qel, qc, ver, frms)) {
7508 // trace already emitted by function above
7509 *err = -1;
7510 goto err;
7511 }
7512
7513 end = beg + pkt->len;
7514 payload = buf_pn + pn_len;
7515 payload_len = end - payload;
7516 aad_len = payload - beg;
7517
7518 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
7519 // trace already emitted by function above
7520 *err = -2;
7521 goto err;
7522 }
7523
7524 end += QUIC_TLS_TAG_LEN;
7525 pkt->len += QUIC_TLS_TAG_LEN;
7526 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
7527 // trace already emitted by function above
7528 *err = -2;
7529 goto err;
7530 }
7531
7532 /* Consume a packet number */
7533 qel->pktns->tx.next_pn++;
7534 qc->tx.prep_bytes += pkt->len;
7535 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
7536 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
7537 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
7538 }
7539 /* Now that a correct packet is built, let us consume <*pos> buffer. */
7540 *pos = end;
7541 /* Attach the built packet to its tree. */
7542 pkt->pn_node.key = pn;
7543 /* Set the packet in fligth length for in flight packet only. */
7544 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
7545 pkt->in_flight_len = pkt->len;
7546 qc->path->prep_in_flight += pkt->len;
7547 }
7548 /* Always reset this flags */
7549 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
7550 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
7551 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
7552 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
7553 }
7554
7555 pkt->pktns = qel->pktns;
7556
7557 ret_pkt = pkt;
7558 leave:
7559 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt);
7560 return ret_pkt;
7561
7562 err:
7563 /* TODO: what about the frames which have been built
7564 * for this packet.
7565 */
7566 free_quic_tx_packet(qc, pkt);
7567 goto leave;
7568}
7569
7570
7571static void __quic_conn_init(void)
7572{
7573 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
7574}
7575INITCALL0(STG_REGISTER, __quic_conn_init);
7576
7577static void __quic_conn_deinit(void)
7578{
7579 BIO_meth_free(ha_quic_meth);
7580}
7581REGISTER_POST_DEINIT(__quic_conn_deinit);
7582
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007583/* Handle a new <dgram> received. Parse each QUIC packets and copied their
7584 * content to a quic-conn instance. The datagram content can be released after
7585 * this function.
7586 *
7587 * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
7588 * to the connection instance. <li> is the attached listener. The caller is
7589 * responsible to ensure that the first packet is destined to this connection
7590 * by comparing CIDs.
7591 *
7592 * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
7593 * function will thus retrieve the connection from the CID tree or allocate a
7594 * new one if possible. <li> is the listener attached to the receiver.
7595 *
7596 * Returns 0 on success else non-zero. If an error happens, some packets from
7597 * the datagram may not have been parsed.
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007598 */
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007599int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
7600 struct listener *li)
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007601{
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007602 struct quic_rx_packet *pkt;
7603 struct quic_conn *qc = NULL;
7604 unsigned char *pos, *end;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007605 struct list *tasklist_head = NULL;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007606
7607 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7608
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007609 pos = dgram->buf;
7610 end = pos + dgram->len;
7611 do {
7612 /* TODO replace zalloc -> alloc. */
7613 pkt = pool_zalloc(pool_head_quic_rx_packet);
7614 if (!pkt) {
7615 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
7616 goto err;
7617 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007618
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007619 pkt->version = NULL;
7620 pkt->pn_offset = 0;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007621
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007622 /* Set flag if pkt is the first one in dgram. */
7623 if (pos == dgram->buf)
7624 pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007625
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007626 LIST_INIT(&pkt->qc_rx_pkt_list);
7627 pkt->time_received = now_ms;
7628 quic_rx_packet_refinc(pkt);
7629 if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
7630 goto next;
Amaury Denoyelle98289692022-10-19 15:37:44 +02007631
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007632 /* Search quic-conn instance for first packet of the datagram.
7633 * quic_rx_packet_parse() is responsible to discard packets
7634 * with different DCID as the first one in the same datagram.
7635 */
7636 if (!qc) {
7637 qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li);
7638 /* qc is NULL if receiving a non Initial packet for an
7639 * unknown connection.
7640 */
7641 if (!qc) {
Amaury Denoyelle98289692022-10-19 15:37:44 +02007642 /* Skip the entire datagram. */
7643 pkt->len = end - pos;
7644 goto next;
7645 }
7646
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007647 dgram->qc = qc;
7648 }
Amaury Denoyelle98289692022-10-19 15:37:44 +02007649
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007650 if (qc_rx_check_closing(qc, pkt)) {
7651 /* Skip the entire datagram. */
7652 pkt->len = end - pos;
7653 goto next;
7654 }
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007655
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007656 /* Detect QUIC connection migration. */
Frédéric Lécaillef6769542023-01-12 10:36:26 +01007657 if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
Amaury Denoyelleeec0b3c2022-12-02 09:57:32 +01007658 if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
7659 /* Skip the entire datagram. */
7660 TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
7661 pkt->len = end - pos;
7662 goto next;
7663 }
7664 }
7665
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007666 qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007667
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007668 next:
7669 pos += pkt->len;
7670 quic_rx_packet_refdec(pkt);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007671
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007672 /* Free rejected packets */
7673 if (!pkt->refcnt) {
7674 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
7675 pool_free(pool_head_quic_rx_packet, pkt);
7676 }
7677 } while (pos < end);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007678
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007679 /* Increasing the received bytes counter by the UDP datagram length
7680 * if this datagram could be associated to a connection.
7681 */
7682 if (dgram->qc)
7683 dgram->qc->rx.bytes += dgram->len;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007684
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007685 /* This must never happen. */
7686 BUG_ON(pos > end);
7687 BUG_ON(pos < end || pos > dgram->buf + dgram->len);
7688 /* Mark this datagram as consumed */
7689 HA_ATOMIC_STORE(&dgram->buf, NULL);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007690
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007691 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7692 return 0;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007693
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007694 err:
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007695 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Amaury Denoyelle8687b632022-09-27 14:22:09 +02007696 return -1;
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007697}
7698
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01007699/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
7700 * CIDs. This can be used to determine if a datagram is addressed to the right
7701 * connection instance.
7702 *
7703 * Returns a boolean value.
7704 */
7705int qc_check_dcid(struct quic_conn *qc, unsigned char *dcid, size_t dcid_len)
7706{
7707 struct ebmb_node *node;
7708 struct quic_connection_id *id;
7709
7710 /* For ODCID, address is concatenated to it after qc.odcid.len so this
7711 * comparison is safe.
7712 */
7713 if ((qc->scid.len == dcid_len &&
7714 memcmp(qc->scid.data, dcid, dcid_len) == 0) ||
7715 (qc->odcid.len == dcid_len &&
Frédéric Lécaille07846cb2023-02-13 16:14:24 +01007716 memcmp(qc->odcid.data, dcid, dcid_len) == 0)) {
Amaury Denoyelle7c9fdd92022-11-16 11:01:02 +01007717 return 1;
7718 }
7719
7720 node = ebmb_lookup(&quic_dghdlrs[tid].cids, dcid, dcid_len);
7721 if (node) {
7722 id = ebmb_entry(node, struct quic_connection_id, node);
7723 if (qc == id->qc)
7724 return 1;
7725 }
7726
7727 return 0;
7728}
7729
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02007730/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
7731 * Returns 1 if succeeded, 0 if not.
7732 */
7733int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
7734 unsigned char **dcid, size_t *dcid_len)
7735{
7736 int ret = 0, long_header;
7737 size_t minlen, skip;
7738
7739 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
7740
7741 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
7742 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
7743 goto err;
7744 }
7745
7746 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
7747 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
7748 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
7749 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
7750 if (end - buf < minlen)
7751 goto err;
7752
7753 buf += skip;
7754 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
7755 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
7756 goto err;
7757
7758 *dcid = buf;
7759
7760 ret = 1;
7761 leave:
7762 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
7763 return ret;
7764
7765 err:
7766 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
7767 goto leave;
7768}
7769
7770/* Notify the MUX layer if alive about an imminent close of <qc>. */
7771void qc_notify_close(struct quic_conn *qc)
7772{
7773 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
7774
7775 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
7776 goto leave;
7777
7778 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
7779 /* wake up the MUX */
7780 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
7781 TRACE_STATE("connection closure notidfied to mux",
7782 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
7783 qc->conn->mux->wake(qc->conn);
7784 }
7785 else
7786 TRACE_STATE("connection closure not notidfied to mux",
7787 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
7788 leave:
7789 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
7790}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007791
Amaury Denoyellee0fe1182023-02-28 15:08:59 +01007792/* Wake-up upper layer if waiting for send to be ready.
7793 *
7794 * Returns 1 if upper layer has been woken up else 0.
7795 */
7796int qc_notify_send(struct quic_conn *qc)
7797{
7798 if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
7799 if (quic_path_prep_data(qc->path)) {
7800 tasklet_wakeup(qc->subs->tasklet);
7801 qc->subs->events &= ~SUB_RETRY_SEND;
7802 if (!qc->subs->events)
7803 qc->subs = NULL;
7804
7805 return 1;
7806 }
7807 }
7808
7809 return 0;
7810}
7811
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007812
7813/* appctx context used by "show quic" command */
7814struct show_quic_ctx {
7815 unsigned int epoch;
7816 struct bref bref; /* back-reference to the quic-conn being dumped */
7817 unsigned int thr;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01007818 int flags;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007819};
7820
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01007821#define QC_CLI_FL_SHOW_ALL 0x1 /* show closing/draining connections */
7822
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007823static int cli_parse_show_quic(char **args, char *payload, struct appctx *appctx, void *private)
7824{
7825 struct show_quic_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
7826
7827 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
7828 return 1;
7829
7830 ctx->epoch = _HA_ATOMIC_FETCH_ADD(&qc_epoch, 1);
7831 ctx->thr = 0;
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01007832 ctx->flags = 0;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007833
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01007834 if (*args[2] && strcmp(args[2], "all") == 0)
7835 ctx->flags |= QC_CLI_FL_SHOW_ALL;
7836
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007837 LIST_INIT(&ctx->bref.users);
7838
7839 return 0;
7840}
7841
7842static int cli_io_handler_dump_quic(struct appctx *appctx)
7843{
7844 struct show_quic_ctx *ctx = appctx->svcctx;
7845 struct stconn *sc = appctx_sc(appctx);
7846 struct quic_conn *qc;
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01007847 struct quic_enc_level *qel;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01007848 struct eb64_node *node;
7849 struct qc_stream_desc *stream;
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01007850 char bufaddr[INET6_ADDRSTRLEN], bufport[6];
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01007851 int expire;
7852 unsigned char cid_len;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007853
7854 thread_isolate();
7855
7856 if (ctx->thr >= global.nbthread)
7857 goto done;
7858
7859 if (unlikely(sc_ic(sc)->flags & CF_SHUTW)) {
7860 /* If we're forced to shut down, we might have to remove our
7861 * reference to the last stream being dumped.
7862 */
7863 if (!LIST_ISEMPTY(&ctx->bref.users))
7864 LIST_DEL_INIT(&ctx->bref.users);
7865 goto done;
7866 }
7867
7868 chunk_reset(&trash);
7869
7870 if (!LIST_ISEMPTY(&ctx->bref.users)) {
7871 /* Remove show_quic_ctx from previous quic_conn instance. */
7872 LIST_DEL_INIT(&ctx->bref.users);
7873 }
7874 else if (!ctx->bref.ref) {
7875 /* First invocation. */
7876 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
7877 }
7878
7879 while (1) {
7880 int done = 0;
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01007881 int i;
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007882
7883 if (ctx->bref.ref == &ha_thread_ctx[ctx->thr].quic_conns) {
7884 done = 1;
7885 }
7886 else {
7887 qc = LIST_ELEM(ctx->bref.ref, struct quic_conn *, el_th_ctx);
7888 if ((int)(qc->qc_epoch - ctx->epoch) > 0)
7889 done = 1;
7890 }
7891
7892 if (done) {
7893 ++ctx->thr;
7894 if (ctx->thr >= global.nbthread)
7895 break;
7896 ctx->bref.ref = ha_thread_ctx[ctx->thr].quic_conns.n;
7897 continue;
7898 }
7899
Amaury Denoyelle10a46de2023-02-09 18:18:45 +01007900 if (!(ctx->flags & QC_CLI_FL_SHOW_ALL) &&
Amaury Denoyelle3f9758e2023-02-01 17:31:02 +01007901 qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
7902 ctx->bref.ref = qc->el_th_ctx.n;
7903 continue;
7904 }
7905
Amaury Denoyelle58d9d5d2023-02-01 11:54:43 +01007906 /* CIDs */
7907 chunk_appendf(&trash, "* %p[%02u]: scid=", qc, qc->tid);
7908 for (cid_len = 0; cid_len < qc->scid.len; ++cid_len)
7909 chunk_appendf(&trash, "%02x", qc->scid.data[cid_len]);
7910 while (cid_len++ < 20)
7911 chunk_appendf(&trash, "..");
7912
7913 chunk_appendf(&trash, " dcid=");
7914 for (cid_len = 0; cid_len < qc->dcid.len; ++cid_len)
7915 chunk_appendf(&trash, "%02x", qc->dcid.data[cid_len]);
7916 while (cid_len++ < 20)
7917 chunk_appendf(&trash, "..");
7918
7919 chunk_appendf(&trash, "\n");
7920
7921 /* Connection state */
7922 if (qc->flags & QUIC_FL_CONN_CLOSING)
7923 chunk_appendf(&trash, " st=closing ");
7924 else if (qc->flags & QUIC_FL_CONN_DRAINING)
7925 chunk_appendf(&trash, " st=draining ");
7926 else if (qc->state < QUIC_HS_ST_CONFIRMED)
7927 chunk_appendf(&trash, " st=handshake ");
7928 else
7929 chunk_appendf(&trash, " st=opened ");
7930
7931 if (qc->mux_state == QC_MUX_NULL)
7932 chunk_appendf(&trash, "mux=null ");
7933 else if (qc->mux_state == QC_MUX_READY)
7934 chunk_appendf(&trash, "mux=ready ");
7935 else
7936 chunk_appendf(&trash, "mux=released ");
7937
7938 expire = qc->idle_timer_task->expire;
7939 chunk_appendf(&trash, "expire=%02ds ",
7940 expire > now_ms ? (expire - now_ms) / 1000 : 0);
7941
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007942 chunk_appendf(&trash, "\n");
7943
Amaury Denoyelleb89c0e22023-02-01 17:04:26 +01007944 /* Socket */
7945 chunk_appendf(&trash, " fd=%d", qc->fd);
7946 if (qc->local_addr.ss_family == AF_INET ||
7947 qc->local_addr.ss_family == AF_INET6) {
7948 addr_to_str(&qc->local_addr, bufaddr, sizeof(bufaddr));
7949 port_to_str(&qc->local_addr, bufport, sizeof(bufport));
7950 chunk_appendf(&trash, " from=%s:%s", bufaddr, bufport);
7951
7952 addr_to_str(&qc->peer_addr, bufaddr, sizeof(bufaddr));
7953 port_to_str(&qc->peer_addr, bufport, sizeof(bufport));
7954 chunk_appendf(&trash, " to=%s:%s", bufaddr, bufport);
7955 }
7956
7957 chunk_appendf(&trash, "\n");
7958
Amaury Denoyelle1b0fc432023-02-01 17:05:10 +01007959 /* Encryption levels */
7960 qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
7961 chunk_appendf(&trash, " [initl] rx.ackrng=%-6zu tx.inflight=%-6zu",
7962 qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight);
7963 qel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
7964 chunk_appendf(&trash, " [hndshk] rx.ackrng=%-6zu tx.inflight=%-6zu\n",
7965 qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight);
7966 qel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
7967 chunk_appendf(&trash, " [0-rtt] rx.ackrng=%-6zu tx.inflight=%-6zu",
7968 qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight);
7969 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
7970 chunk_appendf(&trash, " [1-rtt] rx.ackrng=%-6zu tx.inflight=%-6zu",
7971 qel->pktns->rx.arngs.sz, qel->pktns->tx.in_flight);
7972
7973 chunk_appendf(&trash, "\n");
7974
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01007975 /* Streams */
7976 node = eb64_first(&qc->streams_by_id);
7977 i = 0;
7978 while (node) {
7979 stream = eb64_entry(node, struct qc_stream_desc, by_id);
7980 node = eb64_next(node);
7981
Amaury Denoyellea9de25a2023-02-10 09:25:22 +01007982 chunk_appendf(&trash, " | stream=%-8llu", (unsigned long long)stream->by_id.key);
7983 chunk_appendf(&trash, " off=%-8llu ack=%-8llu",
7984 (unsigned long long)stream->buf_offset,
7985 (unsigned long long)stream->ack_offset);
Amaury Denoyelle2eda63b2023-02-01 17:05:36 +01007986
7987 if (!(++i % 3)) {
7988 chunk_appendf(&trash, "\n");
7989 i = 0;
7990 }
7991 }
7992
7993 chunk_appendf(&trash, "\n");
7994
Amaury Denoyelle15c74702023-02-01 10:18:26 +01007995 if (applet_putchk(appctx, &trash) == -1) {
7996 /* Register show_quic_ctx to quic_conn instance. */
7997 LIST_APPEND(&qc->back_refs, &ctx->bref.users);
7998 goto full;
7999 }
8000
8001 ctx->bref.ref = qc->el_th_ctx.n;
8002 }
8003
8004 done:
8005 thread_release();
8006 return 1;
8007
8008 full:
8009 thread_release();
8010 return 0;
8011}
8012
8013static void cli_release_show_quic(struct appctx *appctx)
8014{
8015 struct show_quic_ctx *ctx = appctx->svcctx;
8016
8017 if (ctx->thr < global.nbthread) {
8018 thread_isolate();
8019 if (!LIST_ISEMPTY(&ctx->bref.users))
8020 LIST_DEL_INIT(&ctx->bref.users);
8021 thread_release();
8022 }
8023}
8024
8025static struct cli_kw_list cli_kws = {{ }, {
8026 { { "show", "quic", NULL }, "show quic : display quic connections status", cli_parse_show_quic, cli_io_handler_dump_quic, cli_release_show_quic },
Frédéric Lécaille91376d62023-02-11 20:24:42 +01008027 {{},}
Amaury Denoyelle15c74702023-02-01 10:18:26 +01008028}};
8029
8030INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
8031
8032static void init_quic()
8033{
8034 int thr;
8035
8036 for (thr = 0; thr < MAX_THREADS; ++thr)
8037 LIST_INIT(&ha_thread_ctx[thr].quic_conns);
8038}
8039INITCALL0(STG_INIT, init_quic);
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +02008040
8041/*
8042 * Local variables:
8043 * c-indent-level: 8
8044 * c-basic-offset: 8
8045 * End:
8046 */