blob: a1fa7dbdecdf918658c5e79b84a284c7f34e8369 [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>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010049#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010050#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010051#include <haproxy/ssl_sock.h>
52#include <haproxy/stream_interface.h>
53#include <haproxy/task.h>
54#include <haproxy/trace.h>
55#include <haproxy/xprt_quic.h>
56
Amaury Denoyellea22d8602021-11-10 15:17:56 +010057/* list of supported QUIC versions by this implementation */
58static int quic_supported_version[] = {
59 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010060 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010061
62 /* placeholder, do not add entry after this */
63 0x0
64};
65
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020066/* This is the values of some QUIC transport parameters when absent.
67 * Should be used to initialize any transport parameters (local or remote)
68 * before updating them with customized values.
69 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010070struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020071 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020072 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
73 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020074 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010075};
76
77/* trace source and events */
78static void quic_trace(enum trace_level level, uint64_t mask, \
79 const struct trace_source *src,
80 const struct ist where, const struct ist func,
81 const void *a1, const void *a2, const void *a3, const void *a4);
82
83static const struct trace_event quic_trace_events[] = {
84 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
85 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
86 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
87 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
88 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
89 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
90 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050091 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010092 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
93 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
94 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
95 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
96 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
97 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
98 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
99 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
100 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
101 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
102 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
103 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
104 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
105 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
106 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
107 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
108 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
109 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
110 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100111 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
112 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
113 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
114 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
115 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
116 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100117 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200118 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
119 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100120 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
121 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100122 { /* end */ }
123};
124
125static const struct name_desc quic_trace_lockon_args[4] = {
126 /* arg1 */ { /* already used by the connection */ },
127 /* arg2 */ { .name="quic", .desc="QUIC transport" },
128 /* arg3 */ { },
129 /* arg4 */ { }
130};
131
132static const struct name_desc quic_trace_decoding[] = {
133#define QUIC_VERB_CLEAN 1
134 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
135 { /* end */ }
136};
137
138
139struct trace_source trace_quic = {
140 .name = IST("quic"),
141 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100142 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100143 .default_cb = quic_trace,
144 .known_events = quic_trace_events,
145 .lockon_args = quic_trace_lockon_args,
146 .decoding = quic_trace_decoding,
147 .report_events = ~0, /* report everything by default */
148};
149
150#define TRACE_SOURCE &trace_quic
151INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
152
153static BIO_METHOD *ha_quic_meth;
154
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200155DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200156DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_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écaille4015cbb2021-12-14 19:29:34 +0100782
783 if (!write_secret)
784 goto tp;
785
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100786 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
787 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100788 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100789 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100790 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100791 }
792
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100793 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100794 tp:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100795 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100796 const unsigned char *buf;
797 size_t buflen;
798
799 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
800 if (!buflen)
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100801 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100802
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100803 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100804 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100805 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100806
807 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100808 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
809 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
810 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
811
812 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
813 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100814 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100815 goto err;
816 }
817
818 memcpy(rx->secret, read_secret, secret_len);
819 rx->secretlen = secret_len;
820 memcpy(tx->secret, write_secret, secret_len);
821 tx->secretlen = secret_len;
822 /* Initialize all the secret keys lengths */
823 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
824 /* Prepare the next key update */
825 if (!quic_tls_key_update(qc))
826 goto err;
827 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100828 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100829 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100830 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100831
832 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100833 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100834 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100835}
836#else
837/* ->set_read_secret callback to derive the RX secrets at <level> encryption
838 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500839 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100840 */
841int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
842 const SSL_CIPHER *cipher,
843 const uint8_t *secret, size_t secret_len)
844{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100845 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100846 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100847 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100848
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100849 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
850 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
851 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100852 goto out;
853 }
854
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100855 tls_ctx->rx.aead = tls_aead(cipher);
856 tls_ctx->rx.md = tls_md(cipher);
857 tls_ctx->rx.hp = tls_hp(cipher);
858
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100859 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
860 goto err;
861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100862 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 +0100863 tls_ctx->rx.key, tls_ctx->rx.keylen,
864 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100865 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
866 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100867 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100868 goto err;
869 }
870
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100871 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100872 const unsigned char *buf;
873 size_t buflen;
874
875 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
876 if (!buflen)
877 goto err;
878
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100879 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100880 goto err;
881 }
882
883 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100884 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100885 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100886
887 return 1;
888
889 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100890 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100891 return 0;
892}
893
894/* ->set_write_secret callback to derive the TX secrets at <level>
895 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500896 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100897 */
898int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
899 const SSL_CIPHER *cipher,
900 const uint8_t *secret, size_t secret_len)
901{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100902 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
903 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 +0100904
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100905 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
906 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
907 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100908 goto out;
909 }
910
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100911 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
912 goto err;
913
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100914 tls_ctx->tx.aead = tls_aead(cipher);
915 tls_ctx->tx.md = tls_md(cipher);
916 tls_ctx->tx.hp = tls_hp(cipher);
917
918 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 +0100919 tls_ctx->tx.key, tls_ctx->tx.keylen,
920 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100921 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
922 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100923 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100924 goto err;
925 }
926
927 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100928 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100929 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100930 return 1;
931
932 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100933 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100934 return 0;
935}
936#endif
937
938/* This function copies the CRYPTO data provided by the TLS stack found at <data>
939 * with <len> as size in CRYPTO buffers dedicated to store the information about
940 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
941 * It fails only if it could not managed to allocate enough CRYPTO buffers to
942 * store all the data.
943 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
944 */
945static int quic_crypto_data_cpy(struct quic_enc_level *qel,
946 const unsigned char *data, size_t len)
947{
948 struct quic_crypto_buf **qcb;
949 /* The remaining byte to store in CRYPTO buffers. */
950 size_t cf_offset, cf_len, *nb_buf;
951 unsigned char *pos;
952
953 nb_buf = &qel->tx.crypto.nb_buf;
954 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
955 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
956 cf_len = len;
957
958 while (len) {
959 size_t to_copy, room;
960
961 pos = (*qcb)->data + (*qcb)->sz;
962 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
963 to_copy = len > room ? room : len;
964 if (to_copy) {
965 memcpy(pos, data, to_copy);
966 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
967 qel->tx.crypto.sz += to_copy;
968 (*qcb)->sz += to_copy;
969 pos += to_copy;
970 len -= to_copy;
971 data += to_copy;
972 }
973 else {
974 struct quic_crypto_buf **tmp;
975
976 tmp = realloc(qel->tx.crypto.bufs,
977 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
978 if (tmp) {
979 qel->tx.crypto.bufs = tmp;
980 qcb = &qel->tx.crypto.bufs[*nb_buf];
981 *qcb = pool_alloc(pool_head_quic_crypto_buf);
982 if (!*qcb)
983 return 0;
984
985 (*qcb)->sz = 0;
986 ++*nb_buf;
987 }
988 else {
989 break;
990 }
991 }
992 }
993
994 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
995 * have been buffered.
996 */
997 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200998 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100999 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001000
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001001 /* There is at most one CRYPTO frame in this packet number
1002 * space. Let's look for it.
1003 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001004 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001005 if (frm->type != QUIC_FT_CRYPTO)
1006 continue;
1007
1008 /* Found */
1009 found = frm;
1010 break;
1011 }
1012
1013 if (found) {
1014 found->crypto.len += cf_len;
1015 }
1016 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001017 frm = pool_alloc(pool_head_quic_frame);
1018 if (!frm)
1019 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001020
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001021 frm->type = QUIC_FT_CRYPTO;
1022 frm->crypto.offset = cf_offset;
1023 frm->crypto.len = cf_len;
1024 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001025 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001026 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001027 }
1028
1029 return len == 0;
1030}
1031
1032
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001033/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1034void quic_set_tls_alert(struct quic_conn *qc, int alert)
1035{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001036 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001037 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001038 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001039}
1040
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001041/* Set the application for <qc> QUIC connection.
1042 * Return 1 if succeeded, 0 if not.
1043 */
1044int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1045{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001046 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1047 qc->app_ops = &h3_ops;
1048 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1049 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001050 else
1051 return 0;
1052
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001053 return 1;
1054}
1055
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001056/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1057 * wants to provide the QUIC layer with CRYPTO data.
1058 * Returns 1 if succeeded, 0 if not.
1059 */
1060int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1061 const uint8_t *data, size_t len)
1062{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001063 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001064 enum quic_tls_enc_level tel;
1065 struct quic_enc_level *qel;
1066
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001067 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1068 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1069 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1070 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001071 goto out;
1072 }
1073
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001074 tel = ssl_to_quic_enc_level(level);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001075 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001076
1077 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001078 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001079 goto err;
1080 }
1081
1082 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001083 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001084 goto err;
1085 }
1086
1087 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001088 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001090 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001091 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001092 return 1;
1093
1094 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001095 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096 return 0;
1097}
1098
1099int ha_quic_flush_flight(SSL *ssl)
1100{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001101 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001102
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001103 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1104 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001105
1106 return 1;
1107}
1108
1109int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1110{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001111 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001112
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001113 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1114 quic_set_tls_alert(qc, alert);
1115 HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001116 return 1;
1117}
1118
1119/* QUIC TLS methods */
1120static SSL_QUIC_METHOD ha_quic_method = {
1121#ifdef OPENSSL_IS_BORINGSSL
1122 .set_read_secret = ha_set_rsec,
1123 .set_write_secret = ha_set_wsec,
1124#else
1125 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1126#endif
1127 .add_handshake_data = ha_quic_add_handshake_data,
1128 .flush_flight = ha_quic_flush_flight,
1129 .send_alert = ha_quic_send_alert,
1130};
1131
1132/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1133 * Returns an error count.
1134 */
1135int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1136{
1137 struct proxy *curproxy = bind_conf->frontend;
1138 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1139 int cfgerr = 0;
1140
1141#if 0
1142 /* XXX Did not manage to use this. */
1143 const char *ciphers =
1144 "TLS_AES_128_GCM_SHA256:"
1145 "TLS_AES_256_GCM_SHA384:"
1146 "TLS_CHACHA20_POLY1305_SHA256:"
1147 "TLS_AES_128_CCM_SHA256";
1148#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001149 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001150 long options =
1151 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1152 SSL_OP_SINGLE_ECDH_USE |
1153 SSL_OP_CIPHER_SERVER_PREFERENCE;
1154 SSL_CTX *ctx;
1155
1156 ctx = SSL_CTX_new(TLS_server_method());
1157 bind_conf->initial_ctx = ctx;
1158
1159 SSL_CTX_set_options(ctx, options);
1160#if 0
1161 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1162 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1163 "for bind '%s' at [%s:%d].\n",
1164 curproxy->id, ciphers,
1165 bind_conf->arg, bind_conf->file, bind_conf->line);
1166 cfgerr++;
1167 }
1168#endif
1169
1170 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1171 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1172 "for bind '%s' at [%s:%d].\n",
1173 curproxy->id, groups,
1174 bind_conf->arg, bind_conf->file, bind_conf->line);
1175 cfgerr++;
1176 }
1177
1178 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1179 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1180 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1181 SSL_CTX_set_default_verify_paths(ctx);
1182
1183#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1184#ifdef OPENSSL_IS_BORINGSSL
1185 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1186 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1187#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1188 if (bind_conf->ssl_conf.early_data) {
1189 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001190 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001191 }
1192 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1193 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1194#else
1195 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1196#endif
1197 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1198#endif
1199 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1200
1201 return cfgerr;
1202}
1203
1204/* Decode an expected packet number from <truncated_on> its truncated value,
1205 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1206 * the number of bits used to encode this packet number (its length in bytes * 8).
1207 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1208 */
1209static uint64_t decode_packet_number(uint64_t largest_pn,
1210 uint32_t truncated_pn, unsigned int pn_nbits)
1211{
1212 uint64_t expected_pn = largest_pn + 1;
1213 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1214 uint64_t pn_hwin = pn_win / 2;
1215 uint64_t pn_mask = pn_win - 1;
1216 uint64_t candidate_pn;
1217
1218
1219 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1220 /* Note that <pn_win> > <pn_hwin>. */
1221 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1222 candidate_pn + pn_hwin <= expected_pn)
1223 return candidate_pn + pn_win;
1224
1225 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1226 return candidate_pn - pn_win;
1227
1228 return candidate_pn;
1229}
1230
1231/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1232 * cryptographic context.
1233 * <largest_pn> is the largest received packet number and <pn> the address of
1234 * the packet number field for this packet with <byte0> address of its first byte.
1235 * <end> points to one byte past the end of this packet.
1236 * Returns 1 if succeeded, 0 if not.
1237 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001238static int qc_do_rm_hp(struct quic_conn *qc,
1239 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001240 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001241 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001242{
1243 int ret, outlen, i, pnlen;
1244 uint64_t packet_number;
1245 uint32_t truncated_pn = 0;
1246 unsigned char mask[5] = {0};
1247 unsigned char *sample;
1248 EVP_CIPHER_CTX *cctx;
1249 unsigned char *hp_key;
1250
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251 /* Check there is enough data in this packet. */
1252 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001253 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001254 return 0;
1255 }
1256
1257 cctx = EVP_CIPHER_CTX_new();
1258 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001259 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001260 return 0;
1261 }
1262
1263 ret = 0;
1264 sample = pn + QUIC_PACKET_PN_MAXLEN;
1265
1266 hp_key = tls_ctx->rx.hp_key;
1267 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1268 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1269 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001270 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001271 goto out;
1272 }
1273
1274 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1275 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1276 for (i = 0; i < pnlen; i++) {
1277 pn[i] ^= mask[i + 1];
1278 truncated_pn = (truncated_pn << 8) | pn[i];
1279 }
1280
1281 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1282 /* Store remaining information for this unprotected header */
1283 pkt->pn = packet_number;
1284 pkt->pnl = pnlen;
1285
1286 ret = 1;
1287
1288 out:
1289 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001290
1291 return ret;
1292}
1293
1294/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1295 * address, with <payload_len> as payload length, <aad> as address of
1296 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1297 * context.
1298 * Returns 1 if succeeded, 0 if not.
1299 */
1300static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1301 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001302 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001303{
1304 unsigned char iv[12];
1305 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001306 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001307 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308
1309 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001310 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001311 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001312 }
1313
1314 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1315 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001316 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001317 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001318 }
1319
1320 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001321
1322 err:
1323 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001324 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001325 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001326}
1327
1328/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1329 * Returns 1 if succeeded, 0 if not.
1330 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001331static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001332{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001333 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001334 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001335 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001336 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001337 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1338 unsigned char *rx_key = tls_ctx->rx.key;
1339
1340 kp_changed = 0;
1341 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1342 /* The two tested bits are not at the same position,
1343 * this is why they are first both inversed.
1344 */
1345 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1346 if (pkt->pn < tls_ctx->rx.pn) {
1347 /* The lowest packet number of a previous key phase
1348 * cannot be null if it really stores previous key phase
1349 * secrets.
1350 */
1351 if (!pkt->qc->ku.prv_rx.pn)
1352 return 0;
1353
1354 rx_iv = pkt->qc->ku.prv_rx.iv;
1355 rx_key = pkt->qc->ku.prv_rx.key;
1356 }
1357 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1358 /* Next key phase */
1359 kp_changed = 1;
1360 rx_iv = pkt->qc->ku.nxt_rx.iv;
1361 rx_key = pkt->qc->ku.nxt_rx.key;
1362 }
1363 }
1364 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001365
1366 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1367 return 0;
1368
1369 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1370 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001371 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001372 if (!ret)
1373 return 0;
1374
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001375 /* Update the keys only if the packet decryption succeeded. */
1376 if (kp_changed) {
1377 quic_tls_rotate_keys(pkt->qc);
1378 /* Toggle the Key Phase bit */
1379 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1380 /* Store the lowest packet number received for the current key phase */
1381 tls_ctx->rx.pn = pkt->pn;
1382 /* Prepare the next key update */
1383 if (!quic_tls_key_update(pkt->qc))
1384 return 0;
1385 }
1386
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001387 /* Update the packet length (required to parse the frames). */
1388 pkt->len = pkt->aad_len + ret;
1389
1390 return 1;
1391}
1392
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001393/* Remove from <qcs> stream the acknowledged frames.
1394 * Never fails.
1395 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001396static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001397{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001398 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001399 struct eb64_node *frm_node;
1400
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001401 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001402 frm_node = eb64_first(&qcs->tx.acked_frms);
1403 while (frm_node) {
1404 struct quic_stream *strm;
1405
1406 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1407 if (strm->offset.key != qcs->tx.ack_offset)
1408 break;
1409
1410 b_del(strm->buf, strm->len);
1411 qcs->tx.ack_offset += strm->len;
1412 frm_node = eb64_next(frm_node);
1413 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001414 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001415 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001416
1417 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001418}
1419
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001420/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001421static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1422 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001423{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001424 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001425
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001426 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001427 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001428 switch (frm->type) {
1429 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1430 {
1431 struct qcs *qcs = frm->stream.qcs;
1432 struct quic_stream *strm = &frm->stream;
1433
1434 if (qcs->tx.ack_offset == strm->offset.key) {
1435 b_del(strm->buf, strm->len);
1436 qcs->tx.ack_offset += strm->len;
1437 LIST_DELETE(&frm->list);
1438 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001439 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001440 }
1441 else {
1442 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1443 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001444 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001445 }
1446 break;
1447 default:
1448 LIST_DELETE(&frm->list);
1449 pool_free(pool_head_quic_frame, frm);
1450 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001451
1452 if (stream_acked) {
1453 struct qcc *qcc = qc->qcc;
1454
1455 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1456 tasklet_wakeup(qcc->subs->tasklet);
1457 qcc->subs->events &= ~SUB_RETRY_SEND;
1458 if (!qcc->subs->events)
1459 qcc->subs = NULL;
1460 }
1461 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001462}
1463
1464/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1465 * deallocating them, and their TX frames.
1466 * Returns the last node reached to be used for the next range.
1467 * May be NULL if <largest> node could not be found.
1468 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001469static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1470 struct eb_root *pkts,
1471 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001472 struct list *newly_acked_pkts,
1473 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001474 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001475{
1476 struct eb64_node *node;
1477 struct quic_tx_packet *pkt;
1478
1479 if (largest_node)
1480 node = largest_node;
1481 else {
1482 node = eb64_lookup(pkts, largest);
1483 while (!node && largest > smallest) {
1484 node = eb64_lookup(pkts, --largest);
1485 }
1486 }
1487
1488 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001489 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001490
1491 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1492 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001493 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001494 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001495 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001496 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001497 node = eb64_prev(node);
1498 eb64_delete(&pkt->pn_node);
1499 }
1500
1501 return node;
1502}
1503
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001504/* Remove all frames from <pkt_frm_list> and reinsert them in the
1505 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001506 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001507static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1508 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001509 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001510{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001511 struct quic_frame *frm, *frmbak;
1512 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001513
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001514 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1515 LIST_DELETE(&frm->list);
1516 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001517 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001518 }
1519
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001520 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001521}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001522
1523/* Free the TX packets of <pkts> list */
1524static inline void free_quic_tx_pkts(struct list *pkts)
1525{
1526 struct quic_tx_packet *pkt, *tmp;
1527
1528 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001529 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001530 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001531 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001532 }
1533}
1534
1535/* Send a packet loss event nofification to the congestion controller
1536 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1537 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1538 * at <now_us> current time.
1539 * Always succeeds.
1540 */
1541static inline void qc_cc_loss_event(struct quic_conn *qc,
1542 unsigned int lost_bytes,
1543 unsigned int newest_time_sent,
1544 unsigned int period,
1545 unsigned int now_us)
1546{
1547 struct quic_cc_event ev = {
1548 .type = QUIC_CC_EVT_LOSS,
1549 .loss.now_ms = now_ms,
1550 .loss.max_ack_delay = qc->max_ack_delay,
1551 .loss.lost_bytes = lost_bytes,
1552 .loss.newest_time_sent = newest_time_sent,
1553 .loss.period = period,
1554 };
1555
1556 quic_cc_event(&qc->path->cc, &ev);
1557}
1558
1559/* Send a packet ack event nofication for each newly acked packet of
1560 * <newly_acked_pkts> list and free them.
1561 * Always succeeds.
1562 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001563static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001564 struct list *newly_acked_pkts)
1565{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001566 struct quic_tx_packet *pkt, *tmp;
1567 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1568
1569 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1570 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001571 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001572 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001573 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001574 ev.ack.acked = pkt->in_flight_len;
1575 ev.ack.time_sent = pkt->time_sent;
1576 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001577 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001578 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001579 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001580 }
1581
1582}
1583
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001584/* Release all the frames attached to <pktns> packet number space */
1585static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1586{
1587 struct quic_frame *frm, *frmbak;
1588
1589 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1590 LIST_DELETE(&frm->list);
1591 pool_free(pool_head_quic_frame, frm);
1592 }
1593}
1594
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001595/* Handle <pkts> list of lost packets detected at <now_us> handling
1596 * their TX frames.
1597 * Send a packet loss event to the congestion controller if
1598 * in flight packet have been lost.
1599 * Also frees the packet in <pkts> list.
1600 * Never fails.
1601 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001602static inline void qc_release_lost_pkts(struct quic_conn *qc,
1603 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001604 struct list *pkts,
1605 uint64_t now_us)
1606{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001607 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001608 uint64_t lost_bytes;
1609
1610 lost_bytes = 0;
1611 oldest_lost = newest_lost = NULL;
1612 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001613 struct list tmp = LIST_HEAD_INIT(tmp);
1614
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001615 lost_bytes += pkt->in_flight_len;
1616 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001617 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001618 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001619 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001621 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001622 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001623 if (!oldest_lost) {
1624 oldest_lost = newest_lost = pkt;
1625 }
1626 else {
1627 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001628 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001629 newest_lost = pkt;
1630 }
1631 }
1632
1633 if (lost_bytes) {
1634 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001635 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001636 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001637 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001638 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001639 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001640 }
1641}
1642
1643/* Look for packet loss from sent packets for <qel> encryption level of a
1644 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1645 * their tree if deemed as lost or set the <loss_time> value the packet number
1646 * space if any not deemed lost.
1647 * Should be called after having received an ACK frame with newly acknowledged
1648 * packets or when the the loss detection timer has expired.
1649 * Always succeeds.
1650 */
1651static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1652 struct quic_conn *qc,
1653 struct list *lost_pkts)
1654{
1655 struct eb_root *pkts;
1656 struct eb64_node *node;
1657 struct quic_loss *ql;
1658 unsigned int loss_delay;
1659
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001660 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001661 pkts = &pktns->tx.pkts;
1662 pktns->tx.loss_time = TICK_ETERNITY;
1663 if (eb_is_empty(pkts))
1664 goto out;
1665
1666 ql = &qc->path->loss;
1667 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1668 loss_delay += loss_delay >> 3;
1669 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1670
1671 node = eb64_first(pkts);
1672 while (node) {
1673 struct quic_tx_packet *pkt;
1674 int64_t largest_acked_pn;
1675 unsigned int loss_time_limit, time_sent;
1676
1677 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001678 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001679 node = eb64_next(node);
1680 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1681 break;
1682
1683 time_sent = pkt->time_sent;
1684 loss_time_limit = tick_add(time_sent, loss_delay);
1685 if (tick_is_le(time_sent, now_ms) ||
1686 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1687 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001688 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001689 }
1690 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001691 if (tick_isset(pktns->tx.loss_time))
1692 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1693 else
1694 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001695 }
1696 }
1697
1698 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001699 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001700}
1701
1702/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1703 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001704 * 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 +01001705 * acked ack-eliciting packet.
1706 * Return 1, if succeeded, 0 if not.
1707 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001708static inline int qc_parse_ack_frm(struct quic_conn *qc,
1709 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001710 struct quic_enc_level *qel,
1711 unsigned int *rtt_sample,
1712 const unsigned char **pos, const unsigned char *end)
1713{
1714 struct quic_ack *ack = &frm->ack;
1715 uint64_t smallest, largest;
1716 struct eb_root *pkts;
1717 struct eb64_node *largest_node;
1718 unsigned int time_sent, pkt_flags;
1719 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1720 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1721
1722 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1723 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001724 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001725 goto err;
1726 }
1727
1728 if (ack->first_ack_range > ack->largest_ack) {
1729 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001730 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001731 goto err;
1732 }
1733
1734 largest = ack->largest_ack;
1735 smallest = largest - ack->first_ack_range;
1736 pkts = &qel->pktns->tx.pkts;
1737 pkt_flags = 0;
1738 largest_node = NULL;
1739 time_sent = 0;
1740
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001741 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 +01001742 largest_node = eb64_lookup(pkts, largest);
1743 if (!largest_node) {
1744 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001745 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001746 }
1747 else {
1748 time_sent = eb64_entry(&largest_node->node,
1749 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001750 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001751 }
1752
1753 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001754 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001755 do {
1756 uint64_t gap, ack_range;
1757
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001758 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1759 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001760 if (!ack->ack_range_num--)
1761 break;
1762
1763 if (!quic_dec_int(&gap, pos, end))
1764 goto err;
1765
1766 if (smallest < gap + 2) {
1767 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001768 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001769 goto err;
1770 }
1771
1772 largest = smallest - gap - 2;
1773 if (!quic_dec_int(&ack_range, pos, end))
1774 goto err;
1775
1776 if (largest < ack_range) {
1777 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001778 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001779 goto err;
1780 }
1781
1782 /* Do not use this node anymore. */
1783 largest_node = NULL;
1784 /* Next range */
1785 smallest = largest - ack_range;
1786
1787 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001788 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001789 } while (1);
1790
1791 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001792 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001793
1794 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1795 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001796 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001797 }
1798
1799 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1800 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001801 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001802 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001803 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001804 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001805 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1806 if (quic_peer_validated_addr(qc))
1807 qc->path->loss.pto_count = 0;
1808 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001809 }
1810
1811
1812 return 1;
1813
1814 err:
1815 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001816 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001817 return 0;
1818}
1819
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001820/* This function gives the detail of the SSL error. It is used only
1821 * if the debug mode and the verbose mode are activated. It dump all
1822 * the SSL error until the stack was empty.
1823 */
1824static forceinline void qc_ssl_dump_errors(struct connection *conn)
1825{
1826 if (unlikely(global.mode & MODE_DEBUG)) {
1827 while (1) {
1828 unsigned long ret;
1829
1830 ret = ERR_get_error();
1831 if (!ret)
1832 return;
1833
1834 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1835 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1836 }
1837 }
1838}
1839
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001840int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1841 const char **str, int *len);
1842
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001843/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1844 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001845 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001846 * Return 1 if succeeded, 0 if not.
1847 */
1848static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001849 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001850 const unsigned char *data, size_t len,
1851 struct quic_rx_packet *pkt,
1852 struct quic_rx_crypto_frm *cf)
1853{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001854 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001855 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001856
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001857 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001858 qc = ctx->qc;
1859
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001860 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001862 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1863 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001864 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001865 goto err;
1866 }
1867
1868 el->rx.crypto.offset += len;
1869 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001870 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001871
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001872 state = HA_ATOMIC_LOAD(&qc->state);
1873 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001874 ssl_err = SSL_do_handshake(ctx->ssl);
1875 if (ssl_err != 1) {
1876 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1877 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1878 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001879 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001880 goto out;
1881 }
1882
1883 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001884 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001885 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001886 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001887 goto err;
1888 }
1889
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001890 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001891 if (qc_is_listener(ctx->qc)) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001892 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001893 /* The connection is ready to be accepted. */
1894 quic_accept_push_qc(qc);
1895 }
1896 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001897 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001898 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001899 } else {
1900 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1901 if (ssl_err != 1) {
1902 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1903 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1904 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001905 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001906 goto out;
1907 }
1908
1909 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001910 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001911 goto err;
1912 }
1913
1914 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001915 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001916 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001917
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001918 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001919 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001920 return 1;
1921
1922 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001923 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001924 return 0;
1925}
1926
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001927/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1928 * <pkt> RX packet.
1929 * Return it if succeeded, NULL if not.
1930 */
1931static inline
1932struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1933 struct quic_rx_packet *pkt)
1934{
1935 struct quic_rx_strm_frm *frm;
1936
1937 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1938 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001939 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001940 frm->len = stream_frm->len;
1941 frm->data = stream_frm->data;
1942 frm->pkt = pkt;
1943 }
1944
1945 return frm;
1946}
1947
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001948/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001949 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001950 */
1951static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1952{
1953 size_t ret;
1954
1955 ret = 0;
1956 while (strm_frm->len) {
1957 size_t try;
1958
1959 try = b_contig_space(buf);
1960 if (!try)
1961 break;
1962
1963 if (try > strm_frm->len)
1964 try = strm_frm->len;
1965 memcpy(b_tail(buf), strm_frm->data, try);
1966 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001967 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001968 b_add(buf, try);
1969 ret += try;
1970 }
1971
1972 return ret;
1973}
1974
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001975/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1976 * Also update <strm_frm> frame to reflect the data which have been consumed.
1977 */
1978static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1979 struct quic_rx_strm_frm *strm_frm)
1980{
1981 size_t ret;
1982
1983 ret = 0;
1984 while (strm_frm->len) {
1985 size_t try;
1986
1987 try = b_contig_space(buf);
1988 if (!try)
1989 break;
1990
1991 if (try > strm_frm->len)
1992 try = strm_frm->len;
1993 memcpy(b_tail(buf), strm_frm->data, try);
1994 strm_frm->len -= try;
1995 strm_frm->offset_node.key += try;
1996 b_add(buf, try);
1997 ret += try;
1998 }
1999
2000 return ret;
2001}
2002
2003/* Process as much as possible RX STREAM frames received for <qcs> */
2004static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
2005{
2006 int total;
2007 struct eb64_node *frm_node;
2008
2009 total = 0;
2010 frm_node = eb64_first(&qcs->rx.frms);
2011 while (frm_node) {
2012 int ret;
2013 struct quic_rx_strm_frm *frm;
2014
2015 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
2016 if (frm->offset_node.key != qcs->rx.offset)
2017 break;
2018
2019 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
2020 qcs->rx.offset += ret;
2021 total += ret;
2022 if (frm->len) {
2023 /* If there is remaining data in this frame
2024 * this is because the destination buffer is full.
2025 */
2026 break;
2027 }
2028
2029 frm_node = eb64_next(frm_node);
2030 quic_rx_packet_refdec(frm->pkt);
2031 eb64_delete(&frm->offset_node);
2032 pool_free(pool_head_quic_rx_strm_frm, frm);
2033 }
2034
2035 return total;
2036}
2037
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002038/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2039 * streams may be open. The data are copied to the stream RX buffer if possible.
2040 * If not, the STREAM frame is stored to be treated again later.
2041 * We rely on the flow control so that not to store too much STREAM frames.
2042 * Return 1 if succeeded, 0 if not.
2043 */
2044static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2045 struct quic_stream *strm_frm,
2046 struct quic_conn *qc)
2047{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002048 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002049 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002050 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002051 struct quic_rx_strm_frm *frm;
2052
2053 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2054 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002055 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002056 return 0;
2057 }
2058
2059 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002060 if (strm_frm->offset.key < strm->rx.offset) {
2061 size_t diff;
2062
2063 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2064 TRACE_PROTO("Already received STREAM data",
2065 QUIC_EV_CONN_PSTRM, qc);
2066 goto out;
2067 }
2068
2069 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2070 diff = strm->rx.offset - strm_frm->offset.key;
2071 strm_frm->offset.key = strm->rx.offset;
2072 strm_frm->len -= diff;
2073 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002074 }
2075
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002076 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002077 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002078 int ret;
2079
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002080 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002081 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002082 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002083
2084 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002085 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002086 strm->rx.offset += ret;
2087 }
2088
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002089 total += qc_treat_rx_strm_frms(strm);
2090 if (total && qc->qcc->app_ops->decode_qcs(strm, strm_frm->fin, qc->qcc->ctx) < 0) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002091 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002092 return 0;
2093 }
2094
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002095 if (!strm_frm->len)
2096 goto out;
2097
2098 store_frm:
2099 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2100 if (!frm) {
2101 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002102 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002103 return 0;
2104 }
2105
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002106 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002107 quic_rx_packet_refinc(pkt);
2108
2109 out:
2110 return 1;
2111}
2112
2113/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2114 * streams may be open. The data are copied to the stream RX buffer if possible.
2115 * If not, the STREAM frame is stored to be treated again later.
2116 * We rely on the flow control so that not to store too much STREAM frames.
2117 * Return 1 if succeeded, 0 if not.
2118 */
2119static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2120 struct quic_stream *strm_frm,
2121 struct quic_conn *qc)
2122{
2123 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002124 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002125 struct quic_rx_strm_frm *frm;
2126 size_t strm_frm_len;
2127
2128 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2129 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002130 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002131 return 0;
2132 }
2133
2134 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002135 if (strm_frm->offset.key < strm->rx.offset) {
2136 size_t diff;
2137
2138 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2139 TRACE_PROTO("Already received STREAM data",
2140 QUIC_EV_CONN_PSTRM, qc);
2141 goto out;
2142 }
2143
2144 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2145 diff = strm->rx.offset - strm_frm->offset.key;
2146 strm_frm->offset.key = strm->rx.offset;
2147 strm_frm->len -= diff;
2148 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002149 }
2150
2151 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002152 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002153 int ret;
2154
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002155 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002156 goto store_frm;
2157
2158 /* qc_strm_cpy() will modify the offset, depending on the number
2159 * of bytes copied.
2160 */
2161 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2162 /* Inform the application of the arrival of this new stream */
2163 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002164 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002165 return 0;
2166 }
2167
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002168 if (ret)
2169 qcs_notify_recv(strm);
2170
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002171 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002172 }
2173 /* Take this frame into an account for the stream flow control */
2174 strm->rx.offset += strm_frm_len;
2175 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002176 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002177 */
2178 if (!strm_frm->len)
2179 goto out;
2180
2181 store_frm:
2182 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2183 if (!frm) {
2184 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002185 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002186 return 0;
2187 }
2188
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002189 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002190 quic_rx_packet_refinc(pkt);
2191
2192 out:
2193 return 1;
2194}
2195
2196static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2197 struct quic_stream *strm_frm,
2198 struct quic_conn *qc)
2199{
2200 if (strm_frm->id & QCS_ID_DIR_BIT)
2201 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2202 else
2203 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2204}
2205
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002206/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002207static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2208 struct quic_conn *qc)
2209{
2210 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002211 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002212 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002213
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002214 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002215 pkts = &qel->pktns->tx.pkts;
2216 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002217 /* Skip the empty packet (they have already been retransmitted) */
2218 while (node) {
2219 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2220 if (!LIST_ISEMPTY(&pkt->frms))
2221 break;
2222 node = eb64_next(node);
2223 }
2224
2225 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002226 return;
2227
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002228 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002229}
2230
2231/* Prepare a fast retransmission during handshake after a client
2232 * has resent Initial packets. According to the RFC a server may retransmit
2233 * up to two datagrams of Initial packets if did not receive all Initial packets
2234 * and resend them coalescing with others (Handshake here).
2235 * (Listener only).
2236 */
2237static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2238{
2239 struct list itmp = LIST_HEAD_INIT(itmp);
2240 struct list htmp = LIST_HEAD_INIT(htmp);
2241 struct quic_frame *frm, *frmbak;
2242
2243 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2244 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2245 struct quic_enc_level *qel = iqel;
2246 struct eb_root *pkts;
2247 struct eb64_node *node;
2248 struct quic_tx_packet *pkt;
2249 struct list *tmp = &itmp;
2250
2251 start:
2252 pkt = NULL;
2253 pkts = &qel->pktns->tx.pkts;
2254 node = eb64_first(pkts);
2255 /* Skip the empty packet (they have already been retransmitted) */
2256 while (node) {
2257 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2258 if (!LIST_ISEMPTY(&pkt->frms))
2259 break;
2260 node = eb64_next(node);
2261 }
2262
2263 if (!pkt)
2264 goto end;
2265
2266 qel->pktns->tx.pto_probe += 1;
2267 requeue:
2268 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2269 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2270 LIST_DELETE(&frm->list);
2271 LIST_APPEND(tmp, &frm->list);
2272 }
2273
2274 if (qel == iqel) {
2275 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2276 pkt = pkt->next;
2277 tmp = &htmp;
2278 hqel->pktns->tx.pto_probe += 1;
2279 goto requeue;
2280 }
2281
2282 qel = hqel;
2283 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002284 goto start;
2285 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002286
2287 end:
2288 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2289 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002290}
2291
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002292/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2293 * as I/O handler context and <qel> as encryption level.
2294 * Returns 1 if succeeded, 0 if failed.
2295 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002296static 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 +01002297 struct quic_enc_level *qel)
2298{
2299 struct quic_frame frm;
2300 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002301 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002302 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002303
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002304 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002305 /* Skip the AAD */
2306 pos = pkt->data + pkt->aad_len;
2307 end = pkt->data + pkt->len;
2308
2309 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002310 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002311 goto err;
2312
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002313 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002314 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002315 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002316 break;
2317 case QUIC_FT_PING:
2318 break;
2319 case QUIC_FT_ACK:
2320 {
2321 unsigned int rtt_sample;
2322
2323 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002324 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002325 goto err;
2326
2327 if (rtt_sample) {
2328 unsigned int ack_delay;
2329
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002330 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002331 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2332 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2333 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002334 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002335 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002336 break;
2337 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002338 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002339 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002340 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002341 {
2342 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002343
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002344 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2345 /* XXX TO DO: <cfdebug> is used only for the traces. */
2346 struct quic_rx_crypto_frm cfdebug = { };
2347
2348 cfdebug.offset_node.key = frm.crypto.offset;
2349 cfdebug.len = frm.crypto.len;
2350 TRACE_PROTO("CRYPTO data discarded",
2351 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2352 break;
2353 }
2354
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002355 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2356 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2357 /* XXX TO DO: <cfdebug> is used only for the traces. */
2358 struct quic_rx_crypto_frm cfdebug = { };
2359
2360 cfdebug.offset_node.key = frm.crypto.offset;
2361 cfdebug.len = frm.crypto.len;
2362 /* Nothing to do */
2363 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002364 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002365 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002366 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2367 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002368 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002369 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002370 else {
2371 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2372 /* XXX TO DO: <cfdebug> is used only for the traces. */
2373 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002374
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002375 cfdebug.offset_node.key = frm.crypto.offset;
2376 cfdebug.len = frm.crypto.len;
2377 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002378 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002379 frm.crypto.len -= diff;
2380 frm.crypto.data += diff;
2381 frm.crypto.offset = qel->rx.crypto.offset;
2382 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002383 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002384
2385 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002386 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002387 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002388
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002389 cfdebug.offset_node.key = frm.crypto.offset;
2390 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002391 if (!qc_provide_cdata(qel, ctx,
2392 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002393 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002394 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002395
2396 break;
2397 }
2398
2399 /* frm.crypto.offset > qel->rx.crypto.offset */
2400 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2401 if (!cf) {
2402 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002403 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002404 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002405 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002406
2407 cf->offset_node.key = frm.crypto.offset;
2408 cf->len = frm.crypto.len;
2409 cf->data = frm.crypto.data;
2410 cf->pkt = pkt;
2411 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2412 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002413 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002414 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002415 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002416 {
2417 struct quic_stream *stream = &frm.stream;
2418
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002419 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002420 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2421 goto err;
2422 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2423 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002424
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002425 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002426 goto err;
2427
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002428 break;
2429 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002430 case QUIC_FT_MAX_DATA:
2431 case QUIC_FT_MAX_STREAM_DATA:
2432 case QUIC_FT_MAX_STREAMS_BIDI:
2433 case QUIC_FT_MAX_STREAMS_UNI:
2434 case QUIC_FT_DATA_BLOCKED:
2435 case QUIC_FT_STREAM_DATA_BLOCKED:
2436 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2437 case QUIC_FT_STREAMS_BLOCKED_UNI:
2438 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002439 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002440 case QUIC_FT_RETIRE_CONNECTION_ID:
2441 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002442 break;
2443 case QUIC_FT_CONNECTION_CLOSE:
2444 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002445 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002446 qc->qcc->flags |= QC_CF_CC_RECV;
2447 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002448 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002449 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002450 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002451 goto err;
2452
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002453 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002454 break;
2455 default:
2456 goto err;
2457 }
2458 }
2459
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002460 if (fast_retrans)
2461 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002462
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002463 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2464 * has successfully parse a Handshake packet. The Initial encryption must also
2465 * be discarded.
2466 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002467 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002468 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002469
2470 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002471 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002472 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002473 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002474 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002475 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002476 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002477 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002478 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002479 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002480 }
2481
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002482 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002483 return 1;
2484
2485 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002486 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002487 return 0;
2488}
2489
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002490/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002491 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002492 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2493 * This function must be called only after having built a correct datagram.
2494 * Always succeeds.
2495 */
2496static inline void qc_set_dg(struct cbuf *cbuf,
2497 uint16_t dglen, struct quic_tx_packet *pkt)
2498{
2499 write_u16(cb_wr(cbuf), dglen);
2500 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2501 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2502}
2503
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002504/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002505 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2506 * several packets in the same datagram. A header made of two fields is added
2507 * to each datagram: the datagram length followed by the address of the first
2508 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002509 * Returns 1 if succeeded, or 0 if something wrong happened.
2510 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002511static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2512 enum quic_tls_enc_level tel,
2513 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002514{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002515 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002516 struct cbuf *cbuf;
2517 unsigned char *end_buf, *end, *pos, *spos;
2518 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2519 /* length of datagrams */
2520 uint16_t dglen;
2521 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002522 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002523 /* Each datagram is prepended with its length followed by the
2524 * address of the first packet in the datagram.
2525 */
2526 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002527
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002528 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2529
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002530 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002531 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002532 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002533 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002535 cbuf = qr->cbuf;
2536 spos = pos = cb_wr(cbuf);
2537 /* Leave at least <dglen> bytes at the end of this buffer
2538 * to ensure there is enough room to mark the end of prepared
2539 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002540 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002541 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2542 first_pkt = prv_pkt = NULL;
2543 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002544 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002545 enum quic_pkt_type pkt_type;
2546
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002547 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002548 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002549 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2550 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002551 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002552 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002553 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002554 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002555 * 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 +01002556 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002557 * and if there is no more CRYPTO data available or in flight
2558 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002559 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002560 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002561 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002562 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002563 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002564 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002565 /* Set the current datagram as prepared into <cbuf> if
2566 * the was already a correct packet which was previously written.
2567 */
2568 if (prv_pkt)
2569 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002570 /* Let's select the next encryption level */
2571 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2572 tel = next_tel;
2573 qel = &qc->els[tel];
2574 /* Build a new datagram */
2575 prv_pkt = NULL;
2576 continue;
2577 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002578 break;
2579 }
2580
2581 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002582 if (!prv_pkt) {
2583 /* Leave room for the datagram header */
2584 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002585 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002586 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2587 }
2588 else {
2589 end = pos + qc->path->mtu;
2590 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002591 }
2592
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002593 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002594 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002595 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002596 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002597 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002598 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002599 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002600 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002601 case -2:
2602 goto err;
2603 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002604 /* If there was already a correct packet present, set the
2605 * current datagram as prepared into <cbuf>.
2606 */
2607 if (prv_pkt) {
2608 qc_set_dg(cbuf, dglen, first_pkt);
2609 goto stop_build;
2610 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002611 goto out;
2612 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002613 break;
2614 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002615
Frédéric Lécaille63556772021-12-29 17:18:21 +01002616 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2617 if (!cur_pkt)
2618 goto err;
2619
2620 total += cur_pkt->len;
2621 /* keep trace of the first packet in the datagram */
2622 if (!first_pkt)
2623 first_pkt = cur_pkt;
2624 /* Attach the current one to the previous one */
2625 if (prv_pkt)
2626 prv_pkt->next = cur_pkt;
2627 /* Let's say we have to build a new dgram */
2628 prv_pkt = NULL;
2629 dglen += cur_pkt->len;
2630 /* Client: discard the Initial encryption keys as soon as
2631 * a handshake packet could be built.
2632 */
2633 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2634 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2635 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2636 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2637 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2638 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002639 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002640 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002641 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2642 }
2643 /* If the data for the current encryption level have all been sent,
2644 * select the next level.
2645 */
2646 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002647 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002648 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2649 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2650 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2651 tel = next_tel;
2652 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002653 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002654 /* If there is data for the next level, do not
2655 * consume a datagram.
2656 */
2657 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002658 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002659 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002660 /* If we have to build a new datagram, set the current datagram as
2661 * prepared into <cbuf>.
2662 */
2663 if (!prv_pkt) {
2664 qc_set_dg(cbuf, dglen, first_pkt);
2665 first_pkt = NULL;
2666 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002667 padding = 0;
2668 }
2669 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002670 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002671 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2672 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002673 }
2674 }
2675
2676 stop_build:
2677 /* Reset <wr> writer index if in front of <rd> index */
2678 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2679 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2680
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002681 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002682 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2683 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2684 /* Mark the end of contiguous data for the reader */
2685 write_u16(cb_wr(cbuf), 0);
2686 cb_add(cbuf, sizeof(uint16_t));
2687 }
2688 }
2689
2690 if (rd && rd <= cbuf->wr) {
2691 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002692 /* Let's try to reuse this buffer */
2693 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002694 }
2695 }
2696
2697 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002698 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002699 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002700
2701 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002702 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002703 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002704}
2705
2706/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002707 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002708 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002709int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002710{
2711 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002712 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002713
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002714 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002715 cbuf = qr->cbuf;
2716 while (cb_contig_data(cbuf)) {
2717 unsigned char *pos;
2718 struct buffer tmpbuf = { };
2719 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2720 uint16_t dglen;
2721 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002722 unsigned int time_sent;
2723
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002724 pos = cb_rd(cbuf);
2725 dglen = read_u16(pos);
2726 /* End of prepared datagrams.
2727 * Reset the reader index only if in front of the writer index.
2728 */
2729 if (!dglen) {
2730 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2731
2732 if (wr && wr < cbuf->rd) {
2733 cb_rd_reset(cbuf);
2734 continue;
2735 }
2736 break;
2737 }
2738
2739 pos += sizeof dglen;
2740 first_pkt = read_ptr(pos);
2741 pos += sizeof first_pkt;
2742 tmpbuf.area = (char *)pos;
2743 tmpbuf.size = tmpbuf.data = dglen;
2744
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002745 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002746 for (pkt = first_pkt; pkt; pkt = pkt->next)
2747 quic_tx_packet_refinc(pkt);
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01002748 if (ctx->xprt->snd_buf(NULL, qc->xprt_ctx,
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002749 &tmpbuf, tmpbuf.data, 0) <= 0) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002750 for (pkt = first_pkt; pkt; pkt = pkt->next)
2751 quic_tx_packet_refdec(pkt);
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002752 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002753 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002754
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002755 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002756 qc->tx.bytes += tmpbuf.data;
2757 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002758
2759 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2760 pkt->time_sent = time_sent;
2761 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2762 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002763 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002764 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002765 qc->path->in_flight += pkt->in_flight_len;
2766 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2767 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002768 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002769 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002770 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002771 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002772 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002773 }
2774 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002775
2776 return 1;
2777}
2778
2779/* Build all the frames which must be sent just after the handshake have succeeded.
2780 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2781 * a HANDSHAKE_DONE frame.
2782 * Return 1 if succeeded, 0 if not.
2783 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002784static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002785{
2786 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002787 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002788 struct quic_frame *frm;
2789
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002790 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002791 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002792 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002793 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002794 if (!frm)
2795 return 0;
2796
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002797 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002798 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002799 }
2800
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002801 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002802 struct quic_connection_id *cid;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01002803 struct listener *l = qc->li;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002804
2805 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002806 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002807 goto err;
2808
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01002809 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002810 if (!cid)
2811 goto err;
2812
Frédéric Lécaille74904a42022-01-27 15:35:56 +01002813 /* insert the allocated CID in the receiver datagram handler tree */
2814 ebmb_insert(&l->rx.dghdlrs[tid]->cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002815
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002816 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002817 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002818 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002819 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002820
2821 return 1;
2822
2823 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002824 return 0;
2825}
2826
2827/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002828void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002829{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002830 struct eb64_node *n;
2831 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002832
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002833 n = eb64_first(&arngs->root);
2834 while (n) {
2835 struct eb64_node *next;
2836
2837 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2838 next = eb64_next(n);
2839 eb64_delete(n);
2840 free(ar);
2841 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002842 }
2843}
2844
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002845/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2846 * descending order.
2847 */
2848static inline size_t sack_gap(struct quic_arng_node *p,
2849 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002850{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002851 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852}
2853
2854
2855/* Remove the last elements of <ack_ranges> list of ack range updating its
2856 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002857 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002858 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002859static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002860{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002861 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002862
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002863 last = eb64_last(&arngs->root);
2864 while (last && arngs->enc_sz > limit) {
2865 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002866
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002867 prev = eb64_prev(last);
2868 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002869 return 0;
2870
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002871 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2872 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2873 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2874 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2875 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2876 --arngs->sz;
2877 eb64_delete(last);
2878 pool_free(pool_head_quic_arng, last);
2879 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002880 }
2881
2882 return 1;
2883}
2884
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002885/* Set the encoded size of <arngs> QUIC ack ranges. */
2886static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2887{
2888 struct eb64_node *node, *next;
2889 struct quic_arng_node *ar, *ar_next;
2890
2891 node = eb64_last(&arngs->root);
2892 if (!node)
2893 return;
2894
2895 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2896 arngs->enc_sz = quic_int_getsize(ar->last) +
2897 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2898
2899 while ((next = eb64_prev(node))) {
2900 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2901 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2902 quic_int_getsize(ar_next->last - ar_next->first.key);
2903 node = next;
2904 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2905 }
2906}
2907
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002908/* Insert <ar> ack range into <argns> tree of ack ranges.
2909 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002910 */
2911static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002912struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2913 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002914{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002915 struct quic_arng_node *new_ar;
2916
2917 new_ar = pool_alloc(pool_head_quic_arng);
2918 if (new_ar) {
2919 new_ar->first.key = ar->first;
2920 new_ar->last = ar->last;
2921 eb64_insert(&arngs->root, &new_ar->first);
2922 arngs->sz++;
2923 }
2924
2925 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002926}
2927
2928/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002929 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002930 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002931 *
2932 * Descending order
2933 * ------------->
2934 * range1 range2
2935 * ..........|--------|..............|--------|
2936 * ^ ^ ^ ^
2937 * | | | |
2938 * last1 first1 last2 first2
2939 * ..........+--------+--------------+--------+......
2940 * diff1 gap12 diff2
2941 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002942 * To encode the previous list of ranges we must encode integers as follows in
2943 * descending order:
2944 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002945 * with diff1 = last1 - first1
2946 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002947 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002948 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002949 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002950int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2951 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002952{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002953 struct eb64_node *le;
2954 struct quic_arng_node *new_node;
2955 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002956
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002957 new = NULL;
2958 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002959 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002960 if (!new_node)
2961 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002962
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002963 goto out;
2964 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002965
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002966 le = eb64_lookup_le(&arngs->root, ar->first);
2967 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002968 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002969 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002970 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002971
2972 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002973 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002974 else {
2975 struct quic_arng_node *le_ar =
2976 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002977
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002978 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002979 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002980 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002981
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002982 if (le_ar->last + 1 >= ar->first) {
2983 le_ar->last = ar->last;
2984 new = le;
2985 new_node = le_ar;
2986 }
2987 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002988 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002989 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002990 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002991
2992 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002994 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002995
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002996 /* Verify that the new inserted node does not overlap the nodes
2997 * which follow it.
2998 */
2999 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003000 struct eb64_node *next;
3001 struct quic_arng_node *next_node;
3002
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003003 while ((next = eb64_next(new))) {
3004 next_node =
3005 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003006 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003007 break;
3008
3009 if (next_node->last > new_node->last)
3010 new_node->last = next_node->last;
3011 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003012 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003013 /* Decrement the size of these ranges. */
3014 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003015 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003016 }
3017
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003018 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003019 quic_arngs_set_enc_sz(arngs);
3020
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003021 return 1;
3022}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003023/* Remove the header protection of packets at <el> encryption level.
3024 * Always succeeds.
3025 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003026static 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 +01003027{
3028 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003029 struct quic_rx_packet *pqpkt;
3030 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003031 struct quic_enc_level *app_qel;
3032
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003033 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3034 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003035 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003036 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003037 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003038 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003039 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040 goto out;
3041 }
3042 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003043 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003044 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003045 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003046 pqpkt->data, pqpkt->data + pqpkt->len)) {
3047 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003048 /* XXX TO DO XXX */
3049 }
3050 else {
3051 /* The AAD includes the packet number field */
3052 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3053 /* Store the packet into the tree of packets to decrypt. */
3054 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003055 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003056 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3057 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003058 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003059 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003061 MT_LIST_DELETE_SAFE(pkttmp1);
3062 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003063 }
3064
3065 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003066 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003067}
3068
3069/* Process all the CRYPTO frame at <el> encryption level.
3070 * Return 1 if succeeded, 0 if not.
3071 */
3072static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003073 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003074{
3075 struct eb64_node *node;
3076
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003077 node = eb64_first(&el->rx.crypto.frms);
3078 while (node) {
3079 struct quic_rx_crypto_frm *cf;
3080
3081 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3082 if (cf->offset_node.key != el->rx.crypto.offset)
3083 break;
3084
3085 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3086 goto err;
3087
3088 node = eb64_next(node);
3089 quic_rx_packet_refdec(cf->pkt);
3090 eb64_delete(&cf->offset_node);
3091 pool_free(pool_head_quic_rx_crypto_frm, cf);
3092 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003093 return 1;
3094
3095 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003096 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003097 return 0;
3098}
3099
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003100/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003101 * 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 +02003102 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003103 * Return 1 if succeeded, 0 if not.
3104 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003105int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3106 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003107{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003109 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003110 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003111 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003112
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003113 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003114 qel = cur_el;
3115 next_tel:
3116 if (!qel)
3117 goto out;
3118
3119 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3120 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003121 while (node) {
3122 struct quic_rx_packet *pkt;
3123
3124 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003125 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003126 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003127 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003128 /* Drop the packet */
3129 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003130 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003131 }
3132 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003133 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003134 /* Drop the packet */
3135 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003136 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003137 }
3138 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003139 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3140
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003141 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003142 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003143 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003144 if (pkt->pn > largest_pn)
3145 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003146 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003147 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003148 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003149 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003150 }
3151 }
3152 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003153 eb64_delete(&pkt->pn_node);
3154 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003156 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003157
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003158 /* Update the largest packet number. */
3159 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003160 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3161 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003162 goto err;
3163
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003164 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003165 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003166 qel = next_el;
3167 goto next_tel;
3168 }
3169
3170 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003171 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003172 return 1;
3173
3174 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003175 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003176 return 0;
3177}
3178
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003179/* Check if it's possible to remove header protection for packets related to
3180 * encryption level <qel>. If <qel> is NULL, assume it's false.
3181 *
3182 * Return true if the operation is possible else false.
3183 */
3184static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3185{
3186 enum quic_tls_enc_level tel;
3187
3188 if (!qel)
3189 return 0;
3190
3191 tel = ssl_to_quic_enc_level(qel->level);
3192
3193 /* check if tls secrets are available */
3194 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
3195 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
3196
3197 if (!(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET))
3198 return 0;
3199
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003200 /* check if the connection layer is ready before using app level */
3201 if (tel == QUIC_TLS_ENC_LEVEL_APP && qc->mux_state != QC_MUX_READY)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003202 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003203
3204 return 1;
3205}
3206
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003207/* QUIC connection packet handler task. */
3208struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003209{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003210 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003211 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003212 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003213 enum quic_tls_enc_level tel, next_tel;
3214 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003215 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003216 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003217
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003218 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003219 qc = ctx->qc;
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003220 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003221 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003222 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003223 TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003224 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3225 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3226 /* The I/O handler has been woken up by the dgram listener
3227 * after the anti-amplification was reached.
3228 */
3229 qc_set_timer(qc);
3230 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3231 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3232 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003233 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003234 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3235 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3236 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003237 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003238 if (st >= QUIC_HS_ST_COMPLETE &&
3239 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3240 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3241 /* There may be remaining Handshake packets to treat and acknowledge. */
3242 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3243 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3244 }
3245 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003246 goto err;
3247
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003248 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003249 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003250
3251 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003252 /* Treat packets waiting for header packet protection decryption */
3253 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003254 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003255
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003256 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3257 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3258 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003259 goto err;
3260
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003261 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3262 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3263 qel = next_qel;
3264 next_qel = NULL;
3265 goto next_level;
3266 }
3267
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003268 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003269 if (st >= QUIC_HS_ST_COMPLETE) {
3270 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3271 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003272 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003273
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003274 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3275 QUIC_FL_TLS_SECRETS_DCD)) {
3276 /* Discard the Handshake keys. */
3277 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3278 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3279 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3280 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003281 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003282 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003283 }
3284
3285 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3286 /* There may be remaining handshake to build (acks) */
3287 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3288 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003289 }
3290
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003291 if (!qr)
3292 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003293 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3294 * be considered.
3295 */
3296 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003297 goto err;
3298 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003299 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003300 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003301 else if (ret == 0)
3302 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003303
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003304 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003305 goto err;
3306
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003307 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003308 /* Check if there is something to do for the next level.
3309 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003310 if (next_qel && next_qel != qel &&
3311 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003312 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003313 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003314 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003315 goto next_level;
3316 }
3317
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003318 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003319 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003320 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321
3322 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003323 if (qr)
3324 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003325 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003326 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003327}
3328
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003329/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003330static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3331{
3332 int i;
3333
3334 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3335 if (qel->tx.crypto.bufs[i]) {
3336 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3337 qel->tx.crypto.bufs[i] = NULL;
3338 }
3339 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003340 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003341}
3342
3343/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003344 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003345 * Returns 1 if succeeded, 0 if not.
3346 */
3347static int quic_conn_enc_level_init(struct quic_conn *qc,
3348 enum quic_tls_enc_level level)
3349{
3350 struct quic_enc_level *qel;
3351
3352 qel = &qc->els[level];
3353 qel->level = quic_to_ssl_enc_level(level);
3354 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3355 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3356 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3357 qel->tls_ctx.rx.flags = 0;
3358 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003359 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003360
3361 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003362 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003363 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003364 qel->rx.crypto.offset = 0;
3365 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003366
3367 /* Allocate only one buffer. */
3368 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3369 if (!qel->tx.crypto.bufs)
3370 goto err;
3371
3372 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3373 if (!qel->tx.crypto.bufs[0])
3374 goto err;
3375
3376 qel->tx.crypto.bufs[0]->sz = 0;
3377 qel->tx.crypto.nb_buf = 1;
3378
3379 qel->tx.crypto.sz = 0;
3380 qel->tx.crypto.offset = 0;
3381
3382 return 1;
3383
3384 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003385 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003386 return 0;
3387}
3388
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003389/* Increment the <qc> refcount.
3390 *
3391 * This operation must be conducted when manipulating the quic_conn outside of
3392 * the connection pinned thread. These threads can only retrieve the connection
3393 * in the CID tree, so this function must be conducted under the CID lock.
3394 */
3395static inline void quic_conn_take(struct quic_conn *qc)
3396{
3397 HA_ATOMIC_INC(&qc->refcount);
3398}
3399
3400/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003401 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003402 */
3403static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003404{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003405 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003406 int i;
3407
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003408 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003409 return;
3410
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003411 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1))
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003412 return;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003413
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003414 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003415 if (conn_ctx) {
3416 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003417 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003418 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003419
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003420 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003421 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003422
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003423 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3424 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003425 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003426}
3427
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003428/* Release the quic_conn <qc>. It will decrement its refcount so that the
3429 * connection will be freed once all threads have finished to work with it. The
3430 * connection is removed from the CIDs tree and thus cannot be found by other
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003431 * threads after it. The connection tasklet is killed.
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003432 *
3433 * Do not use <qc> after it as it may be freed. This function must only be
3434 * called by the thread responsible of the quic_conn tasklet.
3435 */
3436static void quic_conn_release(struct quic_conn *qc)
3437{
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003438 struct ssl_sock_ctx *conn_ctx;
3439
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003440 /* remove the connection from receiver cids trees */
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003441 ebmb_delete(&qc->odcid_node);
3442 ebmb_delete(&qc->scid_node);
3443 free_quic_conn_cids(qc);
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003444
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003445 /* Kill the tasklet. Do not use tasklet_free as this is not thread safe
3446 * as other threads may call tasklet_wakeup after this.
3447 */
3448 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
3449 if (conn_ctx)
3450 tasklet_kill(conn_ctx->wait_event.tasklet);
3451
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003452 quic_conn_drop(qc);
3453}
3454
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003455void quic_close(struct connection *conn, void *xprt_ctx)
3456{
3457 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003458 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003459
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003460 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003461 /* This task must be deleted by the connection-pinned thread. */
3462 if (qc->timer_task) {
3463 task_destroy(qc->timer_task);
3464 qc->timer_task = NULL;
3465 }
3466
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003467 /* Next application data can be dropped. */
3468 qc->mux_state = QC_MUX_RELEASED;
3469
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003470 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9c4da932022-01-21 14:54:58 +01003471
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003472 /* TODO for now release the quic_conn on notification by the upper
3473 * layer. It could be useful to delay it if there is remaining data to
3474 * send or data to be acked.
3475 */
3476 quic_conn_release(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003477}
3478
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003479/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003480static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003481{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003482 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003483 struct quic_conn *qc;
3484 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003485 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003486
3487 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003488 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003489 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003490 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003491 task->expire = TICK_ETERNITY;
3492 pktns = quic_loss_pktns(qc);
3493 if (tick_isset(pktns->tx.loss_time)) {
3494 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3495
3496 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3497 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003498 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3499 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003500 goto out;
3501 }
3502
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003503 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003504 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003505 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003506 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3507 pktns->tx.pto_probe = 1;
3508 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3509 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3510 }
3511 else {
3512 pktns->tx.pto_probe = 2;
3513 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003514 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003515 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003516 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3517 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3518
3519 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3520 hel->pktns->tx.pto_probe = 1;
3521 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3522 iel->pktns->tx.pto_probe = 1;
3523 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003524
3525 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3526 int j;
3527
3528 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3529 continue;
3530
3531 for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++)
3532 qc_prep_fast_retrans(&qc->els[i], qc);
3533 }
3534
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003535 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3536 qc->path->loss.pto_count++;
3537
3538 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003539 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003540
3541 return task;
3542}
3543
3544/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3545 * connections used to identify the first Initial packets of client connecting
3546 * to listeners. This parameter must be NULL for QUIC connections attached
3547 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3548 * <scid> is the source connection ID with <scid_len> as length.
3549 * Returns 1 if succeeded, 0 if not.
3550 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003551static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003552 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003553 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003554{
3555 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003556 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003557 /* Initial CID. */
3558 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003559 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003560 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003562 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003563 qc = pool_zalloc(pool_head_quic_conn);
3564 if (!qc) {
3565 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3566 goto err;
3567 }
3568
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003569 HA_ATOMIC_STORE(&qc->refcount, 0);
3570
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003571 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3572 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003573 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003574 goto err;
3575 }
3576
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003577 qc->cids = EB_ROOT;
3578 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003579 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003580 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003581
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003582 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003583 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003584 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003585 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003586 qc->odcid.addrlen = dcid_addr_len;
3587 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003588
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003589 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590 if (scid_len)
3591 memcpy(qc->dcid.data, scid, scid_len);
3592 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003593 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003594 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003595 }
3596 /* QUIC Client (outgoing connection to servers) */
3597 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003598 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003599 if (dcid_len)
3600 memcpy(qc->dcid.data, dcid, dcid_len);
3601 qc->dcid.len = dcid_len;
3602 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003603 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003604
3605 /* Initialize the output buffer */
3606 qc->obuf.pos = qc->obuf.data;
3607
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003608 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003609 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003610 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003611 goto err;
3612 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003613
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003614 /* insert the allocated CID in the receiver datagram handler tree */
3615 if (server)
3616 ebmb_insert(&l->rx.dghdlrs[tid]->cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003617
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003618 /* Select our SCID which is the first CID with 0 as sequence number. */
3619 qc->scid = icid->cid;
3620
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003621 /* Packet number spaces initialization. */
3622 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3623 quic_pktns_init(&qc->pktns[i]);
3624 /* QUIC encryption level context initialization. */
3625 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003626 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003627 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003628 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003629 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003630 /* Initialize the packet number space. */
3631 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3632 }
3633
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003634 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003635 qc->tps_tls_ext = qc->version & 0xff000000 ?
3636 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3637 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003638 /* TX part. */
3639 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003640 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3641 qc->tx.wbuf = qc->tx.rbuf = 0;
3642 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003643 /* RX part. */
3644 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003645 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003646 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3647 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3648 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003649 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003650 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003651 goto err;
3652 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003654 /* XXX TO DO: Only one path at this time. */
3655 qc->path = &qc->paths[0];
3656 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3657
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01003658 /* required to use MTLIST_IN_LIST */
3659 MT_LIST_INIT(&qc->accept_list);
3660
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003661 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003662
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003663 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003664
3665 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003666 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003667 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003668 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003669}
3670
3671/* Initialize the timer task of <qc> QUIC connection.
3672 * Returns 1 if succeeded, 0 if not.
3673 */
3674static int quic_conn_init_timer(struct quic_conn *qc)
3675{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003676 /* Attach this task to the same thread ID used for the connection */
3677 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003678 if (!qc->timer_task)
3679 return 0;
3680
3681 qc->timer = TICK_ETERNITY;
3682 qc->timer_task->process = process_timer;
3683 qc->timer_task->context = qc->conn->xprt_ctx;
3684
3685 return 1;
3686}
3687
3688/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3689 * past one byte of this buffer.
3690 */
3691static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3692 struct quic_rx_packet *pkt)
3693{
3694 unsigned char dcid_len, scid_len;
3695
3696 /* Version */
3697 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3698 return 0;
3699
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003700 /* Destination Connection ID Length */
3701 dcid_len = *(*buf)++;
3702 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3703 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3704 /* XXX MUST BE DROPPED */
3705 return 0;
3706
3707 if (dcid_len) {
3708 /* Check that the length of this received DCID matches the CID lengths
3709 * of our implementation for non Initials packets only.
3710 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003711 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3712 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003713 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003714 return 0;
3715
3716 memcpy(pkt->dcid.data, *buf, dcid_len);
3717 }
3718
3719 pkt->dcid.len = dcid_len;
3720 *buf += dcid_len;
3721
3722 /* Source Connection ID Length */
3723 scid_len = *(*buf)++;
3724 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3725 /* XXX MUST BE DROPPED */
3726 return 0;
3727
3728 if (scid_len)
3729 memcpy(pkt->scid.data, *buf, scid_len);
3730 pkt->scid.len = scid_len;
3731 *buf += scid_len;
3732
3733 return 1;
3734}
3735
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003736/* Insert <pkt> RX packet in its <qel> RX packets tree */
3737static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3738{
3739 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003740 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003741 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3742 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3743 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003744}
3745
3746/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003747 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3748 * byte past the end of the buffer containing this packet and <beg> the address of
3749 * the packet first byte.
3750 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3751 * Returns 1 if succeeded, 0 if not.
3752 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003753static inline int qc_try_rm_hp(struct quic_conn *qc,
3754 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003755 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003756 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003757 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003758{
3759 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003760 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003761 struct quic_enc_level *qel;
3762 /* Only for traces. */
3763 struct quic_rx_packet *qpkt_trace;
3764
3765 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003766 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003767 /* The packet number is here. This is also the start minus
3768 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3769 * protection.
3770 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003771 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003772
3773 tel = quic_packet_type_enc_level(pkt->type);
3774 qel = &qc->els[tel];
3775
3776 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003777 /* Note that the following function enables us to unprotect the packet
3778 * number and its length subsequently used to decrypt the entire
3779 * packets.
3780 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003781 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3782 qel->pktns->rx.largest_pn, pn, beg, end)) {
3783 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003784 goto err;
3785 }
3786
3787 /* The AAD includes the packet number field found at <pn>. */
3788 pkt->aad_len = pn - beg + pkt->pnl;
3789 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003790 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003791 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003792 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3793 /* If the packet number space has been discarded, this packet
3794 * will be not parsed.
3795 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003796 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003797 goto out;
3798 }
3799
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003800 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003801 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003802 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3803 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003804 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003805 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003806 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003807 goto err;
3808 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003809
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003810 *el = qel;
3811 /* No reference counter incrementation here!!! */
3812 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3813 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3814 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3815 b_add(&qc->rx.buf, pkt->len);
3816 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003817 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003818 return 1;
3819
3820 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003821 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003822 return 0;
3823}
3824
3825/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3826 * Also set <*long_header> to 1 if this form is long, 0 if not.
3827 */
3828static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3829 unsigned char byte0, int *long_header)
3830{
3831 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3832 pkt->type =
3833 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3834 *long_header = 1;
3835 }
3836 else {
3837 pkt->type = QUIC_PACKET_TYPE_SHORT;
3838 *long_header = 0;
3839 }
3840}
3841
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003842/*
3843 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3844 */
3845static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3846{
3847 int j = 0, version;
3848
3849 do {
3850 version = quic_supported_version[j];
3851 if (version == pkt->version)
3852 return 1;
3853
3854 version = quic_supported_version[++j];
3855 } while(version);
3856
3857 return 0;
3858}
3859
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003860/*
3861 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3862 * address <addr>.
3863 * Implementation of RFC9000 6. Version Negotiation
3864 *
3865 * TODO implement a rate-limiting sending of Version Negotiation packets
3866 *
3867 * Returns 0 on success else non-zero
3868 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003869static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3870 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003871{
3872 char buf[256];
3873 int i = 0, j, version;
3874 const socklen_t addrlen = get_addr_len(addr);
3875
3876 /*
3877 * header form
3878 * long header, fixed bit to 0 for Version Negotiation
3879 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003880 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3881 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003882
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003883 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003884 /* null version for Version Negotiation */
3885 buf[i++] = '\x00';
3886 buf[i++] = '\x00';
3887 buf[i++] = '\x00';
3888 buf[i++] = '\x00';
3889
3890 /* source connection id */
3891 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003892 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003893 i += pkt->scid.len;
3894
3895 /* destination connection id */
3896 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003897 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003898 i += pkt->dcid.len;
3899
3900 /* supported version */
3901 j = 0;
3902 do {
3903 version = htonl(quic_supported_version[j]);
3904 memcpy(&buf[i], &version, sizeof(version));
3905 i += sizeof(version);
3906
3907 version = quic_supported_version[++j];
3908 } while (version);
3909
3910 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3911 return 1;
3912
3913 return 0;
3914}
3915
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01003916/* Generate the token to be used in Retry packets. The token is written to
3917 * <buf> which is expected to be <len> bytes.
3918 *
3919 * Various parameters are expected to be encoded in the token. For now, only
3920 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
3921 * as this CID must be repeated by the server in the transport parameters.
3922 *
3923 * TODO add the client address to validate the token origin.
3924 *
3925 * Returns the length of the encoded token or 0 on error.
3926 */
3927static int generate_retry_token(unsigned char *buf, unsigned char len,
3928 struct quic_rx_packet *pkt)
3929{
3930 const size_t token_len = 1 + pkt->dcid.len;
3931 unsigned char i = 0;
3932
3933 if (token_len > len)
3934 return 0;
3935
3936 buf[i++] = pkt->dcid.len;
3937 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
3938 i += pkt->dcid.len;
3939
3940 return i;
3941}
3942
3943/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
3944 * the Initial <pkt> packet.
3945 *
3946 * Returns 0 on success else non-zero.
3947 */
3948static int send_retry(int fd, struct sockaddr_storage *addr,
3949 struct quic_rx_packet *pkt)
3950{
3951 unsigned char buf[128];
3952 int i = 0, token_len;
3953 const socklen_t addrlen = get_addr_len(addr);
3954 struct quic_cid scid;
3955
3956 /* long header + fixed bit + packet type 0x3 */
3957 buf[i++] = 0xf0;
3958 /* version */
3959 buf[i++] = 0x00;
3960 buf[i++] = 0x00;
3961 buf[i++] = 0x00;
3962 buf[i++] = 0x01;
3963
3964 /* Use the SCID from <pkt> for Retry DCID. */
3965 buf[i++] = pkt->scid.len;
3966 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
3967 i += pkt->scid.len;
3968
3969 /* Generate a new CID to be used as SCID for the Retry packet. */
3970 scid.len = QUIC_HAP_CID_LEN;
3971 if (RAND_bytes(scid.data, scid.len) != 1)
3972 return 1;
3973
3974 buf[i++] = scid.len;
3975 memcpy(&buf[i], scid.data, scid.len);
3976 i += scid.len;
3977
3978 /* token */
3979 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
3980 return 1;
3981 i += token_len;
3982
3983 /* token integrity tag */
3984 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
3985 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
3986 pkt->dcid.len, buf, i)) {
3987 return 1;
3988 }
3989
3990 i += QUIC_TLS_TAG_LEN;
3991
3992 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3993 return 1;
3994
3995 return 0;
3996}
3997
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003998/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
3999 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4000 * concatenated to the <pkt> DCID field.
4001 *
4002 * Returns the instance or NULL if not found.
4003 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004004static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004005 struct listener *l,
4006 struct sockaddr_storage *saddr)
4007{
4008 struct quic_conn *qc = NULL;
4009 struct ebmb_node *node;
4010 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004011 /* set if the quic_conn is found in the second DCID tree */
4012 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004013
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004014 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4015 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4016 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4017 /* DCIDs of first packets coming from multiple clients may have
4018 * the same values. Let's distinguish them by concatenating the
4019 * socket addresses.
4020 */
4021 quic_cid_saddr_cat(&pkt->dcid, saddr);
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004022 node = ebmb_lookup(&l->rx.dghdlrs[tid]->odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004023 pkt->dcid.len + pkt->dcid.addrlen);
4024 if (node) {
4025 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4026 goto end;
4027 }
4028 }
4029
4030 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4031 * also for INITIAL/0-RTT non-first packets with the final DCID in
4032 * used.
4033 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004034 node = ebmb_lookup(&l->rx.dghdlrs[tid]->cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004035 if (!node)
4036 goto end;
4037
4038 id = ebmb_entry(node, struct quic_connection_id, node);
4039 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004040 found_in_dcid = 1;
4041
4042 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004043 if (qc)
4044 quic_conn_take(qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004045
4046 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4047 * If already done, this is a noop.
4048 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004049 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004050 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004051
4052 return qc;
4053}
4054
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004055/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4056 * will extract the parameters stored in the token : <odcid>.
4057 *
4058 * Returns 0 on success else non-zero.
4059 */
4060static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4061 struct quic_cid *odcid)
4062{
4063 uint64_t odcid_len;
4064
4065 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4066 return 1;
4067
4068 memcpy(odcid->data, token, odcid_len);
4069 odcid->len = odcid_len;
4070
4071 return 0;
4072}
4073
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004074/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4075 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4076 * parameters of this session.
4077 * This is the responsibility of the caller to check the validity of all the
4078 * pointers passed as parameter to this function.
4079 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4080 * CO_ER_SSL_NO_MEM.
4081 */
4082static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4083 unsigned char *params, size_t params_len)
4084{
4085 int retry;
4086
4087 retry = 1;
4088 retry:
4089 *ssl = SSL_new(ssl_ctx);
4090 if (!*ssl) {
4091 if (!retry--)
4092 goto err;
4093
4094 pool_gc(NULL);
4095 goto retry;
4096 }
4097
4098 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4099 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4100 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
4101 goto err;
4102
4103 SSL_free(*ssl);
4104 *ssl = NULL;
4105 if (!retry--)
4106 goto err;
4107
4108 pool_gc(NULL);
4109 goto retry;
4110 }
4111
4112 return 0;
4113
4114 err:
4115 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4116 return -1;
4117}
4118
4119/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4120 * used to process <qc> received packets. The allocated context is stored in
4121 * <qc.xprt_ctx>.
4122 *
4123 * Returns 0 on success else non-zero.
4124 */
4125int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4126{
4127 struct bind_conf *bc = qc->li->bind_conf;
4128 struct ssl_sock_ctx *ctx = NULL;
4129
4130 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4131 if (!ctx)
4132 goto err;
4133
4134 ctx->wait_event.tasklet = tasklet_new();
4135 if (!ctx->wait_event.tasklet)
4136 goto err;
4137
4138 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4139 ctx->wait_event.tasklet->context = ctx;
4140 ctx->wait_event.events = 0;
4141 ctx->subs = NULL;
4142 ctx->xprt_ctx = NULL;
4143 ctx->qc = qc;
4144
4145 /* Set tasklet tid based on the SCID selected by us for this
4146 * connection. The upper layer will also be binded on the same thread.
4147 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004148 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004149
4150 if (qc_is_listener(qc)) {
4151 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4152 qc->enc_params, qc->enc_params_len) == -1) {
4153 goto err;
4154 }
4155
4156 /* Enabling 0-RTT */
4157 if (bc->ssl_conf.early_data)
4158 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4159
4160 SSL_set_accept_state(ctx->ssl);
4161 }
4162
4163 ctx->xprt = xprt_get(XPRT_QUIC);
4164
4165 /* Store the allocated context in <qc>. */
4166 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
4167
4168 return 0;
4169
4170 err:
4171 if (ctx && ctx->wait_event.tasklet)
4172 tasklet_free(ctx->wait_event.tasklet);
4173 pool_free(pool_head_quic_conn_ctx, ctx);
4174
4175 return 1;
4176}
4177
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004178static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004179 struct quic_rx_packet *pkt, int first_pkt,
4180 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004181{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004182 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004183 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004184 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004185 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004186 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004187 size_t b_cspace;
4188 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004189
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004190 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004191 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004192 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004193 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004194 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4195 /* This ist only to please to traces and distinguish the
4196 * packet with parsed packet number from others.
4197 */
4198 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004199 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004200 goto err;
4201
4202 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004203 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004204 /* XXX TO BE DISCARDED */
4205 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4206 goto err;
4207 }
4208
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004209 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004210 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004211 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004212 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004213 uint64_t len;
4214
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004215 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004216 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4217 goto err;
4218 }
4219
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004220 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4221 * they must have the same DCID.
4222 */
4223 if (!first_pkt &&
4224 (pkt->dcid.len != dgram->dcid_len ||
4225 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4226 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4227 goto err;
4228 }
4229
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004230 /* Retry of Version Negotiation packets are only sent by servers */
4231 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4232 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4233 goto err;
4234 }
4235
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004236 /* RFC9000 6. Version Negotiation */
4237 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004238 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004239 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004240 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4241 goto err;
4242 }
4243
4244 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004245 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004246 }
4247
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004248 /* For Initial packets, and for servers (QUIC clients connections),
4249 * there is no Initial connection IDs storage.
4250 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004251 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004252 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004253
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004254 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4255 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004256 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4257 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004258 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004259
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004260 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4261 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004262 */
4263 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004264
4265 /* TODO Retry should be automatically activated if
4266 * suspect network usage is detected.
4267 */
4268 if (!token_len && l->bind_conf->quic_force_retry) {
4269 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004270 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004271 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4272 goto err;
4273 }
4274
4275 goto err;
4276 }
4277 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004278 pkt->token = buf;
4279 buf += pkt->token_len;
4280 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004281 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004282 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004283 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004284 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4285 goto err;
4286 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004287 }
4288
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004289 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4290 end - buf < len) {
4291 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4292 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004293 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004294
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004295 payload = buf;
4296 pkt->len = len + payload - beg;
4297
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004298 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004299 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004300 int ipv4;
4301 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004302 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004303 const unsigned char *salt = initial_salt_v1;
4304 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004305
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004306 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004307 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004308 goto err;
4309 }
4310
Frédéric Lécailledc364042022-01-27 16:51:54 +01004311 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4312 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4313 goto err;
4314 }
4315
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004316 pkt->saddr = dgram->saddr;
4317 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004318 qc = qc_new_conn(pkt->version, ipv4,
4319 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004320 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004321 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004322 goto err;
4323
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004324 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4325
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004326 odcid = &qc->rx.params.original_destination_connection_id;
4327 /* Copy the transport parameters. */
4328 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004329
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004330 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004331 if (pkt->token_len) {
4332 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4333 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4334 goto err;
4335 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004336 /* Copy retry_source_connection_id transport parameter. */
4337 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4338 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004339 }
4340 else {
4341 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4342 odcid->len = pkt->dcid.len;
4343 }
4344
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004345 /* Copy the initial source connection ID. */
4346 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4347 qc->enc_params_len =
4348 quic_transport_params_encode(qc->enc_params,
4349 qc->enc_params + sizeof qc->enc_params,
4350 &qc->rx.params, 1);
4351 if (!qc->enc_params_len)
4352 goto err;
4353
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004354 if (qc_conn_alloc_ssl_ctx(qc))
4355 goto err;
4356
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004357 /* NOTE: the socket address has been concatenated to the destination ID
4358 * chosen by the client for Initial packets.
4359 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004360 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4361 salt = initial_salt_draft_29;
4362 salt_len = sizeof initial_salt_draft_29;
4363 }
4364 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004365 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004366 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004367 goto err;
4368 }
4369
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004370 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004371 n = ebmb_insert(&l->rx.dghdlrs[tid]->odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004372 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004373
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004374 /* If the insertion failed, it means that another
4375 * thread has already allocated a QUIC connection for
4376 * the same CID. Liberate our allocated connection.
4377 */
4378 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004379 qc_to_purge = qc;
4380
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004381 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4382 pkt->qc = qc;
4383 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004384
4385 quic_conn_take(qc);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004386
4387 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004388 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004389 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004390 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004391 else {
4392 quic_conn_drop(qc_to_purge);
4393 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004394 }
4395 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004396 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004397 }
4398 }
4399 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004400 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004401 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4402 goto err;
4403 }
4404
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004405 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004406 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004407
4408 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4409 * they must have the same DCID.
4410 */
4411 if (!first_pkt &&
4412 (pkt->dcid.len != dgram->dcid_len ||
4413 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4414 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4415 goto err;
4416 }
4417
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004418 buf += QUIC_HAP_CID_LEN;
4419
4420 /* A short packet is the last one of a UDP datagram. */
4421 payload = buf;
4422 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004423
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004424 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004425 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004426 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004427 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4428 goto err;
4429 }
4430
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
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004434
4435 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4436 * they must have the same DCID.
4437 *
4438 * This check must be done after the final update to pkt.len to
4439 * properly drop the packet on failure.
4440 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004441 if (first_pkt && !quic_peer_validated_addr(qc) &&
4442 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4443 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4444 QUIC_EV_CONN_LPKT, qc);
4445 /* Reset the anti-amplification bit. It will be set again
4446 * when sending the next packet if reached again.
4447 */
4448 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4449 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4450 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004451 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004452
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004453 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004454
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004455 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004456 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004457 goto out;
4458 }
4459
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004460 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004461 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004462 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004463 b_cspace = b_contig_space(&qc->rx.buf);
4464 if (b_cspace < pkt->len) {
4465 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004466 if (b_cspace) {
4467 b_putchr(&qc->rx.buf, 0x00);
4468 b_cspace--;
4469 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004470 b_add(&qc->rx.buf, b_cspace);
4471 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4472 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004473 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004474 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004475 goto err;
4476 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004477 }
4478
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004479 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004480 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004481 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004482 goto err;
4483 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004484
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004485 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004486 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004487 if (pkt->aad_len)
4488 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004489 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004490 /* Wake up the connection packet handler task from here only if all
4491 * the contexts have been initialized, especially the mux context
4492 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4493 * will start it if these contexts for the connection are not already
4494 * initialized.
4495 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004496 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004497 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004498 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004499
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004500 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004501
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004502 if (qc)
4503 quic_conn_drop(qc);
4504
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004505 return pkt->len;
4506
4507 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004508 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4509 * This cannot be done by this thread.
4510 */
4511 if (io_cb_wakeup) {
4512 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4513 if (conn_ctx && conn_ctx->wait_event.tasklet)
4514 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4515 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004516 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004517 if (!pkt->len)
4518 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004519 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004520 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004521
4522 if (qc)
4523 quic_conn_drop(qc);
4524
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004525 return -1;
4526}
4527
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004528/* This function builds into <buf> buffer a QUIC long packet header.
4529 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004530 */
4531static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4532 int type, size_t pn_len, struct quic_conn *conn)
4533{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004534 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004535 return 0;
4536
4537 /* #0 byte flags */
4538 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4539 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4540 /* Version */
4541 quic_write_uint32(buf, end, conn->version);
4542 *(*buf)++ = conn->dcid.len;
4543 /* Destination connection ID */
4544 if (conn->dcid.len) {
4545 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4546 *buf += conn->dcid.len;
4547 }
4548 /* Source connection ID */
4549 *(*buf)++ = conn->scid.len;
4550 if (conn->scid.len) {
4551 memcpy(*buf, conn->scid.data, conn->scid.len);
4552 *buf += conn->scid.len;
4553 }
4554
4555 return 1;
4556}
4557
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004558/* This function builds into <buf> buffer a QUIC short packet header.
4559 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004560 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004561static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4562 size_t pn_len, struct quic_conn *conn,
4563 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004564{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004565 if (end - *buf < 1 + conn->dcid.len)
4566 return 0;
4567
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004568 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004569 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4570 ((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 +01004571 /* Destination connection ID */
4572 if (conn->dcid.len) {
4573 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4574 *buf += conn->dcid.len;
4575 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004576
4577 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004578}
4579
4580/* Apply QUIC header protection to the packet with <buf> as first byte address,
4581 * <pn> as address of the Packet number field, <pnlen> being this field length
4582 * with <aead> as AEAD cipher and <key> as secret key.
4583 * Returns 1 if succeeded or 0 if failed.
4584 */
4585static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4586 const EVP_CIPHER *aead, const unsigned char *key)
4587{
4588 int i, ret, outlen;
4589 EVP_CIPHER_CTX *ctx;
4590 /* We need an IV of at least 5 bytes: one byte for bytes #0
4591 * and at most 4 bytes for the packet number
4592 */
4593 unsigned char mask[5] = {0};
4594
4595 ret = 0;
4596 ctx = EVP_CIPHER_CTX_new();
4597 if (!ctx)
4598 return 0;
4599
4600 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4601 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4602 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4603 goto out;
4604
4605 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4606 for (i = 0; i < pnlen; i++)
4607 pn[i] ^= mask[i + 1];
4608
4609 ret = 1;
4610
4611 out:
4612 EVP_CIPHER_CTX_free(ctx);
4613
4614 return ret;
4615}
4616
4617/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4618 * ACK ranges if needed to a value below <limit> in bytes.
4619 * Return 1 if succeeded, 0 if not.
4620 */
4621static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4622{
4623 size_t room, ack_delay_sz;
4624
4625 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4626 /* A frame is made of 1 byte for the frame type. */
4627 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004628 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004629 return 0;
4630
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004631 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004632}
4633
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004634/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4635 * 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 +01004636 * and <*len> the packet Length field initialized with the number of bytes already present
4637 * 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 +02004638 * <headlen> is the number of bytes already present in this packet before building frames.
4639 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004640 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004641 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004642 * Return 1 if succeeded, 0 if not.
4643 */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004644static inline int qc_build_frms(struct list *l,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004645 size_t room, size_t *len, size_t headlen,
4646 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004647 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004648{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004649 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004650 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004651
Frédéric Lécailleea604992020-12-24 13:01:37 +01004652 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004653 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004654 return 0;
4655
Frédéric Lécailleea604992020-12-24 13:01:37 +01004656 /* If we are not probing we must take into an account the congestion
4657 * control window.
4658 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004659 if (!qel->pktns->tx.pto_probe) {
4660 size_t remain = quic_path_prep_data(qc->path);
4661
4662 if (headlen > remain)
4663 return 0;
4664
4665 room = QUIC_MIN(room, remain - headlen);
4666 }
4667
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004668 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004669 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004670 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004671 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004672 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004673
Frédéric Lécailleea604992020-12-24 13:01:37 +01004674 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004675 break;
4676
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004677 switch (cf->type) {
4678 case QUIC_FT_CRYPTO:
4679 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004680 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004681 /* Compute the length of this CRYPTO frame header */
4682 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4683 /* Compute the data length of this CRyPTO frame. */
4684 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4685 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004686 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004687 if (!dlen)
4688 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004689
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004690 /* CRYPTO frame length. */
4691 flen = hlen + quic_int_getsize(dlen) + dlen;
4692 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004693 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004694 /* Add the CRYPTO data length and its encoded length to the packet
4695 * length and the length of this length.
4696 */
4697 *len += flen;
4698 room -= flen;
4699 if (dlen == cf->crypto.len) {
4700 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004701 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004702 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004703 }
4704 else {
4705 struct quic_frame *new_cf;
4706
4707 new_cf = pool_alloc(pool_head_quic_frame);
4708 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004709 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004710 return 0;
4711 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004712
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004713 new_cf->type = QUIC_FT_CRYPTO;
4714 new_cf->crypto.len = dlen;
4715 new_cf->crypto.offset = cf->crypto.offset;
4716 new_cf->crypto.qel = qel;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004717 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004718 /* Consume <dlen> bytes of the current frame. */
4719 cf->crypto.len -= dlen;
4720 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004722 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004723
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004724 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004725 /* Note that these frames are accepted in short packets only without
4726 * "Length" packet field. Here, <*len> is used only to compute the
4727 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004728 *
4729 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004730 * excepting the variable ones. Note that +1 is for the type of this frame.
4731 */
4732 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004733 ((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 +02004734 /* Compute the data length of this STREAM frame. */
4735 avail_room = room - hlen - *len;
4736 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004737 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004738
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004739 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004740 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004741 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4742 dlen = max_available_room(avail_room, &dlen_sz);
4743 if (dlen > cf->stream.len) {
4744 dlen = cf->stream.len;
4745 }
4746 dlen_sz = quic_int_getsize(dlen);
4747 flen = hlen + dlen_sz + dlen;
4748 }
4749 else {
4750 dlen = QUIC_MIN(avail_room, cf->stream.len);
4751 flen = hlen + dlen;
4752 }
4753 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004754 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004755 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004756 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004757 /* Add the STREAM data length and its encoded length to the packet
4758 * length and the length of this length.
4759 */
4760 *len += flen;
4761 room -= flen;
4762 if (dlen == cf->stream.len) {
4763 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004764 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004765 LIST_APPEND(l, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004766 }
4767 else {
4768 struct quic_frame *new_cf;
4769
4770 new_cf = pool_zalloc(pool_head_quic_frame);
4771 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004772 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004773 return 0;
4774 }
4775
4776 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004777 new_cf->stream.qcs = cf->stream.qcs;
4778 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004779 new_cf->stream.id = cf->stream.id;
4780 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4781 new_cf->stream.offset = cf->stream.offset;
4782 new_cf->stream.len = dlen;
4783 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4784 /* FIN bit reset */
4785 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4786 new_cf->stream.data = cf->stream.data;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004787 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004788 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4789 /* Consume <dlen> bytes of the current frame. */
4790 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004791 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004792 cf->stream.data += dlen;
4793 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004794 break;
4795
4796 default:
4797 flen = qc_frm_len(cf);
4798 BUG_ON(!flen);
4799 if (flen > room)
4800 continue;
4801
4802 *len += flen;
4803 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004804 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004805 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004806 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004807 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004808 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004809 }
4810
Frédéric Lécailleea604992020-12-24 13:01:37 +01004811 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004812}
4813
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004814/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004815 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4816 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4817 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004818 * 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 +02004819 * 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 +02004820 * having returned from this function.
4821 * 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 +01004822 * number field in this packet. <pn_len> will also have the packet number
4823 * length as value.
4824 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004825 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004826 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004827static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4828 size_t dglen, struct quic_tx_packet *pkt,
4829 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004830 int ack, int padding, int cc, int probe,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004831 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004832{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004833 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004834 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004835 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4836 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004837 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004838 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004839 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004840 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004841 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004842
Frédéric Lécailleea604992020-12-24 13:01:37 +01004843 /* Length field value with CRYPTO frames if present. */
4844 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004845 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004846 /* When not probing and not acking, and no immediate close is required,
4847 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004848 * the congestion controller window. So, we do not limit the size of this
4849 * packet if we have an ACK frame to send because an ACK frame is not
4850 * ack-eliciting. This size will be limited if we have ack-eliciting
4851 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004852 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004853 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004854 size_t path_room;
4855
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004856 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004857 if (end - beg > path_room)
4858 end = beg + path_room;
4859 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004860
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004861 /* Ensure there is enough room for the TLS encryption tag and a zero token
4862 * length field if any.
4863 */
4864 if (end - pos < QUIC_TLS_TAG_LEN +
4865 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4866 goto no_room;
4867
4868 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004869 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004870 /* packet number length */
4871 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004872 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004873 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004874 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004875 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004876 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004877 goto no_room;
4878
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004879 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4880 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004881 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004882 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004883 /* Build an ACK frame if required. */
4884 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004885 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004886 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004887 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004888 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4889 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4890 * that from here, we do not know if we will have to send a PING frame.
4891 * This will be decided after having computed the ack-eliciting frames
4892 * to be added to this packet.
4893 */
4894 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 +01004895 if (!ack_frm_len)
4896 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004897 }
4898
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004899 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004900 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004901 len_frms = 0;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004902 if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004903 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004904
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004905 /* Initialize the length of the frames built below to <len>.
4906 * If any frame could be successfully built by qc_build_frms(),
4907 * we will have len_frms > len.
4908 */
4909 len_frms = len;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004910 if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004911 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004912 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004913 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004914 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004915
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004916 /* Length (of the remaining data). Must not fail because, the buffer size
4917 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4918 * for the encryption tag. It must be taken into an account for the length
4919 * of this packet.
4920 */
4921 if (len_frms)
4922 len = len_frms + QUIC_TLS_TAG_LEN;
4923 else
4924 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004925 /* CONNECTION_CLOSE frame */
4926 if (cc) {
4927 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004928
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004929 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004930 len += qc_frm_len(&cc_frm);
4931 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004932 add_ping_frm = 0;
4933 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004934 len_sz = quic_int_getsize(len);
4935 /* Add this packet size to <dglen> */
4936 dglen += head_len + len_sz + len;
4937 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4938 /* This is a maximum padding size */
4939 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4940 /* The length field value is of this packet is <len> + <padding_len>
4941 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004942 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004943 * sizes from <padding_len>.
4944 */
4945 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4946 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004947 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004948 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004949 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004950 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004951 add_ping_frm = 1;
4952 len += 1;
4953 }
4954 /* 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 +01004955 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004956 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4957 }
4958
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004959 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4960 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004961
4962 /* Packet number field address. */
4963 *buf_pn = pos;
4964
4965 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004966 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4967 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004968
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004969 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004970 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004971
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004972 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004973 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004974 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004975
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004976 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004977 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004978 ssize_t room = end - pos;
4979 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004980 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004981 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004982 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004983 }
4984 }
4985
4986 /* Build a PING frame if needed. */
4987 if (add_ping_frm) {
4988 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004989 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004990 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004991 }
4992
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004993 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004994 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004995 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004996
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004997 /* Build a PADDING frame if needed. */
4998 if (padding_len) {
4999 frm.type = QUIC_FT_PADDING;
5000 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005001 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005002 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005003 }
5004
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005005 /* If this packet is ack-eliciting and we are probing let's
5006 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005007 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005008 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5009 qel->pktns->tx.pto_probe)
5010 qel->pktns->tx.pto_probe--;
5011
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005012 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005013 LIST_SPLICE(&pkt->frms, &frm_list);
5014 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005015
5016 return 1;
5017
5018 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005019 /* Replace the pre-built frames which could not be add to this packet */
5020 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005021 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005022 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005023}
5024
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005025static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005026{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005027 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005028 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005029 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005030 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005031 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005032 pkt->next = NULL;
5033 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005034}
5035
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005036/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005037static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5038{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005039 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005040
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005041 if (!pkt)
5042 return;
5043
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005044 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005045 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005046 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005047 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005048 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005049}
5050
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005051/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5052 * type for <qc> QUIC connection from <qel> encryption level.
5053 * Return -2 if the packet could not be allocated or encrypted for any reason,
5054 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005055 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005056static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5057 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005058 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005059 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005060 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005061{
5062 /* The pointer to the packet number field. */
5063 unsigned char *buf_pn;
5064 unsigned char *beg, *end, *payload;
5065 int64_t pn;
5066 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005067 struct quic_tls_ctx *tls_ctx;
5068 struct quic_tx_packet *pkt;
5069
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005070 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005071 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005072 pkt = pool_alloc(pool_head_quic_tx_packet);
5073 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005074 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005075 *err = -2;
5076 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005077 }
5078
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005079 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005080 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005081 pn_len = 0;
5082 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005083
5084 pn = qel->pktns->tx.next_pn + 1;
5085 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 +01005086 ack, padding, cc, probe, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005087 *err = -1;
5088 goto err;
5089 }
5090
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005091 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005092 payload = buf_pn + pn_len;
5093 payload_len = end - payload;
5094 aad_len = payload - beg;
5095
5096 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005097 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005098 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005099 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005100 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005101
5102 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005103 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005104 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5105 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005106 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005107 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005108 goto err;
5109 }
5110
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005111 /* Consume a packet number */
5112 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005113 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005114 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5115 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005116 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5117 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005118 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005119 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005120 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005121 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005122 pkt->in_flight_len = pkt->len;
5123 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005124 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005125 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005126 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005127
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005128 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005129
5130 err:
5131 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005132 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005133 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005134}
5135
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005136/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
5137 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005138 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005139static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
5140 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005141{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005142 size_t try, done = 0;
5143
5144 if (!conn_ctrl_ready(conn))
5145 return 0;
5146
5147 if (!fd_recv_ready(conn->handle.fd))
5148 return 0;
5149
5150 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005151
5152 /* read the largest possible block. For this, we perform only one call
5153 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005154 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005155 */
5156 while (count > 0) {
5157 try = b_contig_space(buf);
5158 if (!try)
5159 break;
5160
5161 if (try > count)
5162 try = count;
5163
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005164 b_add(buf, try);
5165 done += try;
5166 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005167 }
5168
5169 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5170 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5171
5172 leave:
5173 return done;
5174
5175 read0:
5176 conn_sock_read0(conn);
5177 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5178
5179 /* Now a final check for a possible asynchronous low-level error
5180 * report. This can happen when a connection receives a reset
5181 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
5182 * we might have come from there by just checking POLL_HUP instead
5183 * of recv()'s return value 0, so we have no way to tell there was
5184 * an error without checking.
5185 */
Willy Tarreauf5090652021-04-06 17:23:40 +02005186 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005187 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5188 goto leave;
5189}
5190
5191
5192/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5193 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5194 * other pending data for example, but this flag is ignored at the moment.
5195 * Only one call to send() is performed, unless the buffer wraps, in which case
5196 * a second call may be performed. The connection's flags are updated with
5197 * whatever special event is detected (error, empty). The caller is responsible
5198 * for taking care of those events and avoiding the call if inappropriate. The
5199 * function does not call the connection's polling update function, so the caller
5200 * is responsible for this. It's up to the caller to update the buffer's contents
5201 * based on the return value.
5202 */
5203static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
5204{
5205 ssize_t ret;
5206 size_t try, done;
5207 int send_flag;
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01005208 struct quic_conn *qc = ((struct ssl_sock_ctx *)xprt_ctx)->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005209
5210 done = 0;
5211 /* send the largest possible block. For this we perform only one call
5212 * to send() unless the buffer wraps and we exactly fill the first hunk,
5213 * in which case we accept to do it once again.
5214 */
5215 while (count) {
5216 try = b_contig_data(buf, done);
5217 if (try > count)
5218 try = count;
5219
5220 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
5221 if (try < count || flags & CO_SFL_MSG_MORE)
5222 send_flag |= MSG_MORE;
5223
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01005224 ret = sendto(qc->li->rx.fd, b_peek(buf, done), try, send_flag,
5225 (struct sockaddr *)&qc->peer_addr, get_addr_len(&qc->peer_addr));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005226 if (ret > 0) {
5227 count -= ret;
5228 done += ret;
5229
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005230 if (ret < try)
5231 break;
5232 }
5233 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01005234 ABORT_NOW();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005235 }
5236 else if (errno != EINTR) {
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01005237 ABORT_NOW();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005238 }
5239 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005240
5241 if (done > 0) {
5242 /* we count the total bytes sent, and the send rate for 32-byte
5243 * blocks. The reason for the latter is that freq_ctr are
5244 * limited to 4GB and that it's not enough per second.
5245 */
5246 _HA_ATOMIC_ADD(&global.out_bytes, done);
5247 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
5248 }
5249 return done;
5250}
5251
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005252/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5253 * event subscriber <es> is not allowed to change from a previous call as long
5254 * as at least one event is still subscribed. The <event_type> must only be a
5255 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5256 */
5257static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5258{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005259 struct qcc *qcc = conn->qc->qcc;
5260
5261 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5262 BUG_ON(qcc->subs && qcc->subs != es);
5263
5264 es->events |= event_type;
5265 qcc->subs = es;
5266
5267 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005268 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005269
5270 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005271 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005272
5273 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005274}
5275
5276/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5277 * The <es> pointer is not allowed to differ from the one passed to the
5278 * subscribe() call. It always returns zero.
5279 */
5280static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5281{
5282 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5283}
5284
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005285/* Store in <xprt_ctx> the context attached to <conn>.
5286 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005287 */
5288static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5289{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005290 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005291
5292 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5293
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005294 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005295 if (*xprt_ctx)
5296 goto out;
5297
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005298 HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005299
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005300 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005301 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005302
5303 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005304}
5305
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005306/* Start the QUIC transport layer */
5307static int qc_xprt_start(struct connection *conn, void *ctx)
5308{
5309 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005310 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005311
5312 qc = conn->qc;
5313 if (!quic_conn_init_timer(qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005314 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005315 return 0;
5316 }
5317
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005318 quic_mux_transport_params_update(qc->qcc);
5319 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5320 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5321 return 0;
5322 }
5323
5324 /* mux-quic can now be considered ready. */
5325 qc->mux_state = QC_MUX_READY;
5326
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005327 tasklet_wakeup(qctx->wait_event.tasklet);
5328 return 1;
5329}
5330
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005331/* transport-layer operations for QUIC connections. */
5332static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005333 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005334 .snd_buf = quic_conn_from_buf,
5335 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005336 .subscribe = quic_conn_subscribe,
5337 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005338 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005339 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005340 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5341 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005342 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005343 .name = "QUIC",
5344};
5345
5346__attribute__((constructor))
5347static void __quic_conn_init(void)
5348{
5349 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5350 xprt_register(XPRT_QUIC, &ssl_quic);
5351}
5352
5353__attribute__((destructor))
5354static void __quic_conn_deinit(void)
5355{
5356 BIO_meth_free(ha_quic_meth);
5357}
5358
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005359/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5360 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005361 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005362 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005363struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005364{
5365 unsigned char *pos;
5366 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005367 struct quic_dghdlr *dghdlr = ctx;
5368 struct quic_dgram *dgram;
5369 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005370
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005371 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
5372 if (!dgram)
5373 goto err;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005374
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005375 pos = dgram->buf;
5376 end = pos + dgram->len;
5377 do {
5378 int ret;
5379 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005380
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005381 pkt = pool_zalloc(pool_head_quic_rx_packet);
5382 if (!pkt)
5383 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005384
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005385 quic_rx_packet_refinc(pkt);
5386 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5387 first_pkt = 0;
5388 pos += pkt->len;
5389 quic_rx_packet_refdec(pkt);
5390 if (ret == -1)
5391 /* If the packet length could not be found, we cannot continue. */
5392 break;
5393 } while (pos < end);
5394
5395 /* Mark this datagram as consumed */
5396 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005397
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005398 /* Increasing the received bytes counter by the UDP datagram length
5399 * if this datagram could be associated to a connection.
5400 */
5401 if (dgram->qc)
5402 dgram->qc->rx.bytes += dgram->len;
5403 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005404
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005405 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005406
5407 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005408 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005409}
5410
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005411/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5412 * Returns 1 if succeeded, 0 if not.
5413 */
5414static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5415 unsigned char **dcid, size_t *dcid_len)
5416{
5417 int long_header;
5418 size_t minlen, skip;
5419
5420 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5421 goto err;
5422
5423 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5424 minlen = long_header ?
5425 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5426 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
5427 if (end - buf <= minlen || !(*buf & QUIC_PACKET_FIXED_BIT))
5428 goto err;
5429
5430 buf += skip;
5431 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5432 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5433 goto err;
5434
5435 *dcid = buf;
5436
5437 return 1;
5438
5439 err:
5440 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5441 return 0;
5442}
5443
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005444/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5445 * correct datagram handler.
5446 * Return 1 if a correct datagram could be found, 0 if not.
5447 */
5448int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5449 struct sockaddr_storage *saddr,
5450 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005451{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005452 struct quic_dgram *dgram;
5453 unsigned char *dcid;
5454 size_t dcid_len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005455 int tid;
5456 struct listener *l = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005457
5458 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005459 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005460
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005461 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005462 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005463 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005464
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005465 tid = quic_get_cid_tid(dcid);
5466 /* All the members must be initialized! */
5467 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005468 dgram->buf = buf;
5469 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005470 dgram->dcid = dcid;
5471 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005472 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005473 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005474 LIST_APPEND(dgrams, &dgram->list);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005475 MT_LIST_APPEND(&l->rx.dghdlrs[tid]->dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005476
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005477 tasklet_wakeup(l->rx.dghdlrs[tid]->task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005478
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005479 return 1;
5480
5481 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005482 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005483}
5484
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005485/* Function to automatically activate QUIC traces on stdout.
5486 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5487 * Main use for now is in the docker image for QUIC interop testing.
5488 */
5489static void quic_init_stdout_traces(void)
5490{
5491#ifdef ENABLE_QUIC_STDOUT_TRACES
5492 trace_quic.sink = sink_find("stdout");
5493 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005494 trace_quic.state = TRACE_STATE_RUNNING;
5495#endif
5496}
5497INITCALL0(STG_INIT, quic_init_stdout_traces);
5498
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005499/*
5500 * Local variables:
5501 * c-indent-level: 8
5502 * c-basic-offset: 8
5503 * End:
5504 */