blob: 6f9284986812b80b3a617e8c08cd2f80f7f8b017 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020025#include <import/ebmbtree.h>
26
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010027#include <haproxy/buf-t.h>
28#include <haproxy/compat.h>
29#include <haproxy/api.h>
30#include <haproxy/debug.h>
31#include <haproxy/tools.h>
32#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010033
34#include <haproxy/connection.h>
35#include <haproxy/fd.h>
36#include <haproxy/freq_ctr.h>
37#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010038#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010039#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010041#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010042#include <haproxy/pipe.h>
43#include <haproxy/proxy.h>
44#include <haproxy/quic_cc.h>
45#include <haproxy/quic_frame.h>
46#include <haproxy/quic_loss.h>
Amaury Denoyellecfa2d562022-01-19 16:01:05 +010047#include <haproxy/quic_sock.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020048#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010049#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010050#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010051#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010052#include <haproxy/ssl_sock.h>
53#include <haproxy/stream_interface.h>
54#include <haproxy/task.h>
55#include <haproxy/trace.h>
56#include <haproxy/xprt_quic.h>
57
Amaury Denoyellea22d8602021-11-10 15:17:56 +010058/* list of supported QUIC versions by this implementation */
59static int quic_supported_version[] = {
60 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010061 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010062
63 /* placeholder, do not add entry after this */
64 0x0
65};
66
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020067/* This is the values of some QUIC transport parameters when absent.
68 * Should be used to initialize any transport parameters (local or remote)
69 * before updating them with customized values.
70 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010071struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020072 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020073 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
74 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020075 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010076};
77
78/* trace source and events */
79static void quic_trace(enum trace_level level, uint64_t mask, \
80 const struct trace_source *src,
81 const struct ist where, const struct ist func,
82 const void *a1, const void *a2, const void *a3, const void *a4);
83
84static const struct trace_event quic_trace_events[] = {
85 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
86 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
87 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
88 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
89 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
90 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
91 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050092 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010093 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
94 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
95 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
96 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
97 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
98 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
99 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
100 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
101 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
102 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
103 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
104 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
105 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
106 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
107 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
108 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
109 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
110 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
111 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100112 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
113 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
114 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
115 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
116 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
117 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100118 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200119 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
120 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100121 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
122 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100123 { /* end */ }
124};
125
126static const struct name_desc quic_trace_lockon_args[4] = {
127 /* arg1 */ { /* already used by the connection */ },
128 /* arg2 */ { .name="quic", .desc="QUIC transport" },
129 /* arg3 */ { },
130 /* arg4 */ { }
131};
132
133static const struct name_desc quic_trace_decoding[] = {
134#define QUIC_VERB_CLEAN 1
135 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
136 { /* end */ }
137};
138
139
140struct trace_source trace_quic = {
141 .name = IST("quic"),
142 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100143 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100144 .default_cb = quic_trace,
145 .known_events = quic_trace_events,
146 .lockon_args = quic_trace_lockon_args,
147 .decoding = quic_trace_decoding,
148 .report_events = ~0, /* report everything by default */
149};
150
151#define TRACE_SOURCE &trace_quic
152INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
153
154static BIO_METHOD *ha_quic_meth;
155
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200156DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100157DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200159 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100161DECLARE_POOL(pool_head_quic_connection_id,
162 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100163DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100164DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100165DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100166DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100167DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100168DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200169DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100170DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100171
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200172static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +0200173 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100174 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +0100175 int padding, int ack, int probe, int cc, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100176
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100177/* Only for debug purpose */
178struct enc_debug_info {
179 unsigned char *payload;
180 size_t payload_len;
181 unsigned char *aad;
182 size_t aad_len;
183 uint64_t pn;
184};
185
186/* Initializes a enc_debug_info struct (only for debug purpose) */
187static inline void enc_debug_info_init(struct enc_debug_info *edi,
188 unsigned char *payload, size_t payload_len,
189 unsigned char *aad, size_t aad_len, uint64_t pn)
190{
191 edi->payload = payload;
192 edi->payload_len = payload_len;
193 edi->aad = aad;
194 edi->aad_len = aad_len;
195 edi->pn = pn;
196}
197
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100198/* Trace callback for QUIC.
199 * These traces always expect that arg1, if non-null, is of type connection.
200 */
201static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
202 const struct ist where, const struct ist func,
203 const void *a1, const void *a2, const void *a3, const void *a4)
204{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100205 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100206
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100207 if (qc) {
208 const struct quic_tls_secrets *secs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100210 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100211 if ((mask & QUIC_EV_CONN_INIT) && qc) {
212 chunk_appendf(&trace_buf, "\n odcid");
213 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100214 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100216 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100217 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100218 }
219
220 if (mask & QUIC_EV_CONN_ADDDATA) {
221 const enum ssl_encryption_level_t *level = a2;
222 const size_t *len = a3;
223
224 if (level) {
225 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
226
227 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
228 }
229 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100230 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100231 }
232 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
233 /* Initial read & write secrets. */
234 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
235 const unsigned char *rx_sec = a2;
236 const unsigned char *tx_sec = a3;
237
238 secs = &qc->els[level].tls_ctx.rx;
239 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
240 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
241 if (rx_sec)
242 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
243 quic_tls_keys_hexdump(&trace_buf, secs);
244 }
245 secs = &qc->els[level].tls_ctx.tx;
246 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
247 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
248 if (tx_sec)
249 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
250 quic_tls_keys_hexdump(&trace_buf, secs);
251 }
252 }
253 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
254 const enum ssl_encryption_level_t *level = a2;
255 const unsigned char *secret = a3;
256 const size_t *secret_len = a4;
257
258 if (level) {
259 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
260
261 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
262 if (secret && secret_len)
263 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
264 secs = &qc->els[lvl].tls_ctx.rx;
265 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
266 quic_tls_keys_hexdump(&trace_buf, secs);
267 }
268 }
269
270 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
271 const enum ssl_encryption_level_t *level = a2;
272 const unsigned char *secret = a3;
273 const size_t *secret_len = a4;
274
275 if (level) {
276 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
277
278 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
279 if (secret && secret_len)
280 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
281 secs = &qc->els[lvl].tls_ctx.tx;
282 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
283 quic_tls_keys_hexdump(&trace_buf, secs);
284 }
285
286 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100287
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100288 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289 const struct quic_tx_packet *pkt = a2;
290 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100291 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100292
293 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100294 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100295 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100296 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100297 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100298 (unsigned long long)qc->path->cwnd,
299 (unsigned long long)qc->path->prep_in_flight,
300 (unsigned long long)qc->path->in_flight,
301 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100302 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100303 }
304 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200305 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100306 if (pkt->pn_node.key != (uint64_t)-1)
307 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100308 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100309 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100310 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
311 (unsigned long long)qc->rx.bytes,
312 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100313 }
314
315 if (room) {
316 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
317 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
318 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100319 }
320 }
321
322 if (mask & QUIC_EV_CONN_HDSHK) {
323 const enum quic_handshake_state *state = a2;
324 const int *err = a3;
325
326 if (state)
327 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
328 if (err)
329 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
330 }
331
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100332 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100333 const struct quic_rx_packet *pkt = a2;
334 const unsigned long *pktlen = a3;
335 const SSL *ssl = a4;
336
337 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100338 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100339 pkt, quic_packet_type_enc_level_char(pkt->type));
340 if (pkt->pnl)
341 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
342 (unsigned long long)pkt->pn);
343 if (pkt->token_len)
344 chunk_appendf(&trace_buf, " toklen=%llu",
345 (unsigned long long)pkt->token_len);
346 if (pkt->aad_len)
347 chunk_appendf(&trace_buf, " aadlen=%llu",
348 (unsigned long long)pkt->aad_len);
349 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
350 pkt->flags, (unsigned long long)pkt->len);
351 }
352 if (pktlen)
353 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
354 if (ssl) {
355 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
356 chunk_appendf(&trace_buf, " el=%c",
357 quic_enc_level_char(ssl_to_quic_enc_level(level)));
358 }
359 }
360
361 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
362 const struct quic_rx_packet *pkt = a2;
363 const struct quic_rx_crypto_frm *cf = a3;
364 const SSL *ssl = a4;
365
366 if (pkt)
367 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
368 quic_packet_type_enc_level_char(pkt->type),
369 (unsigned long long)pkt->pn);
370 if (cf)
371 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
372 (unsigned long long)cf->offset_node.key,
373 (unsigned long long)cf->len);
374 if (ssl) {
375 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200376 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100377 quic_enc_level_char(ssl_to_quic_enc_level(level)));
378 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100379
380 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100381 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100382 }
383
384 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
385 const struct quic_frame *frm = a2;
386
387 if (frm)
388 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
389 }
390
391 if (mask & QUIC_EV_CONN_PHPKTS) {
392 const struct quic_enc_level *qel = a2;
393
394 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100395 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100396 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100397 " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100398 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200399 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100400 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100401 (unsigned long long)qc->path->cwnd,
402 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100403 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100404 (unsigned long long)pktns->tx.in_flight,
405 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100406 }
407 }
408
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100409 if (mask & QUIC_EV_CONN_ENCPKT) {
410 const struct enc_debug_info *edi = a2;
411
412 if (edi)
413 chunk_appendf(&trace_buf,
414 " payload=@%p payload_len=%llu"
415 " aad=@%p aad_len=%llu pn=%llu",
416 edi->payload, (unsigned long long)edi->payload_len,
417 edi->aad, (unsigned long long)edi->aad_len,
418 (unsigned long long)edi->pn);
419 }
420
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100421 if (mask & QUIC_EV_CONN_RMHP) {
422 const struct quic_rx_packet *pkt = a2;
423
424 if (pkt) {
425 const int *ret = a3;
426
427 chunk_appendf(&trace_buf, " pkt@%p", pkt);
428 if (ret && *ret)
429 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
430 pkt->pnl, (unsigned long long)pkt->pn);
431 }
432 }
433
434 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200435 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100436 const unsigned long *val1 = a3;
437 const unsigned long *val2 = a4;
438
439 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100440 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100441 if (val1)
442 chunk_appendf(&trace_buf, " %lu", *val1);
443 if (val2)
444 chunk_appendf(&trace_buf, "..%lu", *val2);
445 }
446
447 if (mask & QUIC_EV_CONN_RTTUPDT) {
448 const unsigned int *rtt_sample = a2;
449 const unsigned int *ack_delay = a3;
450 const struct quic_loss *ql = a4;
451
452 if (rtt_sample)
453 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
454 if (ack_delay)
455 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
456 if (ql)
457 chunk_appendf(&trace_buf,
458 " srtt=%ums rttvar=%ums min_rtt=%ums",
459 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
460 }
461 if (mask & QUIC_EV_CONN_CC) {
462 const struct quic_cc_event *ev = a2;
463 const struct quic_cc *cc = a3;
464
465 if (a2)
466 quic_cc_event_trace(&trace_buf, ev);
467 if (a3)
468 quic_cc_state_trace(&trace_buf, cc);
469 }
470
471 if (mask & QUIC_EV_CONN_PKTLOSS) {
472 const struct quic_pktns *pktns = a2;
473 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100474
475 if (pktns) {
476 chunk_appendf(&trace_buf, " pktns=%s",
477 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
478 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
479 if (pktns->tx.loss_time)
480 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100481 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100482 }
483 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
484 struct quic_tx_packet *pkt;
485
486 chunk_appendf(&trace_buf, " lost_pkts:");
487 list_for_each_entry(pkt, lost_pkts, list)
488 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
489 }
490 }
491
492 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100493 const struct quic_pktns *pktns = a2;
494 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100495 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100496
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100497 if (ifae_pkts)
498 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
499 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100501 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100502 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100503 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
504 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100505 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
506 if (pktns->tx.in_flight)
507 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508 if (pktns->tx.loss_time)
509 chunk_appendf(&trace_buf, " loss_time=%dms",
510 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
511 }
512 if (mask & QUIC_EV_CONN_SPTO) {
513 if (pktns->tx.time_of_last_eliciting)
514 chunk_appendf(&trace_buf, " tole=%dms",
515 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
516 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100517 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100518 }
519 }
520
521 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
522 chunk_appendf(&trace_buf,
523 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
524 }
525 }
526
527 if (mask & QUIC_EV_CONN_SPPKTS) {
528 const struct quic_tx_packet *pkt = a2;
529
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100530 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
531 (unsigned long long)qc->path->cwnd,
532 (unsigned long long)qc->path->prep_in_flight,
533 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100535 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100536 (unsigned long)pkt->pn_node.key,
537 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
538 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100539 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100540 }
541 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100542
543 if (mask & QUIC_EV_CONN_SSLALERT) {
544 const uint8_t *alert = a2;
545 const enum ssl_encryption_level_t *level = a3;
546
547 if (alert)
548 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
549 if (level)
550 chunk_appendf(&trace_buf, " el=%c",
551 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
552 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100553
554 if (mask & QUIC_EV_CONN_BCFRMS) {
555 const size_t *sz1 = a2;
556 const size_t *sz2 = a3;
557 const size_t *sz3 = a4;
558
559 if (sz1)
560 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
561 if (sz2)
562 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
563 if (sz3)
564 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
565 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100566
567 if (mask & QUIC_EV_CONN_PSTRM) {
568 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100569
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100570 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100571 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100572 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100573 }
574 if (mask & QUIC_EV_CONN_LPKT) {
575 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200576 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100577
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100578 if (pkt) {
579 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
580 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
581 if (pkt->pn_node.key != (uint64_t)-1)
582 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
583 }
584
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200585 if (len)
586 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587 }
588
589}
590
591/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100592static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200594 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100595
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100596 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100597 return 1;
598
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200599 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
600 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
601 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
602 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200603 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100604 return 1;
605
606 return 0;
607}
608
609/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
610 * both loss detection and PTO and schedule the task assiated to this timer if needed.
611 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100612static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100614 struct quic_pktns *pktns;
615 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200616 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100617
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100618 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100619 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100620
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100621 pktns = quic_loss_pktns(qc);
622 if (tick_isset(pktns->tx.loss_time)) {
623 qc->timer = pktns->tx.loss_time;
624 goto out;
625 }
626
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100627 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100628 * cancelled for a server which reached the anti-amplification limit.
629 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100630 if (!quic_peer_validated_addr(qc) &&
631 (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100632 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100633 qc->timer = TICK_ETERNITY;
634 goto out;
635 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100636
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100637 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100638 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100639 /* Timer cancellation. */
640 qc->timer = TICK_ETERNITY;
641 goto out;
642 }
643
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200644 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
645 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100646 if (tick_isset(pto))
647 qc->timer = pto;
648 out:
Amaury Denoyelle0a29e132021-12-23 15:06:56 +0100649 if (qc->timer_task && qc->timer != TICK_ETERNITY)
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200650 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100651 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100652}
653
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100654/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
655 * connection.
656 * Return 1 if succeeded, 0 if not.
657 */
658static int quic_tls_key_update(struct quic_conn *qc)
659{
660 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
661 struct quic_tls_secrets *rx, *tx;
662 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
663 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
664
665 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
666 rx = &tls_ctx->rx;
667 tx = &tls_ctx->tx;
668 nxt_rx = &qc->ku.nxt_rx;
669 nxt_tx = &qc->ku.nxt_tx;
670
671 /* Prepare new RX secrets */
672 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
673 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100674 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100675 return 0;
676 }
677
678 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
679 nxt_rx->key, nxt_rx->keylen,
680 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
681 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100682 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100683 return 0;
684 }
685
686 /* Prepare new TX secrets */
687 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
688 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100689 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100690 return 0;
691 }
692
693 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
694 nxt_tx->key, nxt_tx->keylen,
695 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
696 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100697 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100698 return 0;
699 }
700
701 return 1;
702}
703
704/* Rotate the Key Update information for <qc> QUIC connection.
705 * Must be used after having updated them.
706 * Always succeeds.
707 */
708static void quic_tls_rotate_keys(struct quic_conn *qc)
709{
710 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
711 unsigned char *curr_secret, *curr_iv, *curr_key;
712
713 /* Rotate the RX secrets */
714 curr_secret = tls_ctx->rx.secret;
715 curr_iv = tls_ctx->rx.iv;
716 curr_key = tls_ctx->rx.key;
717
718 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
719 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
720 tls_ctx->rx.key = qc->ku.nxt_rx.key;
721
722 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
723 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
724 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
725
726 qc->ku.prv_rx.secret = curr_secret;
727 qc->ku.prv_rx.iv = curr_iv;
728 qc->ku.prv_rx.key = curr_key;
729 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
730
731 /* Update the TX secrets */
732 curr_secret = tls_ctx->tx.secret;
733 curr_iv = tls_ctx->tx.iv;
734 curr_key = tls_ctx->tx.key;
735
736 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
737 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
738 tls_ctx->tx.key = qc->ku.nxt_tx.key;
739
740 qc->ku.nxt_tx.secret = curr_secret;
741 qc->ku.nxt_tx.iv = curr_iv;
742 qc->ku.nxt_tx.key = curr_key;
743}
744
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100745#ifndef OPENSSL_IS_BORINGSSL
746int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
747 const uint8_t *read_secret,
748 const uint8_t *write_secret, size_t secret_len)
749{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100750 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
751 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100752 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100753 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100754
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100755 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100756 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100757 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
758 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100759 goto out;
760 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100761
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100762 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100763 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100764 goto err;
765 }
766
767 rx = &tls_ctx->rx;
768 tx = &tls_ctx->tx;
769
770 rx->aead = tx->aead = tls_aead(cipher);
771 rx->md = tx->md = tls_md(cipher);
772 rx->hp = tx->hp = tls_hp(cipher);
773
774 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
775 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100776 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100777 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100778 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100779 }
780
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100781 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100782 /* Enqueue this connection asap if we could derive O-RTT secrets as
783 * listener. Note that a listener derives only RX secrets for this
784 * level.
785 */
786 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
787 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100788
789 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100790 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100791
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100792 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
793 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100794 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100795 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100796 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100797 }
798
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100799 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100800 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100801 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
802 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
803 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
804
805 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
806 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100807 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100808 goto err;
809 }
810
811 memcpy(rx->secret, read_secret, secret_len);
812 rx->secretlen = secret_len;
813 memcpy(tx->secret, write_secret, secret_len);
814 tx->secretlen = secret_len;
815 /* Initialize all the secret keys lengths */
816 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
817 /* Prepare the next key update */
818 if (!quic_tls_key_update(qc))
819 goto err;
820 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100821
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100822 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100823 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100824 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100825
826 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100827 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100828 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100829}
830#else
831/* ->set_read_secret callback to derive the RX secrets at <level> encryption
832 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500833 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100834 */
835int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
836 const SSL_CIPHER *cipher,
837 const uint8_t *secret, size_t secret_len)
838{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100839 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100840 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100841 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100842
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100843 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
844 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
845 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100846 goto out;
847 }
848
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100849 tls_ctx->rx.aead = tls_aead(cipher);
850 tls_ctx->rx.md = tls_md(cipher);
851 tls_ctx->rx.hp = tls_hp(cipher);
852
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100853 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
854 goto err;
855
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100856 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100857 tls_ctx->rx.key, tls_ctx->rx.keylen,
858 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100859 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
860 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100861 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100862 goto err;
863 }
864
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100865 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100866 const unsigned char *buf;
867 size_t buflen;
868
869 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
870 if (!buflen)
871 goto err;
872
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100873 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100874 goto err;
875 }
876
877 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100878 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100879 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100880
881 return 1;
882
883 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100884 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100885 return 0;
886}
887
888/* ->set_write_secret callback to derive the TX secrets at <level>
889 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500890 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100891 */
892int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
893 const SSL_CIPHER *cipher,
894 const uint8_t *secret, size_t secret_len)
895{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100896 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
897 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100898
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100899 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
900 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
901 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100902 goto out;
903 }
904
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100905 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
906 goto err;
907
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100908 tls_ctx->tx.aead = tls_aead(cipher);
909 tls_ctx->tx.md = tls_md(cipher);
910 tls_ctx->tx.hp = tls_hp(cipher);
911
912 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100913 tls_ctx->tx.key, tls_ctx->tx.keylen,
914 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100915 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
916 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100917 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100918 goto err;
919 }
920
921 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100922 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100923 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100924 return 1;
925
926 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100927 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100928 return 0;
929}
930#endif
931
932/* This function copies the CRYPTO data provided by the TLS stack found at <data>
933 * with <len> as size in CRYPTO buffers dedicated to store the information about
934 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
935 * It fails only if it could not managed to allocate enough CRYPTO buffers to
936 * store all the data.
937 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
938 */
939static int quic_crypto_data_cpy(struct quic_enc_level *qel,
940 const unsigned char *data, size_t len)
941{
942 struct quic_crypto_buf **qcb;
943 /* The remaining byte to store in CRYPTO buffers. */
944 size_t cf_offset, cf_len, *nb_buf;
945 unsigned char *pos;
946
947 nb_buf = &qel->tx.crypto.nb_buf;
948 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
949 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
950 cf_len = len;
951
952 while (len) {
953 size_t to_copy, room;
954
955 pos = (*qcb)->data + (*qcb)->sz;
956 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
957 to_copy = len > room ? room : len;
958 if (to_copy) {
959 memcpy(pos, data, to_copy);
960 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
961 qel->tx.crypto.sz += to_copy;
962 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100963 len -= to_copy;
964 data += to_copy;
965 }
966 else {
967 struct quic_crypto_buf **tmp;
968
969 tmp = realloc(qel->tx.crypto.bufs,
970 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
971 if (tmp) {
972 qel->tx.crypto.bufs = tmp;
973 qcb = &qel->tx.crypto.bufs[*nb_buf];
974 *qcb = pool_alloc(pool_head_quic_crypto_buf);
975 if (!*qcb)
976 return 0;
977
978 (*qcb)->sz = 0;
979 ++*nb_buf;
980 }
981 else {
982 break;
983 }
984 }
985 }
986
987 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
988 * have been buffered.
989 */
990 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200991 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100992 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100993
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100994 /* There is at most one CRYPTO frame in this packet number
995 * space. Let's look for it.
996 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +0100997 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100998 if (frm->type != QUIC_FT_CRYPTO)
999 continue;
1000
1001 /* Found */
1002 found = frm;
1003 break;
1004 }
1005
1006 if (found) {
1007 found->crypto.len += cf_len;
1008 }
1009 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001010 frm = pool_alloc(pool_head_quic_frame);
1011 if (!frm)
1012 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001013
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001014 frm->type = QUIC_FT_CRYPTO;
1015 frm->crypto.offset = cf_offset;
1016 frm->crypto.len = cf_len;
1017 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001018 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001019 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001020 }
1021
1022 return len == 0;
1023}
1024
1025
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001026/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1027void quic_set_tls_alert(struct quic_conn *qc, int alert)
1028{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001029 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001030 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001031 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001032}
1033
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001034/* Set the application for <qc> QUIC connection.
1035 * Return 1 if succeeded, 0 if not.
1036 */
1037int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1038{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001039 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1040 qc->app_ops = &h3_ops;
1041 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1042 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001043 else
1044 return 0;
1045
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001046 return 1;
1047}
1048
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001049/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1050 * wants to provide the QUIC layer with CRYPTO data.
1051 * Returns 1 if succeeded, 0 if not.
1052 */
1053int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1054 const uint8_t *data, size_t len)
1055{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001056 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001057 enum quic_tls_enc_level tel;
1058 struct quic_enc_level *qel;
1059
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001060 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1061 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1062 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1063 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001064 goto out;
1065 }
1066
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001067 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001068 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001069 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001070 goto err;
1071 }
1072
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001073 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001074 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001075 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001076 goto err;
1077 }
1078
1079 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001080 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001081
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001082 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001083 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001084 return 1;
1085
1086 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001087 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001088 return 0;
1089}
1090
1091int ha_quic_flush_flight(SSL *ssl)
1092{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001093 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001094
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001095 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1096 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001097
1098 return 1;
1099}
1100
1101int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1102{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001103 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001104
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001105 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1106 quic_set_tls_alert(qc, alert);
1107 HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001108 return 1;
1109}
1110
1111/* QUIC TLS methods */
1112static SSL_QUIC_METHOD ha_quic_method = {
1113#ifdef OPENSSL_IS_BORINGSSL
1114 .set_read_secret = ha_set_rsec,
1115 .set_write_secret = ha_set_wsec,
1116#else
1117 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1118#endif
1119 .add_handshake_data = ha_quic_add_handshake_data,
1120 .flush_flight = ha_quic_flush_flight,
1121 .send_alert = ha_quic_send_alert,
1122};
1123
1124/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1125 * Returns an error count.
1126 */
1127int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1128{
1129 struct proxy *curproxy = bind_conf->frontend;
1130 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1131 int cfgerr = 0;
1132
1133#if 0
1134 /* XXX Did not manage to use this. */
1135 const char *ciphers =
1136 "TLS_AES_128_GCM_SHA256:"
1137 "TLS_AES_256_GCM_SHA384:"
1138 "TLS_CHACHA20_POLY1305_SHA256:"
1139 "TLS_AES_128_CCM_SHA256";
1140#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001141 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001142 long options =
1143 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1144 SSL_OP_SINGLE_ECDH_USE |
1145 SSL_OP_CIPHER_SERVER_PREFERENCE;
1146 SSL_CTX *ctx;
1147
1148 ctx = SSL_CTX_new(TLS_server_method());
1149 bind_conf->initial_ctx = ctx;
1150
1151 SSL_CTX_set_options(ctx, options);
1152#if 0
1153 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1154 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1155 "for bind '%s' at [%s:%d].\n",
1156 curproxy->id, ciphers,
1157 bind_conf->arg, bind_conf->file, bind_conf->line);
1158 cfgerr++;
1159 }
1160#endif
1161
1162 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1163 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1164 "for bind '%s' at [%s:%d].\n",
1165 curproxy->id, groups,
1166 bind_conf->arg, bind_conf->file, bind_conf->line);
1167 cfgerr++;
1168 }
1169
1170 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1171 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1172 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1173 SSL_CTX_set_default_verify_paths(ctx);
1174
1175#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1176#ifdef OPENSSL_IS_BORINGSSL
1177 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1178 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1179#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1180 if (bind_conf->ssl_conf.early_data) {
1181 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001182 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001183 }
1184 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1185 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1186#else
1187 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1188#endif
1189 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1190#endif
1191 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1192
1193 return cfgerr;
1194}
1195
1196/* Decode an expected packet number from <truncated_on> its truncated value,
1197 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1198 * the number of bits used to encode this packet number (its length in bytes * 8).
1199 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1200 */
1201static uint64_t decode_packet_number(uint64_t largest_pn,
1202 uint32_t truncated_pn, unsigned int pn_nbits)
1203{
1204 uint64_t expected_pn = largest_pn + 1;
1205 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1206 uint64_t pn_hwin = pn_win / 2;
1207 uint64_t pn_mask = pn_win - 1;
1208 uint64_t candidate_pn;
1209
1210
1211 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1212 /* Note that <pn_win> > <pn_hwin>. */
1213 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1214 candidate_pn + pn_hwin <= expected_pn)
1215 return candidate_pn + pn_win;
1216
1217 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1218 return candidate_pn - pn_win;
1219
1220 return candidate_pn;
1221}
1222
1223/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1224 * cryptographic context.
1225 * <largest_pn> is the largest received packet number and <pn> the address of
1226 * the packet number field for this packet with <byte0> address of its first byte.
1227 * <end> points to one byte past the end of this packet.
1228 * Returns 1 if succeeded, 0 if not.
1229 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001230static int qc_do_rm_hp(struct quic_conn *qc,
1231 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001232 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001233 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001234{
1235 int ret, outlen, i, pnlen;
1236 uint64_t packet_number;
1237 uint32_t truncated_pn = 0;
1238 unsigned char mask[5] = {0};
1239 unsigned char *sample;
1240 EVP_CIPHER_CTX *cctx;
1241 unsigned char *hp_key;
1242
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001243 /* Check there is enough data in this packet. */
1244 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001245 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001246 return 0;
1247 }
1248
1249 cctx = EVP_CIPHER_CTX_new();
1250 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001251 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001252 return 0;
1253 }
1254
1255 ret = 0;
1256 sample = pn + QUIC_PACKET_PN_MAXLEN;
1257
1258 hp_key = tls_ctx->rx.hp_key;
1259 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1260 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1261 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001262 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001263 goto out;
1264 }
1265
1266 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1267 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1268 for (i = 0; i < pnlen; i++) {
1269 pn[i] ^= mask[i + 1];
1270 truncated_pn = (truncated_pn << 8) | pn[i];
1271 }
1272
1273 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1274 /* Store remaining information for this unprotected header */
1275 pkt->pn = packet_number;
1276 pkt->pnl = pnlen;
1277
1278 ret = 1;
1279
1280 out:
1281 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001282
1283 return ret;
1284}
1285
1286/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1287 * address, with <payload_len> as payload length, <aad> as address of
1288 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1289 * context.
1290 * Returns 1 if succeeded, 0 if not.
1291 */
1292static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1293 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001294 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001295{
1296 unsigned char iv[12];
1297 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001298 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001299 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001300
1301 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001302 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001303 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001304 }
1305
1306 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1307 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001308 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001309 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001310 }
1311
1312 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001313
1314 err:
1315 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001316 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001317 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001318}
1319
1320/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1321 * Returns 1 if succeeded, 0 if not.
1322 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001323static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001324{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001325 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001326 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001327 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001328 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001329 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1330 unsigned char *rx_key = tls_ctx->rx.key;
1331
1332 kp_changed = 0;
1333 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1334 /* The two tested bits are not at the same position,
1335 * this is why they are first both inversed.
1336 */
1337 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1338 if (pkt->pn < tls_ctx->rx.pn) {
1339 /* The lowest packet number of a previous key phase
1340 * cannot be null if it really stores previous key phase
1341 * secrets.
1342 */
1343 if (!pkt->qc->ku.prv_rx.pn)
1344 return 0;
1345
1346 rx_iv = pkt->qc->ku.prv_rx.iv;
1347 rx_key = pkt->qc->ku.prv_rx.key;
1348 }
1349 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1350 /* Next key phase */
1351 kp_changed = 1;
1352 rx_iv = pkt->qc->ku.nxt_rx.iv;
1353 rx_key = pkt->qc->ku.nxt_rx.key;
1354 }
1355 }
1356 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001357
1358 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1359 return 0;
1360
1361 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1362 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001363 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001364 if (!ret)
1365 return 0;
1366
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001367 /* Update the keys only if the packet decryption succeeded. */
1368 if (kp_changed) {
1369 quic_tls_rotate_keys(pkt->qc);
1370 /* Toggle the Key Phase bit */
1371 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1372 /* Store the lowest packet number received for the current key phase */
1373 tls_ctx->rx.pn = pkt->pn;
1374 /* Prepare the next key update */
1375 if (!quic_tls_key_update(pkt->qc))
1376 return 0;
1377 }
1378
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001379 /* Update the packet length (required to parse the frames). */
1380 pkt->len = pkt->aad_len + ret;
1381
1382 return 1;
1383}
1384
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001385/* Remove from <qcs> stream the acknowledged frames.
1386 * Never fails.
1387 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001388static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001389{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001390 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001391 struct eb64_node *frm_node;
1392
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001393 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001394 frm_node = eb64_first(&qcs->tx.acked_frms);
1395 while (frm_node) {
1396 struct quic_stream *strm;
1397
1398 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001399 if (strm->offset.key > qcs->tx.ack_offset)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001400 break;
1401
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001402 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1403 const size_t diff = strm->offset.key + strm->len -
1404 qcs->tx.ack_offset;
1405 qcs->tx.ack_offset += diff;
1406 b_del(strm->buf, diff);
1407 ret = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001408
1409 if (!b_data(strm->buf)) {
1410 b_free(strm->buf);
1411 offer_buffers(NULL, 1);
1412 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001413 }
1414
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001415 frm_node = eb64_next(frm_node);
1416 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001417
1418 /* TODO
1419 *
1420 * memleak: The quic_frame container of the quic_stream should
1421 * be liberated here, as in qc_treat_acked_tx_frm. However this
1422 * code seems to cause a bug which can lead to interrupted
1423 * transfers.
1424 *
1425 * struct quic_frame frm = container_of(strm, struct quic_frame, stream);
1426 * LIST_DELETE(&frm->list);
1427 * pool_free(pool_head_quic_frame, frm);
1428 */
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001429 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001430
1431 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001432}
1433
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001434/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001435static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1436 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001437{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001438 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001439
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001440 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001441 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001442 switch (frm->type) {
1443 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1444 {
1445 struct qcs *qcs = frm->stream.qcs;
1446 struct quic_stream *strm = &frm->stream;
1447
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001448 if (strm->offset.key <= qcs->tx.ack_offset) {
1449 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1450 const size_t diff = strm->offset.key + strm->len -
1451 qcs->tx.ack_offset;
1452 qcs->tx.ack_offset += diff;
1453 b_del(strm->buf, diff);
1454 stream_acked = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001455
1456 if (!b_data(strm->buf)) {
1457 b_free(strm->buf);
1458 offer_buffers(NULL, 1);
1459 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001460 }
1461
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001462 LIST_DELETE(&frm->list);
1463 pool_free(pool_head_quic_frame, frm);
1464 }
1465 else {
1466 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1467 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001468
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001469 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001470 }
1471 break;
1472 default:
1473 LIST_DELETE(&frm->list);
1474 pool_free(pool_head_quic_frame, frm);
1475 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001476
1477 if (stream_acked) {
1478 struct qcc *qcc = qc->qcc;
1479
1480 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1481 tasklet_wakeup(qcc->subs->tasklet);
1482 qcc->subs->events &= ~SUB_RETRY_SEND;
1483 if (!qcc->subs->events)
1484 qcc->subs = NULL;
1485 }
1486 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001487}
1488
1489/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1490 * deallocating them, and their TX frames.
1491 * Returns the last node reached to be used for the next range.
1492 * May be NULL if <largest> node could not be found.
1493 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001494static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1495 struct eb_root *pkts,
1496 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001497 struct list *newly_acked_pkts,
1498 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001499 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001500{
1501 struct eb64_node *node;
1502 struct quic_tx_packet *pkt;
1503
1504 if (largest_node)
1505 node = largest_node;
1506 else {
1507 node = eb64_lookup(pkts, largest);
1508 while (!node && largest > smallest) {
1509 node = eb64_lookup(pkts, --largest);
1510 }
1511 }
1512
1513 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001514 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001515
1516 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1517 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001518 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001519 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001520 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001521 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001522 node = eb64_prev(node);
1523 eb64_delete(&pkt->pn_node);
1524 }
1525
1526 return node;
1527}
1528
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001529/* Remove all frames from <pkt_frm_list> and reinsert them in the
1530 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001531 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001532static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1533 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001534 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001535{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001536 struct quic_frame *frm, *frmbak;
1537 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001538
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001539 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1540 LIST_DELETE(&frm->list);
1541 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001542 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001543 }
1544
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001545 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001546}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001547
1548/* Free the TX packets of <pkts> list */
1549static inline void free_quic_tx_pkts(struct list *pkts)
1550{
1551 struct quic_tx_packet *pkt, *tmp;
1552
1553 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001554 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001555 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001556 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001557 }
1558}
1559
1560/* Send a packet loss event nofification to the congestion controller
1561 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1562 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1563 * at <now_us> current time.
1564 * Always succeeds.
1565 */
1566static inline void qc_cc_loss_event(struct quic_conn *qc,
1567 unsigned int lost_bytes,
1568 unsigned int newest_time_sent,
1569 unsigned int period,
1570 unsigned int now_us)
1571{
1572 struct quic_cc_event ev = {
1573 .type = QUIC_CC_EVT_LOSS,
1574 .loss.now_ms = now_ms,
1575 .loss.max_ack_delay = qc->max_ack_delay,
1576 .loss.lost_bytes = lost_bytes,
1577 .loss.newest_time_sent = newest_time_sent,
1578 .loss.period = period,
1579 };
1580
1581 quic_cc_event(&qc->path->cc, &ev);
1582}
1583
1584/* Send a packet ack event nofication for each newly acked packet of
1585 * <newly_acked_pkts> list and free them.
1586 * Always succeeds.
1587 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001588static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001589 struct list *newly_acked_pkts)
1590{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001591 struct quic_tx_packet *pkt, *tmp;
1592 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1593
1594 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1595 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001596 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001597 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001598 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001599 ev.ack.acked = pkt->in_flight_len;
1600 ev.ack.time_sent = pkt->time_sent;
1601 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001602 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001603 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001604 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001605 }
1606
1607}
1608
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001609/* Release all the frames attached to <pktns> packet number space */
1610static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1611{
1612 struct quic_frame *frm, *frmbak;
1613
1614 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1615 LIST_DELETE(&frm->list);
1616 pool_free(pool_head_quic_frame, frm);
1617 }
1618}
1619
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620/* Handle <pkts> list of lost packets detected at <now_us> handling
1621 * their TX frames.
1622 * Send a packet loss event to the congestion controller if
1623 * in flight packet have been lost.
1624 * Also frees the packet in <pkts> list.
1625 * Never fails.
1626 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001627static inline void qc_release_lost_pkts(struct quic_conn *qc,
1628 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001629 struct list *pkts,
1630 uint64_t now_us)
1631{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001632 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001633 uint64_t lost_bytes;
1634
1635 lost_bytes = 0;
1636 oldest_lost = newest_lost = NULL;
1637 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001638 struct list tmp = LIST_HEAD_INIT(tmp);
1639
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001640 lost_bytes += pkt->in_flight_len;
1641 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001642 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001643 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001644 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001645 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001646 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001647 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001648 if (!oldest_lost) {
1649 oldest_lost = newest_lost = pkt;
1650 }
1651 else {
1652 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001653 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001654 newest_lost = pkt;
1655 }
1656 }
1657
1658 if (lost_bytes) {
1659 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001660 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001661 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001662 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001663 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001664 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001665 }
1666}
1667
1668/* Look for packet loss from sent packets for <qel> encryption level of a
1669 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1670 * their tree if deemed as lost or set the <loss_time> value the packet number
1671 * space if any not deemed lost.
1672 * Should be called after having received an ACK frame with newly acknowledged
1673 * packets or when the the loss detection timer has expired.
1674 * Always succeeds.
1675 */
1676static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1677 struct quic_conn *qc,
1678 struct list *lost_pkts)
1679{
1680 struct eb_root *pkts;
1681 struct eb64_node *node;
1682 struct quic_loss *ql;
1683 unsigned int loss_delay;
1684
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001685 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001686 pkts = &pktns->tx.pkts;
1687 pktns->tx.loss_time = TICK_ETERNITY;
1688 if (eb_is_empty(pkts))
1689 goto out;
1690
1691 ql = &qc->path->loss;
1692 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001693 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1694
1695 node = eb64_first(pkts);
1696 while (node) {
1697 struct quic_tx_packet *pkt;
1698 int64_t largest_acked_pn;
1699 unsigned int loss_time_limit, time_sent;
1700
1701 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001702 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001703 node = eb64_next(node);
1704 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1705 break;
1706
1707 time_sent = pkt->time_sent;
1708 loss_time_limit = tick_add(time_sent, loss_delay);
1709 if (tick_is_le(time_sent, now_ms) ||
1710 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1711 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001712 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001713 }
1714 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001715 if (tick_isset(pktns->tx.loss_time))
1716 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1717 else
1718 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001719 }
1720 }
1721
1722 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001723 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001724}
1725
1726/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1727 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001728 * if the largest acked packet was newly acked and if there was at least one newly
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001729 * acked ack-eliciting packet.
1730 * Return 1, if succeeded, 0 if not.
1731 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001732static inline int qc_parse_ack_frm(struct quic_conn *qc,
1733 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001734 struct quic_enc_level *qel,
1735 unsigned int *rtt_sample,
1736 const unsigned char **pos, const unsigned char *end)
1737{
1738 struct quic_ack *ack = &frm->ack;
1739 uint64_t smallest, largest;
1740 struct eb_root *pkts;
1741 struct eb64_node *largest_node;
1742 unsigned int time_sent, pkt_flags;
1743 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1744 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1745
1746 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1747 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001748 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001749 goto err;
1750 }
1751
1752 if (ack->first_ack_range > ack->largest_ack) {
1753 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001754 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001755 goto err;
1756 }
1757
1758 largest = ack->largest_ack;
1759 smallest = largest - ack->first_ack_range;
1760 pkts = &qel->pktns->tx.pkts;
1761 pkt_flags = 0;
1762 largest_node = NULL;
1763 time_sent = 0;
1764
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001765 if ((int64_t)ack->largest_ack > HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001766 largest_node = eb64_lookup(pkts, largest);
1767 if (!largest_node) {
1768 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001769 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001770 }
1771 else {
1772 time_sent = eb64_entry(&largest_node->node,
1773 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001774 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001775 }
1776
1777 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001778 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001779 do {
1780 uint64_t gap, ack_range;
1781
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001782 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1783 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001784 if (!ack->ack_range_num--)
1785 break;
1786
1787 if (!quic_dec_int(&gap, pos, end))
1788 goto err;
1789
1790 if (smallest < gap + 2) {
1791 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001792 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001793 goto err;
1794 }
1795
1796 largest = smallest - gap - 2;
1797 if (!quic_dec_int(&ack_range, pos, end))
1798 goto err;
1799
1800 if (largest < ack_range) {
1801 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001802 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001803 goto err;
1804 }
1805
1806 /* Do not use this node anymore. */
1807 largest_node = NULL;
1808 /* Next range */
1809 smallest = largest - ack_range;
1810
1811 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001812 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001813 } while (1);
1814
1815 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001816 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001817
1818 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1819 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001820 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001821 }
1822
1823 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1824 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001825 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001826 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001827 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001828 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001829 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1830 if (quic_peer_validated_addr(qc))
1831 qc->path->loss.pto_count = 0;
1832 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001833 }
1834
1835
1836 return 1;
1837
1838 err:
1839 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001840 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001841 return 0;
1842}
1843
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001844/* This function gives the detail of the SSL error. It is used only
1845 * if the debug mode and the verbose mode are activated. It dump all
1846 * the SSL error until the stack was empty.
1847 */
1848static forceinline void qc_ssl_dump_errors(struct connection *conn)
1849{
1850 if (unlikely(global.mode & MODE_DEBUG)) {
1851 while (1) {
1852 unsigned long ret;
1853
1854 ret = ERR_get_error();
1855 if (!ret)
1856 return;
1857
1858 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1859 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1860 }
1861 }
1862}
1863
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001864int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1865 const char **str, int *len);
1866
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001867/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1868 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001869 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001870 * Return 1 if succeeded, 0 if not.
1871 */
1872static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001873 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001874 const unsigned char *data, size_t len,
1875 struct quic_rx_packet *pkt,
1876 struct quic_rx_crypto_frm *cf)
1877{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001878 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001879 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001880
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001881 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001882 qc = ctx->qc;
1883
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001884 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1885
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001886 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1887 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001888 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001889 goto err;
1890 }
1891
1892 el->rx.crypto.offset += len;
1893 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001894 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001895
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001896 state = HA_ATOMIC_LOAD(&qc->state);
1897 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001898 ssl_err = SSL_do_handshake(ctx->ssl);
1899 if (ssl_err != 1) {
1900 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1901 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1902 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001903 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 goto out;
1905 }
1906
1907 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001908 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001909 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001910 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001911 goto err;
1912 }
1913
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001914 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001915 if (qc_is_listener(ctx->qc)) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001916 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001917 /* The connection is ready to be accepted. */
1918 quic_accept_push_qc(qc);
1919 }
1920 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001921 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001922 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001923 } else {
1924 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1925 if (ssl_err != 1) {
1926 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1927 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1928 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001929 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001930 goto out;
1931 }
1932
1933 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001934 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001935 goto err;
1936 }
1937
1938 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001939 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001940 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001941
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001942 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001943 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944 return 1;
1945
1946 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001947 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001948 return 0;
1949}
1950
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001951/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1952 * <pkt> RX packet.
1953 * Return it if succeeded, NULL if not.
1954 */
1955static inline
1956struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1957 struct quic_rx_packet *pkt)
1958{
1959 struct quic_rx_strm_frm *frm;
1960
1961 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1962 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001963 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001964 frm->len = stream_frm->len;
1965 frm->data = stream_frm->data;
1966 frm->pkt = pkt;
1967 }
1968
1969 return frm;
1970}
1971
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001972/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001973 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001974 */
1975static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1976{
1977 size_t ret;
1978
1979 ret = 0;
1980 while (strm_frm->len) {
1981 size_t try;
1982
1983 try = b_contig_space(buf);
1984 if (!try)
1985 break;
1986
1987 if (try > strm_frm->len)
1988 try = strm_frm->len;
1989 memcpy(b_tail(buf), strm_frm->data, try);
1990 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001991 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001992 b_add(buf, try);
1993 ret += try;
1994 }
1995
1996 return ret;
1997}
1998
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001999/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
Amaury Denoyelled1c76f22022-02-21 17:53:38 +01002000 *
2001 * Note that <strm_frm> is not updated as it is implied that the frame may be
2002 * present in a tree and offset node is used as the key. The caller should
2003 * update offset/lenght of the frame after the function call.
2004 *
2005 * Return the total count of copied bytes.
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002006 */
2007static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
2008 struct quic_rx_strm_frm *strm_frm)
2009{
Amaury Denoyelled1c76f22022-02-21 17:53:38 +01002010 size_t flen = strm_frm->len;
2011 size_t ret = 0;
2012 size_t try;
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002013
2014 ret = 0;
Amaury Denoyelled1c76f22022-02-21 17:53:38 +01002015 while (flen && (try = b_contig_space(buf))) {
2016 if (try > flen)
2017 try = flen;
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002018
Amaury Denoyelled1c76f22022-02-21 17:53:38 +01002019 memcpy(b_tail(buf), strm_frm->data + ret, try);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002020 b_add(buf, try);
2021 ret += try;
Amaury Denoyelled1c76f22022-02-21 17:53:38 +01002022 flen -= try;
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002023 }
2024
2025 return ret;
2026}
2027
2028/* Process as much as possible RX STREAM frames received for <qcs> */
2029static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
2030{
2031 int total;
2032 struct eb64_node *frm_node;
2033
2034 total = 0;
2035 frm_node = eb64_first(&qcs->rx.frms);
2036 while (frm_node) {
2037 int ret;
2038 struct quic_rx_strm_frm *frm;
2039
2040 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
2041 if (frm->offset_node.key != qcs->rx.offset)
2042 break;
2043
2044 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
2045 qcs->rx.offset += ret;
2046 total += ret;
Amaury Denoyelle43235672022-02-21 19:08:44 +01002047
2048 BUG_ON(frm->len < ret);
2049 if (frm->len - ret > 0) {
2050 /* Remove the frame from the tree before updating the
2051 * offset field.
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002052 */
Amaury Denoyelled1c76f22022-02-21 17:53:38 +01002053 eb64_delete(&frm->offset_node);
2054 frm->offset_node.key += ret;
Amaury Denoyelle43235672022-02-21 19:08:44 +01002055 frm->data += ret;
Amaury Denoyelled1c76f22022-02-21 17:53:38 +01002056 frm->len -= ret;
2057 eb64_insert(&qcs->rx.frms, &frm->offset_node);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002058 break;
2059 }
2060
2061 frm_node = eb64_next(frm_node);
2062 quic_rx_packet_refdec(frm->pkt);
2063 eb64_delete(&frm->offset_node);
2064 pool_free(pool_head_quic_rx_strm_frm, frm);
2065 }
2066
2067 return total;
2068}
2069
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002070/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2071 * streams may be open. The data are copied to the stream RX buffer if possible.
2072 * If not, the STREAM frame is stored to be treated again later.
2073 * We rely on the flow control so that not to store too much STREAM frames.
2074 * Return 1 if succeeded, 0 if not.
2075 */
2076static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2077 struct quic_stream *strm_frm,
2078 struct quic_conn *qc)
2079{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002080 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002081 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002082 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002083 struct quic_rx_strm_frm *frm;
Amaury Denoyelle6a2c2f42022-02-15 10:57:16 +01002084 char fin = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002085
2086 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2087 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002088 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002089 return 0;
2090 }
2091
2092 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002093 if (strm_frm->offset.key < strm->rx.offset) {
2094 size_t diff;
2095
2096 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2097 TRACE_PROTO("Already received STREAM data",
2098 QUIC_EV_CONN_PSTRM, qc);
2099 goto out;
2100 }
2101
2102 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2103 diff = strm->rx.offset - strm_frm->offset.key;
2104 strm_frm->offset.key = strm->rx.offset;
2105 strm_frm->len -= diff;
2106 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002107 }
2108
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002109 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002110 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002111 int ret;
2112
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002113 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002114 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002115 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002116
2117 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002118 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002119 strm->rx.offset += ret;
2120 }
2121
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002122 total += qc_treat_rx_strm_frms(strm);
Amaury Denoyelle6a2c2f42022-02-15 10:57:16 +01002123 /* FIN is set only if all data were copied. */
2124 fin = strm_frm->fin && !strm_frm->len;
2125 if (total && qc->qcc->app_ops->decode_qcs(strm, fin, qc->qcc->ctx) < 0) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002126 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002127 return 0;
2128 }
2129
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002130 if (!strm_frm->len)
2131 goto out;
2132
2133 store_frm:
2134 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2135 if (!frm) {
2136 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002137 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002138 return 0;
2139 }
2140
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002141 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002142 quic_rx_packet_refinc(pkt);
2143
2144 out:
2145 return 1;
2146}
2147
2148/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2149 * streams may be open. The data are copied to the stream RX buffer if possible.
2150 * If not, the STREAM frame is stored to be treated again later.
2151 * We rely on the flow control so that not to store too much STREAM frames.
2152 * Return 1 if succeeded, 0 if not.
2153 */
2154static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2155 struct quic_stream *strm_frm,
2156 struct quic_conn *qc)
2157{
2158 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002159 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002160 struct quic_rx_strm_frm *frm;
2161 size_t strm_frm_len;
2162
2163 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2164 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002165 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002166 return 0;
2167 }
2168
2169 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002170 if (strm_frm->offset.key < strm->rx.offset) {
2171 size_t diff;
2172
2173 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2174 TRACE_PROTO("Already received STREAM data",
2175 QUIC_EV_CONN_PSTRM, qc);
2176 goto out;
2177 }
2178
2179 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2180 diff = strm->rx.offset - strm_frm->offset.key;
2181 strm_frm->offset.key = strm->rx.offset;
2182 strm_frm->len -= diff;
2183 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002184 }
2185
2186 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002187 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002188 int ret;
2189
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002190 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002191 goto store_frm;
2192
2193 /* qc_strm_cpy() will modify the offset, depending on the number
2194 * of bytes copied.
2195 */
2196 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2197 /* Inform the application of the arrival of this new stream */
2198 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002199 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002200 return 0;
2201 }
2202
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002203 if (ret)
2204 qcs_notify_recv(strm);
2205
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002206 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002207 }
2208 /* Take this frame into an account for the stream flow control */
2209 strm->rx.offset += strm_frm_len;
2210 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002211 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002212 */
2213 if (!strm_frm->len)
2214 goto out;
2215
2216 store_frm:
2217 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2218 if (!frm) {
2219 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002220 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002221 return 0;
2222 }
2223
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002224 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002225 quic_rx_packet_refinc(pkt);
2226
2227 out:
2228 return 1;
2229}
2230
2231static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2232 struct quic_stream *strm_frm,
2233 struct quic_conn *qc)
2234{
2235 if (strm_frm->id & QCS_ID_DIR_BIT)
2236 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2237 else
2238 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2239}
2240
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002241/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002242static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2243 struct quic_conn *qc)
2244{
2245 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002246 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002247 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002248
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002249 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002250 pkts = &qel->pktns->tx.pkts;
2251 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002252 /* Skip the empty packet (they have already been retransmitted) */
2253 while (node) {
2254 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2255 if (!LIST_ISEMPTY(&pkt->frms))
2256 break;
2257 node = eb64_next(node);
2258 }
2259
2260 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002261 return;
2262
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002263 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002264}
2265
2266/* Prepare a fast retransmission during handshake after a client
2267 * has resent Initial packets. According to the RFC a server may retransmit
2268 * up to two datagrams of Initial packets if did not receive all Initial packets
2269 * and resend them coalescing with others (Handshake here).
2270 * (Listener only).
2271 */
2272static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2273{
2274 struct list itmp = LIST_HEAD_INIT(itmp);
2275 struct list htmp = LIST_HEAD_INIT(htmp);
2276 struct quic_frame *frm, *frmbak;
2277
2278 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2279 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2280 struct quic_enc_level *qel = iqel;
2281 struct eb_root *pkts;
2282 struct eb64_node *node;
2283 struct quic_tx_packet *pkt;
2284 struct list *tmp = &itmp;
2285
2286 start:
2287 pkt = NULL;
2288 pkts = &qel->pktns->tx.pkts;
2289 node = eb64_first(pkts);
2290 /* Skip the empty packet (they have already been retransmitted) */
2291 while (node) {
2292 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2293 if (!LIST_ISEMPTY(&pkt->frms))
2294 break;
2295 node = eb64_next(node);
2296 }
2297
2298 if (!pkt)
2299 goto end;
2300
2301 qel->pktns->tx.pto_probe += 1;
2302 requeue:
2303 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2304 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2305 LIST_DELETE(&frm->list);
2306 LIST_APPEND(tmp, &frm->list);
2307 }
2308
2309 if (qel == iqel) {
2310 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2311 pkt = pkt->next;
2312 tmp = &htmp;
2313 hqel->pktns->tx.pto_probe += 1;
2314 goto requeue;
2315 }
2316
2317 qel = hqel;
2318 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002319 goto start;
2320 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002321
2322 end:
2323 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2324 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002325}
2326
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002327/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2328 * as I/O handler context and <qel> as encryption level.
2329 * Returns 1 if succeeded, 0 if failed.
2330 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002331static int qc_parse_pkt_frms(struct quic_rx_packet *pkt, struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002332 struct quic_enc_level *qel)
2333{
2334 struct quic_frame frm;
2335 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002336 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002337 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002338
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002339 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002340 /* Skip the AAD */
2341 pos = pkt->data + pkt->aad_len;
2342 end = pkt->data + pkt->len;
2343
2344 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002345 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002346 goto err;
2347
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002348 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002349 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002350 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002351 break;
2352 case QUIC_FT_PING:
2353 break;
2354 case QUIC_FT_ACK:
2355 {
2356 unsigned int rtt_sample;
2357
2358 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002359 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002360 goto err;
2361
2362 if (rtt_sample) {
2363 unsigned int ack_delay;
2364
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002365 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002366 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2367 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2368 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002369 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002370 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002371 break;
2372 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002373 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002374 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002375 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002376 {
2377 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002378
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002379 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2380 /* XXX TO DO: <cfdebug> is used only for the traces. */
2381 struct quic_rx_crypto_frm cfdebug = { };
2382
2383 cfdebug.offset_node.key = frm.crypto.offset;
2384 cfdebug.len = frm.crypto.len;
2385 TRACE_PROTO("CRYPTO data discarded",
2386 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2387 break;
2388 }
2389
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002390 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2391 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2392 /* XXX TO DO: <cfdebug> is used only for the traces. */
2393 struct quic_rx_crypto_frm cfdebug = { };
2394
2395 cfdebug.offset_node.key = frm.crypto.offset;
2396 cfdebug.len = frm.crypto.len;
2397 /* Nothing to do */
2398 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002399 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002400 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002401 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2402 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002403 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002404 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002405 else {
2406 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2407 /* XXX TO DO: <cfdebug> is used only for the traces. */
2408 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002409
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002410 cfdebug.offset_node.key = frm.crypto.offset;
2411 cfdebug.len = frm.crypto.len;
2412 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002413 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002414 frm.crypto.len -= diff;
2415 frm.crypto.data += diff;
2416 frm.crypto.offset = qel->rx.crypto.offset;
2417 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002418 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002419
2420 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002421 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002422 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002423
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002424 cfdebug.offset_node.key = frm.crypto.offset;
2425 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002426 if (!qc_provide_cdata(qel, ctx,
2427 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002428 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002429 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002430
2431 break;
2432 }
2433
2434 /* frm.crypto.offset > qel->rx.crypto.offset */
2435 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2436 if (!cf) {
2437 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002438 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002439 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002440 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002441
2442 cf->offset_node.key = frm.crypto.offset;
2443 cf->len = frm.crypto.len;
2444 cf->data = frm.crypto.data;
2445 cf->pkt = pkt;
2446 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2447 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002448 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002449 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002450 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002451 {
2452 struct quic_stream *stream = &frm.stream;
2453
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002454 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002455 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2456 goto err;
2457 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2458 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002459
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002460 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002461 goto err;
2462
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002463 break;
2464 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002465 case QUIC_FT_MAX_DATA:
2466 case QUIC_FT_MAX_STREAM_DATA:
2467 case QUIC_FT_MAX_STREAMS_BIDI:
2468 case QUIC_FT_MAX_STREAMS_UNI:
2469 case QUIC_FT_DATA_BLOCKED:
2470 case QUIC_FT_STREAM_DATA_BLOCKED:
2471 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2472 case QUIC_FT_STREAMS_BLOCKED_UNI:
2473 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002474 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002475 case QUIC_FT_RETIRE_CONNECTION_ID:
2476 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002477 break;
2478 case QUIC_FT_CONNECTION_CLOSE:
2479 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002480 /* warn the mux to close the connection */
Amaury Denoyelle4af65952022-02-15 11:06:15 +01002481 if (qc->mux_state == QC_MUX_READY)
2482 qc->qcc->flags |= QC_CF_CC_RECV;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002483 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002484 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002485 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002486 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002487 goto err;
2488
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002489 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002490 break;
2491 default:
2492 goto err;
2493 }
2494 }
2495
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002496 if (fast_retrans)
2497 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002498
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002499 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2500 * has successfully parse a Handshake packet. The Initial encryption must also
2501 * be discarded.
2502 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002503 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002504 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002505
2506 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002507 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002508 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002509 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002510 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002511 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002512 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002513 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002514 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002515 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002516 }
2517
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002518 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002519 return 1;
2520
2521 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002522 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002523 return 0;
2524}
2525
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002526/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002527 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002528 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2529 * This function must be called only after having built a correct datagram.
2530 * Always succeeds.
2531 */
2532static inline void qc_set_dg(struct cbuf *cbuf,
2533 uint16_t dglen, struct quic_tx_packet *pkt)
2534{
2535 write_u16(cb_wr(cbuf), dglen);
2536 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2537 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2538}
2539
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002540/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002541 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2542 * several packets in the same datagram. A header made of two fields is added
2543 * to each datagram: the datagram length followed by the address of the first
2544 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002545 * Returns 1 if succeeded, or 0 if something wrong happened.
2546 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002547static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2548 enum quic_tls_enc_level tel,
2549 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002550{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002551 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002552 struct cbuf *cbuf;
2553 unsigned char *end_buf, *end, *pos, *spos;
2554 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2555 /* length of datagrams */
2556 uint16_t dglen;
2557 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002558 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002559 /* Each datagram is prepended with its length followed by the
2560 * address of the first packet in the datagram.
2561 */
2562 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002563
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002564 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2565
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002566 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002567 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002568 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002569 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002570 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002571 cbuf = qr->cbuf;
2572 spos = pos = cb_wr(cbuf);
2573 /* Leave at least <dglen> bytes at the end of this buffer
2574 * to ensure there is enough room to mark the end of prepared
2575 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002576 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002577 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2578 first_pkt = prv_pkt = NULL;
2579 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002580 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002581 enum quic_pkt_type pkt_type;
2582
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002583 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002584 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002585 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2586 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002587 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002588 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002589 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002590 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002591 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002592 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002593 * and if there is no more CRYPTO data available or in flight
2594 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002595 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002596 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002597 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002598 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002599 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002600 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002601 /* Set the current datagram as prepared into <cbuf> if
2602 * the was already a correct packet which was previously written.
2603 */
2604 if (prv_pkt)
2605 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002606 /* Let's select the next encryption level */
2607 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2608 tel = next_tel;
2609 qel = &qc->els[tel];
2610 /* Build a new datagram */
2611 prv_pkt = NULL;
2612 continue;
2613 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002614 break;
2615 }
2616
2617 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002618 if (!prv_pkt) {
2619 /* Leave room for the datagram header */
2620 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002621 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002622 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2623 }
2624 else {
2625 end = pos + qc->path->mtu;
2626 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002627 }
2628
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002629 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002630 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002631 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002632 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002633 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002634 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002635 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002636 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002637 case -2:
2638 goto err;
2639 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002640 /* If there was already a correct packet present, set the
2641 * current datagram as prepared into <cbuf>.
2642 */
2643 if (prv_pkt) {
2644 qc_set_dg(cbuf, dglen, first_pkt);
2645 goto stop_build;
2646 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002647 goto out;
2648 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002649 break;
2650 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002651
Frédéric Lécaille63556772021-12-29 17:18:21 +01002652 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2653 if (!cur_pkt)
2654 goto err;
2655
2656 total += cur_pkt->len;
2657 /* keep trace of the first packet in the datagram */
2658 if (!first_pkt)
2659 first_pkt = cur_pkt;
2660 /* Attach the current one to the previous one */
2661 if (prv_pkt)
2662 prv_pkt->next = cur_pkt;
2663 /* Let's say we have to build a new dgram */
2664 prv_pkt = NULL;
2665 dglen += cur_pkt->len;
2666 /* Client: discard the Initial encryption keys as soon as
2667 * a handshake packet could be built.
2668 */
2669 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2670 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2671 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2672 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2673 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2674 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002675 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002676 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002677 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2678 }
2679 /* If the data for the current encryption level have all been sent,
2680 * select the next level.
2681 */
2682 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002683 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002684 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2685 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2686 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2687 tel = next_tel;
2688 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002689 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002690 /* If there is data for the next level, do not
2691 * consume a datagram.
2692 */
2693 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002694 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002695 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002696 /* If we have to build a new datagram, set the current datagram as
2697 * prepared into <cbuf>.
2698 */
2699 if (!prv_pkt) {
2700 qc_set_dg(cbuf, dglen, first_pkt);
2701 first_pkt = NULL;
2702 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002703 padding = 0;
2704 }
2705 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002706 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002707 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2708 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002709 }
2710 }
2711
2712 stop_build:
2713 /* Reset <wr> writer index if in front of <rd> index */
2714 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2715 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2716
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002717 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002718 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2719 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2720 /* Mark the end of contiguous data for the reader */
2721 write_u16(cb_wr(cbuf), 0);
2722 cb_add(cbuf, sizeof(uint16_t));
2723 }
2724 }
2725
2726 if (rd && rd <= cbuf->wr) {
2727 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002728 /* Let's try to reuse this buffer */
2729 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002730 }
2731 }
2732
2733 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002734 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002735 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002736
2737 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002738 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002739 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002740}
2741
2742/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002743 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002744 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002745int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002746{
2747 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002748 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002749
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002750 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002751 cbuf = qr->cbuf;
2752 while (cb_contig_data(cbuf)) {
2753 unsigned char *pos;
2754 struct buffer tmpbuf = { };
2755 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2756 uint16_t dglen;
2757 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002758 unsigned int time_sent;
2759
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002760 pos = cb_rd(cbuf);
2761 dglen = read_u16(pos);
2762 /* End of prepared datagrams.
2763 * Reset the reader index only if in front of the writer index.
2764 */
2765 if (!dglen) {
2766 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2767
2768 if (wr && wr < cbuf->rd) {
2769 cb_rd_reset(cbuf);
2770 continue;
2771 }
2772 break;
2773 }
2774
2775 pos += sizeof dglen;
2776 first_pkt = read_ptr(pos);
2777 pos += sizeof first_pkt;
2778 tmpbuf.area = (char *)pos;
2779 tmpbuf.size = tmpbuf.data = dglen;
2780
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002781 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002782 for (pkt = first_pkt; pkt; pkt = pkt->next)
2783 quic_tx_packet_refinc(pkt);
Amaury Denoyelle58a77042022-02-09 15:43:07 +01002784 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002785 for (pkt = first_pkt; pkt; pkt = pkt->next)
2786 quic_tx_packet_refdec(pkt);
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002787 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002788 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002789
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002790 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002791 qc->tx.bytes += tmpbuf.data;
2792 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002793
2794 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2795 pkt->time_sent = time_sent;
2796 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2797 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002798 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002799 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002800 qc->path->in_flight += pkt->in_flight_len;
2801 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2802 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002803 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002804 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002805 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002806 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002807 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002808 }
2809 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002810
2811 return 1;
2812}
2813
2814/* Build all the frames which must be sent just after the handshake have succeeded.
2815 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2816 * a HANDSHAKE_DONE frame.
2817 * Return 1 if succeeded, 0 if not.
2818 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002819static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002820{
2821 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002822 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002823 struct quic_frame *frm;
2824
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002825 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002826 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002827 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002828 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002829 if (!frm)
2830 return 0;
2831
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002832 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002833 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002834 }
2835
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002836 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002837 struct quic_connection_id *cid;
2838
2839 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002840 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002841 goto err;
2842
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01002843 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002844 if (!cid)
2845 goto err;
2846
Frédéric Lécaille74904a42022-01-27 15:35:56 +01002847 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01002848 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002849
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002850 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002851 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002853 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002854
2855 return 1;
2856
2857 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002858 return 0;
2859}
2860
2861/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002862void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002863{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002864 struct eb64_node *n;
2865 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002866
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002867 n = eb64_first(&arngs->root);
2868 while (n) {
2869 struct eb64_node *next;
2870
2871 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2872 next = eb64_next(n);
2873 eb64_delete(n);
2874 free(ar);
2875 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002876 }
2877}
2878
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002879/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2880 * descending order.
2881 */
2882static inline size_t sack_gap(struct quic_arng_node *p,
2883 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002884{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002885 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002886}
2887
2888
2889/* Remove the last elements of <ack_ranges> list of ack range updating its
2890 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002891 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002892 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002893static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002894{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002895 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002896
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002897 last = eb64_last(&arngs->root);
2898 while (last && arngs->enc_sz > limit) {
2899 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002900
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002901 prev = eb64_prev(last);
2902 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002903 return 0;
2904
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002905 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2906 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2907 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2908 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2909 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2910 --arngs->sz;
2911 eb64_delete(last);
2912 pool_free(pool_head_quic_arng, last);
2913 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002914 }
2915
2916 return 1;
2917}
2918
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002919/* Set the encoded size of <arngs> QUIC ack ranges. */
2920static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2921{
2922 struct eb64_node *node, *next;
2923 struct quic_arng_node *ar, *ar_next;
2924
2925 node = eb64_last(&arngs->root);
2926 if (!node)
2927 return;
2928
2929 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2930 arngs->enc_sz = quic_int_getsize(ar->last) +
2931 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2932
2933 while ((next = eb64_prev(node))) {
2934 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2935 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2936 quic_int_getsize(ar_next->last - ar_next->first.key);
2937 node = next;
2938 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2939 }
2940}
2941
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002942/* Insert <ar> ack range into <argns> tree of ack ranges.
2943 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002944 */
2945static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002946struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2947 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002948{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002949 struct quic_arng_node *new_ar;
2950
2951 new_ar = pool_alloc(pool_head_quic_arng);
2952 if (new_ar) {
2953 new_ar->first.key = ar->first;
2954 new_ar->last = ar->last;
2955 eb64_insert(&arngs->root, &new_ar->first);
2956 arngs->sz++;
2957 }
2958
2959 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002960}
2961
2962/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002963 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002964 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002965 *
2966 * Descending order
2967 * ------------->
2968 * range1 range2
2969 * ..........|--------|..............|--------|
2970 * ^ ^ ^ ^
2971 * | | | |
2972 * last1 first1 last2 first2
2973 * ..........+--------+--------------+--------+......
2974 * diff1 gap12 diff2
2975 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002976 * To encode the previous list of ranges we must encode integers as follows in
2977 * descending order:
2978 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002979 * with diff1 = last1 - first1
2980 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002981 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002982 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002983 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002984int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2985 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002986{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002987 struct eb64_node *le;
2988 struct quic_arng_node *new_node;
2989 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002990
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002991 new = NULL;
2992 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002993 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002994 if (!new_node)
2995 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002997 goto out;
2998 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003000 le = eb64_lookup_le(&arngs->root, ar->first);
3001 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003002 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003003 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003004 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003005
3006 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003007 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003008 else {
3009 struct quic_arng_node *le_ar =
3010 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003011
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003012 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003013 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003014 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003015
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003016 if (le_ar->last + 1 >= ar->first) {
3017 le_ar->last = ar->last;
3018 new = le;
3019 new_node = le_ar;
3020 }
3021 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003022 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003023 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003024 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003025
3026 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003027 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003028 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003029
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003030 /* Verify that the new inserted node does not overlap the nodes
3031 * which follow it.
3032 */
3033 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003034 struct eb64_node *next;
3035 struct quic_arng_node *next_node;
3036
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003037 while ((next = eb64_next(new))) {
3038 next_node =
3039 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003040 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003041 break;
3042
3043 if (next_node->last > new_node->last)
3044 new_node->last = next_node->last;
3045 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003046 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003047 /* Decrement the size of these ranges. */
3048 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003049 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003050 }
3051
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003052 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003053 quic_arngs_set_enc_sz(arngs);
3054
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003055 return 1;
3056}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003057/* Remove the header protection of packets at <el> encryption level.
3058 * Always succeeds.
3059 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003060static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003061{
3062 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003063 struct quic_rx_packet *pqpkt;
3064 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003065 struct quic_enc_level *app_qel;
3066
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003067 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3068 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003069 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003070 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003071 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003072 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003073 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003074 goto out;
3075 }
3076 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003077 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003078 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003079 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003080 pqpkt->data, pqpkt->data + pqpkt->len)) {
3081 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003082 /* XXX TO DO XXX */
3083 }
3084 else {
3085 /* The AAD includes the packet number field */
3086 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3087 /* Store the packet into the tree of packets to decrypt. */
3088 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003089 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003090 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3091 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003092 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003093 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003094 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003095 MT_LIST_DELETE_SAFE(pkttmp1);
3096 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003097 }
3098
3099 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003100 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003101}
3102
3103/* Process all the CRYPTO frame at <el> encryption level.
3104 * Return 1 if succeeded, 0 if not.
3105 */
3106static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003107 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108{
3109 struct eb64_node *node;
3110
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003111 node = eb64_first(&el->rx.crypto.frms);
3112 while (node) {
3113 struct quic_rx_crypto_frm *cf;
3114
3115 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3116 if (cf->offset_node.key != el->rx.crypto.offset)
3117 break;
3118
3119 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3120 goto err;
3121
3122 node = eb64_next(node);
3123 quic_rx_packet_refdec(cf->pkt);
3124 eb64_delete(&cf->offset_node);
3125 pool_free(pool_head_quic_rx_crypto_frm, cf);
3126 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003127 return 1;
3128
3129 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003130 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003131 return 0;
3132}
3133
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003134/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003135 * This is the caller responsibility to check that <cur_el> is different of <next_el>
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003136 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003137 * Return 1 if succeeded, 0 if not.
3138 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003139int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3140 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003141{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003142 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003143 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003144 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003145 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003146
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003147 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003148 qel = cur_el;
3149 next_tel:
3150 if (!qel)
3151 goto out;
3152
3153 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3154 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155 while (node) {
3156 struct quic_rx_packet *pkt;
3157
3158 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003159 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003160 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003161 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003162 /* Drop the packet */
3163 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003164 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003165 }
3166 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003167 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003168 /* Drop the packet */
3169 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003170 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003171 }
3172 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003173 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3174
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003175 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003176 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003177 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003178 if (pkt->pn > largest_pn)
3179 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003180 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003181 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003182 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003183 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003184 }
3185 }
3186 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003187 eb64_delete(&pkt->pn_node);
3188 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003189 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003190 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003191
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003192 /* Update the largest packet number. */
3193 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003194 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3195 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003196 goto err;
3197
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003198 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003199 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003200 qel = next_el;
3201 goto next_tel;
3202 }
3203
3204 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003205 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003206 return 1;
3207
3208 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003209 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003210 return 0;
3211}
3212
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003213/* Check if it's possible to remove header protection for packets related to
3214 * encryption level <qel>. If <qel> is NULL, assume it's false.
3215 *
3216 * Return true if the operation is possible else false.
3217 */
3218static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3219{
3220 enum quic_tls_enc_level tel;
3221
3222 if (!qel)
3223 return 0;
3224
3225 tel = ssl_to_quic_enc_level(qel->level);
3226
3227 /* check if tls secrets are available */
3228 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
3229 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
3230
3231 if (!(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET))
3232 return 0;
3233
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003234 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003235 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
3236 qc->mux_state != QC_MUX_READY)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003237 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003238
3239 return 1;
3240}
3241
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003242/* QUIC connection packet handler task. */
3243struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003244{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003245 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003246 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003247 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003248 enum quic_tls_enc_level tel, next_tel;
3249 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003250 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003251 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003252
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003253 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003254 qc = ctx->qc;
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003255 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003256 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003257 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003258 TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003259 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3260 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3261 /* The I/O handler has been woken up by the dgram listener
3262 * after the anti-amplification was reached.
3263 */
3264 qc_set_timer(qc);
3265 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3266 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3267 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003268 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003269 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3270 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3271 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003272 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003273 if (st >= QUIC_HS_ST_COMPLETE &&
3274 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3275 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3276 /* There may be remaining Handshake packets to treat and acknowledge. */
3277 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3278 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3279 }
3280 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003281 goto err;
3282
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003283 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003284 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003285
3286 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003287 /* Treat packets waiting for header packet protection decryption */
3288 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003289 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003290
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003291 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3292 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3293 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003294 goto err;
3295
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003296 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3297 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3298 qel = next_qel;
3299 next_qel = NULL;
3300 goto next_level;
3301 }
3302
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003303 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003304 if (st >= QUIC_HS_ST_COMPLETE) {
3305 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3306 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003307 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003308
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003309 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3310 QUIC_FL_TLS_SECRETS_DCD)) {
3311 /* Discard the Handshake keys. */
3312 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3313 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3314 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3315 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003316 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003317 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003318 }
3319
3320 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3321 /* There may be remaining handshake to build (acks) */
3322 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3323 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003324 }
3325
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003326 if (!qr)
3327 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003328 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3329 * be considered.
3330 */
3331 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003332 goto err;
3333 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003334 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003335 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003336 else if (ret == 0)
3337 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003338
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003339 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003340 goto err;
3341
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003342 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003343 /* Check if there is something to do for the next level.
3344 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003345 if (next_qel && next_qel != qel &&
3346 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003347 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003348 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003349 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350 goto next_level;
3351 }
3352
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003353 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003354 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003355 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003356
3357 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003358 if (qr)
3359 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003360 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003361 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003362}
3363
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003364/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3366{
3367 int i;
3368
3369 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3370 if (qel->tx.crypto.bufs[i]) {
3371 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3372 qel->tx.crypto.bufs[i] = NULL;
3373 }
3374 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003375 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003376}
3377
3378/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003379 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003380 * Returns 1 if succeeded, 0 if not.
3381 */
3382static int quic_conn_enc_level_init(struct quic_conn *qc,
3383 enum quic_tls_enc_level level)
3384{
3385 struct quic_enc_level *qel;
3386
3387 qel = &qc->els[level];
3388 qel->level = quic_to_ssl_enc_level(level);
3389 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3390 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3391 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3392 qel->tls_ctx.rx.flags = 0;
3393 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003394 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003395
3396 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003397 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003398 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003399 qel->rx.crypto.offset = 0;
3400 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003401
3402 /* Allocate only one buffer. */
3403 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3404 if (!qel->tx.crypto.bufs)
3405 goto err;
3406
3407 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3408 if (!qel->tx.crypto.bufs[0])
3409 goto err;
3410
3411 qel->tx.crypto.bufs[0]->sz = 0;
3412 qel->tx.crypto.nb_buf = 1;
3413
3414 qel->tx.crypto.sz = 0;
3415 qel->tx.crypto.offset = 0;
3416
3417 return 1;
3418
3419 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003420 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003421 return 0;
3422}
3423
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003424/* Increment the <qc> refcount.
3425 *
3426 * This operation must be conducted when manipulating the quic_conn outside of
3427 * the connection pinned thread. These threads can only retrieve the connection
3428 * in the CID tree, so this function must be conducted under the CID lock.
3429 */
3430static inline void quic_conn_take(struct quic_conn *qc)
3431{
3432 HA_ATOMIC_INC(&qc->refcount);
3433}
3434
3435/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003436 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003437 */
3438static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003439{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003440 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003441 int i;
3442
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003443 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003444 return;
3445
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003446 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1))
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003447 return;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003448
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003449 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003450 if (conn_ctx) {
3451 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003452 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003453 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003454
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003455 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003456 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003457
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003458 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3459 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003460 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003461}
3462
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003463/* Release the quic_conn <qc>. It will decrement its refcount so that the
3464 * connection will be freed once all threads have finished to work with it. The
3465 * connection is removed from the CIDs tree and thus cannot be found by other
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003466 * threads after it. The connection tasklet is killed.
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003467 *
3468 * Do not use <qc> after it as it may be freed. This function must only be
3469 * called by the thread responsible of the quic_conn tasklet.
3470 */
3471static void quic_conn_release(struct quic_conn *qc)
3472{
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003473 struct ssl_sock_ctx *conn_ctx;
3474
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003475 /* remove the connection from receiver cids trees */
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003476 ebmb_delete(&qc->odcid_node);
3477 ebmb_delete(&qc->scid_node);
3478 free_quic_conn_cids(qc);
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003479
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003480 /* Kill the tasklet. Do not use tasklet_free as this is not thread safe
3481 * as other threads may call tasklet_wakeup after this.
3482 */
3483 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
3484 if (conn_ctx)
3485 tasklet_kill(conn_ctx->wait_event.tasklet);
3486
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003487 quic_conn_drop(qc);
3488}
3489
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003490void quic_close(struct connection *conn, void *xprt_ctx)
3491{
3492 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003493 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003494
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003495 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003496 /* This task must be deleted by the connection-pinned thread. */
3497 if (qc->timer_task) {
3498 task_destroy(qc->timer_task);
3499 qc->timer_task = NULL;
3500 }
3501
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003502 /* Next application data can be dropped. */
3503 qc->mux_state = QC_MUX_RELEASED;
3504
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003505 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9c4da932022-01-21 14:54:58 +01003506
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003507 /* TODO for now release the quic_conn on notification by the upper
3508 * layer. It could be useful to delay it if there is remaining data to
3509 * send or data to be acked.
3510 */
3511 quic_conn_release(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003512}
3513
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003514/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003515static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003516{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003517 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003518 struct quic_conn *qc;
3519 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003520 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521
3522 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003523 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003524 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003525 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003526 task->expire = TICK_ETERNITY;
3527 pktns = quic_loss_pktns(qc);
3528 if (tick_isset(pktns->tx.loss_time)) {
3529 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3530
3531 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3532 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003533 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3534 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003535 goto out;
3536 }
3537
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003538 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003539 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003540 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003541 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3542 pktns->tx.pto_probe = 1;
3543 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3544 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3545 }
3546 else {
3547 pktns->tx.pto_probe = 2;
3548 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003549 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003550 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003551 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3552 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3553
3554 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3555 hel->pktns->tx.pto_probe = 1;
3556 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3557 iel->pktns->tx.pto_probe = 1;
3558 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003559
3560 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003561 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3562 continue;
3563
Frédéric Lécaille53c7d8d2022-02-15 12:00:55 +01003564 qc_prep_fast_retrans(&qc->els[i], qc);
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003565 }
3566
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003567 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3568 qc->path->loss.pto_count++;
3569
3570 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003571 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003572
3573 return task;
3574}
3575
3576/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3577 * connections used to identify the first Initial packets of client connecting
3578 * to listeners. This parameter must be NULL for QUIC connections attached
3579 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3580 * <scid> is the source connection ID with <scid_len> as length.
3581 * Returns 1 if succeeded, 0 if not.
3582 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003583static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003584 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003585 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003586{
3587 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003588 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003589 /* Initial CID. */
3590 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003591 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003592 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003593
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003594 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003595 qc = pool_zalloc(pool_head_quic_conn);
3596 if (!qc) {
3597 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3598 goto err;
3599 }
3600
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003601 HA_ATOMIC_STORE(&qc->refcount, 0);
3602
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003603 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3604 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003605 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003606 goto err;
3607 }
3608
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003609 qc->cids = EB_ROOT;
3610 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003611 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003612 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003613
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003614 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003615 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003616 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003617 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003618 qc->odcid.addrlen = dcid_addr_len;
3619 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003620
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003621 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003622 if (scid_len)
3623 memcpy(qc->dcid.data, scid, scid_len);
3624 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003625 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003626 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003627 }
3628 /* QUIC Client (outgoing connection to servers) */
3629 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003630 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003631 if (dcid_len)
3632 memcpy(qc->dcid.data, dcid, dcid_len);
3633 qc->dcid.len = dcid_len;
3634 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003635 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003636
3637 /* Initialize the output buffer */
3638 qc->obuf.pos = qc->obuf.data;
3639
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003640 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003641 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003642 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003643 goto err;
3644 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003645
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003646 /* insert the allocated CID in the receiver datagram handler tree */
3647 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003648 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003649
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003650 /* Select our SCID which is the first CID with 0 as sequence number. */
3651 qc->scid = icid->cid;
3652
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653 /* Packet number spaces initialization. */
3654 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3655 quic_pktns_init(&qc->pktns[i]);
3656 /* QUIC encryption level context initialization. */
3657 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003658 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003659 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003660 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003661 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003662 /* Initialize the packet number space. */
3663 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3664 }
3665
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003666 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003667 qc->tps_tls_ext = qc->version & 0xff000000 ?
3668 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3669 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003670 /* TX part. */
3671 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003672 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3673 qc->tx.wbuf = qc->tx.rbuf = 0;
3674 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003675 /* RX part. */
3676 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003677 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003678 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003679 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003680 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003681 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003682 goto err;
3683 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003684
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003685 /* XXX TO DO: Only one path at this time. */
3686 qc->path = &qc->paths[0];
3687 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3688
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01003689 /* required to use MTLIST_IN_LIST */
3690 MT_LIST_INIT(&qc->accept_list);
3691
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003692 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003694 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003695
3696 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003697 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003698 pool_free(pool_head_quic_conn_rxbuf, buf_area);
3699 if (qc)
3700 qc->rx.buf.area = NULL;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003701 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003702 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003703}
3704
3705/* Initialize the timer task of <qc> QUIC connection.
3706 * Returns 1 if succeeded, 0 if not.
3707 */
3708static int quic_conn_init_timer(struct quic_conn *qc)
3709{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003710 /* Attach this task to the same thread ID used for the connection */
3711 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712 if (!qc->timer_task)
3713 return 0;
3714
3715 qc->timer = TICK_ETERNITY;
3716 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01003717 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003718
3719 return 1;
3720}
3721
3722/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3723 * past one byte of this buffer.
3724 */
3725static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3726 struct quic_rx_packet *pkt)
3727{
3728 unsigned char dcid_len, scid_len;
3729
3730 /* Version */
3731 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3732 return 0;
3733
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003734 /* Destination Connection ID Length */
3735 dcid_len = *(*buf)++;
3736 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3737 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3738 /* XXX MUST BE DROPPED */
3739 return 0;
3740
3741 if (dcid_len) {
3742 /* Check that the length of this received DCID matches the CID lengths
3743 * of our implementation for non Initials packets only.
3744 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003745 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3746 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003747 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003748 return 0;
3749
3750 memcpy(pkt->dcid.data, *buf, dcid_len);
3751 }
3752
3753 pkt->dcid.len = dcid_len;
3754 *buf += dcid_len;
3755
3756 /* Source Connection ID Length */
3757 scid_len = *(*buf)++;
3758 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3759 /* XXX MUST BE DROPPED */
3760 return 0;
3761
3762 if (scid_len)
3763 memcpy(pkt->scid.data, *buf, scid_len);
3764 pkt->scid.len = scid_len;
3765 *buf += scid_len;
3766
3767 return 1;
3768}
3769
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003770/* Insert <pkt> RX packet in its <qel> RX packets tree */
3771static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3772{
3773 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003774 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003775 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3776 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3777 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003778}
3779
3780/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003781 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3782 * byte past the end of the buffer containing this packet and <beg> the address of
3783 * the packet first byte.
3784 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3785 * Returns 1 if succeeded, 0 if not.
3786 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003787static inline int qc_try_rm_hp(struct quic_conn *qc,
3788 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003789 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003790 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003791 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792{
3793 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003794 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003795 struct quic_enc_level *qel;
3796 /* Only for traces. */
3797 struct quic_rx_packet *qpkt_trace;
3798
3799 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003800 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003801 /* The packet number is here. This is also the start minus
3802 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3803 * protection.
3804 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003805 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003806
3807 tel = quic_packet_type_enc_level(pkt->type);
3808 qel = &qc->els[tel];
3809
3810 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003811 /* Note that the following function enables us to unprotect the packet
3812 * number and its length subsequently used to decrypt the entire
3813 * packets.
3814 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003815 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3816 qel->pktns->rx.largest_pn, pn, beg, end)) {
3817 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003818 goto err;
3819 }
3820
3821 /* The AAD includes the packet number field found at <pn>. */
3822 pkt->aad_len = pn - beg + pkt->pnl;
3823 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003824 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003825 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003826 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3827 /* If the packet number space has been discarded, this packet
3828 * will be not parsed.
3829 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003830 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003831 goto out;
3832 }
3833
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003834 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003835 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003836 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3837 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003838 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003839 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003840 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003841 goto err;
3842 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003843
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003844 *el = qel;
3845 /* No reference counter incrementation here!!! */
3846 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3847 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3848 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3849 b_add(&qc->rx.buf, pkt->len);
3850 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003851 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003852 return 1;
3853
3854 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003855 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003856 return 0;
3857}
3858
3859/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3860 * Also set <*long_header> to 1 if this form is long, 0 if not.
3861 */
3862static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3863 unsigned char byte0, int *long_header)
3864{
3865 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3866 pkt->type =
3867 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3868 *long_header = 1;
3869 }
3870 else {
3871 pkt->type = QUIC_PACKET_TYPE_SHORT;
3872 *long_header = 0;
3873 }
3874}
3875
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003876/*
3877 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3878 */
3879static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3880{
3881 int j = 0, version;
3882
3883 do {
3884 version = quic_supported_version[j];
3885 if (version == pkt->version)
3886 return 1;
3887
3888 version = quic_supported_version[++j];
3889 } while(version);
3890
3891 return 0;
3892}
3893
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003894/*
3895 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3896 * address <addr>.
3897 * Implementation of RFC9000 6. Version Negotiation
3898 *
3899 * TODO implement a rate-limiting sending of Version Negotiation packets
3900 *
3901 * Returns 0 on success else non-zero
3902 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003903static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3904 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003905{
3906 char buf[256];
3907 int i = 0, j, version;
3908 const socklen_t addrlen = get_addr_len(addr);
3909
3910 /*
3911 * header form
3912 * long header, fixed bit to 0 for Version Negotiation
3913 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003914 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3915 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003916
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003917 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003918 /* null version for Version Negotiation */
3919 buf[i++] = '\x00';
3920 buf[i++] = '\x00';
3921 buf[i++] = '\x00';
3922 buf[i++] = '\x00';
3923
3924 /* source connection id */
3925 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003926 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003927 i += pkt->scid.len;
3928
3929 /* destination connection id */
3930 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003931 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003932 i += pkt->dcid.len;
3933
3934 /* supported version */
3935 j = 0;
3936 do {
3937 version = htonl(quic_supported_version[j]);
3938 memcpy(&buf[i], &version, sizeof(version));
3939 i += sizeof(version);
3940
3941 version = quic_supported_version[++j];
3942 } while (version);
3943
3944 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3945 return 1;
3946
3947 return 0;
3948}
3949
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01003950/* Generate the token to be used in Retry packets. The token is written to
3951 * <buf> which is expected to be <len> bytes.
3952 *
3953 * Various parameters are expected to be encoded in the token. For now, only
3954 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
3955 * as this CID must be repeated by the server in the transport parameters.
3956 *
3957 * TODO add the client address to validate the token origin.
3958 *
3959 * Returns the length of the encoded token or 0 on error.
3960 */
3961static int generate_retry_token(unsigned char *buf, unsigned char len,
3962 struct quic_rx_packet *pkt)
3963{
3964 const size_t token_len = 1 + pkt->dcid.len;
3965 unsigned char i = 0;
3966
3967 if (token_len > len)
3968 return 0;
3969
3970 buf[i++] = pkt->dcid.len;
3971 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
3972 i += pkt->dcid.len;
3973
3974 return i;
3975}
3976
3977/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
3978 * the Initial <pkt> packet.
3979 *
3980 * Returns 0 on success else non-zero.
3981 */
3982static int send_retry(int fd, struct sockaddr_storage *addr,
3983 struct quic_rx_packet *pkt)
3984{
3985 unsigned char buf[128];
3986 int i = 0, token_len;
3987 const socklen_t addrlen = get_addr_len(addr);
3988 struct quic_cid scid;
3989
3990 /* long header + fixed bit + packet type 0x3 */
3991 buf[i++] = 0xf0;
3992 /* version */
3993 buf[i++] = 0x00;
3994 buf[i++] = 0x00;
3995 buf[i++] = 0x00;
3996 buf[i++] = 0x01;
3997
3998 /* Use the SCID from <pkt> for Retry DCID. */
3999 buf[i++] = pkt->scid.len;
4000 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4001 i += pkt->scid.len;
4002
4003 /* Generate a new CID to be used as SCID for the Retry packet. */
4004 scid.len = QUIC_HAP_CID_LEN;
4005 if (RAND_bytes(scid.data, scid.len) != 1)
4006 return 1;
4007
4008 buf[i++] = scid.len;
4009 memcpy(&buf[i], scid.data, scid.len);
4010 i += scid.len;
4011
4012 /* token */
4013 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
4014 return 1;
4015 i += token_len;
4016
4017 /* token integrity tag */
4018 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4019 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4020 pkt->dcid.len, buf, i)) {
4021 return 1;
4022 }
4023
4024 i += QUIC_TLS_TAG_LEN;
4025
4026 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4027 return 1;
4028
4029 return 0;
4030}
4031
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004032/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4033 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4034 * concatenated to the <pkt> DCID field.
4035 *
4036 * Returns the instance or NULL if not found.
4037 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004038static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004039 struct listener *l,
4040 struct sockaddr_storage *saddr)
4041{
4042 struct quic_conn *qc = NULL;
4043 struct ebmb_node *node;
4044 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004045 /* set if the quic_conn is found in the second DCID tree */
4046 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004047
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004048 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4049 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4050 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4051 /* DCIDs of first packets coming from multiple clients may have
4052 * the same values. Let's distinguish them by concatenating the
4053 * socket addresses.
4054 */
4055 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004056 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004057 pkt->dcid.len + pkt->dcid.addrlen);
4058 if (node) {
4059 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4060 goto end;
4061 }
4062 }
4063
4064 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4065 * also for INITIAL/0-RTT non-first packets with the final DCID in
4066 * used.
4067 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004068 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004069 if (!node)
4070 goto end;
4071
4072 id = ebmb_entry(node, struct quic_connection_id, node);
4073 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004074 found_in_dcid = 1;
4075
4076 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004077 if (qc)
4078 quic_conn_take(qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004079
4080 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4081 * If already done, this is a noop.
4082 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004083 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004084 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004085
4086 return qc;
4087}
4088
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004089/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4090 * will extract the parameters stored in the token : <odcid>.
4091 *
4092 * Returns 0 on success else non-zero.
4093 */
4094static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4095 struct quic_cid *odcid)
4096{
4097 uint64_t odcid_len;
4098
4099 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4100 return 1;
4101
4102 memcpy(odcid->data, token, odcid_len);
4103 odcid->len = odcid_len;
4104
4105 return 0;
4106}
4107
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004108/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4109 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4110 * parameters of this session.
4111 * This is the responsibility of the caller to check the validity of all the
4112 * pointers passed as parameter to this function.
4113 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4114 * CO_ER_SSL_NO_MEM.
4115 */
4116static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4117 unsigned char *params, size_t params_len)
4118{
4119 int retry;
4120
4121 retry = 1;
4122 retry:
4123 *ssl = SSL_new(ssl_ctx);
4124 if (!*ssl) {
4125 if (!retry--)
4126 goto err;
4127
4128 pool_gc(NULL);
4129 goto retry;
4130 }
4131
4132 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4133 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4134 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004135 SSL_free(*ssl);
4136 *ssl = NULL;
4137 if (!retry--)
4138 goto err;
4139
4140 pool_gc(NULL);
4141 goto retry;
4142 }
4143
4144 return 0;
4145
4146 err:
4147 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4148 return -1;
4149}
4150
4151/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4152 * used to process <qc> received packets. The allocated context is stored in
4153 * <qc.xprt_ctx>.
4154 *
4155 * Returns 0 on success else non-zero.
4156 */
4157int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4158{
4159 struct bind_conf *bc = qc->li->bind_conf;
4160 struct ssl_sock_ctx *ctx = NULL;
4161
4162 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4163 if (!ctx)
4164 goto err;
4165
4166 ctx->wait_event.tasklet = tasklet_new();
4167 if (!ctx->wait_event.tasklet)
4168 goto err;
4169
4170 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4171 ctx->wait_event.tasklet->context = ctx;
4172 ctx->wait_event.events = 0;
4173 ctx->subs = NULL;
4174 ctx->xprt_ctx = NULL;
4175 ctx->qc = qc;
4176
4177 /* Set tasklet tid based on the SCID selected by us for this
4178 * connection. The upper layer will also be binded on the same thread.
4179 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004180 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004181
4182 if (qc_is_listener(qc)) {
4183 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4184 qc->enc_params, qc->enc_params_len) == -1) {
4185 goto err;
4186 }
4187
4188 /* Enabling 0-RTT */
4189 if (bc->ssl_conf.early_data)
4190 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4191
4192 SSL_set_accept_state(ctx->ssl);
4193 }
4194
4195 ctx->xprt = xprt_get(XPRT_QUIC);
4196
4197 /* Store the allocated context in <qc>. */
4198 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
4199
4200 return 0;
4201
4202 err:
4203 if (ctx && ctx->wait_event.tasklet)
4204 tasklet_free(ctx->wait_event.tasklet);
4205 pool_free(pool_head_quic_conn_ctx, ctx);
4206
4207 return 1;
4208}
4209
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004210static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004211 struct quic_rx_packet *pkt, int first_pkt,
4212 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004213{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004214 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004215 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004216 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004217 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004218 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004219 size_t b_cspace;
4220 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004221
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004222 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004223 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004224 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004225 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004226 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4227 /* This ist only to please to traces and distinguish the
4228 * packet with parsed packet number from others.
4229 */
4230 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004231 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004232 goto err;
4233
4234 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004235 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004236 /* XXX TO BE DISCARDED */
4237 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4238 goto err;
4239 }
4240
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004241 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004242 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004243 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004244 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004245 uint64_t len;
4246
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004247 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004248 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4249 goto err;
4250 }
4251
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004252 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4253 * they must have the same DCID.
4254 */
4255 if (!first_pkt &&
4256 (pkt->dcid.len != dgram->dcid_len ||
4257 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4258 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4259 goto err;
4260 }
4261
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004262 /* Retry of Version Negotiation packets are only sent by servers */
4263 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4264 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4265 goto err;
4266 }
4267
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004268 /* RFC9000 6. Version Negotiation */
4269 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004270 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004271 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004272 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4273 goto err;
4274 }
4275
4276 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004277 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004278 }
4279
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004280 /* For Initial packets, and for servers (QUIC clients connections),
4281 * there is no Initial connection IDs storage.
4282 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004283 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004284 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004285
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004286 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4287 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004288 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4289 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004290 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004291
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004292 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4293 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004294 */
4295 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004296
4297 /* TODO Retry should be automatically activated if
4298 * suspect network usage is detected.
4299 */
4300 if (!token_len && l->bind_conf->quic_force_retry) {
4301 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004302 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004303 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4304 goto err;
4305 }
4306
4307 goto err;
4308 }
4309 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004310 pkt->token = buf;
4311 buf += pkt->token_len;
4312 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004313 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004314 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004315 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004316 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4317 goto err;
4318 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004319 }
4320
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004321 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4322 end - buf < len) {
4323 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4324 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004325 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004326
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004327 payload = buf;
4328 pkt->len = len + payload - beg;
4329
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004330 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004331 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004332 int ipv4;
4333 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004334 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004335 const unsigned char *salt = initial_salt_v1;
4336 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004337
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004338 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004339 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004340 goto err;
4341 }
4342
Frédéric Lécailledc364042022-01-27 16:51:54 +01004343 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4344 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4345 goto err;
4346 }
4347
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004348 pkt->saddr = dgram->saddr;
4349 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004350 qc = qc_new_conn(pkt->version, ipv4,
4351 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004352 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004353 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004354 goto err;
4355
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004356 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4357
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004358 odcid = &qc->rx.params.original_destination_connection_id;
4359 /* Copy the transport parameters. */
4360 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004361
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004362 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004363 if (pkt->token_len) {
4364 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4365 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4366 goto err;
4367 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004368 /* Copy retry_source_connection_id transport parameter. */
4369 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4370 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004371 }
4372 else {
4373 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4374 odcid->len = pkt->dcid.len;
4375 }
4376
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004377 /* Copy the initial source connection ID. */
4378 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4379 qc->enc_params_len =
4380 quic_transport_params_encode(qc->enc_params,
4381 qc->enc_params + sizeof qc->enc_params,
4382 &qc->rx.params, 1);
4383 if (!qc->enc_params_len)
4384 goto err;
4385
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004386 if (qc_conn_alloc_ssl_ctx(qc))
4387 goto err;
4388
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004389 if (!quic_conn_init_timer(qc))
4390 goto err;
4391
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004392 /* NOTE: the socket address has been concatenated to the destination ID
4393 * chosen by the client for Initial packets.
4394 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004395 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4396 salt = initial_salt_draft_29;
4397 salt_len = sizeof initial_salt_draft_29;
4398 }
4399 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004400 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004401 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004402 goto err;
4403 }
4404
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004405 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004406 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004407 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004408
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004409 /* If the insertion failed, it means that another
4410 * thread has already allocated a QUIC connection for
4411 * the same CID. Liberate our allocated connection.
4412 */
4413 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004414 qc_to_purge = qc;
4415
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004416 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4417 pkt->qc = qc;
4418 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004419
4420 quic_conn_take(qc);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004421
4422 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004423 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004424 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004425 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004426 else {
4427 quic_conn_drop(qc_to_purge);
4428 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004429 }
4430 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004431 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004432 }
4433 }
4434 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004435 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004436 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4437 goto err;
4438 }
4439
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004440 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004441 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004442
4443 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4444 * they must have the same DCID.
4445 */
4446 if (!first_pkt &&
4447 (pkt->dcid.len != dgram->dcid_len ||
4448 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4449 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4450 goto err;
4451 }
4452
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004453 buf += QUIC_HAP_CID_LEN;
4454
4455 /* A short packet is the last one of a UDP datagram. */
4456 payload = buf;
4457 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004458
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004459 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004460 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004461 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004462 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4463 goto err;
4464 }
4465
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004466 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467 }
4468
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004469
4470 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4471 * they must have the same DCID.
4472 *
4473 * This check must be done after the final update to pkt.len to
4474 * properly drop the packet on failure.
4475 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004476 if (first_pkt && !quic_peer_validated_addr(qc) &&
4477 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4478 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4479 QUIC_EV_CONN_LPKT, qc);
4480 /* Reset the anti-amplification bit. It will be set again
4481 * when sending the next packet if reached again.
4482 */
4483 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4484 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4485 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004486 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004487
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004488 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004489
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004490 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004491 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004492 goto out;
4493 }
4494
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004495 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004496 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004497 b_cspace = b_contig_space(&qc->rx.buf);
4498 if (b_cspace < pkt->len) {
4499 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004500 if (b_cspace) {
4501 b_putchr(&qc->rx.buf, 0x00);
4502 b_cspace--;
4503 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004504 b_add(&qc->rx.buf, b_cspace);
4505 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004506 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004507 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004508 goto err;
4509 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004510 }
4511
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004512 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004513 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004514 goto err;
4515 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004516
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004517 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004518 if (pkt->aad_len)
4519 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004520 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004521 /* Wake up the connection packet handler task from here only if all
4522 * the contexts have been initialized, especially the mux context
4523 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4524 * will start it if these contexts for the connection are not already
4525 * initialized.
4526 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004527 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004528 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004529 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004530
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004531 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004532
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004533 if (qc)
4534 quic_conn_drop(qc);
4535
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004536 return pkt->len;
4537
4538 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004539 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4540 * This cannot be done by this thread.
4541 */
4542 if (io_cb_wakeup) {
4543 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4544 if (conn_ctx && conn_ctx->wait_event.tasklet)
4545 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4546 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004547 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004548 if (!pkt->len)
4549 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004550 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004551 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004552
4553 if (qc)
4554 quic_conn_drop(qc);
4555
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004556 return -1;
4557}
4558
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004559/* This function builds into <buf> buffer a QUIC long packet header.
4560 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004561 */
4562static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4563 int type, size_t pn_len, struct quic_conn *conn)
4564{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004565 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004566 return 0;
4567
4568 /* #0 byte flags */
4569 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4570 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4571 /* Version */
4572 quic_write_uint32(buf, end, conn->version);
4573 *(*buf)++ = conn->dcid.len;
4574 /* Destination connection ID */
4575 if (conn->dcid.len) {
4576 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4577 *buf += conn->dcid.len;
4578 }
4579 /* Source connection ID */
4580 *(*buf)++ = conn->scid.len;
4581 if (conn->scid.len) {
4582 memcpy(*buf, conn->scid.data, conn->scid.len);
4583 *buf += conn->scid.len;
4584 }
4585
4586 return 1;
4587}
4588
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004589/* This function builds into <buf> buffer a QUIC short packet header.
4590 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004591 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004592static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4593 size_t pn_len, struct quic_conn *conn,
4594 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004595{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004596 if (end - *buf < 1 + conn->dcid.len)
4597 return 0;
4598
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004599 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004600 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4601 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004602 /* Destination connection ID */
4603 if (conn->dcid.len) {
4604 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4605 *buf += conn->dcid.len;
4606 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004607
4608 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004609}
4610
4611/* Apply QUIC header protection to the packet with <buf> as first byte address,
4612 * <pn> as address of the Packet number field, <pnlen> being this field length
4613 * with <aead> as AEAD cipher and <key> as secret key.
4614 * Returns 1 if succeeded or 0 if failed.
4615 */
4616static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4617 const EVP_CIPHER *aead, const unsigned char *key)
4618{
4619 int i, ret, outlen;
4620 EVP_CIPHER_CTX *ctx;
4621 /* We need an IV of at least 5 bytes: one byte for bytes #0
4622 * and at most 4 bytes for the packet number
4623 */
4624 unsigned char mask[5] = {0};
4625
4626 ret = 0;
4627 ctx = EVP_CIPHER_CTX_new();
4628 if (!ctx)
4629 return 0;
4630
4631 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4632 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4633 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4634 goto out;
4635
4636 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4637 for (i = 0; i < pnlen; i++)
4638 pn[i] ^= mask[i + 1];
4639
4640 ret = 1;
4641
4642 out:
4643 EVP_CIPHER_CTX_free(ctx);
4644
4645 return ret;
4646}
4647
4648/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4649 * ACK ranges if needed to a value below <limit> in bytes.
4650 * Return 1 if succeeded, 0 if not.
4651 */
4652static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4653{
4654 size_t room, ack_delay_sz;
4655
4656 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4657 /* A frame is made of 1 byte for the frame type. */
4658 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004659 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004660 return 0;
4661
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004662 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004663}
4664
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004665/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4666 * for <qel> encryption level to be encoded in a buffer with <room> as available room,
Frédéric Lécailleea604992020-12-24 13:01:37 +01004667 * and <*len> the packet Length field initialized with the number of bytes already present
4668 * in this buffer which must be taken into an account for the Length packet field value.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004669 * <headlen> is the number of bytes already present in this packet before building frames.
4670 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004671 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004672 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004673 * Return 1 if succeeded, 0 if not.
4674 */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004675static inline int qc_build_frms(struct list *l,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004676 size_t room, size_t *len, size_t headlen,
4677 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004678 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004679{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004680 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004681 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004682
Frédéric Lécailleea604992020-12-24 13:01:37 +01004683 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004684 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004685 return 0;
4686
Frédéric Lécailleea604992020-12-24 13:01:37 +01004687 /* If we are not probing we must take into an account the congestion
4688 * control window.
4689 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004690 if (!qel->pktns->tx.pto_probe) {
4691 size_t remain = quic_path_prep_data(qc->path);
4692
4693 if (headlen > remain)
4694 return 0;
4695
4696 room = QUIC_MIN(room, remain - headlen);
4697 }
4698
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004699 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004700 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004701 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004702 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004703 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004704
Frédéric Lécailleea604992020-12-24 13:01:37 +01004705 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004706 break;
4707
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004708 switch (cf->type) {
4709 case QUIC_FT_CRYPTO:
4710 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004711 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004712 /* Compute the length of this CRYPTO frame header */
4713 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4714 /* Compute the data length of this CRyPTO frame. */
4715 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4716 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004717 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004718 if (!dlen)
4719 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004720
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004721 /* CRYPTO frame length. */
4722 flen = hlen + quic_int_getsize(dlen) + dlen;
4723 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004724 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004725 /* Add the CRYPTO data length and its encoded length to the packet
4726 * length and the length of this length.
4727 */
4728 *len += flen;
4729 room -= flen;
4730 if (dlen == cf->crypto.len) {
4731 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004732 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004733 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004734 }
4735 else {
4736 struct quic_frame *new_cf;
4737
4738 new_cf = pool_alloc(pool_head_quic_frame);
4739 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004740 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004741 return 0;
4742 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004743
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004744 new_cf->type = QUIC_FT_CRYPTO;
4745 new_cf->crypto.len = dlen;
4746 new_cf->crypto.offset = cf->crypto.offset;
4747 new_cf->crypto.qel = qel;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004748 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004749 /* Consume <dlen> bytes of the current frame. */
4750 cf->crypto.len -= dlen;
4751 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004752 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004753 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004754
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004755 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004756 /* Note that these frames are accepted in short packets only without
4757 * "Length" packet field. Here, <*len> is used only to compute the
4758 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004759 *
4760 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004761 * excepting the variable ones. Note that +1 is for the type of this frame.
4762 */
4763 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004764 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004765 /* Compute the data length of this STREAM frame. */
4766 avail_room = room - hlen - *len;
4767 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004768 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004769
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004770 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004771 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004772 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4773 dlen = max_available_room(avail_room, &dlen_sz);
4774 if (dlen > cf->stream.len) {
4775 dlen = cf->stream.len;
4776 }
4777 dlen_sz = quic_int_getsize(dlen);
4778 flen = hlen + dlen_sz + dlen;
4779 }
4780 else {
4781 dlen = QUIC_MIN(avail_room, cf->stream.len);
4782 flen = hlen + dlen;
4783 }
4784 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004785 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004786 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004787 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004788 /* Add the STREAM data length and its encoded length to the packet
4789 * length and the length of this length.
4790 */
4791 *len += flen;
4792 room -= flen;
4793 if (dlen == cf->stream.len) {
4794 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004795 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004796 LIST_APPEND(l, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004797 }
4798 else {
4799 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004800 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004801
4802 new_cf = pool_zalloc(pool_head_quic_frame);
4803 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004804 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004805 return 0;
4806 }
4807
4808 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004809 new_cf->stream.qcs = cf->stream.qcs;
4810 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004811 new_cf->stream.id = cf->stream.id;
4812 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4813 new_cf->stream.offset = cf->stream.offset;
4814 new_cf->stream.len = dlen;
4815 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4816 /* FIN bit reset */
4817 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4818 new_cf->stream.data = cf->stream.data;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004819 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004820 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4821 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004822 cf_buf = b_make(b_orig(cf->stream.buf),
4823 b_size(cf->stream.buf),
4824 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004825 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004826 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004827 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004828 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004829 break;
4830
4831 default:
4832 flen = qc_frm_len(cf);
4833 BUG_ON(!flen);
4834 if (flen > room)
4835 continue;
4836
4837 *len += flen;
4838 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004839 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004840 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004841 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004842 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004843 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004844 }
4845
Frédéric Lécailleea604992020-12-24 13:01:37 +01004846 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004847}
4848
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004849/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004850 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4851 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4852 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004853 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004854 * reserved so that to ensure there is enough room to build this AEAD TAG after
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004855 * having returned from this function.
4856 * This function also updates the value of <buf_pn> pointer to point to the packet
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004857 * number field in this packet. <pn_len> will also have the packet number
4858 * length as value.
4859 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004860 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004861 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004862static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4863 size_t dglen, struct quic_tx_packet *pkt,
4864 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004865 int ack, int padding, int cc, int probe,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004866 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004867{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004868 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004869 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004870 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4871 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004872 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004873 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004874 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004875 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004876 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004877
Frédéric Lécailleea604992020-12-24 13:01:37 +01004878 /* Length field value with CRYPTO frames if present. */
4879 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004880 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004881 /* When not probing and not acking, and no immediate close is required,
4882 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004883 * the congestion controller window. So, we do not limit the size of this
4884 * packet if we have an ACK frame to send because an ACK frame is not
4885 * ack-eliciting. This size will be limited if we have ack-eliciting
4886 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004887 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004888 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004889 size_t path_room;
4890
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004891 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004892 if (end - beg > path_room)
4893 end = beg + path_room;
4894 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004895
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004896 /* Ensure there is enough room for the TLS encryption tag and a zero token
4897 * length field if any.
4898 */
4899 if (end - pos < QUIC_TLS_TAG_LEN +
4900 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4901 goto no_room;
4902
4903 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004904 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004905 /* packet number length */
4906 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004907 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004908 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004909 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004910 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004911 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004912 goto no_room;
4913
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004914 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4915 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004916 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004917 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004918 /* Build an ACK frame if required. */
4919 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004920 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004921 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004922 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004923 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4924 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4925 * that from here, we do not know if we will have to send a PING frame.
4926 * This will be decided after having computed the ack-eliciting frames
4927 * to be added to this packet.
4928 */
4929 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - 1 - *pn_len - pos);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004930 if (!ack_frm_len)
4931 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004932 }
4933
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004934 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004935 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004936 len_frms = 0;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004937 if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004938 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004939
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004940 /* Initialize the length of the frames built below to <len>.
4941 * If any frame could be successfully built by qc_build_frms(),
4942 * we will have len_frms > len.
4943 */
4944 len_frms = len;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004945 if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004946 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004947 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004948 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004949 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004950
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004951 /* Length (of the remaining data). Must not fail because, the buffer size
4952 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4953 * for the encryption tag. It must be taken into an account for the length
4954 * of this packet.
4955 */
4956 if (len_frms)
4957 len = len_frms + QUIC_TLS_TAG_LEN;
4958 else
4959 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004960 /* CONNECTION_CLOSE frame */
4961 if (cc) {
4962 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004963
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004964 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004965 len += qc_frm_len(&cc_frm);
4966 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004967 add_ping_frm = 0;
4968 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004969 len_sz = quic_int_getsize(len);
4970 /* Add this packet size to <dglen> */
4971 dglen += head_len + len_sz + len;
4972 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4973 /* This is a maximum padding size */
4974 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4975 /* The length field value is of this packet is <len> + <padding_len>
4976 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004977 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004978 * sizes from <padding_len>.
4979 */
4980 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4981 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004982 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004983 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004984 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004985 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004986 add_ping_frm = 1;
4987 len += 1;
4988 }
4989 /* If there is no frame at all to follow, add at least a PADDING frame. */
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004990 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004991 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4992 }
4993
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004994 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4995 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004996
4997 /* Packet number field address. */
4998 *buf_pn = pos;
4999
5000 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005001 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5002 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005003
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005004 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005005 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005006
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005007 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005008 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005009 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005010
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005011 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005012 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005013 ssize_t room = end - pos;
5014 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005015 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005016 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005017 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005018 }
5019 }
5020
5021 /* Build a PING frame if needed. */
5022 if (add_ping_frm) {
5023 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005024 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005025 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005026 }
5027
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005028 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005029 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005030 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005031
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005032 /* Build a PADDING frame if needed. */
5033 if (padding_len) {
5034 frm.type = QUIC_FT_PADDING;
5035 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005036 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005037 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005038 }
5039
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005040 /* If this packet is ack-eliciting and we are probing let's
5041 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005042 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005043 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5044 qel->pktns->tx.pto_probe)
5045 qel->pktns->tx.pto_probe--;
5046
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005047 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005048 LIST_SPLICE(&pkt->frms, &frm_list);
5049 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005050
5051 return 1;
5052
5053 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005054 /* Replace the pre-built frames which could not be add to this packet */
5055 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005056 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005057 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005058}
5059
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005060static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005061{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005062 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005063 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005064 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005065 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005066 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005067 pkt->next = NULL;
5068 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005069}
5070
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005071/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005072static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5073{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005074 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005075
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005076 if (!pkt)
5077 return;
5078
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005079 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005080 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005081 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005082 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005083 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005084}
5085
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005086/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5087 * type for <qc> QUIC connection from <qel> encryption level.
5088 * Return -2 if the packet could not be allocated or encrypted for any reason,
5089 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005090 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005091static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5092 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005093 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005094 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005095 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005096{
5097 /* The pointer to the packet number field. */
5098 unsigned char *buf_pn;
5099 unsigned char *beg, *end, *payload;
5100 int64_t pn;
5101 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005102 struct quic_tls_ctx *tls_ctx;
5103 struct quic_tx_packet *pkt;
5104
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005105 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005106 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005107 pkt = pool_alloc(pool_head_quic_tx_packet);
5108 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005109 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005110 *err = -2;
5111 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005112 }
5113
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005114 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005115 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005116 pn_len = 0;
5117 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005118
5119 pn = qel->pktns->tx.next_pn + 1;
5120 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005121 ack, padding, cc, probe, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005122 *err = -1;
5123 goto err;
5124 }
5125
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005126 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005127 payload = buf_pn + pn_len;
5128 payload_len = end - payload;
5129 aad_len = payload - beg;
5130
5131 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005132 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005133 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005134 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005135 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136
5137 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005138 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005139 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5140 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005141 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005142 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005143 goto err;
5144 }
5145
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005146 /* Consume a packet number */
5147 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005148 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005149 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5150 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005151 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5152 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005153 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005154 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005155 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005156 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005157 pkt->in_flight_len = pkt->len;
5158 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005159 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005160 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005161 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005162
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005163 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005164
5165 err:
5166 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005167 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005168 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005169}
5170
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005171
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005172/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5173 * event subscriber <es> is not allowed to change from a previous call as long
5174 * as at least one event is still subscribed. The <event_type> must only be a
5175 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5176 */
5177static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5178{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005179 struct qcc *qcc = conn->qc->qcc;
5180
5181 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5182 BUG_ON(qcc->subs && qcc->subs != es);
5183
5184 es->events |= event_type;
5185 qcc->subs = es;
5186
5187 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005188 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005189
5190 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005191 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005192
5193 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005194}
5195
5196/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5197 * The <es> pointer is not allowed to differ from the one passed to the
5198 * subscribe() call. It always returns zero.
5199 */
5200static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5201{
5202 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5203}
5204
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005205/* Store in <xprt_ctx> the context attached to <conn>.
5206 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005207 */
5208static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5209{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005210 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005211
5212 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5213
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005214 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005215 if (*xprt_ctx)
5216 goto out;
5217
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005218 HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005219
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005220 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005221 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005222
5223 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005224}
5225
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005226/* Start the QUIC transport layer */
5227static int qc_xprt_start(struct connection *conn, void *ctx)
5228{
5229 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005230 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005231
5232 qc = conn->qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005233 quic_mux_transport_params_update(qc->qcc);
5234 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5235 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5236 return 0;
5237 }
5238
5239 /* mux-quic can now be considered ready. */
5240 qc->mux_state = QC_MUX_READY;
5241
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005242 tasklet_wakeup(qctx->wait_event.tasklet);
5243 return 1;
5244}
5245
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005246/* transport-layer operations for QUIC connections. */
5247static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005248 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005249 .subscribe = quic_conn_subscribe,
5250 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005251 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005252 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005253 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5254 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005255 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005256 .name = "QUIC",
5257};
5258
5259__attribute__((constructor))
5260static void __quic_conn_init(void)
5261{
5262 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5263 xprt_register(XPRT_QUIC, &ssl_quic);
5264}
5265
5266__attribute__((destructor))
5267static void __quic_conn_deinit(void)
5268{
5269 BIO_meth_free(ha_quic_meth);
5270}
5271
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005272/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5273 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005274 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005275 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005276struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005277{
5278 unsigned char *pos;
5279 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005280 struct quic_dghdlr *dghdlr = ctx;
5281 struct quic_dgram *dgram;
5282 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005283
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005284 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005285 pos = dgram->buf;
5286 end = pos + dgram->len;
5287 do {
5288 int ret;
5289 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005290
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005291 pkt = pool_zalloc(pool_head_quic_rx_packet);
5292 if (!pkt)
5293 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005294
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005295 quic_rx_packet_refinc(pkt);
5296 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5297 first_pkt = 0;
5298 pos += pkt->len;
5299 quic_rx_packet_refdec(pkt);
5300 if (ret == -1)
5301 /* If the packet length could not be found, we cannot continue. */
5302 break;
5303 } while (pos < end);
5304
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005305 /* Increasing the received bytes counter by the UDP datagram length
5306 * if this datagram could be associated to a connection.
5307 */
5308 if (dgram->qc)
5309 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005310
5311 /* Mark this datagram as consumed */
5312 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005313 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005314
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005315 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005316
5317 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005318 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005319}
5320
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005321/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5322 * Returns 1 if succeeded, 0 if not.
5323 */
5324static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5325 unsigned char **dcid, size_t *dcid_len)
5326{
5327 int long_header;
5328 size_t minlen, skip;
5329
5330 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5331 goto err;
5332
5333 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5334 minlen = long_header ?
5335 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5336 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005337 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005338 goto err;
5339
5340 buf += skip;
5341 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5342 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5343 goto err;
5344
5345 *dcid = buf;
5346
5347 return 1;
5348
5349 err:
5350 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5351 return 0;
5352}
5353
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005354/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5355 * correct datagram handler.
5356 * Return 1 if a correct datagram could be found, 0 if not.
5357 */
5358int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5359 struct sockaddr_storage *saddr,
5360 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005361{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005362 struct quic_dgram *dgram;
5363 unsigned char *dcid;
5364 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005365 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005366
5367 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005368 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005369
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005370 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005371 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005372 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005373
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005374 cid_tid = quic_get_cid_tid(dcid);
5375
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005376 /* All the members must be initialized! */
5377 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005378 dgram->buf = buf;
5379 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005380 dgram->dcid = dcid;
5381 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005382 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005383 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005384 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005385 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005386
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005387 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005388
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005389 return 1;
5390
5391 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005392 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005393}
5394
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005395/* Function to automatically activate QUIC traces on stdout.
5396 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5397 * Main use for now is in the docker image for QUIC interop testing.
5398 */
5399static void quic_init_stdout_traces(void)
5400{
5401#ifdef ENABLE_QUIC_STDOUT_TRACES
5402 trace_quic.sink = sink_find("stdout");
5403 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005404 trace_quic.state = TRACE_STATE_RUNNING;
5405#endif
5406}
5407INITCALL0(STG_INIT, quic_init_stdout_traces);
5408
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005409/*
5410 * Local variables:
5411 * c-indent-level: 8
5412 * c-basic-offset: 8
5413 * End:
5414 */